Back to index

glibc  2.9
tst-preadwrite.c
Go to the documentation of this file.
00001 /* Tests for pread and pwrite.
00002    Copyright (C) 1998, 2000 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <errno.h>
00022 #include <error.h>
00023 #include <string.h>
00024 #include <unistd.h>
00025 
00026 
00027 /* Allow testing of the 64-bit versions as well.  */
00028 #ifndef PREAD
00029 # define PREAD pread
00030 # define PWRITE pwrite
00031 #endif
00032 
00033 #define STRINGIFY(s) STRINGIFY2 (s)
00034 #define STRINGIFY2(s) #s
00035 
00036 /* Prototype for our test function.  */
00037 extern void do_prepare (int argc, char *argv[]);
00038 extern int do_test (int argc, char *argv[]);
00039 
00040 /* We have a preparation function.  */
00041 #define PREPARE do_prepare
00042 
00043 /* We might need a bit longer timeout.  */
00044 #define TIMEOUT 20 /* sec */
00045 
00046 /* This defines the `main' function and some more.  */
00047 #include <test-skeleton.c>
00048 
00049 /* These are for the temporary file we generate.  */
00050 char *name;
00051 int fd;
00052 
00053 void
00054 do_prepare (int argc, char *argv[])
00055 {
00056    size_t name_len;
00057 
00058 #define FNAME FNAME2(TRUNCATE)
00059 #define FNAME2(s) "/" STRINGIFY(s) "XXXXXX"
00060 
00061    name_len = strlen (test_dir);
00062    name = malloc (name_len + sizeof (FNAME));
00063    if (name == NULL)
00064      error (EXIT_FAILURE, errno, "cannot allocate file name");
00065    mempcpy (mempcpy (name, test_dir, name_len), FNAME, sizeof (FNAME));
00066    add_temp_file (name);
00067 
00068    /* Open our test file.   */
00069    fd = mkstemp (name);
00070    if (fd == -1)
00071      error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
00072 }
00073 
00074 
00075 int
00076 do_test (int argc, char *argv[])
00077 {
00078   char buf[1000];
00079   char res[1000];
00080   int i;
00081 
00082   memset (buf, '\0', sizeof (buf));
00083   memset (res, '\xff', sizeof (res));
00084 
00085   if (write (fd, buf, sizeof (buf)) != sizeof (buf))
00086     error (EXIT_FAILURE, errno, "during write");
00087 
00088   for (i = 100; i < 200; ++i)
00089     buf[i] = i;
00090   if (PWRITE (fd, buf + 100, 100, 100) != 100)
00091     error (EXIT_FAILURE, errno, "during %s", STRINGIFY (PWRITE));
00092 
00093   for (i = 450; i < 600; ++i)
00094     buf[i] = i;
00095   if (PWRITE (fd, buf + 450, 150, 450) != 150)
00096     error (EXIT_FAILURE, errno, "during %s", STRINGIFY (PWRITE));
00097 
00098   if (PREAD (fd, res, sizeof (buf) - 50, 50) != sizeof (buf) - 50)
00099     error (EXIT_FAILURE, errno, "during %s", STRINGIFY (PREAD));
00100 
00101   close (fd);
00102   unlink (name);
00103 
00104   return memcmp (buf + 50, res, sizeof (buf) - 50);
00105 }