forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathftruncate.c
More file actions
39 lines (31 loc) · 901 Bytes
/
ftruncate.c
File metadata and controls
39 lines (31 loc) · 901 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <sys/stat.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>
#define BASE_DIR "/tmp"
#define OUTPUT_DIR BASE_DIR "/testdir"
#define PATH OUTPUT_DIR "/output.txt"
int main(void) {
struct stat st;
int fd;
(void)st;
assert(0 == mkdir(OUTPUT_DIR, 0755));
fd = open(PATH, O_CREAT | O_WRONLY, 0666);
assert(fd != -1);
/* Verify that the file is initially empty. */
assert(0 == fstat(fd, &st));
assert(st.st_size == 0);
assert(0 == lseek(fd, 0, SEEK_CUR));
/* Increase the file size using ftruncate(). */
assert(0 == ftruncate(fd, 500));
assert(0 == fstat(fd, &st));
assert(st.st_size == 500);
assert(0 == lseek(fd, 0, SEEK_CUR));
/* Truncate the file using ftruncate(). */
assert(0 == ftruncate(fd, 300));
assert(0 == fstat(fd, &st));
assert(st.st_size == 300);
assert(0 == lseek(fd, 0, SEEK_CUR));
assert(0 == close(fd));
return 0;
}