fcntl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* Copyright (C) 1991-2016 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: 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. #if defined __USE_XOPEN || defined __USE_XOPEN2K8
  60. # define __need_timespec
  61. # include <time.h>
  62. # include <bits/stat.h>
  63. # define S_IFMT __S_IFMT
  64. # define S_IFDIR __S_IFDIR
  65. # define S_IFCHR __S_IFCHR
  66. # define S_IFBLK __S_IFBLK
  67. # define S_IFREG __S_IFREG
  68. # ifdef __S_IFIFO
  69. # define S_IFIFO __S_IFIFO
  70. # endif
  71. # ifdef __S_IFLNK
  72. # define S_IFLNK __S_IFLNK
  73. # endif
  74. # if (defined __USE_UNIX98 || defined __USE_XOPEN2K8) && defined __S_IFSOCK
  75. # define S_IFSOCK __S_IFSOCK
  76. # endif
  77. /* Protection bits. */
  78. # define S_ISUID __S_ISUID /* Set user ID on execution. */
  79. # define S_ISGID __S_ISGID /* Set group ID on execution. */
  80. # if defined __USE_MISC || defined __USE_XOPEN
  81. /* Save swapped text after use (sticky bit). This is pretty well obsolete. */
  82. # define S_ISVTX __S_ISVTX
  83. # endif
  84. # define S_IRUSR __S_IREAD /* Read by owner. */
  85. # define S_IWUSR __S_IWRITE /* Write by owner. */
  86. # define S_IXUSR __S_IEXEC /* Execute by owner. */
  87. /* Read, write, and execute by owner. */
  88. # define S_IRWXU (__S_IREAD|__S_IWRITE|__S_IEXEC)
  89. # define S_IRGRP (S_IRUSR >> 3) /* Read by group. */
  90. # define S_IWGRP (S_IWUSR >> 3) /* Write by group. */
  91. # define S_IXGRP (S_IXUSR >> 3) /* Execute by group. */
  92. /* Read, write, and execute by group. */
  93. # define S_IRWXG (S_IRWXU >> 3)
  94. # define S_IROTH (S_IRGRP >> 3) /* Read by others. */
  95. # define S_IWOTH (S_IWGRP >> 3) /* Write by others. */
  96. # define S_IXOTH (S_IXGRP >> 3) /* Execute by others. */
  97. /* Read, write, and execute by others. */
  98. # define S_IRWXO (S_IRWXG >> 3)
  99. #endif
  100. #ifdef __USE_MISC
  101. # ifndef R_OK /* Verbatim from <unistd.h>. Ugh. */
  102. /* Values for the second argument to access.
  103. These may be OR'd together. */
  104. # define R_OK 4 /* Test for read permission. */
  105. # define W_OK 2 /* Test for write permission. */
  106. # define X_OK 1 /* Test for execute permission. */
  107. # define F_OK 0 /* Test for existence. */
  108. # endif
  109. #endif /* Use misc. */
  110. /* XPG wants the following symbols. <stdio.h> has the same definitions. */
  111. #if defined __USE_XOPEN || defined __USE_XOPEN2K8
  112. # define SEEK_SET 0 /* Seek from beginning of file. */
  113. # define SEEK_CUR 1 /* Seek from current position. */
  114. # define SEEK_END 2 /* Seek from end of file. */
  115. #endif /* XPG */
  116. /* The constants AT_REMOVEDIR and AT_EACCESS have the same value. AT_EASSESS
  117. is meaningful only to faccessat, while AT_REMOVEDIR is meaningful only to
  118. unlinkat. The two functions do completely different things and therefore,
  119. the flags can be allowed to overlap. For example, passing AT_REMOVEDIR to
  120. faccessat would be undefined behavior and thus treating it equivalent to
  121. AT_EACCESS is valid undefined behavior. */
  122. #ifdef __USE_ATFILE
  123. # define AT_FDCWD -100 /* Special value used to indicate
  124. the *at functions should use the
  125. current working directory. */
  126. # define AT_SYMLINK_NOFOLLOW 0x100 /* Do not follow symbolic links. */
  127. # define AT_REMOVEDIR 0x200 /* Remove directory instead of
  128. unlinking file. */
  129. # define AT_SYMLINK_FOLLOW 0x400 /* Follow symbolic links. */
  130. # ifdef __USE_GNU
  131. # define AT_NO_AUTOMOUNT 0x800 /* Suppress terminal automount
  132. traversal. */
  133. # define AT_EMPTY_PATH 0x1000 /* Allow empty relative pathname. */
  134. # endif
  135. # define AT_EACCESS 0x200 /* Test access permitted for
  136. effective IDs, not real IDs. */
  137. #endif
  138. /* Do the file control operation described by CMD on FD.
  139. The remaining arguments are interpreted depending on CMD.
  140. This function is a cancellation point and therefore not marked with
  141. __THROW. */
  142. extern int fcntl (int __fd, int __cmd, ...);
  143. /* Open FILE and return a new file descriptor for it, or -1 on error.
  144. OFLAG determines the type of access used. If O_CREAT or O_TMPFILE is set
  145. in OFLAG, the third argument is taken as a `mode_t', the mode of the
  146. created file.
  147. This function is a cancellation point and therefore not marked with
  148. __THROW. */
  149. #ifndef __USE_FILE_OFFSET64
  150. extern int open (const char *__file, int __oflag, ...) __nonnull ((1));
  151. #else
  152. # ifdef __REDIRECT
  153. extern int __REDIRECT (open, (const char *__file, int __oflag, ...), open64)
  154. __nonnull ((1));
  155. # else
  156. # define open open64
  157. # endif
  158. #endif
  159. #ifdef __USE_LARGEFILE64
  160. extern int open64 (const char *__file, int __oflag, ...) __nonnull ((1));
  161. #endif
  162. #ifdef __USE_ATFILE
  163. /* Similar to `open' but a relative path name is interpreted relative to
  164. the directory for which FD is a descriptor.
  165. NOTE: some other `openat' implementation support additional functionality
  166. through this interface, especially using the O_XATTR flag. This is not
  167. yet supported here.
  168. This function is a cancellation point and therefore not marked with
  169. __THROW. */
  170. # ifndef __USE_FILE_OFFSET64
  171. extern int openat (int __fd, const char *__file, int __oflag, ...)
  172. __nonnull ((2));
  173. # else
  174. # ifdef __REDIRECT
  175. extern int __REDIRECT (openat, (int __fd, const char *__file, int __oflag,
  176. ...), openat64) __nonnull ((2));
  177. # else
  178. # define openat openat64
  179. # endif
  180. # endif
  181. # ifdef __USE_LARGEFILE64
  182. extern int openat64 (int __fd, const char *__file, int __oflag, ...)
  183. __nonnull ((2));
  184. # endif
  185. #endif
  186. /* Create and open FILE, with mode MODE. This takes an `int' MODE
  187. argument because that is what `mode_t' will be widened to.
  188. This function is a cancellation point and therefore not marked with
  189. __THROW. */
  190. #ifndef __USE_FILE_OFFSET64
  191. extern int creat (const char *__file, mode_t __mode) __nonnull ((1));
  192. #else
  193. # ifdef __REDIRECT
  194. extern int __REDIRECT (creat, (const char *__file, mode_t __mode),
  195. creat64) __nonnull ((1));
  196. # else
  197. # define creat creat64
  198. # endif
  199. #endif
  200. #ifdef __USE_LARGEFILE64
  201. extern int creat64 (const char *__file, mode_t __mode) __nonnull ((1));
  202. #endif
  203. #if !defined F_LOCK && (defined __USE_MISC || (defined __USE_XOPEN_EXTENDED \
  204. && !defined __USE_POSIX))
  205. /* NOTE: These declarations also appear in <unistd.h>; be sure to keep both
  206. files consistent. Some systems have them there and some here, and some
  207. software depends on the macros being defined without including both. */
  208. /* `lockf' is a simpler interface to the locking facilities of `fcntl'.
  209. LEN is always relative to the current file position.
  210. The CMD argument is one of the following. */
  211. # define F_ULOCK 0 /* Unlock a previously locked region. */
  212. # define F_LOCK 1 /* Lock a region for exclusive use. */
  213. # define F_TLOCK 2 /* Test and lock a region for exclusive use. */
  214. # define F_TEST 3 /* Test a region for other processes locks. */
  215. # ifndef __USE_FILE_OFFSET64
  216. extern int lockf (int __fd, int __cmd, off_t __len);
  217. # else
  218. # ifdef __REDIRECT
  219. extern int __REDIRECT (lockf, (int __fd, int __cmd, __off64_t __len), lockf64);
  220. # else
  221. # define lockf lockf64
  222. # endif
  223. # endif
  224. # ifdef __USE_LARGEFILE64
  225. extern int lockf64 (int __fd, int __cmd, off64_t __len);
  226. # endif
  227. #endif
  228. #ifdef __USE_XOPEN2K
  229. /* Advice the system about the expected behaviour of the application with
  230. respect to the file associated with FD. */
  231. # ifndef __USE_FILE_OFFSET64
  232. extern int posix_fadvise (int __fd, off_t __offset, off_t __len,
  233. int __advise) __THROW;
  234. # else
  235. # ifdef __REDIRECT_NTH
  236. extern int __REDIRECT_NTH (posix_fadvise, (int __fd, __off64_t __offset,
  237. __off64_t __len, int __advise),
  238. posix_fadvise64);
  239. # else
  240. # define posix_fadvise posix_fadvise64
  241. # endif
  242. # endif
  243. # ifdef __USE_LARGEFILE64
  244. extern int posix_fadvise64 (int __fd, off64_t __offset, off64_t __len,
  245. int __advise) __THROW;
  246. # endif
  247. /* Reserve storage for the data of the file associated with FD.
  248. This function is a possible cancellation point and therefore not
  249. marked with __THROW. */
  250. # ifndef __USE_FILE_OFFSET64
  251. extern int posix_fallocate (int __fd, off_t __offset, off_t __len);
  252. # else
  253. # ifdef __REDIRECT
  254. extern int __REDIRECT (posix_fallocate, (int __fd, __off64_t __offset,
  255. __off64_t __len),
  256. posix_fallocate64);
  257. # else
  258. # define posix_fallocate posix_fallocate64
  259. # endif
  260. # endif
  261. # ifdef __USE_LARGEFILE64
  262. extern int posix_fallocate64 (int __fd, off64_t __offset, off64_t __len);
  263. # endif
  264. #endif
  265. /* Define some inlines helping to catch common problems. */
  266. #if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function \
  267. && defined __va_arg_pack_len
  268. # include <bits/fcntl2.h>
  269. #endif
  270. __END_DECLS
  271. #endif /* fcntl.h */