unistd.in.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /* Substitute for and wrapper around <unistd.h>.
  2. Copyright (C) 2003-2009 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program 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
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. #ifndef _GL_UNISTD_H
  15. #if __GNUC__ >= 3
  16. @PRAGMA_SYSTEM_HEADER@
  17. #endif
  18. /* The include_next requires a split double-inclusion guard. */
  19. #if @HAVE_UNISTD_H@
  20. # @INCLUDE_NEXT@ @NEXT_UNISTD_H@
  21. #endif
  22. #ifndef _GL_UNISTD_H
  23. #define _GL_UNISTD_H
  24. /* mingw doesn't define the SEEK_* or *_FILENO macros in <unistd.h>. */
  25. #if !(defined SEEK_CUR && defined SEEK_END && defined SEEK_SET)
  26. # include <stdio.h>
  27. #endif
  28. /* mingw fails to declare _exit in <unistd.h>. */
  29. /* mingw, BeOS, Haiku declare environ in <stdlib.h>, not in <unistd.h>. */
  30. #include <stdlib.h>
  31. #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
  32. /* Get ssize_t. */
  33. # include <sys/types.h>
  34. #endif
  35. #if @GNULIB_GETHOSTNAME@
  36. /* Get all possible declarations of gethostname(). */
  37. # if @UNISTD_H_HAVE_WINSOCK2_H@
  38. # include <winsock2.h>
  39. # if !defined _GL_SYS_SOCKET_H
  40. # undef socket
  41. # define socket socket_used_without_including_sys_socket_h
  42. # undef connect
  43. # define connect connect_used_without_including_sys_socket_h
  44. # undef accept
  45. # define accept accept_used_without_including_sys_socket_h
  46. # undef bind
  47. # define bind bind_used_without_including_sys_socket_h
  48. # undef getpeername
  49. # define getpeername getpeername_used_without_including_sys_socket_h
  50. # undef getsockname
  51. # define getsockname getsockname_used_without_including_sys_socket_h
  52. # undef getsockopt
  53. # define getsockopt getsockopt_used_without_including_sys_socket_h
  54. # undef listen
  55. # define listen listen_used_without_including_sys_socket_h
  56. # undef recv
  57. # define recv recv_used_without_including_sys_socket_h
  58. # undef send
  59. # define send send_used_without_including_sys_socket_h
  60. # undef recvfrom
  61. # define recvfrom recvfrom_used_without_including_sys_socket_h
  62. # undef sendto
  63. # define sendto sendto_used_without_including_sys_socket_h
  64. # undef setsockopt
  65. # define setsockopt setsockopt_used_without_including_sys_socket_h
  66. # undef shutdown
  67. # define shutdown shutdown_used_without_including_sys_socket_h
  68. # endif
  69. # if !defined _GL_SYS_SELECT_H
  70. # undef select
  71. # define select select_used_without_including_sys_select_h
  72. # endif
  73. # endif
  74. #endif
  75. /* The definition of GL_LINK_WARNING is copied here. */
  76. /* OS/2 EMX lacks these macros. */
  77. #ifndef STDIN_FILENO
  78. # define STDIN_FILENO 0
  79. #endif
  80. #ifndef STDOUT_FILENO
  81. # define STDOUT_FILENO 1
  82. #endif
  83. #ifndef STDERR_FILENO
  84. # define STDERR_FILENO 2
  85. #endif
  86. /* Declare overridden functions. */
  87. #ifdef __cplusplus
  88. extern "C" {
  89. #endif
  90. #if @GNULIB_CHOWN@
  91. # if @REPLACE_CHOWN@
  92. # ifndef REPLACE_CHOWN
  93. # define REPLACE_CHOWN 1
  94. # endif
  95. # if REPLACE_CHOWN
  96. /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
  97. to GID (if GID is not -1). Follow symbolic links.
  98. Return 0 if successful, otherwise -1 and errno set.
  99. See the POSIX:2001 specification
  100. <http://www.opengroup.org/susv3xsh/chown.html>. */
  101. # define chown rpl_chown
  102. extern int chown (const char *file, uid_t uid, gid_t gid);
  103. # endif
  104. # endif
  105. #elif defined GNULIB_POSIXCHECK
  106. # undef chown
  107. # define chown(f,u,g) \
  108. (GL_LINK_WARNING ("chown fails to follow symlinks on some systems and " \
  109. "doesn't treat a uid or gid of -1 on some systems - " \
  110. "use gnulib module chown for portability"), \
  111. chown (f, u, g))
  112. #endif
  113. #if @GNULIB_CLOSE@
  114. # if @REPLACE_CLOSE@
  115. /* Automatically included by modules that need a replacement for close. */
  116. # undef close
  117. # define close rpl_close
  118. extern int close (int);
  119. # endif
  120. #elif @UNISTD_H_HAVE_WINSOCK2_H@
  121. # undef close
  122. # define close close_used_without_requesting_gnulib_module_close
  123. #elif defined GNULIB_POSIXCHECK
  124. # undef close
  125. # define close(f) \
  126. (GL_LINK_WARNING ("close does not portably work on sockets - " \
  127. "use gnulib module close for portability"), \
  128. close (f))
  129. #endif
  130. #if @GNULIB_DUP2@
  131. # if !@HAVE_DUP2@
  132. /* Copy the file descriptor OLDFD into file descriptor NEWFD. Do nothing if
  133. NEWFD = OLDFD, otherwise close NEWFD first if it is open.
  134. Return 0 if successful, otherwise -1 and errno set.
  135. See the POSIX:2001 specification
  136. <http://www.opengroup.org/susv3xsh/dup2.html>. */
  137. extern int dup2 (int oldfd, int newfd);
  138. # endif
  139. #elif defined GNULIB_POSIXCHECK
  140. # undef dup2
  141. # define dup2(o,n) \
  142. (GL_LINK_WARNING ("dup2 is unportable - " \
  143. "use gnulib module dup2 for portability"), \
  144. dup2 (o, n))
  145. #endif
  146. #if @GNULIB_ENVIRON@
  147. # if !@HAVE_DECL_ENVIRON@
  148. /* Set of environment variables and values. An array of strings of the form
  149. "VARIABLE=VALUE", terminated with a NULL. */
  150. # if defined __APPLE__ && defined __MACH__
  151. # include <crt_externs.h>
  152. # define environ (*_NSGetEnviron ())
  153. # else
  154. extern char **environ;
  155. # endif
  156. # endif
  157. #elif defined GNULIB_POSIXCHECK
  158. # undef environ
  159. # define environ \
  160. (GL_LINK_WARNING ("environ is unportable - " \
  161. "use gnulib module environ for portability"), \
  162. environ)
  163. #endif
  164. #if @GNULIB_EUIDACCESS@
  165. # if !@HAVE_EUIDACCESS@
  166. /* Like access(), except that is uses the effective user id and group id of
  167. the current process. */
  168. extern int euidaccess (const char *filename, int mode);
  169. # endif
  170. #elif defined GNULIB_POSIXCHECK
  171. # undef euidaccess
  172. # define euidaccess(f,m) \
  173. (GL_LINK_WARNING ("euidaccess is unportable - " \
  174. "use gnulib module euidaccess for portability"), \
  175. euidaccess (f, m))
  176. #endif
  177. #if @GNULIB_FCHDIR@
  178. # if @REPLACE_FCHDIR@
  179. /* Change the process' current working directory to the directory on which
  180. the given file descriptor is open.
  181. Return 0 if successful, otherwise -1 and errno set.
  182. See the POSIX:2001 specification
  183. <http://www.opengroup.org/susv3xsh/fchdir.html>. */
  184. extern int fchdir (int /*fd*/);
  185. # define dup rpl_dup
  186. extern int dup (int);
  187. # define dup2 rpl_dup2
  188. extern int dup2 (int, int);
  189. # endif
  190. #elif defined GNULIB_POSIXCHECK
  191. # undef fchdir
  192. # define fchdir(f) \
  193. (GL_LINK_WARNING ("fchdir is unportable - " \
  194. "use gnulib module fchdir for portability"), \
  195. fchdir (f))
  196. #endif
  197. #if @GNULIB_FSYNC@
  198. /* Synchronize changes to a file.
  199. Return 0 if successful, otherwise -1 and errno set.
  200. See POSIX:2001 specification
  201. <http://www.opengroup.org/susv3xsh/fsync.html>. */
  202. # if !@HAVE_FSYNC@
  203. extern int fsync (int fd);
  204. # endif
  205. #elif defined GNULIB_POSIXCHECK
  206. # undef fsync
  207. # define fsync(fd) \
  208. (GL_LINK_WARNING ("fsync is unportable - " \
  209. "use gnulib module fsync for portability"), \
  210. fsync (fd))
  211. #endif
  212. #if @GNULIB_FTRUNCATE@
  213. # if !@HAVE_FTRUNCATE@
  214. /* Change the size of the file to which FD is opened to become equal to LENGTH.
  215. Return 0 if successful, otherwise -1 and errno set.
  216. See the POSIX:2001 specification
  217. <http://www.opengroup.org/susv3xsh/ftruncate.html>. */
  218. extern int ftruncate (int fd, off_t length);
  219. # endif
  220. #elif defined GNULIB_POSIXCHECK
  221. # undef ftruncate
  222. # define ftruncate(f,l) \
  223. (GL_LINK_WARNING ("ftruncate is unportable - " \
  224. "use gnulib module ftruncate for portability"), \
  225. ftruncate (f, l))
  226. #endif
  227. #if @GNULIB_GETCWD@
  228. /* Include the headers that might declare getcwd so that they will not
  229. cause confusion if included after this file. */
  230. # include <stdlib.h>
  231. # if @REPLACE_GETCWD@
  232. /* Get the name of the current working directory, and put it in SIZE bytes
  233. of BUF.
  234. Return BUF if successful, or NULL if the directory couldn't be determined
  235. or SIZE was too small.
  236. See the POSIX:2001 specification
  237. <http://www.opengroup.org/susv3xsh/getcwd.html>.
  238. Additionally, the gnulib module 'getcwd' guarantees the following GNU
  239. extension: If BUF is NULL, an array is allocated with 'malloc'; the array
  240. is SIZE bytes long, unless SIZE == 0, in which case it is as big as
  241. necessary. */
  242. # define getcwd rpl_getcwd
  243. extern char * getcwd (char *buf, size_t size);
  244. # endif
  245. #elif defined GNULIB_POSIXCHECK
  246. # undef getcwd
  247. # define getcwd(b,s) \
  248. (GL_LINK_WARNING ("getcwd is unportable - " \
  249. "use gnulib module getcwd for portability"), \
  250. getcwd (b, s))
  251. #endif
  252. #if @GNULIB_GETDOMAINNAME@
  253. /* Return the NIS domain name of the machine.
  254. WARNING! The NIS domain name is unrelated to the fully qualified host name
  255. of the machine. It is also unrelated to email addresses.
  256. WARNING! The NIS domain name is usually the empty string or "(none)" when
  257. not using NIS.
  258. Put up to LEN bytes of the NIS domain name into NAME.
  259. Null terminate it if the name is shorter than LEN.
  260. If the NIS domain name is longer than LEN, set errno = EINVAL and return -1.
  261. Return 0 if successful, otherwise set errno and return -1. */
  262. # if !@HAVE_GETDOMAINNAME@
  263. extern int getdomainname(char *name, size_t len);
  264. # endif
  265. #elif defined GNULIB_POSIXCHECK
  266. # undef getdomainname
  267. # define getdomainname(n,l) \
  268. (GL_LINK_WARNING ("getdomainname is unportable - " \
  269. "use gnulib module getdomainname for portability"), \
  270. getdomainname (n, l))
  271. #endif
  272. #if @GNULIB_GETDTABLESIZE@
  273. # if !@HAVE_GETDTABLESIZE@
  274. /* Return the maximum number of file descriptors in the current process. */
  275. extern int getdtablesize (void);
  276. # endif
  277. #elif defined GNULIB_POSIXCHECK
  278. # undef getdtablesize
  279. # define getdtablesize() \
  280. (GL_LINK_WARNING ("getdtablesize is unportable - " \
  281. "use gnulib module getdtablesize for portability"), \
  282. getdtablesize ())
  283. #endif
  284. #if @GNULIB_GETHOSTNAME@
  285. /* Return the standard host name of the machine.
  286. WARNING! The host name may or may not be fully qualified.
  287. Put up to LEN bytes of the host name into NAME.
  288. Null terminate it if the name is shorter than LEN.
  289. If the host name is longer than LEN, set errno = EINVAL and return -1.
  290. Return 0 if successful, otherwise set errno and return -1. */
  291. # if @UNISTD_H_HAVE_WINSOCK2_H@
  292. # undef gethostname
  293. # define gethostname rpl_gethostname
  294. # endif
  295. # if @UNISTD_H_HAVE_WINSOCK2_H@ || !@HAVE_GETHOSTNAME@
  296. extern int gethostname(char *name, size_t len);
  297. # endif
  298. #elif @UNISTD_H_HAVE_WINSOCK2_H@
  299. # undef gethostname
  300. # define gethostname gethostname_used_without_requesting_gnulib_module_gethostname
  301. #elif defined GNULIB_POSIXCHECK
  302. # undef gethostname
  303. # define gethostname(n,l) \
  304. (GL_LINK_WARNING ("gethostname is unportable - " \
  305. "use gnulib module gethostname for portability"), \
  306. gethostname (n, l))
  307. #endif
  308. #if @GNULIB_GETLOGIN_R@
  309. /* Copies the user's login name to NAME.
  310. The array pointed to by NAME has room for SIZE bytes.
  311. Returns 0 if successful. Upon error, an error number is returned, or -1 in
  312. the case that the login name cannot be found but no specific error is
  313. provided (this case is hopefully rare but is left open by the POSIX spec).
  314. See <http://www.opengroup.org/susv3xsh/getlogin.html>.
  315. */
  316. # if !@HAVE_DECL_GETLOGIN_R@
  317. # include <stddef.h>
  318. extern int getlogin_r (char *name, size_t size);
  319. # endif
  320. #elif defined GNULIB_POSIXCHECK
  321. # undef getlogin_r
  322. # define getlogin_r(n,s) \
  323. (GL_LINK_WARNING ("getlogin_r is unportable - " \
  324. "use gnulib module getlogin_r for portability"), \
  325. getlogin_r (n, s))
  326. #endif
  327. #if @GNULIB_GETPAGESIZE@
  328. # if @REPLACE_GETPAGESIZE@
  329. # define getpagesize rpl_getpagesize
  330. extern int getpagesize (void);
  331. # elif !@HAVE_GETPAGESIZE@
  332. /* This is for POSIX systems. */
  333. # if !defined getpagesize && defined _SC_PAGESIZE
  334. # if ! (defined __VMS && __VMS_VER < 70000000)
  335. # define getpagesize() sysconf (_SC_PAGESIZE)
  336. # endif
  337. # endif
  338. /* This is for older VMS. */
  339. # if !defined getpagesize && defined __VMS
  340. # ifdef __ALPHA
  341. # define getpagesize() 8192
  342. # else
  343. # define getpagesize() 512
  344. # endif
  345. # endif
  346. /* This is for BeOS. */
  347. # if !defined getpagesize && @HAVE_OS_H@
  348. # include <OS.h>
  349. # if defined B_PAGE_SIZE
  350. # define getpagesize() B_PAGE_SIZE
  351. # endif
  352. # endif
  353. /* This is for AmigaOS4.0. */
  354. # if !defined getpagesize && defined __amigaos4__
  355. # define getpagesize() 2048
  356. # endif
  357. /* This is for older Unix systems. */
  358. # if !defined getpagesize && @HAVE_SYS_PARAM_H@
  359. # include <sys/param.h>
  360. # ifdef EXEC_PAGESIZE
  361. # define getpagesize() EXEC_PAGESIZE
  362. # else
  363. # ifdef NBPG
  364. # ifndef CLSIZE
  365. # define CLSIZE 1
  366. # endif
  367. # define getpagesize() (NBPG * CLSIZE)
  368. # else
  369. # ifdef NBPC
  370. # define getpagesize() NBPC
  371. # endif
  372. # endif
  373. # endif
  374. # endif
  375. # endif
  376. #elif defined GNULIB_POSIXCHECK
  377. # undef getpagesize
  378. # define getpagesize() \
  379. (GL_LINK_WARNING ("getpagesize is unportable - " \
  380. "use gnulib module getpagesize for portability"), \
  381. getpagesize ())
  382. #endif
  383. #if @GNULIB_GETUSERSHELL@
  384. # if !@HAVE_GETUSERSHELL@
  385. /* Return the next valid login shell on the system, or NULL when the end of
  386. the list has been reached. */
  387. extern char *getusershell (void);
  388. /* Rewind to pointer that is advanced at each getusershell() call. */
  389. extern void setusershell (void);
  390. /* Free the pointer that is advanced at each getusershell() call and
  391. associated resources. */
  392. extern void endusershell (void);
  393. # endif
  394. #elif defined GNULIB_POSIXCHECK
  395. # undef getusershell
  396. # define getusershell() \
  397. (GL_LINK_WARNING ("getusershell is unportable - " \
  398. "use gnulib module getusershell for portability"), \
  399. getusershell ())
  400. # undef setusershell
  401. # define setusershell() \
  402. (GL_LINK_WARNING ("setusershell is unportable - " \
  403. "use gnulib module getusershell for portability"), \
  404. setusershell ())
  405. # undef endusershell
  406. # define endusershell() \
  407. (GL_LINK_WARNING ("endusershell is unportable - " \
  408. "use gnulib module getusershell for portability"), \
  409. endusershell ())
  410. #endif
  411. #if @GNULIB_LCHOWN@
  412. # if @REPLACE_LCHOWN@
  413. /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
  414. to GID (if GID is not -1). Do not follow symbolic links.
  415. Return 0 if successful, otherwise -1 and errno set.
  416. See the POSIX:2001 specification
  417. <http://www.opengroup.org/susv3xsh/lchown.html>. */
  418. # define lchown rpl_lchown
  419. extern int lchown (char const *file, uid_t owner, gid_t group);
  420. # endif
  421. #elif defined GNULIB_POSIXCHECK
  422. # undef lchown
  423. # define lchown(f,u,g) \
  424. (GL_LINK_WARNING ("lchown is unportable to pre-POSIX.1-2001 " \
  425. "systems - use gnulib module lchown for portability"), \
  426. lchown (f, u, g))
  427. #endif
  428. #if @GNULIB_LINK@
  429. /* Create a new hard link for an existing file.
  430. Return 0 if successful, otherwise -1 and errno set.
  431. See POSIX:2001 specification
  432. <http://www.opengroup.org/susv3xsh/link.html>. */
  433. # if !@HAVE_LINK@
  434. extern int link (const char *path1, const char *path2);
  435. # endif
  436. #elif defined GNULIB_POSIXCHECK
  437. # undef link
  438. # define link(path1,path2) \
  439. (GL_LINK_WARNING ("link is unportable - " \
  440. "use gnulib module link for portability"), \
  441. link (path1, path2))
  442. #endif
  443. #if @GNULIB_LSEEK@
  444. # if @REPLACE_LSEEK@
  445. /* Set the offset of FD relative to SEEK_SET, SEEK_CUR, or SEEK_END.
  446. Return the new offset if successful, otherwise -1 and errno set.
  447. See the POSIX:2001 specification
  448. <http://www.opengroup.org/susv3xsh/lseek.html>. */
  449. # define lseek rpl_lseek
  450. extern off_t lseek (int fd, off_t offset, int whence);
  451. # endif
  452. #elif defined GNULIB_POSIXCHECK
  453. # undef lseek
  454. # define lseek(f,o,w) \
  455. (GL_LINK_WARNING ("lseek does not fail with ESPIPE on pipes on some " \
  456. "systems - use gnulib module lseek for portability"), \
  457. lseek (f, o, w))
  458. #endif
  459. #if @GNULIB_READLINK@
  460. /* Read the contents of the symbolic link FILE and place the first BUFSIZE
  461. bytes of it into BUF. Return the number of bytes placed into BUF if
  462. successful, otherwise -1 and errno set.
  463. See the POSIX:2001 specification
  464. <http://www.opengroup.org/susv3xsh/readlink.html>. */
  465. # if !@HAVE_READLINK@
  466. # include <stddef.h>
  467. extern int readlink (const char *file, char *buf, size_t bufsize);
  468. # endif
  469. #elif defined GNULIB_POSIXCHECK
  470. # undef readlink
  471. # define readlink(f,b,s) \
  472. (GL_LINK_WARNING ("readlink is unportable - " \
  473. "use gnulib module readlink for portability"), \
  474. readlink (f, b, s))
  475. #endif
  476. #if @GNULIB_SLEEP@
  477. /* Pause the execution of the current thread for N seconds.
  478. Returns the number of seconds left to sleep.
  479. See the POSIX:2001 specification
  480. <http://www.opengroup.org/susv3xsh/sleep.html>. */
  481. # if !@HAVE_SLEEP@
  482. extern unsigned int sleep (unsigned int n);
  483. # endif
  484. #elif defined GNULIB_POSIXCHECK
  485. # undef sleep
  486. # define sleep(n) \
  487. (GL_LINK_WARNING ("sleep is unportable - " \
  488. "use gnulib module sleep for portability"), \
  489. sleep (n))
  490. #endif
  491. #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
  492. /* Write up to COUNT bytes starting at BUF to file descriptor FD.
  493. See the POSIX:2001 specification
  494. <http://www.opengroup.org/susv3xsh/write.html>. */
  495. # undef write
  496. # define write rpl_write
  497. extern ssize_t write (int fd, const void *buf, size_t count);
  498. #endif
  499. #ifdef FCHDIR_REPLACEMENT
  500. /* gnulib internal function. */
  501. extern void _gl_unregister_fd (int fd);
  502. #endif
  503. #ifdef __cplusplus
  504. }
  505. #endif
  506. #endif /* _GL_UNISTD_H */
  507. #endif /* _GL_UNISTD_H */