1 /* Copyright (C) 1991-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library 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 GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
19 * POSIX Standard: 5.6 File Characteristics <sys/stat.h>
27 #include <bits/types.h> /* For __mode_t and __dev_t. */
30 # include <bits/types/struct_timespec.h>
33 #if defined __USE_XOPEN || defined __USE_XOPEN2K
34 /* The Single Unix specification says that some more types are
37 # include <bits/types/time_t.h>
39 # ifndef __dev_t_defined
40 typedef __dev_t dev_t
;
41 # define __dev_t_defined
44 # ifndef __gid_t_defined
45 typedef __gid_t gid_t
;
46 # define __gid_t_defined
49 # ifndef __ino_t_defined
50 # ifndef __USE_FILE_OFFSET64
51 typedef __ino_t ino_t
;
53 typedef __ino64_t ino_t
;
55 # define __ino_t_defined
58 # ifndef __mode_t_defined
59 typedef __mode_t mode_t
;
60 # define __mode_t_defined
63 # ifndef __nlink_t_defined
64 typedef __nlink_t nlink_t
;
65 # define __nlink_t_defined
68 # ifndef __off_t_defined
69 # ifndef __USE_FILE_OFFSET64
70 typedef __off_t off_t
;
72 typedef __off64_t off_t
;
74 # define __off_t_defined
77 # ifndef __uid_t_defined
78 typedef __uid_t uid_t
;
79 # define __uid_t_defined
84 # ifndef __blkcnt_t_defined
85 # ifndef __USE_FILE_OFFSET64
86 typedef __blkcnt_t blkcnt_t
;
88 typedef __blkcnt64_t blkcnt_t
;
90 # define __blkcnt_t_defined
93 # ifndef __blksize_t_defined
94 typedef __blksize_t blksize_t
;
95 # define __blksize_t_defined
101 #include <bits/stat.h>
103 #if defined __USE_MISC || defined __USE_XOPEN
104 # define S_IFMT __S_IFMT
105 # define S_IFDIR __S_IFDIR
106 # define S_IFCHR __S_IFCHR
107 # define S_IFBLK __S_IFBLK
108 # define S_IFREG __S_IFREG
110 # define S_IFIFO __S_IFIFO
113 # define S_IFLNK __S_IFLNK
115 # if (defined __USE_MISC || defined __USE_XOPEN_EXTENDED) \
116 && defined __S_IFSOCK
117 # define S_IFSOCK __S_IFSOCK
121 /* Test macros for file types. */
123 #define __S_ISTYPE(mode, mask) (((mode) & __S_IFMT) == (mask))
125 #define S_ISDIR(mode) __S_ISTYPE((mode), __S_IFDIR)
126 #define S_ISCHR(mode) __S_ISTYPE((mode), __S_IFCHR)
127 #define S_ISBLK(mode) __S_ISTYPE((mode), __S_IFBLK)
128 #define S_ISREG(mode) __S_ISTYPE((mode), __S_IFREG)
130 # define S_ISFIFO(mode) __S_ISTYPE((mode), __S_IFIFO)
133 # define S_ISLNK(mode) __S_ISTYPE((mode), __S_IFLNK)
136 #if defined __USE_MISC && !defined __S_IFLNK
137 # define S_ISLNK(mode) 0
140 #if (defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K) \
141 && defined __S_IFSOCK
142 # define S_ISSOCK(mode) __S_ISTYPE((mode), __S_IFSOCK)
143 #elif defined __USE_XOPEN2K
144 # define S_ISSOCK(mode) 0
147 /* These are from POSIX.1b. If the objects are not implemented using separate
148 distinct file types, the macros always will evaluate to zero. Unlike the
149 other S_* macros the following three take a pointer to a `struct stat'
150 object as the argument. */
151 #ifdef __USE_POSIX199309
152 # define S_TYPEISMQ(buf) __S_TYPEISMQ(buf)
153 # define S_TYPEISSEM(buf) __S_TYPEISSEM(buf)
154 # define S_TYPEISSHM(buf) __S_TYPEISSHM(buf)
158 /* Protection bits. */
160 #define S_ISUID __S_ISUID /* Set user ID on execution. */
161 #define S_ISGID __S_ISGID /* Set group ID on execution. */
163 #if defined __USE_MISC || defined __USE_XOPEN
164 /* Save swapped text after use (sticky bit). This is pretty well obsolete. */
165 # define S_ISVTX __S_ISVTX
168 #define S_IRUSR __S_IREAD /* Read by owner. */
169 #define S_IWUSR __S_IWRITE /* Write by owner. */
170 #define S_IXUSR __S_IEXEC /* Execute by owner. */
171 /* Read, write, and execute by owner. */
172 #define S_IRWXU (__S_IREAD|__S_IWRITE|__S_IEXEC)
175 # define S_IREAD S_IRUSR
176 # define S_IWRITE S_IWUSR
177 # define S_IEXEC S_IXUSR
180 #define S_IRGRP (S_IRUSR >> 3) /* Read by group. */
181 #define S_IWGRP (S_IWUSR >> 3) /* Write by group. */
182 #define S_IXGRP (S_IXUSR >> 3) /* Execute by group. */
183 /* Read, write, and execute by group. */
184 #define S_IRWXG (S_IRWXU >> 3)
186 #define S_IROTH (S_IRGRP >> 3) /* Read by others. */
187 #define S_IWOTH (S_IWGRP >> 3) /* Write by others. */
188 #define S_IXOTH (S_IXGRP >> 3) /* Execute by others. */
189 /* Read, write, and execute by others. */
190 #define S_IRWXO (S_IRWXG >> 3)
194 /* Macros for common mode bit masks. */
195 # define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO) /* 0777 */
196 # define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)/* 07777 */
197 # define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)/* 0666*/
199 # define S_BLKSIZE 512 /* Block size for `st_blocks'. */
203 #ifndef __USE_FILE_OFFSET64
204 /* Get file attributes for FILE and put them in BUF. */
205 extern int stat (const char *__restrict __file
,
206 struct stat
*__restrict __buf
) __THROW
__nonnull ((1, 2));
208 /* Get file attributes for the file, device, pipe, or socket
209 that file descriptor FD is open on and put them in BUF. */
210 extern int fstat (int __fd
, struct stat
*__buf
) __THROW
__nonnull ((2));
212 # ifdef __USE_TIME_BITS64
213 # ifdef __REDIRECT_NTH
214 extern int __REDIRECT_NTH (stat
, (const char *__restrict __file
,
215 struct stat
*__restrict __buf
),
218 extern int __REDIRECT_NTH (fstat
, (int __fd
, struct stat
*__buf
),
222 # define stat __stat64_time64
223 # define fstat __fstat64_time64
226 # ifdef __REDIRECT_NTH
227 extern int __REDIRECT_NTH (stat
, (const char *__restrict __file
,
228 struct stat
*__restrict __buf
), stat64
)
230 extern int __REDIRECT_NTH (fstat
, (int __fd
, struct stat
*__buf
), fstat64
)
234 # define fstat fstat64
238 #ifdef __USE_LARGEFILE64
239 # ifndef __USE_TIME_BITS64
240 extern int stat64 (const char *__restrict __file
,
241 struct stat64
*__restrict __buf
) __THROW
__nonnull ((1, 2));
242 extern int fstat64 (int __fd
, struct stat64
*__buf
) __THROW
__nonnull ((2));
244 # ifdef __REDIRECT_NTH
245 extern int __REDIRECT_NTH (stat64
, (const char *__restrict __file
,
246 struct stat64
*__restrict __buf
),
249 extern int __REDIRECT_NTH (fstat64
, (int __fd
, struct stat64
*__buf
),
253 # define stat64 __stat64_time64
254 # define fstat64 __fstat64_time
260 /* Similar to stat, get the attributes for FILE and put them in BUF.
261 Relative path names are interpreted relative to FD unless FD is
263 # ifndef __USE_FILE_OFFSET64
264 extern int fstatat (int __fd
, const char *__restrict __file
,
265 struct stat
*__restrict __buf
, int __flag
)
266 __THROW
__nonnull ((2, 3));
268 # ifdef __USE_TIME_BITS64
269 # ifdef __REDIRECT_NTH
270 extern int __REDIRECT_NTH (fstatat
, (int __fd
, const char *__restrict __file
,
271 struct stat
*__restrict __buf
,
273 __fstatat64_time64
) __nonnull ((2, 3));
275 # define fstatat __fstatat64_time64
278 # ifdef __REDIRECT_NTH
279 extern int __REDIRECT_NTH (fstatat
, (int __fd
, const char *__restrict __file
,
280 struct stat
*__restrict __buf
,
282 fstatat64
) __nonnull ((2, 3));
284 # define fstatat fstatat64
289 # ifdef __USE_LARGEFILE64
290 # ifndef __USE_TIME_BITS64
291 extern int fstatat64 (int __fd
, const char *__restrict __file
,
292 struct stat64
*__restrict __buf
, int __flag
)
293 __THROW
__nonnull ((2, 3));
295 # ifdef __REDIRECT_NTH
296 extern int __REDIRECT_NTH (fstatat64
, (int __fd
,
297 const char *__restrict __file
,
298 struct stat64
*__restrict __buf
,
303 # define fstatat64 __fstatat64_time64
309 #if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K
310 # ifndef __USE_FILE_OFFSET64
311 /* Get file attributes about FILE and put them in BUF.
312 If FILE is a symbolic link, do not follow it. */
313 extern int lstat (const char *__restrict __file
,
314 struct stat
*__restrict __buf
) __THROW
__nonnull ((1, 2));
316 # ifdef __USE_TIME_BITS64
317 # ifdef __REDIRECT_NTH
318 extern int __REDIRECT_NTH (lstat
,
319 (const char *__restrict __file
,
320 struct stat
*__restrict __buf
), __lstat64_time64
)
323 # define lstat __lstat64_time64
326 # ifdef __REDIRECT_NTH
327 extern int __REDIRECT_NTH (lstat
,
328 (const char *__restrict __file
,
329 struct stat
*__restrict __buf
), lstat64
)
332 # define lstat lstat64
336 # ifdef __USE_LARGEFILE64
337 # ifndef __USE_TIME_BITS64
338 extern int lstat64 (const char *__restrict __file
,
339 struct stat64
*__restrict __buf
)
340 __THROW
__nonnull ((1, 2));
342 extern int __REDIRECT_NTH (lstat64
, (const char *__restrict __file
,
343 struct stat64
*__restrict __buf
),
350 /* Set file access permissions for FILE to MODE.
351 If FILE is a symbolic link, this affects its target instead. */
352 extern int chmod (const char *__file
, __mode_t __mode
)
353 __THROW
__nonnull ((1));
356 /* Set file access permissions for FILE to MODE.
357 If FILE is a symbolic link, this affects the link itself
358 rather than its target. */
359 extern int lchmod (const char *__file
, __mode_t __mode
)
360 __THROW
__nonnull ((1));
363 /* Set file access permissions of the file FD is open on to MODE. */
364 #if defined __USE_POSIX199309 || defined __USE_XOPEN_EXTENDED
365 extern int fchmod (int __fd
, __mode_t __mode
) __THROW
;
369 /* Set file access permissions of FILE relative to
370 the directory FD is open on. */
371 extern int fchmodat (int __fd
, const char *__file
, __mode_t __mode
,
373 __THROW
__nonnull ((2)) __wur
;
374 #endif /* Use ATFILE. */
378 /* Set the file creation mask of the current process to MASK,
379 and return the old creation mask. */
380 extern __mode_t
umask (__mode_t __mask
) __THROW
;
383 /* Get the current `umask' value without changing it.
384 This function is only available under the GNU Hurd. */
385 extern __mode_t
getumask (void) __THROW
;
388 /* Create a new directory named PATH, with permission bits MODE. */
389 extern int mkdir (const char *__path
, __mode_t __mode
)
390 __THROW
__nonnull ((1));
393 /* Like mkdir, create a new directory with permission bits MODE. But
394 interpret relative PATH names relative to the directory associated
396 extern int mkdirat (int __fd
, const char *__path
, __mode_t __mode
)
397 __THROW
__nonnull ((2));
400 /* Create a device file named PATH, with permission and special bits MODE
401 and device number DEV (which can be constructed from major and minor
402 device numbers with the `makedev' macro above). */
403 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
404 extern int mknod (const char *__path
, __mode_t __mode
, __dev_t __dev
)
405 __THROW
__nonnull ((1));
408 /* Like mknod, create a new device file with permission bits MODE and
409 device number DEV. But interpret relative PATH names relative to
410 the directory associated with FD. */
411 extern int mknodat (int __fd
, const char *__path
, __mode_t __mode
,
412 __dev_t __dev
) __THROW
__nonnull ((2));
417 /* Create a new FIFO named PATH, with permission bits MODE. */
418 extern int mkfifo (const char *__path
, __mode_t __mode
)
419 __THROW
__nonnull ((1));
422 /* Like mkfifo, create a new FIFO with permission bits MODE. But
423 interpret relative PATH names relative to the directory associated
425 extern int mkfifoat (int __fd
, const char *__path
, __mode_t __mode
)
426 __THROW
__nonnull ((2));
430 # ifndef __USE_TIME_BITS64
431 /* Set file access and modification times relative to directory file
433 extern int utimensat (int __fd
, const char *__path
,
434 const struct timespec __times
[2],
436 __THROW
__nonnull ((2));
438 # ifdef __REDIRECT_NTH
439 extern int __REDIRECT_NTH (utimensat
, (int fd
, const char *__path
,
440 const struct timespec __times
[2],
442 __utimensat64
) __nonnull ((2));
444 # define utimensat __utimensat64
449 #ifdef __USE_XOPEN2K8
450 # ifndef __USE_TIME_BITS64
451 /* Set file access and modification times of the file associated with FD. */
452 extern int futimens (int __fd
, const struct timespec __times
[2]) __THROW
;
455 # ifdef __REDIRECT_NTH
456 extern int __REDIRECT_NTH (futimens
, (int fd
, const struct timespec __times
[2]),
459 # define futimens __futimens64
465 # include <bits/statx.h>
471 #endif /* sys/stat.h */