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@
46 # include <sys/types.h>
49 /* Get getopt(), optarg, optind, opterr, optopt. */
50 #if @GNULIB_UNISTD_H_GETOPT@
54 #if @GNULIB_GETHOSTNAME@
55 /* Get all possible declarations of gethostname(). */
56 # if @UNISTD_H_HAVE_WINSOCK2_H@
57 # include <winsock2.h>
58 # if !defined _GL_SYS_SOCKET_H
60 # define socket socket_used_without_including_sys_socket_h
62 # define connect connect_used_without_including_sys_socket_h
64 # define accept accept_used_without_including_sys_socket_h
66 # define bind bind_used_without_including_sys_socket_h
68 # define getpeername getpeername_used_without_including_sys_socket_h
70 # define getsockname getsockname_used_without_including_sys_socket_h
72 # define getsockopt getsockopt_used_without_including_sys_socket_h
74 # define listen listen_used_without_including_sys_socket_h
76 # define recv recv_used_without_including_sys_socket_h
78 # define send send_used_without_including_sys_socket_h
80 # define recvfrom recvfrom_used_without_including_sys_socket_h
82 # define sendto sendto_used_without_including_sys_socket_h
84 # define setsockopt setsockopt_used_without_including_sys_socket_h
86 # define shutdown shutdown_used_without_including_sys_socket_h
88 # if !defined _GL_SYS_SELECT_H
90 # define select select_used_without_including_sys_select_h
95 /* The definition of GL_LINK_WARNING is copied here. */
98 /* OS/2 EMX lacks these macros. */
100 # define STDIN_FILENO 0
102 #ifndef STDOUT_FILENO
103 # define STDOUT_FILENO 1
105 #ifndef STDERR_FILENO
106 # define STDERR_FILENO 2
109 /* Ensure *_OK functions exist. */
118 /* Declare overridden functions. */
127 # ifndef REPLACE_CHOWN
128 # define REPLACE_CHOWN 1
131 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
132 to GID (if GID is not -1). Follow symbolic links.
133 Return 0 if successful, otherwise -1 and errno set.
134 See the POSIX:2001 specification
135 <http://www.opengroup.org/susv3xsh/chown.html>. */
136 # define chown rpl_chown
137 extern int chown (const char *file, uid_t uid, gid_t gid);
140 #elif defined GNULIB_POSIXCHECK
142 # define chown(f,u,g) \
143 (GL_LINK_WARNING ("chown fails to follow symlinks on some systems and " \
144 "doesn't treat a uid or gid of -1 on some systems - " \
145 "use gnulib module chown for portability"), \
150 #if @GNULIB_FCHOWNAT@
151 # if @REPLACE_FCHOWNAT@
153 # define fchownat rpl_fchownat
155 # if !@HAVE_FCHOWNAT@ || @REPLACE_FCHOWNAT@
156 extern int fchownat (int fd, char const *file, uid_t owner, gid_t group, int flag);
158 #elif defined GNULIB_POSIXCHECK
160 # define fchownat(d,n,o,g,f) \
161 (GL_LINK_WARNING ("fchownat is not portable - " \
162 "use gnulib module openat for portability"), \
163 fchownat (d, n, o, g, f))
168 # if @REPLACE_UNLINK@
170 # define unlink rpl_unlink
171 extern int unlink (char const *file);
173 #elif defined GNULIB_POSIXCHECK
176 (GL_LINK_WARNING ("unlink is not portable - " \
177 "use gnulib module unlink for portability"), \
182 #if @GNULIB_UNLINKAT@
183 # if @REPLACE_UNLINKAT@
185 # define unlinkat rpl_unlinkat
187 # if !@HAVE_UNLINKAT@ || @REPLACE_UNLINKAT@
188 extern int unlinkat (int fd, char const *file, int flag);
190 #elif defined GNULIB_POSIXCHECK
192 # define unlinkat(d,n,f) \
193 (GL_LINK_WARNING ("unlinkat is not portable - " \
194 "use gnulib module openat for portability"), \
199 #if @GNULIB_FACCESSAT@
200 # if !@HAVE_FACCESSAT@
201 int faccessat (int fd, char const *file, int mode, int flag);
203 #elif defined GNULIB_POSIXCHECK
205 # define faccessat(d,n,m,f) \
206 (GL_LINK_WARNING ("faccessat is not portable - " \
207 "use gnulib module faccessat for portability"), \
208 faccessat (d, n, m, f))
211 #if @GNULIB_SYMLINKAT@
212 # if !@HAVE_SYMLINKAT@
213 int symlinkat (char const *contents, int fd, char const *file);
215 #elif defined GNULIB_POSIXCHECK
217 # define symlinkat(c,d,n) \
218 (GL_LINK_WARNING ("symlinkat is not portable - " \
219 "use gnulib module symlinkat for portability"), \
223 #if @GNULIB_READLINKAT@
224 # if !@HAVE_READLINKAT@
225 ssize_t readlinkat (int fd, char const *file, char *buf, size_t len);
227 #elif defined GNULIB_POSIXCHECK
229 # define readlinkat(d,n,b,l) \
230 (GL_LINK_WARNING ("readlinkat is not portable - " \
231 "use gnulib module symlinkat for portability"), \
232 readlinkat (d, n, b, l))
237 /* Automatically included by modules that need a replacement for close. */
239 # define close rpl_close
240 extern int close (int);
242 #elif @UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS@
244 # define close close_used_without_requesting_gnulib_module_close
245 #elif defined GNULIB_POSIXCHECK
248 (GL_LINK_WARNING ("close does not portably work on sockets - " \
249 "use gnulib module close for portability"), \
255 extern int dup (int);
260 # define dup2 rpl_dup2
262 # if !@HAVE_DUP2@ || @REPLACE_DUP2@
263 /* Copy the file descriptor OLDFD into file descriptor NEWFD. Do nothing if
264 NEWFD = OLDFD, otherwise close NEWFD first if it is open.
265 Return newfd if successful, otherwise -1 and errno set.
266 See the POSIX:2001 specification
267 <http://www.opengroup.org/susv3xsh/dup2.html>. */
268 extern int dup2 (int oldfd, int newfd);
270 #elif defined GNULIB_POSIXCHECK
273 (GL_LINK_WARNING ("dup2 is unportable - " \
274 "use gnulib module dup2 for portability"), \
280 /* Copy the file descriptor OLDFD into file descriptor NEWFD, with the
282 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
283 and O_TEXT, O_BINARY (defined in "binary-io.h").
284 Close NEWFD first if it is open.
285 Return newfd if successful, otherwise -1 and errno set.
286 See the Linux man page at
287 <http://www.kernel.org/doc/man-pages/online/pages/man2/dup3.2.html>. */
289 # define dup3 rpl_dup3
291 extern int dup3 (int oldfd, int newfd, int flags);
292 #elif defined GNULIB_POSIXCHECK
294 # define dup3(o,n,f) \
295 (GL_LINK_WARNING ("dup3 is unportable - " \
296 "use gnulib module dup3 for portability"), \
302 # if !@HAVE_DECL_ENVIRON@
303 /* Set of environment variables and values. An array of strings of the form
304 "VARIABLE=VALUE", terminated with a NULL. */
305 # if defined __APPLE__ && defined __MACH__
306 # include <crt_externs.h>
307 # define environ (*_NSGetEnviron ())
309 extern char **environ;
312 #elif defined GNULIB_POSIXCHECK
315 (GL_LINK_WARNING ("environ is unportable - " \
316 "use gnulib module environ for portability"), \
321 #if @GNULIB_EUIDACCESS@
322 # if !@HAVE_EUIDACCESS@
323 /* Like access(), except that is uses the effective user id and group id of
324 the current process. */
325 extern int euidaccess (const char *filename, int mode);
327 #elif defined GNULIB_POSIXCHECK
329 # define euidaccess(f,m) \
330 (GL_LINK_WARNING ("euidaccess is unportable - " \
331 "use gnulib module euidaccess for portability"), \
337 # if @REPLACE_FCHDIR@
338 /* Change the process' current working directory to the directory on which
339 the given file descriptor is open.
340 Return 0 if successful, otherwise -1 and errno set.
341 See the POSIX:2001 specification
342 <http://www.opengroup.org/susv3xsh/fchdir.html>. */
343 extern int fchdir (int /*fd*/);
345 /* Gnulib internal hooks needed to maintain the fchdir metadata. */
346 extern int _gl_register_fd (int fd, const char *filename);
347 extern void _gl_unregister_fd (int fd);
348 extern int _gl_register_dup (int oldfd, int newfd);
349 extern const char *_gl_directory_name (int fd);
352 #elif defined GNULIB_POSIXCHECK
355 (GL_LINK_WARNING ("fchdir is unportable - " \
356 "use gnulib module fchdir for portability"), \
362 /* Synchronize changes to a file.
363 Return 0 if successful, otherwise -1 and errno set.
364 See POSIX:2001 specification
365 <http://www.opengroup.org/susv3xsh/fsync.html>. */
367 extern int fsync (int fd);
369 #elif defined GNULIB_POSIXCHECK
372 (GL_LINK_WARNING ("fsync is unportable - " \
373 "use gnulib module fsync for portability"), \
378 #if @GNULIB_FTRUNCATE@
379 # if !@HAVE_FTRUNCATE@
380 /* Change the size of the file to which FD is opened to become equal to LENGTH.
381 Return 0 if successful, otherwise -1 and errno set.
382 See the POSIX:2001 specification
383 <http://www.opengroup.org/susv3xsh/ftruncate.html>. */
384 extern int ftruncate (int fd, off_t length);
386 #elif defined GNULIB_POSIXCHECK
388 # define ftruncate(f,l) \
389 (GL_LINK_WARNING ("ftruncate is unportable - " \
390 "use gnulib module ftruncate for portability"), \
396 /* Include the headers that might declare getcwd so that they will not
397 cause confusion if included after this file. */
399 # if @REPLACE_GETCWD@
400 /* Get the name of the current working directory, and put it in SIZE bytes
402 Return BUF if successful, or NULL if the directory couldn't be determined
403 or SIZE was too small.
404 See the POSIX:2001 specification
405 <http://www.opengroup.org/susv3xsh/getcwd.html>.
406 Additionally, the gnulib module 'getcwd' guarantees the following GNU
407 extension: If BUF is NULL, an array is allocated with 'malloc'; the array
408 is SIZE bytes long, unless SIZE == 0, in which case it is as big as
410 # define getcwd rpl_getcwd
411 extern char * getcwd (char *buf, size_t size);
413 #elif defined GNULIB_POSIXCHECK
415 # define getcwd(b,s) \
416 (GL_LINK_WARNING ("getcwd is unportable - " \
417 "use gnulib module getcwd for portability"), \
422 #if @GNULIB_GETDOMAINNAME@
423 /* Return the NIS domain name of the machine.
424 WARNING! The NIS domain name is unrelated to the fully qualified host name
425 of the machine. It is also unrelated to email addresses.
426 WARNING! The NIS domain name is usually the empty string or "(none)" when
429 Put up to LEN bytes of the NIS domain name into NAME.
430 Null terminate it if the name is shorter than LEN.
431 If the NIS domain name is longer than LEN, set errno = EINVAL and return -1.
432 Return 0 if successful, otherwise set errno and return -1. */
433 # if !@HAVE_GETDOMAINNAME@
434 extern int getdomainname(char *name, size_t len);
436 #elif defined GNULIB_POSIXCHECK
437 # undef getdomainname
438 # define getdomainname(n,l) \
439 (GL_LINK_WARNING ("getdomainname is unportable - " \
440 "use gnulib module getdomainname for portability"), \
441 getdomainname (n, l))
445 #if @GNULIB_GETDTABLESIZE@
446 # if !@HAVE_GETDTABLESIZE@
447 /* Return the maximum number of file descriptors in the current process.
448 In POSIX, this is same as sysconf (_SC_OPEN_MAX). */
449 extern int getdtablesize (void);
451 #elif defined GNULIB_POSIXCHECK
452 # undef getdtablesize
453 # define getdtablesize() \
454 (GL_LINK_WARNING ("getdtablesize is unportable - " \
455 "use gnulib module getdtablesize for portability"), \
460 #if @GNULIB_GETHOSTNAME@
461 /* Return the standard host name of the machine.
462 WARNING! The host name may or may not be fully qualified.
464 Put up to LEN bytes of the host name into NAME.
465 Null terminate it if the name is shorter than LEN.
466 If the host name is longer than LEN, set errno = EINVAL and return -1.
467 Return 0 if successful, otherwise set errno and return -1. */
468 # if @UNISTD_H_HAVE_WINSOCK2_H@
470 # define gethostname rpl_gethostname
472 # if @UNISTD_H_HAVE_WINSOCK2_H@ || !@HAVE_GETHOSTNAME@
473 extern int gethostname(char *name, size_t len);
475 #elif @UNISTD_H_HAVE_WINSOCK2_H@
477 # define gethostname gethostname_used_without_requesting_gnulib_module_gethostname
478 #elif defined GNULIB_POSIXCHECK
480 # define gethostname(n,l) \
481 (GL_LINK_WARNING ("gethostname is unportable - " \
482 "use gnulib module gethostname for portability"), \
487 #if @GNULIB_GETLOGIN_R@
488 /* Copies the user's login name to NAME.
489 The array pointed to by NAME has room for SIZE bytes.
491 Returns 0 if successful. Upon error, an error number is returned, or -1 in
492 the case that the login name cannot be found but no specific error is
493 provided (this case is hopefully rare but is left open by the POSIX spec).
495 See <http://www.opengroup.org/susv3xsh/getlogin.html>.
497 # if !@HAVE_DECL_GETLOGIN_R@
498 extern int getlogin_r (char *name, size_t size);
500 #elif defined GNULIB_POSIXCHECK
502 # define getlogin_r(n,s) \
503 (GL_LINK_WARNING ("getlogin_r is unportable - " \
504 "use gnulib module getlogin_r for portability"), \
509 #if @GNULIB_GETPAGESIZE@
510 # if @REPLACE_GETPAGESIZE@
511 # define getpagesize rpl_getpagesize
512 extern int getpagesize (void);
513 # elif !@HAVE_GETPAGESIZE@
514 /* This is for POSIX systems. */
515 # if !defined getpagesize && defined _SC_PAGESIZE
516 # if ! (defined __VMS && __VMS_VER < 70000000)
517 # define getpagesize() sysconf (_SC_PAGESIZE)
520 /* This is for older VMS. */
521 # if !defined getpagesize && defined __VMS
523 # define getpagesize() 8192
525 # define getpagesize() 512
528 /* This is for BeOS. */
529 # if !defined getpagesize && @HAVE_OS_H@
531 # if defined B_PAGE_SIZE
532 # define getpagesize() B_PAGE_SIZE
535 /* This is for AmigaOS4.0. */
536 # if !defined getpagesize && defined __amigaos4__
537 # define getpagesize() 2048
539 /* This is for older Unix systems. */
540 # if !defined getpagesize && @HAVE_SYS_PARAM_H@
541 # include <sys/param.h>
542 # ifdef EXEC_PAGESIZE
543 # define getpagesize() EXEC_PAGESIZE
549 # define getpagesize() (NBPG * CLSIZE)
552 # define getpagesize() NBPC
558 #elif defined GNULIB_POSIXCHECK
560 # define getpagesize() \
561 (GL_LINK_WARNING ("getpagesize is unportable - " \
562 "use gnulib module getpagesize for portability"), \
567 #if @GNULIB_GETUSERSHELL@
568 # if !@HAVE_GETUSERSHELL@
569 /* Return the next valid login shell on the system, or NULL when the end of
570 the list has been reached. */
571 extern char *getusershell (void);
572 /* Rewind to pointer that is advanced at each getusershell() call. */
573 extern void setusershell (void);
574 /* Free the pointer that is advanced at each getusershell() call and
575 associated resources. */
576 extern void endusershell (void);
578 #elif defined GNULIB_POSIXCHECK
580 # define getusershell() \
581 (GL_LINK_WARNING ("getusershell is unportable - " \
582 "use gnulib module getusershell for portability"), \
585 # define setusershell() \
586 (GL_LINK_WARNING ("setusershell is unportable - " \
587 "use gnulib module getusershell for portability"), \
590 # define endusershell() \
591 (GL_LINK_WARNING ("endusershell is unportable - " \
592 "use gnulib module getusershell for portability"), \
598 # if @REPLACE_LCHOWN@
599 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
600 to GID (if GID is not -1). Do not follow symbolic links.
601 Return 0 if successful, otherwise -1 and errno set.
602 See the POSIX:2001 specification
603 <http://www.opengroup.org/susv3xsh/lchown.html>. */
604 # define lchown rpl_lchown
605 extern int lchown (char const *file, uid_t owner, gid_t group);
607 #elif defined GNULIB_POSIXCHECK
609 # define lchown(f,u,g) \
610 (GL_LINK_WARNING ("lchown is unportable to pre-POSIX.1-2001 " \
611 "systems - use gnulib module lchown for portability"), \
618 # define link rpl_link
620 /* Create a new hard link for an existing file.
621 Return 0 if successful, otherwise -1 and errno set.
622 See POSIX:2001 specification
623 <http://www.opengroup.org/susv3xsh/link.html>. */
624 # if !@HAVE_LINK@ || @REPLACE_LINK@
625 extern int link (const char *path1, const char *path2);
627 #elif defined GNULIB_POSIXCHECK
629 # define link(path1,path2) \
630 (GL_LINK_WARNING ("link is unportable - " \
631 "use gnulib module link for portability"), \
638 /* Set the offset of FD relative to SEEK_SET, SEEK_CUR, or SEEK_END.
639 Return the new offset if successful, otherwise -1 and errno set.
640 See the POSIX:2001 specification
641 <http://www.opengroup.org/susv3xsh/lseek.html>. */
642 # define lseek rpl_lseek
643 extern off_t lseek (int fd, off_t offset, int whence);
645 #elif defined GNULIB_POSIXCHECK
647 # define lseek(f,o,w) \
648 (GL_LINK_WARNING ("lseek does not fail with ESPIPE on pipes on some " \
649 "systems - use gnulib module lseek for portability"), \
655 /* Create a pipe, applying the given flags when opening the read-end of the
656 pipe and the write-end of the pipe.
657 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
658 and O_TEXT, O_BINARY (defined in "binary-io.h").
659 Store the read-end as fd[0] and the write-end as fd[1].
660 Return 0 upon success, or -1 with errno set upon failure.
661 See also the Linux man page at
662 <http://www.kernel.org/doc/man-pages/online/pages/man2/pipe2.2.html>. */
664 # define pipe2 rpl_pipe2
666 extern int pipe2 (int fd[2], int flags);
667 #elif defined GNULIB_POSIXCHECK
669 # define pipe2(f,o) \
670 (GL_LINK_WARNING ("pipe2 is unportable - " \
671 "use gnulib module pipe2 for portability"), \
676 #if @GNULIB_READLINK@
677 /* Read the contents of the symbolic link FILE and place the first BUFSIZE
678 bytes of it into BUF. Return the number of bytes placed into BUF if
679 successful, otherwise -1 and errno set.
680 See the POSIX:2001 specification
681 <http://www.opengroup.org/susv3xsh/readlink.html>. */
682 # if !@HAVE_READLINK@
683 extern int readlink (const char *file, char *buf, size_t bufsize);
685 #elif defined GNULIB_POSIXCHECK
687 # define readlink(f,b,s) \
688 (GL_LINK_WARNING ("readlink is unportable - " \
689 "use gnulib module readlink for portability"), \
696 # define rmdir rpl_rmdir
697 /* Remove the directory DIR. */
698 extern int rmdir (char const *name);
700 #elif defined GNULIB_POSIXCHECK
703 (GL_LINK_WARNING ("rmdir is unportable - " \
704 "use gnulib module rmdir for portability"), \
710 /* Pause the execution of the current thread for N seconds.
711 Returns the number of seconds left to sleep.
712 See the POSIX:2001 specification
713 <http://www.opengroup.org/susv3xsh/sleep.html>. */
715 extern unsigned int sleep (unsigned int n);
717 #elif defined GNULIB_POSIXCHECK
720 (GL_LINK_WARNING ("sleep is unportable - " \
721 "use gnulib module sleep for portability"), \
726 #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
727 /* Write up to COUNT bytes starting at BUF to file descriptor FD.
728 See the POSIX:2001 specification
729 <http://www.opengroup.org/susv3xsh/write.html>. */
731 # define write rpl_write
732 extern ssize_t write (int fd, const void *buf, size_t count);
741 #endif /* _GL_UNISTD_H */
742 #endif /* _GL_UNISTD_H */