binutils/
[external/binutils.git] / sim / testsuite / sim / cris / c / seek3.c
1 /* Check for a sim bug, whereby the position was always unsigned
2    (truncation instead of sign-extension for 64-bit hosts).  */
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10
11 int
12 main (void)
13 {
14   FILE *f;
15   const char fname[] = "sk1test.dat";
16   const char tsttxt[]
17     = "A random line of text, used to test correct read, write and seek.\n";
18   char buf[sizeof tsttxt] = "";
19   const char correct[] = "correct";
20   char buf2[sizeof correct] = {0};
21   int fd;
22
23   f = fopen (fname, "wb");
24   if (f == NULL
25       || fwrite (tsttxt, 1, strlen (tsttxt), f) != strlen (tsttxt)
26       || fclose (f) != 0)
27     {
28       printf ("fail\n");
29       exit (1);
30     }
31
32   /* We have to use file-descriptor calls instead of stream calls to
33      provoke the bug (for stream calls, the lseek call is canonicalized
34      to use SEEK_SET).  */
35   fd = open (fname, O_RDONLY);
36   if (fd < 0
37       || read (fd, buf, strlen (tsttxt)) != strlen (tsttxt)
38       || strcmp (buf, tsttxt) != 0
39       || lseek (fd, -30L, SEEK_CUR) != 36
40       || read (fd, buf2, strlen (correct)) != strlen (correct)
41       || strcmp (buf2, correct) != 0)
42     {
43       printf ("fail\n");
44       exit (1);
45     }
46
47   printf ("pass\n");
48   exit (0);
49 }