* io/Makefile (tests): Add tst-posix_fallocate.
[glibc.git] / io / tst-posix_fallocate.c
blob35c6489fde0e5d1ff4332e0a98d998ad718f92b8
1 #include <sys/stat.h>
3 static void do_prepare (void);
4 #define PREPARE(argc, argv) do_prepare ()
5 static int do_test (void);
6 #define TEST_FUNCTION do_test ()
7 #include <test-skeleton.c>
9 static int fd;
11 static void
12 do_prepare (void)
14 fd = create_temp_file ("tst-posix_fallocate.", NULL);
15 if (fd == -1)
17 printf ("cannot create temporary file: %m\n");
18 exit (1);
23 static int
24 do_test (void)
26 struct stat64 st;
28 if (fstat64 (fd, &st) != 0)
30 puts ("1st fstat failed");
31 return 1;
34 if (st.st_size != 0)
36 puts ("file not created with size 0");
37 return 1;
40 if (posix_fallocate (fd, 512, 768) != 0)
42 puts ("1st posix_fallocate call failed");
43 return 1;
46 if (fstat64 (fd, &st) != 0)
48 puts ("2nd fstat failed");
49 return 1;
52 if (st.st_size != 512 + 768)
54 printf ("file size after first posix_fallocate call is %llu, expected %u\n",
55 (unsigned long long int) st.st_size, 512u + 768u);
56 return 1;
59 if (posix_fallocate (fd, 0, 1024) != 0)
61 puts ("2nd posix_fallocate call failed");
62 return 1;
65 if (fstat64 (fd, &st) != 0)
67 puts ("3rd fstat failed");
68 return 1;
71 if (st.st_size != 512 + 768)
73 puts ("file size changed in second posix_fallocate");
74 return 1;
77 if (posix_fallocate (fd, 2048, 64) != 0)
79 puts ("3rd posix_fallocate call failed");
80 return 1;
83 if (fstat64 (fd, &st) != 0)
85 puts ("4th fstat failed");
86 return 1;
89 if (st.st_size != 2048 + 64)
91 printf ("file size after first posix_fallocate call is %llu, expected %u\n",
92 (unsigned long long int) st.st_size, 2048u + 64u);
93 return 1;
96 close (fd);
98 return 0;