This source file includes following definitions.
- orig_truncate
- truncate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #define _GL_INCLUDING_UNISTD_H
21 #include <config.h>
22
23
24 #include <unistd.h>
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <sys/stat.h>
30 #undef _GL_INCLUDING_UNISTD_H
31
32 #if TRUNCATE_TRAILING_SLASH_BUG
33 static int
34 orig_truncate (const char *filename, off_t length)
35 {
36 return truncate (filename, length);
37 }
38 #endif
39
40
41
42
43 #include "unistd.h"
44
45 int
46 truncate (const char *filename, off_t length)
47 {
48 #if TRUNCATE_TRAILING_SLASH_BUG
49
50 size_t len = strlen (filename);
51 if (len && filename[len - 1] == '/')
52 {
53 struct stat st;
54 if (stat (filename, &st) == 0)
55 errno = (S_ISDIR (st.st_mode) ? EISDIR : ENOTDIR);
56 return -1;
57 }
58 return orig_truncate (filename, length);
59 #else
60 int fd;
61
62 if (length == 0)
63 {
64 fd = open (filename, O_WRONLY | O_TRUNC | O_CLOEXEC);
65 if (fd < 0)
66 return -1;
67 }
68 else
69 {
70 fd = open (filename, O_WRONLY | O_CLOEXEC);
71 if (fd < 0)
72 return -1;
73 if (ftruncate (fd, length) < 0)
74 {
75 int saved_errno = errno;
76 close (fd);
77 errno = saved_errno;
78 return -1;
79 }
80 }
81 close (fd);
82 return 0;
83 #endif
84 }