stat.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /* Copyright (C) 1991-2017 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. /*
  15. * POSIX Standard: 5.6 File Characteristics <sys/stat.h>
  16. */
  17. #ifndef _SYS_STAT_H
  18. #define _SYS_STAT_H 1
  19. #include <features.h>
  20. #include <bits/types.h> /* For __mode_t and __dev_t. */
  21. #ifdef __USE_ATFILE
  22. # include <bits/types/struct_timespec.h>
  23. #endif
  24. #if defined __USE_XOPEN || defined __USE_XOPEN2K
  25. /* The Single Unix specification says that some more types are
  26. available here. */
  27. # include <bits/types/time_t.h>
  28. # ifndef __dev_t_defined
  29. typedef __dev_t dev_t;
  30. # define __dev_t_defined
  31. # endif
  32. # ifndef __gid_t_defined
  33. typedef __gid_t gid_t;
  34. # define __gid_t_defined
  35. # endif
  36. # ifndef __ino_t_defined
  37. # ifndef __USE_FILE_OFFSET64
  38. typedef __ino_t ino_t;
  39. # else
  40. typedef __ino64_t ino_t;
  41. # endif
  42. # define __ino_t_defined
  43. # endif
  44. # ifndef __mode_t_defined
  45. typedef __mode_t mode_t;
  46. # define __mode_t_defined
  47. # endif
  48. # ifndef __nlink_t_defined
  49. typedef __nlink_t nlink_t;
  50. # define __nlink_t_defined
  51. # endif
  52. # ifndef __off_t_defined
  53. # ifndef __USE_FILE_OFFSET64
  54. typedef __off_t off_t;
  55. # else
  56. typedef __off64_t off_t;
  57. # endif
  58. # define __off_t_defined
  59. # endif
  60. # ifndef __uid_t_defined
  61. typedef __uid_t uid_t;
  62. # define __uid_t_defined
  63. # endif
  64. #endif /* X/Open */
  65. #ifdef __USE_UNIX98
  66. # ifndef __blkcnt_t_defined
  67. # ifndef __USE_FILE_OFFSET64
  68. typedef __blkcnt_t blkcnt_t;
  69. # else
  70. typedef __blkcnt64_t blkcnt_t;
  71. # endif
  72. # define __blkcnt_t_defined
  73. # endif
  74. # ifndef __blksize_t_defined
  75. typedef __blksize_t blksize_t;
  76. # define __blksize_t_defined
  77. # endif
  78. #endif /* Unix98 */
  79. __BEGIN_DECLS
  80. #include <bits/stat.h>
  81. #if defined __USE_MISC || defined __USE_XOPEN
  82. # define S_IFMT __S_IFMT
  83. # define S_IFDIR __S_IFDIR
  84. # define S_IFCHR __S_IFCHR
  85. # define S_IFBLK __S_IFBLK
  86. # define S_IFREG __S_IFREG
  87. # ifdef __S_IFIFO
  88. # define S_IFIFO __S_IFIFO
  89. # endif
  90. # ifdef __S_IFLNK
  91. # define S_IFLNK __S_IFLNK
  92. # endif
  93. # if (defined __USE_MISC || defined __USE_XOPEN_EXTENDED) \
  94. && defined __S_IFSOCK
  95. # define S_IFSOCK __S_IFSOCK
  96. # endif
  97. #endif
  98. /* Test macros for file types. */
  99. #define __S_ISTYPE(mode, mask) (((mode) & __S_IFMT) == (mask))
  100. #define S_ISDIR(mode) __S_ISTYPE((mode), __S_IFDIR)
  101. #define S_ISCHR(mode) __S_ISTYPE((mode), __S_IFCHR)
  102. #define S_ISBLK(mode) __S_ISTYPE((mode), __S_IFBLK)
  103. #define S_ISREG(mode) __S_ISTYPE((mode), __S_IFREG)
  104. #ifdef __S_IFIFO
  105. # define S_ISFIFO(mode) __S_ISTYPE((mode), __S_IFIFO)
  106. #endif
  107. #ifdef __S_IFLNK
  108. # define S_ISLNK(mode) __S_ISTYPE((mode), __S_IFLNK)
  109. #endif
  110. #if defined __USE_MISC && !defined __S_IFLNK
  111. # define S_ISLNK(mode) 0
  112. #endif
  113. #if (defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K) \
  114. && defined __S_IFSOCK
  115. # define S_ISSOCK(mode) __S_ISTYPE((mode), __S_IFSOCK)
  116. #elif defined __USE_XOPEN2K
  117. # define S_ISSOCK(mode) 0
  118. #endif
  119. /* These are from POSIX.1b. If the objects are not implemented using separate
  120. distinct file types, the macros always will evaluate to zero. Unlike the
  121. other S_* macros the following three take a pointer to a `struct stat'
  122. object as the argument. */
  123. #ifdef __USE_POSIX199309
  124. # define S_TYPEISMQ(buf) __S_TYPEISMQ(buf)
  125. # define S_TYPEISSEM(buf) __S_TYPEISSEM(buf)
  126. # define S_TYPEISSHM(buf) __S_TYPEISSHM(buf)
  127. #endif
  128. /* Protection bits. */
  129. #define S_ISUID __S_ISUID /* Set user ID on execution. */
  130. #define S_ISGID __S_ISGID /* Set group ID on execution. */
  131. #if defined __USE_MISC || defined __USE_XOPEN
  132. /* Save swapped text after use (sticky bit). This is pretty well obsolete. */
  133. # define S_ISVTX __S_ISVTX
  134. #endif
  135. #define S_IRUSR __S_IREAD /* Read by owner. */
  136. #define S_IWUSR __S_IWRITE /* Write by owner. */
  137. #define S_IXUSR __S_IEXEC /* Execute by owner. */
  138. /* Read, write, and execute by owner. */
  139. #define S_IRWXU (__S_IREAD|__S_IWRITE|__S_IEXEC)
  140. #ifdef __USE_MISC
  141. # define S_IREAD S_IRUSR
  142. # define S_IWRITE S_IWUSR
  143. # define S_IEXEC S_IXUSR
  144. #endif
  145. #define S_IRGRP (S_IRUSR >> 3) /* Read by group. */
  146. #define S_IWGRP (S_IWUSR >> 3) /* Write by group. */
  147. #define S_IXGRP (S_IXUSR >> 3) /* Execute by group. */
  148. /* Read, write, and execute by group. */
  149. #define S_IRWXG (S_IRWXU >> 3)
  150. #define S_IROTH (S_IRGRP >> 3) /* Read by others. */
  151. #define S_IWOTH (S_IWGRP >> 3) /* Write by others. */
  152. #define S_IXOTH (S_IXGRP >> 3) /* Execute by others. */
  153. /* Read, write, and execute by others. */
  154. #define S_IRWXO (S_IRWXG >> 3)
  155. #ifdef __USE_MISC
  156. /* Macros for common mode bit masks. */
  157. # define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO) /* 0777 */
  158. # define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)/* 07777 */
  159. # define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)/* 0666*/
  160. # define S_BLKSIZE 512 /* Block size for `st_blocks'. */
  161. #endif
  162. #ifndef __USE_FILE_OFFSET64
  163. /* Get file attributes for FILE and put them in BUF. */
  164. extern int stat (const char *__restrict __file,
  165. struct stat *__restrict __buf) __THROW __nonnull ((1, 2));
  166. /* Get file attributes for the file, device, pipe, or socket
  167. that file descriptor FD is open on and put them in BUF. */
  168. extern int fstat (int __fd, struct stat *__buf) __THROW __nonnull ((2));
  169. #else
  170. # ifdef __REDIRECT_NTH
  171. extern int __REDIRECT_NTH (stat, (const char *__restrict __file,
  172. struct stat *__restrict __buf), stat64)
  173. __nonnull ((1, 2));
  174. extern int __REDIRECT_NTH (fstat, (int __fd, struct stat *__buf), fstat64)
  175. __nonnull ((2));
  176. # else
  177. # define stat stat64
  178. # define fstat fstat64
  179. # endif
  180. #endif
  181. #ifdef __USE_LARGEFILE64
  182. extern int stat64 (const char *__restrict __file,
  183. struct stat64 *__restrict __buf) __THROW __nonnull ((1, 2));
  184. extern int fstat64 (int __fd, struct stat64 *__buf) __THROW __nonnull ((2));
  185. #endif
  186. #ifdef __USE_ATFILE
  187. /* Similar to stat, get the attributes for FILE and put them in BUF.
  188. Relative path names are interpreted relative to FD unless FD is
  189. AT_FDCWD. */
  190. # ifndef __USE_FILE_OFFSET64
  191. extern int fstatat (int __fd, const char *__restrict __file,
  192. struct stat *__restrict __buf, int __flag)
  193. __THROW __nonnull ((2, 3));
  194. # else
  195. # ifdef __REDIRECT_NTH
  196. extern int __REDIRECT_NTH (fstatat, (int __fd, const char *__restrict __file,
  197. struct stat *__restrict __buf,
  198. int __flag),
  199. fstatat64) __nonnull ((2, 3));
  200. # else
  201. # define fstatat fstatat64
  202. # endif
  203. # endif
  204. # ifdef __USE_LARGEFILE64
  205. extern int fstatat64 (int __fd, const char *__restrict __file,
  206. struct stat64 *__restrict __buf, int __flag)
  207. __THROW __nonnull ((2, 3));
  208. # endif
  209. #endif
  210. #if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K
  211. # ifndef __USE_FILE_OFFSET64
  212. /* Get file attributes about FILE and put them in BUF.
  213. If FILE is a symbolic link, do not follow it. */
  214. extern int lstat (const char *__restrict __file,
  215. struct stat *__restrict __buf) __THROW __nonnull ((1, 2));
  216. # else
  217. # ifdef __REDIRECT_NTH
  218. extern int __REDIRECT_NTH (lstat,
  219. (const char *__restrict __file,
  220. struct stat *__restrict __buf), lstat64)
  221. __nonnull ((1, 2));
  222. # else
  223. # define lstat lstat64
  224. # endif
  225. # endif
  226. # ifdef __USE_LARGEFILE64
  227. extern int lstat64 (const char *__restrict __file,
  228. struct stat64 *__restrict __buf)
  229. __THROW __nonnull ((1, 2));
  230. # endif
  231. #endif
  232. /* Set file access permissions for FILE to MODE.
  233. If FILE is a symbolic link, this affects its target instead. */
  234. extern int chmod (const char *__file, __mode_t __mode)
  235. __THROW __nonnull ((1));
  236. #ifdef __USE_MISC
  237. /* Set file access permissions for FILE to MODE.
  238. If FILE is a symbolic link, this affects the link itself
  239. rather than its target. */
  240. extern int lchmod (const char *__file, __mode_t __mode)
  241. __THROW __nonnull ((1));
  242. #endif
  243. /* Set file access permissions of the file FD is open on to MODE. */
  244. #if defined __USE_POSIX199309 || defined __USE_XOPEN_EXTENDED
  245. extern int fchmod (int __fd, __mode_t __mode) __THROW;
  246. #endif
  247. #ifdef __USE_ATFILE
  248. /* Set file access permissions of FILE relative to
  249. the directory FD is open on. */
  250. extern int fchmodat (int __fd, const char *__file, __mode_t __mode,
  251. int __flag)
  252. __THROW __nonnull ((2)) __wur;
  253. #endif /* Use ATFILE. */
  254. /* Set the file creation mask of the current process to MASK,
  255. and return the old creation mask. */
  256. extern __mode_t umask (__mode_t __mask) __THROW;
  257. #ifdef __USE_GNU
  258. /* Get the current `umask' value without changing it.
  259. This function is only available under the GNU Hurd. */
  260. extern __mode_t getumask (void) __THROW;
  261. #endif
  262. /* Create a new directory named PATH, with permission bits MODE. */
  263. extern int mkdir (const char *__path, __mode_t __mode)
  264. __THROW __nonnull ((1));
  265. #ifdef __USE_ATFILE
  266. /* Like mkdir, create a new directory with permission bits MODE. But
  267. interpret relative PATH names relative to the directory associated
  268. with FD. */
  269. extern int mkdirat (int __fd, const char *__path, __mode_t __mode)
  270. __THROW __nonnull ((2));
  271. #endif
  272. /* Create a device file named PATH, with permission and special bits MODE
  273. and device number DEV (which can be constructed from major and minor
  274. device numbers with the `makedev' macro above). */
  275. #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
  276. extern int mknod (const char *__path, __mode_t __mode, __dev_t __dev)
  277. __THROW __nonnull ((1));
  278. # ifdef __USE_ATFILE
  279. /* Like mknod, create a new device file with permission bits MODE and
  280. device number DEV. But interpret relative PATH names relative to
  281. the directory associated with FD. */
  282. extern int mknodat (int __fd, const char *__path, __mode_t __mode,
  283. __dev_t __dev) __THROW __nonnull ((2));
  284. # endif
  285. #endif
  286. /* Create a new FIFO named PATH, with permission bits MODE. */
  287. extern int mkfifo (const char *__path, __mode_t __mode)
  288. __THROW __nonnull ((1));
  289. #ifdef __USE_ATFILE
  290. /* Like mkfifo, create a new FIFO with permission bits MODE. But
  291. interpret relative PATH names relative to the directory associated
  292. with FD. */
  293. extern int mkfifoat (int __fd, const char *__path, __mode_t __mode)
  294. __THROW __nonnull ((2));
  295. #endif
  296. #ifdef __USE_ATFILE
  297. /* Set file access and modification times relative to directory file
  298. descriptor. */
  299. extern int utimensat (int __fd, const char *__path,
  300. const struct timespec __times[2],
  301. int __flags)
  302. __THROW __nonnull ((2));
  303. #endif
  304. #ifdef __USE_XOPEN2K8
  305. /* Set file access and modification times of the file associated with FD. */
  306. extern int futimens (int __fd, const struct timespec __times[2]) __THROW;
  307. #endif
  308. /* To allow the `struct stat' structure and the file type `mode_t'
  309. bits to vary without changing shared library major version number,
  310. the `stat' family of functions and `mknod' are in fact inline
  311. wrappers around calls to `xstat', `fxstat', `lxstat', and `xmknod',
  312. which all take a leading version-number argument designating the
  313. data structure and bits used. <bits/stat.h> defines _STAT_VER with
  314. the version number corresponding to `struct stat' as defined in
  315. that file; and _MKNOD_VER with the version number corresponding to
  316. the S_IF* macros defined therein. It is arranged that when not
  317. inlined these function are always statically linked; that way a
  318. dynamically-linked executable always encodes the version number
  319. corresponding to the data structures it uses, so the `x' functions
  320. in the shared library can adapt without needing to recompile all
  321. callers. */
  322. #ifndef _STAT_VER
  323. # define _STAT_VER 0
  324. #endif
  325. #ifndef _MKNOD_VER
  326. # define _MKNOD_VER 0
  327. #endif
  328. /* Wrappers for stat and mknod system calls. */
  329. #ifndef __USE_FILE_OFFSET64
  330. extern int __fxstat (int __ver, int __fildes, struct stat *__stat_buf)
  331. __THROW __nonnull ((3));
  332. extern int __xstat (int __ver, const char *__filename,
  333. struct stat *__stat_buf) __THROW __nonnull ((2, 3));
  334. extern int __lxstat (int __ver, const char *__filename,
  335. struct stat *__stat_buf) __THROW __nonnull ((2, 3));
  336. extern int __fxstatat (int __ver, int __fildes, const char *__filename,
  337. struct stat *__stat_buf, int __flag)
  338. __THROW __nonnull ((3, 4));
  339. #else
  340. # ifdef __REDIRECT_NTH
  341. extern int __REDIRECT_NTH (__fxstat, (int __ver, int __fildes,
  342. struct stat *__stat_buf), __fxstat64)
  343. __nonnull ((3));
  344. extern int __REDIRECT_NTH (__xstat, (int __ver, const char *__filename,
  345. struct stat *__stat_buf), __xstat64)
  346. __nonnull ((2, 3));
  347. extern int __REDIRECT_NTH (__lxstat, (int __ver, const char *__filename,
  348. struct stat *__stat_buf), __lxstat64)
  349. __nonnull ((2, 3));
  350. extern int __REDIRECT_NTH (__fxstatat, (int __ver, int __fildes,
  351. const char *__filename,
  352. struct stat *__stat_buf, int __flag),
  353. __fxstatat64) __nonnull ((3, 4));
  354. # else
  355. # define __fxstat __fxstat64
  356. # define __xstat __xstat64
  357. # define __lxstat __lxstat64
  358. # endif
  359. #endif
  360. #ifdef __USE_LARGEFILE64
  361. extern int __fxstat64 (int __ver, int __fildes, struct stat64 *__stat_buf)
  362. __THROW __nonnull ((3));
  363. extern int __xstat64 (int __ver, const char *__filename,
  364. struct stat64 *__stat_buf) __THROW __nonnull ((2, 3));
  365. extern int __lxstat64 (int __ver, const char *__filename,
  366. struct stat64 *__stat_buf) __THROW __nonnull ((2, 3));
  367. extern int __fxstatat64 (int __ver, int __fildes, const char *__filename,
  368. struct stat64 *__stat_buf, int __flag)
  369. __THROW __nonnull ((3, 4));
  370. #endif
  371. extern int __xmknod (int __ver, const char *__path, __mode_t __mode,
  372. __dev_t *__dev) __THROW __nonnull ((2, 4));
  373. extern int __xmknodat (int __ver, int __fd, const char *__path,
  374. __mode_t __mode, __dev_t *__dev)
  375. __THROW __nonnull ((3, 5));
  376. #ifdef __USE_EXTERN_INLINES
  377. /* Inlined versions of the real stat and mknod functions. */
  378. __extern_inline int
  379. __NTH (stat (const char *__path, struct stat *__statbuf))
  380. {
  381. return __xstat (_STAT_VER, __path, __statbuf);
  382. }
  383. # if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
  384. __extern_inline int
  385. __NTH (lstat (const char *__path, struct stat *__statbuf))
  386. {
  387. return __lxstat (_STAT_VER, __path, __statbuf);
  388. }
  389. # endif
  390. __extern_inline int
  391. __NTH (fstat (int __fd, struct stat *__statbuf))
  392. {
  393. return __fxstat (_STAT_VER, __fd, __statbuf);
  394. }
  395. # ifdef __USE_ATFILE
  396. __extern_inline int
  397. __NTH (fstatat (int __fd, const char *__filename, struct stat *__statbuf,
  398. int __flag))
  399. {
  400. return __fxstatat (_STAT_VER, __fd, __filename, __statbuf, __flag);
  401. }
  402. # endif
  403. # ifdef __USE_MISC
  404. __extern_inline int
  405. __NTH (mknod (const char *__path, __mode_t __mode, __dev_t __dev))
  406. {
  407. return __xmknod (_MKNOD_VER, __path, __mode, &__dev);
  408. }
  409. # endif
  410. # ifdef __USE_ATFILE
  411. __extern_inline int
  412. __NTH (mknodat (int __fd, const char *__path, __mode_t __mode,
  413. __dev_t __dev))
  414. {
  415. return __xmknodat (_MKNOD_VER, __fd, __path, __mode, &__dev);
  416. }
  417. # endif
  418. # if defined __USE_LARGEFILE64 \
  419. && (! defined __USE_FILE_OFFSET64 \
  420. || (defined __REDIRECT_NTH && defined __OPTIMIZE__))
  421. __extern_inline int
  422. __NTH (stat64 (const char *__path, struct stat64 *__statbuf))
  423. {
  424. return __xstat64 (_STAT_VER, __path, __statbuf);
  425. }
  426. # if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
  427. __extern_inline int
  428. __NTH (lstat64 (const char *__path, struct stat64 *__statbuf))
  429. {
  430. return __lxstat64 (_STAT_VER, __path, __statbuf);
  431. }
  432. # endif
  433. __extern_inline int
  434. __NTH (fstat64 (int __fd, struct stat64 *__statbuf))
  435. {
  436. return __fxstat64 (_STAT_VER, __fd, __statbuf);
  437. }
  438. # ifdef __USE_ATFILE
  439. __extern_inline int
  440. __NTH (fstatat64 (int __fd, const char *__filename, struct stat64 *__statbuf,
  441. int __flag))
  442. {
  443. return __fxstatat64 (_STAT_VER, __fd, __filename, __statbuf, __flag);
  444. }
  445. # endif
  446. # endif
  447. #endif
  448. __END_DECLS
  449. #endif /* sys/stat.h */