fcntl.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. /* Do the file control operation described by CMD on FD.
  117. The remaining arguments are interpreted depending on CMD.
  118. This function is a cancellation point and therefore not marked with
  119. __THROW. */
  120. extern int fcntl (int __fd, int __cmd, ...);
  121. /* Open FILE and return a new file descriptor for it, or -1 on error.
  122. OFLAG determines the type of access used. If O_CREAT or O_TMPFILE is set
  123. in OFLAG, the third argument is taken as a `mode_t', the mode of the
  124. created file.
  125. This function is a cancellation point and therefore not marked with
  126. __THROW. */
  127. #ifndef __USE_FILE_OFFSET64
  128. extern int open (const char *__file, int __oflag, ...) __nonnull ((1));
  129. #else
  130. # ifdef __REDIRECT
  131. extern int __REDIRECT (open, (const char *__file, int __oflag, ...), open64)
  132. __nonnull ((1));
  133. # else
  134. # define open open64
  135. # endif
  136. #endif
  137. #ifdef __USE_LARGEFILE64
  138. extern int open64 (const char *__file, int __oflag, ...) __nonnull ((1));
  139. #endif
  140. #ifdef __USE_ATFILE
  141. /* Similar to `open' but a relative path name is interpreted relative to
  142. the directory for which FD is a descriptor.
  143. NOTE: some other `openat' implementation support additional functionality
  144. through this interface, especially using the O_XATTR flag. This is not
  145. yet supported here.
  146. This function is a cancellation point and therefore not marked with
  147. __THROW. */
  148. # ifndef __USE_FILE_OFFSET64
  149. extern int openat (int __fd, const char *__file, int __oflag, ...)
  150. __nonnull ((2));
  151. # else
  152. # ifdef __REDIRECT
  153. extern int __REDIRECT (openat, (int __fd, const char *__file, int __oflag,
  154. ...), openat64) __nonnull ((2));
  155. # else
  156. # define openat openat64
  157. # endif
  158. # endif
  159. # ifdef __USE_LARGEFILE64
  160. extern int openat64 (int __fd, const char *__file, int __oflag, ...)
  161. __nonnull ((2));
  162. # endif
  163. #endif
  164. /* Create and open FILE, with mode MODE. This takes an `int' MODE
  165. argument because that is what `mode_t' will be widened to.
  166. This function is a cancellation point and therefore not marked with
  167. __THROW. */
  168. #ifndef __USE_FILE_OFFSET64
  169. extern int creat (const char *__file, mode_t __mode) __nonnull ((1));
  170. #else
  171. # ifdef __REDIRECT
  172. extern int __REDIRECT (creat, (const char *__file, mode_t __mode),
  173. creat64) __nonnull ((1));
  174. # else
  175. # define creat creat64
  176. # endif
  177. #endif
  178. #ifdef __USE_LARGEFILE64
  179. extern int creat64 (const char *__file, mode_t __mode) __nonnull ((1));
  180. #endif
  181. #if !defined F_LOCK && (defined __USE_MISC || (defined __USE_XOPEN_EXTENDED \
  182. && !defined __USE_POSIX))
  183. /* NOTE: These declarations also appear in <unistd.h>; be sure to keep both
  184. files consistent. Some systems have them there and some here, and some
  185. software depends on the macros being defined without including both. */
  186. /* `lockf' is a simpler interface to the locking facilities of `fcntl'.
  187. LEN is always relative to the current file position.
  188. The CMD argument is one of the following. */
  189. # define F_ULOCK 0 /* Unlock a previously locked region. */
  190. # define F_LOCK 1 /* Lock a region for exclusive use. */
  191. # define F_TLOCK 2 /* Test and lock a region for exclusive use. */
  192. # define F_TEST 3 /* Test a region for other processes locks. */
  193. # ifndef __USE_FILE_OFFSET64
  194. extern int lockf (int __fd, int __cmd, off_t __len);
  195. # else
  196. # ifdef __REDIRECT
  197. extern int __REDIRECT (lockf, (int __fd, int __cmd, __off64_t __len), lockf64);
  198. # else
  199. # define lockf lockf64
  200. # endif
  201. # endif
  202. # ifdef __USE_LARGEFILE64
  203. extern int lockf64 (int __fd, int __cmd, off64_t __len);
  204. # endif
  205. #endif
  206. #ifdef __USE_XOPEN2K
  207. /* Advice the system about the expected behaviour of the application with
  208. respect to the file associated with FD. */
  209. # ifndef __USE_FILE_OFFSET64
  210. extern int posix_fadvise (int __fd, off_t __offset, off_t __len,
  211. int __advise) __THROW;
  212. # else
  213. # ifdef __REDIRECT_NTH
  214. extern int __REDIRECT_NTH (posix_fadvise, (int __fd, __off64_t __offset,
  215. __off64_t __len, int __advise),
  216. posix_fadvise64);
  217. # else
  218. # define posix_fadvise posix_fadvise64
  219. # endif
  220. # endif
  221. # ifdef __USE_LARGEFILE64
  222. extern int posix_fadvise64 (int __fd, off64_t __offset, off64_t __len,
  223. int __advise) __THROW;
  224. # endif
  225. /* Reserve storage for the data of the file associated with FD.
  226. This function is a possible cancellation point and therefore not
  227. marked with __THROW. */
  228. # ifndef __USE_FILE_OFFSET64
  229. extern int posix_fallocate (int __fd, off_t __offset, off_t __len);
  230. # else
  231. # ifdef __REDIRECT
  232. extern int __REDIRECT (posix_fallocate, (int __fd, __off64_t __offset,
  233. __off64_t __len),
  234. posix_fallocate64);
  235. # else
  236. # define posix_fallocate posix_fallocate64
  237. # endif
  238. # endif
  239. # ifdef __USE_LARGEFILE64
  240. extern int posix_fallocate64 (int __fd, off64_t __offset, off64_t __len);
  241. # endif
  242. #endif
  243. /* Define some inlines helping to catch common problems. */
  244. #if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function \
  245. && defined __va_arg_pack_len
  246. # include <bits/fcntl2.h>
  247. #endif
  248. __END_DECLS
  249. #endif /* fcntl.h */