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