int
run_main (int, ACE_TCHAR *[])
{
    ACE_START_TEST (ACE_TEXT ("Mem_Map_Test"));

#if !defined (ACE_LACKS_MMAP)

    // = Initialize the temporary variable names

    ACE_TCHAR test_file[MAXPATHLEN + 1];
    ACE_TCHAR temp_file1[MAXPATHLEN + 1];
    ACE_TCHAR temp_file2[MAXPATHLEN + 1];

    // Get the temporary directory
    // - 18 is for the filenames, ace_mem_map_temp_1 is the longest
    if (ACE::get_temp_dir (test_file, MAXPATHLEN - 18) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("Temporary path too long\n")),
                          -1);

    // Copy the temp directory to the other variables
    ACE_OS::strcpy (temp_file1, test_file);
    ACE_OS::strcpy (temp_file2, test_file);

    // Add the filenames to the end
    ACE_OS::strcat (test_file,
                    ACE_TEXT ("ace_mem_map_test"));
    ACE_OS::strcat (temp_file1,
                    ACE_TEXT ("ace_mem_map_temp_1"));
    ACE_OS::strcat (temp_file2,
                    ACE_TEXT ("ace_mem_map_temp_2"));

    // First create a test file to work on
    if (create_test_file (test_file, LINE_LENGTH, NUM_LINES) != 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("Create test file failed\n")),
                          -1);
    ACE_Mem_Map mmap;

    // First memory map the test file
    if (mmap.map (test_file) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%n: %p\n%a"),
                           ACE_TEXT ("mmap")),
                          -1);

    // Now create a temporary file for intermediate processing
    ACE_HANDLE temp_file_handle = ACE_OS::open (temp_file1,
                                  O_RDWR | O_TRUNC | O_CREAT,
                                  ACE_DEFAULT_FILE_PERMS);

    if (temp_file_handle == ACE_INVALID_HANDLE)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("Open failed\n")),
                          -1);

    // Reverse the original file and write the output to the temporary
    // file.
    reverse_file (temp_file_handle,
                  (char *) mmap.addr (),
                  mmap.size ());

    ACE_OS::close (temp_file_handle);

    ACE_Mem_Map temp_mmap;

    // Now memory map the temporary file
    if (temp_mmap.map (temp_file1) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%n: %p\n%a"),
                           ACE_TEXT ("mmap")),
                          -1);

    if ((temp_file_handle = ACE_OS::open (temp_file2,
                                          O_RDWR | O_TRUNC | O_CREAT,
                                          ACE_DEFAULT_FILE_PERMS))
            == ACE_INVALID_HANDLE)
        ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Open failed\n")), -1);

    // Now reverse the temporary file and write everything to the second
    // temporary file.
    reverse_file (temp_file_handle,
                  (char *) temp_mmap.addr (),
                  temp_mmap.size ());

    ACE_OS::close (temp_file_handle);

    // Memory map the second temporary file
    ACE_Mem_Map temp_mmap2;

    if (temp_mmap2.map (temp_file2) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%n: %p\n%a"),
                           ACE_TEXT ("mmap")),
                          -1);

    // Now do a memcmp -- the orig file and the second temporary file
    // should be identical.
    ACE_ASSERT (ACE_OS::memcmp (temp_mmap2.addr (),
                                mmap.addr (),
                                mmap.size ()) == 0);

    // Delete the test file
    mmap.remove ();

    // Delete ACE_TEMP_TEST_FILE
    temp_mmap.remove ();

    // Delete ACE_TEMP_TEST_FILE_2
    temp_mmap2.remove ();

#else /* !ACE_LACKS_MMAP */

    ACE_ERROR ((LM_INFO,
                ACE_TEXT ("mmap is not supported on this platform\n")));

#endif /* !ACE_LACKS_MMAP */

    ACE_END_TEST;
    return 0;
}