Back to index

glibc  2.9
bug-fseek.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;
00008 
00009 
00010 static void do_prepare (void);
00011 #define PREPARE(argc, argv) do_prepare ()
00012 static int do_test (void);
00013 #define TEST_FUNCTION do_test ()
00014 #include <test-skeleton.c>
00015 
00016 
00017 static void
00018 do_prepare (void)
00019 {
00020   static const char pattern[] = "12345678901234567890";
00021   int fd = create_temp_file ("bug-fseek.", &fname);
00022   if (fd == -1)
00023     {
00024       printf ("cannot create temporary file: %m\n");
00025       exit (1);
00026     }
00027 
00028   if (write (fd, pattern, sizeof (pattern)) != sizeof (pattern))
00029     {
00030       perror ("short write");
00031       abort ();
00032     }
00033   close (fd);
00034 }
00035 
00036 
00037 
00038 static int
00039 do_test (void)
00040 {
00041   FILE *f;
00042   int result = 0;
00043   char buf[10];
00044 
00045 
00046   if ((f = fopen (fname, "r")) == (FILE *) NULL)
00047     {
00048       perror ("fopen(\"r\")");
00049     }
00050 
00051   fread (buf, 3, 1, f);
00052   errno = 0;
00053   if (fseek (f, -10, SEEK_CUR) == 0)
00054     {
00055       printf ("fseek() for r to before start of file worked!\n");
00056       result = 1;
00057     }
00058   else if (errno != EINVAL)
00059     {
00060       printf ("\
00061 fseek() for r to before start of file did not set errno to EINVAL.  \
00062 Got %d instead\n",
00063         errno);
00064       result = 1;
00065     }
00066 
00067   fclose (f);
00068 
00069 
00070   if ((f = fopen (fname, "r+")) == (FILE *) NULL)
00071     {
00072       perror ("fopen(\"r+\")");
00073     }
00074 
00075   fread (buf, 3, 1, f);
00076   errno = 0;
00077   if (fseek (f, -10, SEEK_CUR) == 0)
00078     {
00079       printf ("fseek() for r+ to before start of file worked!\n");
00080       result = 1;
00081     }
00082   else if (errno != EINVAL)
00083     {
00084       printf ("\
00085 fseek() for r+ to before start of file did not set errno to EINVAL.  \
00086 Got %d instead\n",
00087         errno);
00088       result = 1;
00089     }
00090 
00091   fclose (f);
00092 
00093 
00094   if ((f = fopen (fname, "r+")) == (FILE *) NULL)
00095     {
00096       perror ("fopen(\"r+\")");
00097     }
00098 
00099   fread (buf, 3, 1, f);
00100   if (ftell (f) != 3)
00101     {
00102       puts ("ftell failed");
00103       return 1;
00104     }
00105   errno = 0;
00106   if (fseek (f, -10, SEEK_CUR) == 0)
00107     {
00108       printf ("fseek() for r+ to before start of file worked!\n");
00109       result = 1;
00110     }
00111   else if (errno != EINVAL)
00112     {
00113       printf ("\
00114 fseek() for r+ to before start of file did not set errno to EINVAL.  \
00115 Got %d instead\n",
00116         errno);
00117       result = 1;
00118     }
00119 
00120   fclose (f);
00121 
00122   return result;
00123 }