fcntl.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* Copyright (C) 1991-2020 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. <https://www.gnu.org/licenses/>. */
  14. /*
  15. * POSIX Standard: 6.5 File Control Operations <fcntl.h>
  16. */
  17. #ifndef _FCNTL_H
  18. #define _FCNTL_H 1
  19. #include <features.h>
  20. /* This must be early so <bits/fcntl.h> can define types winningly. */
  21. __BEGIN_DECLS
  22. /* Get __mode_t, __dev_t and __off_t .*/
  23. #include <bits/types.h>
  24. /* Get the definitions of O_*, F_*, FD_*: all the
  25. numbers and flag bits for `open', `fcntl', et al. */
  26. #include <bits/fcntl.h>
  27. /* Detect if open needs mode as a third argument (or for openat as a fourth
  28. argument). */
  29. #ifdef __O_TMPFILE
  30. # define __OPEN_NEEDS_MODE(oflag) \
  31. (((oflag) & O_CREAT) != 0 || ((oflag) & __O_TMPFILE) == __O_TMPFILE)
  32. #else
  33. # define __OPEN_NEEDS_MODE(oflag) (((oflag) & O_CREAT) != 0)
  34. #endif
  35. /* POSIX.1-2001 specifies that these types are defined by <fcntl.h>.
  36. Earlier POSIX standards permitted any type ending in `_t' to be defined
  37. by any POSIX header, so we don't conditionalize the definitions here. */
  38. #ifndef __mode_t_defined
  39. typedef __mode_t mode_t;
  40. # define __mode_t_defined
  41. #endif
  42. #ifndef __off_t_defined
  43. # ifndef __USE_FILE_OFFSET64
  44. typedef __off_t off_t;
  45. # else
  46. typedef __off64_t off_t;
  47. # endif
  48. # define __off_t_defined
  49. #endif
  50. #if defined __USE_LARGEFILE64 && !defined __off64_t_defined
  51. typedef __off64_t off64_t;
  52. # define __off64_t_defined
  53. #endif
  54. #ifndef __pid_t_defined
  55. typedef __pid_t pid_t;
  56. # define __pid_t_defined
  57. #endif
  58. /* For XPG all symbols from <sys/stat.h> should also be available. */
  59. #ifdef __USE_XOPEN2K8
  60. # include <bits/types/struct_timespec.h>
  61. #endif
  62. #if defined __USE_XOPEN || defined __USE_XOPEN2K8
  63. # include <bits/stat.h>
  64. # define S_IFMT __S_IFMT
  65. # define S_IFDIR __S_IFDIR
  66. # define S_IFCHR __S_IFCHR
  67. # define S_IFBLK __S_IFBLK
  68. # define S_IFREG __S_IFREG
  69. # ifdef __S_IFIFO
  70. # define S_IFIFO __S_IFIFO
  71. # endif
  72. # ifdef __S_IFLNK
  73. # define S_IFLNK __S_IFLNK
  74. # endif
  75. # if (defined __USE_UNIX98 || defined __USE_XOPEN2K8) && defined __S_IFSOCK
  76. # define S_IFSOCK __S_IFSOCK
  77. # endif
  78. /* Protection bits. */
  79. # define S_ISUID __S_ISUID /* Set user ID on execution. */
  80. # define S_ISGID __S_ISGID /* Set group ID on execution. */
  81. # if defined __USE_MISC || defined __USE_XOPEN
  82. /* Save swapped text after use (sticky bit). This is pretty well obsolete. */
  83. # define S_ISVTX __S_ISVTX
  84. # endif
  85. # define S_IRUSR __S_IREAD /* Read by owner. */
  86. # define S_IWUSR __S_IWRITE /* Write by owner. */
  87. # define S_IXUSR __S_IEXEC /* Execute by owner. */
  88. /* Read, write, and execute by owner. */
  89. # define S_IRWXU (__S_IREAD|__S_IWRITE|__S_IEXEC)
  90. # define S_IRGRP (S_IRUSR >> 3) /* Read by group. */
  91. # define S_IWGRP (S_IWUSR >> 3) /* Write by group. */
  92. # define S_IXGRP (S_IXUSR >> 3) /* Execute by group. */
  93. /* Read, write, and execute by group. */
  94. # define S_IRWXG (S_IRWXU >> 3)
  95. # define S_IROTH (S_IRGRP >> 3) /* Read by others. */
  96. # define S_IWOTH (S_IWGRP >> 3) /* Write by others. */
  97. # define S_IXOTH (S_IXGRP >> 3) /* Execute by others. */
  98. /* Read, write, and execute by others. */
  99. # define S_IRWXO (S_IRWXG >> 3)
  100. #endif
  101. #ifdef __USE_MISC
  102. # ifndef R_OK /* Verbatim from <unistd.h>. Ugh. */
  103. /* Values for the second argument to access.
  104. These may be OR'd together. */
  105. # define R_OK 4 /* Test for read permission. */
  106. # define W_OK 2 /* Test for write permission. */
  107. # define X_OK 1 /* Test for execute permission. */
  108. # define F_OK 0 /* Test for existence. */
  109. # endif
  110. #endif /* Use misc. */
  111. /* XPG wants the following symbols. <stdio.h> has the same definitions. */
  112. #if defined __USE_XOPEN || defined __USE_XOPEN2K8
  113. # define SEEK_SET 0 /* Seek from beginning of file. */
  114. # define SEEK_CUR 1 /* Seek from current position. */
  115. # define SEEK_END 2 /* Seek from end of file. */
  116. #endif /* XPG */
  117. /* Do the file control operation described by CMD on FD.
  118. The remaining arguments are interpreted depending on CMD.
  119. This function is a cancellation point and therefore not marked with
  120. __THROW. */
  121. #ifndef __USE_FILE_OFFSET64
  122. extern int fcntl (int __fd, int __cmd, ...);
  123. #else
  124. # ifdef __REDIRECT
  125. extern int __REDIRECT (fcntl, (int __fd, int __cmd, ...), fcntl64);
  126. # else
  127. # define fcntl fcntl64
  128. # endif
  129. #endif
  130. #ifdef __USE_LARGEFILE64
  131. extern int fcntl64 (int __fd, int __cmd, ...);
  132. #endif
  133. /* Open FILE and return a new file descriptor for it, or -1 on error.
  134. OFLAG determines the type of access used. If O_CREAT or O_TMPFILE is set
  135. in OFLAG, the third argument is taken as a `mode_t', the mode of the
  136. created file.
  137. This function is a cancellation point and therefore not marked with
  138. __THROW. */
  139. #ifndef __USE_FILE_OFFSET64
  140. extern int open (const char *__file, int __oflag, ...) __nonnull ((1));
  141. #else
  142. # ifdef __REDIRECT
  143. extern int __REDIRECT (open, (const char *__file, int __oflag, ...), open64)
  144. __nonnull ((1));
  145. # else
  146. # define open open64
  147. # endif
  148. #endif
  149. #ifdef __USE_LARGEFILE64
  150. extern int open64 (const char *__file, int __oflag, ...) __nonnull ((1));
  151. #endif
  152. #ifdef __USE_ATFILE
  153. /* Similar to `open' but a relative path name is interpreted relative to
  154. the directory for which FD is a descriptor.
  155. NOTE: some other `openat' implementation support additional functionality
  156. through this interface, especially using the O_XATTR flag. This is not
  157. yet supported here.
  158. This function is a cancellation point and therefore not marked with
  159. __THROW. */
  160. # ifndef __USE_FILE_OFFSET64
  161. extern int openat (int __fd, const char *__file, int __oflag, ...)
  162. __nonnull ((2));
  163. # else
  164. # ifdef __REDIRECT
  165. extern int __REDIRECT (openat, (int __fd, const char *__file, int __oflag,
  166. ...), openat64) __nonnull ((2));
  167. # else
  168. # define openat openat64
  169. # endif
  170. # endif
  171. # ifdef __USE_LARGEFILE64
  172. extern int openat64 (int __fd, const char *__file, int __oflag, ...)
  173. __nonnull ((2));
  174. # endif
  175. #endif
  176. /* Create and open FILE, with mode MODE. This takes an `int' MODE
  177. argument because that is what `mode_t' will be widened to.
  178. This function is a cancellation point and therefore not marked with
  179. __THROW. */
  180. #ifndef __USE_FILE_OFFSET64
  181. extern int creat (const char *__file, mode_t __mode) __nonnull ((1));
  182. #else
  183. # ifdef __REDIRECT
  184. extern int __REDIRECT (creat, (const char *__file, mode_t __mode),
  185. creat64) __nonnull ((1));
  186. # else
  187. # define creat creat64
  188. # endif
  189. #endif
  190. #ifdef __USE_LARGEFILE64
  191. extern int creat64 (const char *__file, mode_t __mode) __nonnull ((1));
  192. #endif
  193. #if !defined F_LOCK && (defined __USE_MISC || (defined __USE_XOPEN_EXTENDED \
  194. && !defined __USE_POSIX))
  195. /* NOTE: These declarations also appear in <unistd.h>; be sure to keep both
  196. files consistent. Some systems have them there and some here, and some
  197. software depends on the macros being defined without including both. */
  198. /* `lockf' is a simpler interface to the locking facilities of `fcntl'.
  199. LEN is always relative to the current file position.
  200. The CMD argument is one of the following. */
  201. # define F_ULOCK 0 /* Unlock a previously locked region. */
  202. # define F_LOCK 1 /* Lock a region for exclusive use. */
  203. # define F_TLOCK 2 /* Test and lock a region for exclusive use. */
  204. # define F_TEST 3 /* Test a region for other processes locks. */
  205. # ifndef __USE_FILE_OFFSET64
  206. extern int lockf (int __fd, int __cmd, off_t __len);
  207. # else
  208. # ifdef __REDIRECT
  209. extern int __REDIRECT (lockf, (int __fd, int __cmd, __off64_t __len), lockf64);
  210. # else
  211. # define lockf lockf64
  212. # endif
  213. # endif
  214. # ifdef __USE_LARGEFILE64
  215. extern int lockf64 (int __fd, int __cmd, off64_t __len);
  216. # endif
  217. #endif
  218. #ifdef __USE_XOPEN2K
  219. /* Advice the system about the expected behaviour of the application with
  220. respect to the file associated with FD. */
  221. # ifndef __USE_FILE_OFFSET64
  222. extern int posix_fadvise (int __fd, off_t __offset, off_t __len,
  223. int __advise) __THROW;
  224. # else
  225. # ifdef __REDIRECT_NTH
  226. extern int __REDIRECT_NTH (posix_fadvise, (int __fd, __off64_t __offset,
  227. __off64_t __len, int __advise),
  228. posix_fadvise64);
  229. # else
  230. # define posix_fadvise posix_fadvise64
  231. # endif
  232. # endif
  233. # ifdef __USE_LARGEFILE64
  234. extern int posix_fadvise64 (int __fd, off64_t __offset, off64_t __len,
  235. int __advise) __THROW;
  236. # endif
  237. /* Reserve storage for the data of the file associated with FD.
  238. This function is a possible cancellation point and therefore not
  239. marked with __THROW. */
  240. # ifndef __USE_FILE_OFFSET64
  241. extern int posix_fallocate (int __fd, off_t __offset, off_t __len);
  242. # else
  243. # ifdef __REDIRECT
  244. extern int __REDIRECT (posix_fallocate, (int __fd, __off64_t __offset,
  245. __off64_t __len),
  246. posix_fallocate64);
  247. # else
  248. # define posix_fallocate posix_fallocate64
  249. # endif
  250. # endif
  251. # ifdef __USE_LARGEFILE64
  252. extern int posix_fallocate64 (int __fd, off64_t __offset, off64_t __len);
  253. # endif
  254. #endif
  255. /* Define some inlines helping to catch common problems. */
  256. #if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function \
  257. && defined __va_arg_pack_len
  258. # include <bits/fcntl2.h>
  259. #endif
  260. __END_DECLS
  261. #endif /* fcntl.h */