• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <sys/stat.h>
2 #include <assert.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 
6 #define BASE_DIR "/tmp"
7 #define OUTPUT_DIR BASE_DIR "/testdir"
8 #define PATH OUTPUT_DIR "/output.txt"
9 
main(void)10 int main(void) {
11   struct stat st;
12   int fd;
13 
14   (void)st;
15   assert(0 == mkdir(OUTPUT_DIR, 0755));
16 
17   fd = open(PATH, O_CREAT | O_WRONLY, 0666);
18   assert(fd != -1);
19 
20   /* Verify that the file is initially empty. */
21   assert(0 == fstat(fd, &st));
22   assert(st.st_size == 0);
23   assert(0 == lseek(fd, 0, SEEK_CUR));
24 
25   /* Increase the file size using ftruncate(). */
26   assert(0 == ftruncate(fd, 500));
27   assert(0 == fstat(fd, &st));
28   assert(st.st_size == 500);
29   assert(0 == lseek(fd, 0, SEEK_CUR));
30 
31   /* Truncate the file using ftruncate(). */
32   assert(0 == ftruncate(fd, 300));
33   assert(0 == fstat(fd, &st));
34   assert(st.st_size == 300);
35   assert(0 == lseek(fd, 0, SEEK_CUR));
36 
37   assert(0 == close(fd));
38   return 0;
39 }
40