1 /* truncate emulations for native Windows.
2 Copyright (C) 2017-2021 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, see <https://www.gnu.org/licenses/>. */
16
17 /* If the user's config.h happens to include <unistd.h>, let it include only
18 the system's <unistd.h> here, so that orig_faccessat doesn't recurse to
19 rpl_faccessat. */
20 #define _GL_INCLUDING_UNISTD_H
21 #include <config.h>
22
23 /* Specification. */
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)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
35 {
36 return truncate (filename, length);
37 }
38 #endif
39
40 /* Write "unistd.h" here, not <unistd.h>, otherwise OSF/1 5.1 DTK cc
41 eliminates this include because of the preliminary #include <unistd.h>
42 above. */
43 #include "unistd.h"
44
45 int
46 truncate (const char *filename, off_t length)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
47 {
48 #if TRUNCATE_TRAILING_SLASH_BUG
49 /* Use the original truncate(), but correct the trailing slash handling. */
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 }