1 /* Check that the ftruncate syscall works trivially.
2 #notarget: cris*-*-elf
3 */
4 
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 
9 void
perr(const char * s)10 perr (const char *s)
11 {
12   perror (s);
13   exit (1);
14 }
15 
16 int
main(void)17 main (void)
18 {
19   FILE *f;
20   const char fname[] = "sk1test.dat";
21   const char tsttxt1[]
22     = "This is the first and only line of this file.\n";
23   const char tsttxt2[] = "Now there is a second line.\n";
24   char buf[sizeof (tsttxt1) + sizeof (tsttxt2) - 1] = "";
25 
26   f = fopen (fname, "w+");
27   if (f == NULL
28       || fwrite (tsttxt1, 1, strlen (tsttxt1), f) != strlen (tsttxt1))
29     perr ("open or fwrite");
30 
31   if (fflush (f) != 0)
32     perr ("fflush");
33 
34   if (ftruncate (fileno (f), strlen(tsttxt1) - 20) != 0)
35     perr ("ftruncate");
36 
37   if (fclose (f) != 0)
38     perr ("fclose");
39 
40   f = fopen (fname, "r");
41   if (f == NULL
42       || fread (buf, 1, sizeof (buf), f) != strlen (tsttxt1) - 20
43       || strncmp (buf, tsttxt1, strlen (tsttxt1) - 20) != 0
44       || fclose (f) != 0)
45     {
46       printf ("fail\n");
47       exit (1);
48     }
49 
50   printf ("pass\n");
51   exit (0);
52 }
53