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