Home | History | Annotate | Line # | Download | only in c
      1  1.1  christos /* Check for a sim bug, whereby an invalid seek (to a negative offset)
      2  1.1  christos    did not return an error.  */
      3  1.1  christos #include <stdio.h>
      4  1.1  christos #include <stdlib.h>
      5  1.1  christos #include <string.h>
      6  1.1  christos #include <errno.h>
      7  1.1  christos #include <sys/types.h>
      8  1.1  christos #include <sys/stat.h>
      9  1.1  christos #include <fcntl.h>
     10  1.1  christos #include <unistd.h>
     11  1.1  christos 
     12  1.1  christos int
     13  1.1  christos main (void)
     14  1.1  christos {
     15  1.1  christos   FILE *f;
     16  1.1  christos   const char fname[] = "sk1test.dat";
     17  1.1  christos   const char tsttxt[]
     18  1.1  christos     = "A random line of text, used to test correct read, write and seek.\n";
     19  1.1  christos   char buf[sizeof tsttxt] = "";
     20  1.1  christos   int fd;
     21  1.1  christos 
     22  1.1  christos   f = fopen (fname, "wb");
     23  1.1  christos   if (f == NULL
     24  1.1  christos       || fwrite (tsttxt, 1, strlen (tsttxt), f) != strlen (tsttxt)
     25  1.1  christos       || fclose (f) != 0)
     26  1.1  christos     {
     27  1.1  christos       printf ("fail\n");
     28  1.1  christos       exit (1);
     29  1.1  christos     }
     30  1.1  christos 
     31  1.1  christos   fd = open (fname, O_RDONLY);
     32  1.1  christos   if (fd < 0
     33  1.1  christos       || lseek (fd, -1L, SEEK_CUR) != -1
     34  1.1  christos       || errno != EINVAL
     35  1.1  christos       || read (fd, buf, strlen (tsttxt)) != strlen (tsttxt)
     36  1.1  christos       || strcmp (buf, tsttxt) != 0)
     37  1.1  christos     {
     38  1.1  christos       printf ("fail\n");
     39  1.1  christos       exit (1);
     40  1.1  christos     }
     41  1.1  christos 
     42  1.1  christos   printf ("pass\n");
     43  1.1  christos   exit (0);
     44  1.1  christos }
     45