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