Back to index

glibc  2.9
tst-rndseek.c
Go to the documentation of this file.
00001 #include <errno.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <unistd.h>
00005 
00006 
00007 static char fname[] = "/tmp/rndseek.XXXXXX";
00008 static char tempdata[65 * 1024];
00009 
00010 
00011 static int do_test (void);
00012 #define TEST_FUNCTION do_test ()
00013 #define TIMEOUT 10
00014 
00015 #include "../test-skeleton.c"
00016 
00017 
00018 static int
00019 fp_test (const char *name, FILE *fp)
00020 {
00021   int result = 0;
00022   int rounds = 10000;
00023 
00024   do
00025     {
00026       int idx = random () % (sizeof (tempdata) - 2);
00027       char ch1;
00028       char ch2;
00029 
00030       if (fseek (fp, idx, SEEK_SET) != 0)
00031        {
00032          printf ("%s: %d: fseek failed: %m\n", name, rounds);
00033          result = 1;
00034          break;
00035        }
00036 
00037       ch1 = fgetc_unlocked (fp);
00038       ch2 = tempdata[idx];
00039       if (ch1 != ch2)
00040        {
00041          printf ("%s: %d: character at index %d not what is expected ('%c' vs '%c')\n",
00042                 name, rounds, idx, ch1, ch2);
00043          result = 1;
00044          break;
00045        }
00046 
00047       ch1 = fgetc (fp);
00048       ch2 = tempdata[idx + 1];
00049       if (ch1 != ch2)
00050        {
00051          printf ("%s: %d: character at index %d not what is expected ('%c' vs '%c')\n",
00052                 name, rounds, idx + 1, ch1, ch2);
00053          result = 1;
00054          break;
00055        }
00056     }
00057   while (--rounds > 0);
00058 
00059   fclose (fp);
00060 
00061   return result;
00062 }
00063 
00064 
00065 static int
00066 do_test (void)
00067 {
00068   int fd;
00069   FILE *fp;
00070   size_t i;
00071   int result;
00072 
00073   fd = mkstemp (fname);
00074   if (fd == -1)
00075     {
00076       printf ("cannot open temporary file: %m\n");
00077       return 1;
00078     }
00079   /* Make sure the file gets removed.  */
00080   add_temp_file (fname);
00081 
00082   /* Repeatability demands this.  */
00083   srandom (42);
00084 
00085   /* First create some temporary data.  */
00086   for (i = 0; i < sizeof (tempdata); ++i)
00087     tempdata[i] = 'a' + random () % 26;
00088 
00089   /* Write this data to a file.  */
00090   if (TEMP_FAILURE_RETRY (write (fd, tempdata, sizeof (tempdata)))
00091       != sizeof (tempdata))
00092     {
00093       printf ("cannot wrote data to temporary file: %m\n");
00094       return 1;
00095     }
00096 
00097   /* Now try reading the data.  */
00098   fp = fdopen (dup (fd), "r");
00099   if (fp == NULL)
00100     {
00101       printf ("cannot duplicate temporary file descriptor: %m\n");
00102       return 1;
00103     }
00104 
00105   rewind (fp);
00106   for (i = 0; i < sizeof (tempdata); ++i)
00107     {
00108       int ch0 = fgetc (fp);
00109       char ch1 = ch0;
00110       char ch2 = tempdata[i];
00111 
00112       if (ch0 == EOF)
00113        {
00114          puts ("premature end of file while reading data");
00115          return 1;
00116        }
00117 
00118       if (ch1 != ch2)
00119        {
00120          printf ("%zd: '%c' vs '%c'\n", i, ch1, ch2);
00121          return 1;
00122        }
00123     }
00124 
00125   result = fp_test ("fdopen(\"r\")", fp);
00126 
00127   fp = fopen (fname, "r");
00128   result |= fp_test ("fopen(\"r\")", fp);
00129 
00130   fp = fopen64 (fname, "r");
00131   result |= fp_test ("fopen64(\"r\")", fp);
00132 
00133   /* The "rw" mode will prevent the mmap-using code from being used.  */
00134   fp = fdopen (fd, "rw");
00135   result = fp_test ("fdopen(\"rw\")", fp);
00136 
00137   fp = fopen (fname, "rw");
00138   result |= fp_test ("fopen(\"rw\")", fp);
00139 
00140   fp = fopen64 (fname, "rw");
00141   result |= fp_test ("fopen64(\"rw\")", fp);
00142 
00143   return result;
00144 }