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_GETGROUPS@
410 # if @REPLACE_GETGROUPS@
412 # define getgroups rpl_getgroups
414 # if !@HAVE_GETGROUPS@ || @REPLACE_GETGROUPS@
415 /* Return the supplemental groups that the current process belongs to.
416 It is unspecified whether the effective group id is in the list.
417 If N is 0, return the group count; otherwise, N describes how many
418 entries are available in GROUPS. Return -1 and set errno if N is
419 not 0 and not large enough. Fails with ENOSYS on some systems. */
420 int getgroups (int n, gid_t *groups);
422 #elif defined GNULIB_POSIXCHECK
424 # define getgroups(n,g) \
425 (GL_LINK_WARNING ("getgroups is unportable - " \
426 "use gnulib module getgroups for portability"), \
431 #if @GNULIB_GETHOSTNAME@
432 /* Return the standard host name of the machine.
433 WARNING! The host name may or may not be fully qualified.
435 Put up to LEN bytes of the host name into NAME.
436 Null terminate it if the name is shorter than LEN.
437 If the host name is longer than LEN, set errno = EINVAL and return -1.
438 Return 0 if successful, otherwise set errno and return -1. */
439 # if @UNISTD_H_HAVE_WINSOCK2_H@
441 # define gethostname rpl_gethostname
443 # if @UNISTD_H_HAVE_WINSOCK2_H@ || !@HAVE_GETHOSTNAME@
444 extern int gethostname(char *name, size_t len);
446 #elif @UNISTD_H_HAVE_WINSOCK2_H@
448 # define gethostname gethostname_used_without_requesting_gnulib_module_gethostname
449 #elif defined GNULIB_POSIXCHECK
451 # define gethostname(n,l) \
452 (GL_LINK_WARNING ("gethostname is unportable - " \
453 "use gnulib module gethostname for portability"), \
458 #if @GNULIB_GETLOGIN_R@
459 /* Copies the user's login name to NAME.
460 The array pointed to by NAME has room for SIZE bytes.
462 Returns 0 if successful. Upon error, an error number is returned, or -1 in
463 the case that the login name cannot be found but no specific error is
464 provided (this case is hopefully rare but is left open by the POSIX spec).
466 See <http://www.opengroup.org/susv3xsh/getlogin.html>.
468 # if !@HAVE_DECL_GETLOGIN_R@
469 extern int getlogin_r (char *name, size_t size);
471 #elif defined GNULIB_POSIXCHECK
473 # define getlogin_r(n,s) \
474 (GL_LINK_WARNING ("getlogin_r is unportable - " \
475 "use gnulib module getlogin_r for portability"), \
480 #if @GNULIB_GETPAGESIZE@
481 # if @REPLACE_GETPAGESIZE@
482 # define getpagesize rpl_getpagesize
483 extern int getpagesize (void);
484 # elif !@HAVE_GETPAGESIZE@
485 /* This is for POSIX systems. */
486 # if !defined getpagesize && defined _SC_PAGESIZE
487 # if ! (defined __VMS && __VMS_VER < 70000000)
488 # define getpagesize() sysconf (_SC_PAGESIZE)
491 /* This is for older VMS. */
492 # if !defined getpagesize && defined __VMS
494 # define getpagesize() 8192
496 # define getpagesize() 512
499 /* This is for BeOS. */
500 # if !defined getpagesize && @HAVE_OS_H@
502 # if defined B_PAGE_SIZE
503 # define getpagesize() B_PAGE_SIZE
506 /* This is for AmigaOS4.0. */
507 # if !defined getpagesize && defined __amigaos4__
508 # define getpagesize() 2048
510 /* This is for older Unix systems. */
511 # if !defined getpagesize && @HAVE_SYS_PARAM_H@
512 # include <sys/param.h>
513 # ifdef EXEC_PAGESIZE
514 # define getpagesize() EXEC_PAGESIZE
520 # define getpagesize() (NBPG * CLSIZE)
523 # define getpagesize() NBPC
529 #elif defined GNULIB_POSIXCHECK
531 # define getpagesize() \
532 (GL_LINK_WARNING ("getpagesize is unportable - " \
533 "use gnulib module getpagesize for portability"), \
538 #if @GNULIB_GETUSERSHELL@
539 # if !@HAVE_GETUSERSHELL@
540 /* Return the next valid login shell on the system, or NULL when the end of
541 the list has been reached. */
542 extern char *getusershell (void);
543 /* Rewind to pointer that is advanced at each getusershell() call. */
544 extern void setusershell (void);
545 /* Free the pointer that is advanced at each getusershell() call and
546 associated resources. */
547 extern void endusershell (void);
549 #elif defined GNULIB_POSIXCHECK
551 # define getusershell() \
552 (GL_LINK_WARNING ("getusershell is unportable - " \
553 "use gnulib module getusershell for portability"), \
556 # define setusershell() \
557 (GL_LINK_WARNING ("setusershell is unportable - " \
558 "use gnulib module getusershell for portability"), \
561 # define endusershell() \
562 (GL_LINK_WARNING ("endusershell is unportable - " \
563 "use gnulib module getusershell for portability"), \
569 # if @REPLACE_LCHOWN@
570 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
571 to GID (if GID is not -1). Do not follow symbolic links.
572 Return 0 if successful, otherwise -1 and errno set.
573 See the POSIX:2001 specification
574 <http://www.opengroup.org/susv3xsh/lchown.html>. */
575 # define lchown rpl_lchown
576 extern int lchown (char const *file, uid_t owner, gid_t group);
578 #elif defined GNULIB_POSIXCHECK
580 # define lchown(f,u,g) \
581 (GL_LINK_WARNING ("lchown is unportable to pre-POSIX.1-2001 " \
582 "systems - use gnulib module lchown for portability"), \
589 # define link rpl_link
591 /* Create a new hard link for an existing file.
592 Return 0 if successful, otherwise -1 and errno set.
593 See POSIX:2001 specification
594 <http://www.opengroup.org/susv3xsh/link.html>. */
595 # if !@HAVE_LINK@ || @REPLACE_LINK@
596 extern int link (const char *path1, const char *path2);
598 #elif defined GNULIB_POSIXCHECK
600 # define link(path1,path2) \
601 (GL_LINK_WARNING ("link is unportable - " \
602 "use gnulib module link for portability"), \
607 # if @REPLACE_LINKAT@
609 # define linkat rpl_linkat
611 /* Create a new hard link for an existing file, relative to two
612 directories. FLAG controls whether symlinks are followed.
613 Return 0 if successful, otherwise -1 and errno set. */
614 # if !@HAVE_LINKAT@ || @REPLACE_LINKAT@
615 extern int linkat (int fd1, const char *path1, int fd2, const char *path2,
618 #elif defined GNULIB_POSIXCHECK
620 # define link(f1,path1,f2,path2,f) \
621 (GL_LINK_WARNING ("linkat is unportable - " \
622 "use gnulib module linkat for portability"), \
623 linkat (f1, path1, f2, path2,f))
628 /* Set the offset of FD relative to SEEK_SET, SEEK_CUR, or SEEK_END.
629 Return the new offset if successful, otherwise -1 and errno set.
630 See the POSIX:2001 specification
631 <http://www.opengroup.org/susv3xsh/lseek.html>. */
632 # define lseek rpl_lseek
633 extern off_t lseek (int fd, off_t offset, int whence);
635 #elif defined GNULIB_POSIXCHECK
637 # define lseek(f,o,w) \
638 (GL_LINK_WARNING ("lseek does not fail with ESPIPE on pipes on some " \
639 "systems - use gnulib module lseek for portability"), \
645 /* Create a pipe, applying the given flags when opening the read-end of the
646 pipe and the write-end of the pipe.
647 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
648 and O_TEXT, O_BINARY (defined in "binary-io.h").
649 Store the read-end as fd[0] and the write-end as fd[1].
650 Return 0 upon success, or -1 with errno set upon failure.
651 See also the Linux man page at
652 <http://www.kernel.org/doc/man-pages/online/pages/man2/pipe2.2.html>. */
654 # define pipe2 rpl_pipe2
656 extern int pipe2 (int fd[2], int flags);
657 #elif defined GNULIB_POSIXCHECK
659 # define pipe2(f,o) \
660 (GL_LINK_WARNING ("pipe2 is unportable - " \
661 "use gnulib module pipe2 for portability"), \
666 #if @GNULIB_READLINK@
667 # if @REPLACE_READLINK@
668 # define readlink rpl_readlink
670 /* Read the contents of the symbolic link FILE and place the first BUFSIZE
671 bytes of it into BUF. Return the number of bytes placed into BUF if
672 successful, otherwise -1 and errno set.
673 See the POSIX:2001 specification
674 <http://www.opengroup.org/susv3xsh/readlink.html>. */
675 # if !@HAVE_READLINK@ || @REPLACE_READLINK@
676 extern ssize_t readlink (const char *file, char *buf, size_t bufsize);
678 #elif defined GNULIB_POSIXCHECK
680 # define readlink(f,b,s) \
681 (GL_LINK_WARNING ("readlink is unportable - " \
682 "use gnulib module readlink for portability"), \
687 #if @GNULIB_READLINKAT@
688 # if !@HAVE_READLINKAT@
689 ssize_t readlinkat (int fd, char const *file, char *buf, size_t len);
691 #elif defined GNULIB_POSIXCHECK
693 # define readlinkat(d,n,b,l) \
694 (GL_LINK_WARNING ("readlinkat is not portable - " \
695 "use gnulib module symlinkat for portability"), \
696 readlinkat (d, n, b, l))
702 # define rmdir rpl_rmdir
703 /* Remove the directory DIR. */
704 extern int rmdir (char const *name);
706 #elif defined GNULIB_POSIXCHECK
709 (GL_LINK_WARNING ("rmdir is unportable - " \
710 "use gnulib module rmdir for portability"), \
716 /* Pause the execution of the current thread for N seconds.
717 Returns the number of seconds left to sleep.
718 See the POSIX:2001 specification
719 <http://www.opengroup.org/susv3xsh/sleep.html>. */
721 extern unsigned int sleep (unsigned int n);
723 #elif defined GNULIB_POSIXCHECK
726 (GL_LINK_WARNING ("sleep is unportable - " \
727 "use gnulib module sleep for portability"), \
733 # if @REPLACE_SYMLINK@
735 # define symlink rpl_symlink
737 # if !@HAVE_SYMLINK@ || @REPLACE_SYMLINK@
738 int symlink (char const *contents, char const *file);
740 #elif defined GNULIB_POSIXCHECK
742 # define symlink(c,n) \
743 (GL_LINK_WARNING ("symlink is not portable - " \
744 "use gnulib module symlink for portability"), \
749 #if @GNULIB_SYMLINKAT@
750 # if !@HAVE_SYMLINKAT@
751 int symlinkat (char const *contents, int fd, char const *file);
753 #elif defined GNULIB_POSIXCHECK
755 # define symlinkat(c,d,n) \
756 (GL_LINK_WARNING ("symlinkat is not portable - " \
757 "use gnulib module symlinkat for portability"), \
763 # if @REPLACE_UNLINK@
765 # define unlink rpl_unlink
766 extern int unlink (char const *file);
768 #elif defined GNULIB_POSIXCHECK
771 (GL_LINK_WARNING ("unlink is not portable - " \
772 "use gnulib module unlink for portability"), \
777 #if @GNULIB_UNLINKAT@
778 # if @REPLACE_UNLINKAT@
780 # define unlinkat rpl_unlinkat
782 # if !@HAVE_UNLINKAT@ || @REPLACE_UNLINKAT@
783 extern int unlinkat (int fd, char const *file, int flag);
785 #elif defined GNULIB_POSIXCHECK
787 # define unlinkat(d,n,f) \
788 (GL_LINK_WARNING ("unlinkat is not portable - " \
789 "use gnulib module openat for portability"), \
794 #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
795 /* Write up to COUNT bytes starting at BUF to file descriptor FD.
796 See the POSIX:2001 specification
797 <http://www.opengroup.org/susv3xsh/write.html>. */
799 # define write rpl_write
800 extern ssize_t write (int fd, const void *buf, size_t count);
809 #endif /* _GL_UNISTD_H */
810 #endif /* _GL_UNISTD_H */