1 /* Substitute for and wrapper around <unistd.h>.
2 Copyright (C) 2003-2009 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 @PRAGMA_SYSTEM_HEADER@
24 /* The include_next requires a split double-inclusion guard. */
26 # @INCLUDE_NEXT@ @NEXT_UNISTD_H@
32 /* NetBSD 5.0 mis-defines NULL. Also get size_t. */
35 /* mingw doesn't define the SEEK_* or *_FILENO macros in <unistd.h>. */
36 #if !(defined SEEK_CUR && defined SEEK_END && defined SEEK_SET)
40 /* mingw fails to declare _exit in <unistd.h>. */
41 /* mingw, BeOS, Haiku declare environ in <stdlib.h>, not in <unistd.h>. */
44 #if ((@GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@) \
45 || (@GNULIB_READLINK@ && (!@HAVE_READLINK@ || @REPLACE_READLINK@)) \
46 || (@GNULIB_READLINKAT@ && !@HAVE_READLINKAT@))
48 # include <sys/types.h>
51 /* Get getopt(), optarg, optind, opterr, optopt. */
52 #if @GNULIB_UNISTD_H_GETOPT@ && !defined _GL_SYSTEM_GETOPT
56 #if @GNULIB_GETHOSTNAME@
57 /* Get all possible declarations of gethostname(). */
58 # if @UNISTD_H_HAVE_WINSOCK2_H@
59 # include <winsock2.h>
60 # if !defined _GL_SYS_SOCKET_H
62 # define socket socket_used_without_including_sys_socket_h
64 # define connect connect_used_without_including_sys_socket_h
66 # define accept accept_used_without_including_sys_socket_h
68 # define bind bind_used_without_including_sys_socket_h
70 # define getpeername getpeername_used_without_including_sys_socket_h
72 # define getsockname getsockname_used_without_including_sys_socket_h
74 # define getsockopt getsockopt_used_without_including_sys_socket_h
76 # define listen listen_used_without_including_sys_socket_h
78 # define recv recv_used_without_including_sys_socket_h
80 # define send send_used_without_including_sys_socket_h
82 # define recvfrom recvfrom_used_without_including_sys_socket_h
84 # define sendto sendto_used_without_including_sys_socket_h
86 # define setsockopt setsockopt_used_without_including_sys_socket_h
88 # define shutdown shutdown_used_without_including_sys_socket_h
90 # if !defined _GL_SYS_SELECT_H
92 # define select select_used_without_including_sys_select_h
97 /* The definition of GL_LINK_WARNING is copied here. */
100 /* OS/2 EMX lacks these macros. */
102 # define STDIN_FILENO 0
104 #ifndef STDOUT_FILENO
105 # define STDOUT_FILENO 1
107 #ifndef STDERR_FILENO
108 # define STDERR_FILENO 2
111 /* Ensure *_OK macros exist. */
120 /* Declare overridden functions. */
129 # ifndef REPLACE_CHOWN
130 # define REPLACE_CHOWN 1
133 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
134 to GID (if GID is not -1). Follow symbolic links.
135 Return 0 if successful, otherwise -1 and errno set.
136 See the POSIX:2001 specification
137 <http://www.opengroup.org/susv3xsh/chown.html>. */
138 # define chown rpl_chown
139 extern int chown (const char *file, uid_t uid, gid_t gid);
142 #elif defined GNULIB_POSIXCHECK
144 # define chown(f,u,g) \
145 (GL_LINK_WARNING ("chown fails to follow symlinks on some systems and " \
146 "doesn't treat a uid or gid of -1 on some systems - " \
147 "use gnulib module chown for portability"), \
154 /* Automatically included by modules that need a replacement for close. */
156 # define close rpl_close
157 extern int close (int);
159 #elif @UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS@
161 # define close close_used_without_requesting_gnulib_module_close
162 #elif defined GNULIB_POSIXCHECK
165 (GL_LINK_WARNING ("close does not portably work on sockets - " \
166 "use gnulib module close for portability"), \
173 extern int dup (int);
179 # define dup2 rpl_dup2
181 # if !@HAVE_DUP2@ || @REPLACE_DUP2@
182 /* Copy the file descriptor OLDFD into file descriptor NEWFD. Do nothing if
183 NEWFD = OLDFD, otherwise close NEWFD first if it is open.
184 Return newfd if successful, otherwise -1 and errno set.
185 See the POSIX:2001 specification
186 <http://www.opengroup.org/susv3xsh/dup2.html>. */
187 extern int dup2 (int oldfd, int newfd);
189 #elif defined GNULIB_POSIXCHECK
192 (GL_LINK_WARNING ("dup2 is unportable - " \
193 "use gnulib module dup2 for portability"), \
199 /* Copy the file descriptor OLDFD into file descriptor NEWFD, with the
201 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
202 and O_TEXT, O_BINARY (defined in "binary-io.h").
203 Close NEWFD first if it is open.
204 Return newfd if successful, otherwise -1 and errno set.
205 See the Linux man page at
206 <http://www.kernel.org/doc/man-pages/online/pages/man2/dup3.2.html>. */
208 # define dup3 rpl_dup3
210 extern int dup3 (int oldfd, int newfd, int flags);
211 #elif defined GNULIB_POSIXCHECK
213 # define dup3(o,n,f) \
214 (GL_LINK_WARNING ("dup3 is unportable - " \
215 "use gnulib module dup3 for portability"), \
221 # if !@HAVE_DECL_ENVIRON@
222 /* Set of environment variables and values. An array of strings of the form
223 "VARIABLE=VALUE", terminated with a NULL. */
224 # if defined __APPLE__ && defined __MACH__
225 # include <crt_externs.h>
226 # define environ (*_NSGetEnviron ())
228 extern char **environ;
231 #elif defined GNULIB_POSIXCHECK
234 (GL_LINK_WARNING ("environ is unportable - " \
235 "use gnulib module environ for portability"), \
240 #if @GNULIB_EUIDACCESS@
241 # if !@HAVE_EUIDACCESS@
242 /* Like access(), except that it uses the effective user id and group id of
243 the current process. */
244 extern int euidaccess (const char *filename, int mode);
246 #elif defined GNULIB_POSIXCHECK
248 # define euidaccess(f,m) \
249 (GL_LINK_WARNING ("euidaccess is unportable - " \
250 "use gnulib module euidaccess for portability"), \
255 #if @GNULIB_FACCESSAT@
256 # if !@HAVE_FACCESSAT@
257 int faccessat (int fd, char const *file, int mode, int flag);
259 #elif defined GNULIB_POSIXCHECK
261 # define faccessat(d,n,m,f) \
262 (GL_LINK_WARNING ("faccessat is not portable - " \
263 "use gnulib module faccessat for portability"), \
264 faccessat (d, n, m, f))
269 # if @REPLACE_FCHDIR@
270 /* Change the process' current working directory to the directory on which
271 the given file descriptor is open.
272 Return 0 if successful, otherwise -1 and errno set.
273 See the POSIX:2001 specification
274 <http://www.opengroup.org/susv3xsh/fchdir.html>. */
275 extern int fchdir (int /*fd*/);
277 /* Gnulib internal hooks needed to maintain the fchdir metadata. */
278 extern int _gl_register_fd (int fd, const char *filename);
279 extern void _gl_unregister_fd (int fd);
280 extern int _gl_register_dup (int oldfd, int newfd);
281 extern const char *_gl_directory_name (int fd);
284 #elif defined GNULIB_POSIXCHECK
287 (GL_LINK_WARNING ("fchdir is unportable - " \
288 "use gnulib module fchdir for portability"), \
293 #if @GNULIB_FCHOWNAT@
294 # if @REPLACE_FCHOWNAT@
296 # define fchownat rpl_fchownat
298 # if !@HAVE_FCHOWNAT@ || @REPLACE_FCHOWNAT@
299 extern int fchownat (int fd, char const *file, uid_t owner, gid_t group, int flag);
301 #elif defined GNULIB_POSIXCHECK
303 # define fchownat(d,n,o,g,f) \
304 (GL_LINK_WARNING ("fchownat is not portable - " \
305 "use gnulib module openat for portability"), \
306 fchownat (d, n, o, g, f))
311 /* Synchronize changes to a file.
312 Return 0 if successful, otherwise -1 and errno set.
313 See POSIX:2001 specification
314 <http://www.opengroup.org/susv3xsh/fsync.html>. */
316 extern int fsync (int fd);
318 #elif defined GNULIB_POSIXCHECK
321 (GL_LINK_WARNING ("fsync is unportable - " \
322 "use gnulib module fsync for portability"), \
327 #if @GNULIB_FTRUNCATE@
328 # if !@HAVE_FTRUNCATE@
329 /* Change the size of the file to which FD is opened to become equal to LENGTH.
330 Return 0 if successful, otherwise -1 and errno set.
331 See the POSIX:2001 specification
332 <http://www.opengroup.org/susv3xsh/ftruncate.html>. */
333 extern int ftruncate (int fd, off_t length);
335 #elif defined GNULIB_POSIXCHECK
337 # define ftruncate(f,l) \
338 (GL_LINK_WARNING ("ftruncate is unportable - " \
339 "use gnulib module ftruncate for portability"), \
345 /* Include the headers that might declare getcwd so that they will not
346 cause confusion if included after this file. */
348 # if @REPLACE_GETCWD@
349 /* Get the name of the current working directory, and put it in SIZE bytes
351 Return BUF if successful, or NULL if the directory couldn't be determined
352 or SIZE was too small.
353 See the POSIX:2001 specification
354 <http://www.opengroup.org/susv3xsh/getcwd.html>.
355 Additionally, the gnulib module 'getcwd' guarantees the following GNU
356 extension: If BUF is NULL, an array is allocated with 'malloc'; the array
357 is SIZE bytes long, unless SIZE == 0, in which case it is as big as
359 # define getcwd rpl_getcwd
360 extern char * getcwd (char *buf, size_t size);
362 #elif defined GNULIB_POSIXCHECK
364 # define getcwd(b,s) \
365 (GL_LINK_WARNING ("getcwd is unportable - " \
366 "use gnulib module getcwd for portability"), \
371 #if @GNULIB_GETDOMAINNAME@
372 /* Return the NIS domain name of the machine.
373 WARNING! The NIS domain name is unrelated to the fully qualified host name
374 of the machine. It is also unrelated to email addresses.
375 WARNING! The NIS domain name is usually the empty string or "(none)" when
378 Put up to LEN bytes of the NIS domain name into NAME.
379 Null terminate it if the name is shorter than LEN.
380 If the NIS domain name is longer than LEN, set errno = EINVAL and return -1.
381 Return 0 if successful, otherwise set errno and return -1. */
382 # if !@HAVE_GETDOMAINNAME@
383 extern int getdomainname(char *name, size_t len);
385 #elif defined GNULIB_POSIXCHECK
386 # undef getdomainname
387 # define getdomainname(n,l) \
388 (GL_LINK_WARNING ("getdomainname is unportable - " \
389 "use gnulib module getdomainname for portability"), \
390 getdomainname (n, l))
394 #if @GNULIB_GETDTABLESIZE@
395 # if !@HAVE_GETDTABLESIZE@
396 /* Return the maximum number of file descriptors in the current process.
397 In POSIX, this is same as sysconf (_SC_OPEN_MAX). */
398 extern int getdtablesize (void);
400 #elif defined GNULIB_POSIXCHECK
401 # undef getdtablesize
402 # define getdtablesize() \
403 (GL_LINK_WARNING ("getdtablesize is unportable - " \
404 "use gnulib module getdtablesize for portability"), \
409 #if @GNULIB_GETHOSTNAME@
410 /* Return the standard host name of the machine.
411 WARNING! The host name may or may not be fully qualified.
413 Put up to LEN bytes of the host name into NAME.
414 Null terminate it if the name is shorter than LEN.
415 If the host name is longer than LEN, set errno = EINVAL and return -1.
416 Return 0 if successful, otherwise set errno and return -1. */
417 # if @UNISTD_H_HAVE_WINSOCK2_H@
419 # define gethostname rpl_gethostname
421 # if @UNISTD_H_HAVE_WINSOCK2_H@ || !@HAVE_GETHOSTNAME@
422 extern int gethostname(char *name, size_t len);
424 #elif @UNISTD_H_HAVE_WINSOCK2_H@
426 # define gethostname gethostname_used_without_requesting_gnulib_module_gethostname
427 #elif defined GNULIB_POSIXCHECK
429 # define gethostname(n,l) \
430 (GL_LINK_WARNING ("gethostname is unportable - " \
431 "use gnulib module gethostname for portability"), \
436 #if @GNULIB_GETLOGIN_R@
437 /* Copies the user's login name to NAME.
438 The array pointed to by NAME has room for SIZE bytes.
440 Returns 0 if successful. Upon error, an error number is returned, or -1 in
441 the case that the login name cannot be found but no specific error is
442 provided (this case is hopefully rare but is left open by the POSIX spec).
444 See <http://www.opengroup.org/susv3xsh/getlogin.html>.
446 # if !@HAVE_DECL_GETLOGIN_R@
447 extern int getlogin_r (char *name, size_t size);
449 #elif defined GNULIB_POSIXCHECK
451 # define getlogin_r(n,s) \
452 (GL_LINK_WARNING ("getlogin_r is unportable - " \
453 "use gnulib module getlogin_r for portability"), \
458 #if @GNULIB_GETPAGESIZE@
459 # if @REPLACE_GETPAGESIZE@
460 # define getpagesize rpl_getpagesize
461 extern int getpagesize (void);
462 # elif !@HAVE_GETPAGESIZE@
463 /* This is for POSIX systems. */
464 # if !defined getpagesize && defined _SC_PAGESIZE
465 # if ! (defined __VMS && __VMS_VER < 70000000)
466 # define getpagesize() sysconf (_SC_PAGESIZE)
469 /* This is for older VMS. */
470 # if !defined getpagesize && defined __VMS
472 # define getpagesize() 8192
474 # define getpagesize() 512
477 /* This is for BeOS. */
478 # if !defined getpagesize && @HAVE_OS_H@
480 # if defined B_PAGE_SIZE
481 # define getpagesize() B_PAGE_SIZE
484 /* This is for AmigaOS4.0. */
485 # if !defined getpagesize && defined __amigaos4__
486 # define getpagesize() 2048
488 /* This is for older Unix systems. */
489 # if !defined getpagesize && @HAVE_SYS_PARAM_H@
490 # include <sys/param.h>
491 # ifdef EXEC_PAGESIZE
492 # define getpagesize() EXEC_PAGESIZE
498 # define getpagesize() (NBPG * CLSIZE)
501 # define getpagesize() NBPC
507 #elif defined GNULIB_POSIXCHECK
509 # define getpagesize() \
510 (GL_LINK_WARNING ("getpagesize is unportable - " \
511 "use gnulib module getpagesize for portability"), \
516 #if @GNULIB_GETUSERSHELL@
517 # if !@HAVE_GETUSERSHELL@
518 /* Return the next valid login shell on the system, or NULL when the end of
519 the list has been reached. */
520 extern char *getusershell (void);
521 /* Rewind to pointer that is advanced at each getusershell() call. */
522 extern void setusershell (void);
523 /* Free the pointer that is advanced at each getusershell() call and
524 associated resources. */
525 extern void endusershell (void);
527 #elif defined GNULIB_POSIXCHECK
529 # define getusershell() \
530 (GL_LINK_WARNING ("getusershell is unportable - " \
531 "use gnulib module getusershell for portability"), \
534 # define setusershell() \
535 (GL_LINK_WARNING ("setusershell is unportable - " \
536 "use gnulib module getusershell for portability"), \
539 # define endusershell() \
540 (GL_LINK_WARNING ("endusershell is unportable - " \
541 "use gnulib module getusershell for portability"), \
547 # if @REPLACE_LCHOWN@
548 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
549 to GID (if GID is not -1). Do not follow symbolic links.
550 Return 0 if successful, otherwise -1 and errno set.
551 See the POSIX:2001 specification
552 <http://www.opengroup.org/susv3xsh/lchown.html>. */
553 # define lchown rpl_lchown
554 extern int lchown (char const *file, uid_t owner, gid_t group);
556 #elif defined GNULIB_POSIXCHECK
558 # define lchown(f,u,g) \
559 (GL_LINK_WARNING ("lchown is unportable to pre-POSIX.1-2001 " \
560 "systems - use gnulib module lchown for portability"), \
567 # define link rpl_link
569 /* Create a new hard link for an existing file.
570 Return 0 if successful, otherwise -1 and errno set.
571 See POSIX:2001 specification
572 <http://www.opengroup.org/susv3xsh/link.html>. */
573 # if !@HAVE_LINK@ || @REPLACE_LINK@
574 extern int link (const char *path1, const char *path2);
576 #elif defined GNULIB_POSIXCHECK
578 # define link(path1,path2) \
579 (GL_LINK_WARNING ("link is unportable - " \
580 "use gnulib module link for portability"), \
585 # if @REPLACE_LINKAT@
587 # define linkat rpl_linkat
589 /* Create a new hard link for an existing file, relative to two
590 directories. FLAG controls whether symlinks are followed.
591 Return 0 if successful, otherwise -1 and errno set. */
592 # if !@HAVE_LINKAT@ || @REPLACE_LINKAT@
593 extern int linkat (int fd1, const char *path1, int fd2, const char *path2,
596 #elif defined GNULIB_POSIXCHECK
598 # define link(f1,path1,f2,path2,f) \
599 (GL_LINK_WARNING ("linkat is unportable - " \
600 "use gnulib module linkat for portability"), \
601 linkat (f1, path1, f2, path2,f))
606 /* Set the offset of FD relative to SEEK_SET, SEEK_CUR, or SEEK_END.
607 Return the new offset if successful, otherwise -1 and errno set.
608 See the POSIX:2001 specification
609 <http://www.opengroup.org/susv3xsh/lseek.html>. */
610 # define lseek rpl_lseek
611 extern off_t lseek (int fd, off_t offset, int whence);
613 #elif defined GNULIB_POSIXCHECK
615 # define lseek(f,o,w) \
616 (GL_LINK_WARNING ("lseek does not fail with ESPIPE on pipes on some " \
617 "systems - use gnulib module lseek for portability"), \
623 /* Create a pipe, applying the given flags when opening the read-end of the
624 pipe and the write-end of the pipe.
625 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
626 and O_TEXT, O_BINARY (defined in "binary-io.h").
627 Store the read-end as fd[0] and the write-end as fd[1].
628 Return 0 upon success, or -1 with errno set upon failure.
629 See also the Linux man page at
630 <http://www.kernel.org/doc/man-pages/online/pages/man2/pipe2.2.html>. */
632 # define pipe2 rpl_pipe2
634 extern int pipe2 (int fd[2], int flags);
635 #elif defined GNULIB_POSIXCHECK
637 # define pipe2(f,o) \
638 (GL_LINK_WARNING ("pipe2 is unportable - " \
639 "use gnulib module pipe2 for portability"), \
644 #if @GNULIB_READLINK@
645 # if @REPLACE_READLINK@
646 # define readlink rpl_readlink
648 /* Read the contents of the symbolic link FILE and place the first BUFSIZE
649 bytes of it into BUF. Return the number of bytes placed into BUF if
650 successful, otherwise -1 and errno set.
651 See the POSIX:2001 specification
652 <http://www.opengroup.org/susv3xsh/readlink.html>. */
653 # if !@HAVE_READLINK@ || @REPLACE_READLINK@
654 extern ssize_t readlink (const char *file, char *buf, size_t bufsize);
656 #elif defined GNULIB_POSIXCHECK
658 # define readlink(f,b,s) \
659 (GL_LINK_WARNING ("readlink is unportable - " \
660 "use gnulib module readlink for portability"), \
665 #if @GNULIB_READLINKAT@
666 # if !@HAVE_READLINKAT@
667 ssize_t readlinkat (int fd, char const *file, char *buf, size_t len);
669 #elif defined GNULIB_POSIXCHECK
671 # define readlinkat(d,n,b,l) \
672 (GL_LINK_WARNING ("readlinkat is not portable - " \
673 "use gnulib module symlinkat for portability"), \
674 readlinkat (d, n, b, l))
680 # define rmdir rpl_rmdir
681 /* Remove the directory DIR. */
682 extern int rmdir (char const *name);
684 #elif defined GNULIB_POSIXCHECK
687 (GL_LINK_WARNING ("rmdir is unportable - " \
688 "use gnulib module rmdir for portability"), \
694 /* Pause the execution of the current thread for N seconds.
695 Returns the number of seconds left to sleep.
696 See the POSIX:2001 specification
697 <http://www.opengroup.org/susv3xsh/sleep.html>. */
699 extern unsigned int sleep (unsigned int n);
701 #elif defined GNULIB_POSIXCHECK
704 (GL_LINK_WARNING ("sleep is unportable - " \
705 "use gnulib module sleep for portability"), \
711 # if @REPLACE_SYMLINK@
713 # define symlink rpl_symlink
715 # if !@HAVE_SYMLINK@ || @REPLACE_SYMLINK@
716 int symlink (char const *contents, char const *file);
718 #elif defined GNULIB_POSIXCHECK
720 # define symlink(c,n) \
721 (GL_LINK_WARNING ("symlink is not portable - " \
722 "use gnulib module symlink for portability"), \
727 #if @GNULIB_SYMLINKAT@
728 # if !@HAVE_SYMLINKAT@
729 int symlinkat (char const *contents, int fd, char const *file);
731 #elif defined GNULIB_POSIXCHECK
733 # define symlinkat(c,d,n) \
734 (GL_LINK_WARNING ("symlinkat is not portable - " \
735 "use gnulib module symlinkat for portability"), \
741 # if @REPLACE_UNLINK@
743 # define unlink rpl_unlink
744 extern int unlink (char const *file);
746 #elif defined GNULIB_POSIXCHECK
749 (GL_LINK_WARNING ("unlink is not portable - " \
750 "use gnulib module unlink for portability"), \
755 #if @GNULIB_UNLINKAT@
756 # if @REPLACE_UNLINKAT@
758 # define unlinkat rpl_unlinkat
760 # if !@HAVE_UNLINKAT@ || @REPLACE_UNLINKAT@
761 extern int unlinkat (int fd, char const *file, int flag);
763 #elif defined GNULIB_POSIXCHECK
765 # define unlinkat(d,n,f) \
766 (GL_LINK_WARNING ("unlinkat is not portable - " \
767 "use gnulib module openat for portability"), \
772 #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
773 /* Write up to COUNT bytes starting at BUF to file descriptor FD.
774 See the POSIX:2001 specification
775 <http://www.opengroup.org/susv3xsh/write.html>. */
777 # define write rpl_write
778 extern ssize_t write (int fd, const void *buf, size_t count);
787 #endif /* _GL_UNISTD_H */
788 #endif /* _GL_UNISTD_H */