1 /* truncate emulations for native Windows.
2 Copyright (C) 2017-2024 Free Software Foundation, Inc.
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)
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.
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/>. */
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
20 #define _GL_INCLUDING_UNISTD_H
30 #undef _GL_INCLUDING_UNISTD_H
32 #if TRUNCATE_TRAILING_SLASH_BUG
34 orig_truncate (const char *filename
, off_t length
)
36 return truncate (filename
, length
);
41 /* Write "unistd.h" here, not <unistd.h>, otherwise OSF/1 5.1 DTK cc
42 eliminates this include because of the preliminary #include <unistd.h>
50 truncate (const char *filename
, off_t length
)
52 #if TRUNCATE_TRAILING_SLASH_BUG
53 /* Use the original truncate(), but correct the trailing slash handling. */
54 size_t len
= strlen (filename
);
55 if (len
&& filename
[len
- 1] == '/')
58 if (stat (filename
, &st
) == 0)
59 errno
= (S_ISDIR (st
.st_mode
) ? EISDIR
: ENOTDIR
);
62 return orig_truncate (filename
, length
);
68 fd
= open (filename
, O_WRONLY
| O_TRUNC
| O_CLOEXEC
);
74 fd
= open (filename
, O_WRONLY
| O_CLOEXEC
);
77 if (ftruncate (fd
, length
) < 0)
79 int saved_errno
= errno
;