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"), \
151 # if @REPLACE_FCHOWNAT@
153 # define fchownat rpl_fchownat
155 # if !@HAVE_FCHOWNAT@ || @REPLACE_FCHOWNAT@
156 int fchownat (int fd, char const *file, uid_t owner, gid_t group, int flag);
158 # if !@HAVE_UNLINKAT@
159 int unlinkat (int fd, char const *file, int flag);
161 #elif defined GNULIB_POSIXCHECK
163 # define fchownat(d,n,o,g,f) \
164 (GL_LINK_WARNING ("fchownat is not portable - " \
165 "use gnulib module openat for portability"), \
166 fchownat (d, n, o, g, f))
168 # define unlinkat(d,n,f) \
169 (GL_LINK_WARNING ("unlinkat is not portable - " \
170 "use gnulib module openat for portability"), \
172 #endif /* @GNULIB_OPENAT@ */
175 #if @GNULIB_FACCESSAT@
176 # if !@HAVE_FACCESSAT@
177 int faccessat (int fd, char const *file, int mode, int flag);
179 #elif defined GNULIB_POSIXCHECK
181 # define faccessat(d,n,m,f) \
182 (GL_LINK_WARNING ("faccessat is not portable - " \
183 "use gnulib module faccessat for portability"), \
184 faccessat (d, n, m, f))
187 #if @GNULIB_SYMLINKAT@
188 # if !@HAVE_SYMLINKAT@
189 int symlinkat (char const *contents, int fd, char const *file);
191 # if !@HAVE_READLINKAT@
192 ssize_t readlinkat (int fd, char const *file, char *buf, size_t len);
194 #elif defined GNULIB_POSIXCHECK
196 # define symlinkat(c,d,n) \
197 (GL_LINK_WARNING ("symlinkat is not portable - " \
198 "use gnulib module symlinkat for portability"), \
201 # define readlinkat(d,n,b,l) \
202 (GL_LINK_WARNING ("faccessat is not portable - " \
203 "use gnulib module symlinkat for portability"), \
204 readlinkat (d, n, b, l))
209 /* Automatically included by modules that need a replacement for close. */
211 # define close rpl_close
212 extern int close (int);
214 #elif @UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS@
216 # define close close_used_without_requesting_gnulib_module_close
217 #elif defined GNULIB_POSIXCHECK
220 (GL_LINK_WARNING ("close does not portably work on sockets - " \
221 "use gnulib module close for portability"), \
228 # define dup2 rpl_dup2
230 # if !@HAVE_DUP2@ || @REPLACE_DUP2@
231 /* Copy the file descriptor OLDFD into file descriptor NEWFD. Do nothing if
232 NEWFD = OLDFD, otherwise close NEWFD first if it is open.
233 Return newfd if successful, otherwise -1 and errno set.
234 See the POSIX:2001 specification
235 <http://www.opengroup.org/susv3xsh/dup2.html>. */
236 extern int dup2 (int oldfd, int newfd);
238 #elif defined GNULIB_POSIXCHECK
241 (GL_LINK_WARNING ("dup2 is unportable - " \
242 "use gnulib module dup2 for portability"), \
248 /* Copy the file descriptor OLDFD into file descriptor NEWFD, with the
250 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
251 and O_TEXT, O_BINARY (defined in "binary-io.h").
252 Close NEWFD first if it is open.
253 Return newfd if successful, otherwise -1 and errno set.
254 See the Linux man page at
255 <http://www.kernel.org/doc/man-pages/online/pages/man2/dup3.2.html>. */
257 # define dup3 rpl_dup3
259 extern int dup3 (int oldfd, int newfd, int flags);
260 #elif defined GNULIB_POSIXCHECK
262 # define dup3(o,n,f) \
263 (GL_LINK_WARNING ("dup3 is unportable - " \
264 "use gnulib module dup3 for portability"), \
270 # if !@HAVE_DECL_ENVIRON@
271 /* Set of environment variables and values. An array of strings of the form
272 "VARIABLE=VALUE", terminated with a NULL. */
273 # if defined __APPLE__ && defined __MACH__
274 # include <crt_externs.h>
275 # define environ (*_NSGetEnviron ())
277 extern char **environ;
280 #elif defined GNULIB_POSIXCHECK
283 (GL_LINK_WARNING ("environ is unportable - " \
284 "use gnulib module environ for portability"), \
289 #if @GNULIB_EUIDACCESS@
290 # if !@HAVE_EUIDACCESS@
291 /* Like access(), except that is uses the effective user id and group id of
292 the current process. */
293 extern int euidaccess (const char *filename, int mode);
295 #elif defined GNULIB_POSIXCHECK
297 # define euidaccess(f,m) \
298 (GL_LINK_WARNING ("euidaccess is unportable - " \
299 "use gnulib module euidaccess for portability"), \
305 # if @REPLACE_FCHDIR@
307 /* Change the process' current working directory to the directory on which
308 the given file descriptor is open.
309 Return 0 if successful, otherwise -1 and errno set.
310 See the POSIX:2001 specification
311 <http://www.opengroup.org/susv3xsh/fchdir.html>. */
312 extern int fchdir (int /*fd*/);
315 extern int dup (int);
317 /* Gnulib internal hooks needed to maintain the fchdir metadata. */
318 extern int _gl_register_fd (int fd, const char *filename);
319 extern void _gl_unregister_fd (int fd);
320 extern int _gl_register_dup (int oldfd, int newfd);
321 extern const char *_gl_directory_name (int fd);
324 #elif defined GNULIB_POSIXCHECK
327 (GL_LINK_WARNING ("fchdir is unportable - " \
328 "use gnulib module fchdir for portability"), \
334 /* Synchronize changes to a file.
335 Return 0 if successful, otherwise -1 and errno set.
336 See POSIX:2001 specification
337 <http://www.opengroup.org/susv3xsh/fsync.html>. */
339 extern int fsync (int fd);
341 #elif defined GNULIB_POSIXCHECK
344 (GL_LINK_WARNING ("fsync is unportable - " \
345 "use gnulib module fsync for portability"), \
350 #if @GNULIB_FTRUNCATE@
351 # if !@HAVE_FTRUNCATE@
352 /* Change the size of the file to which FD is opened to become equal to LENGTH.
353 Return 0 if successful, otherwise -1 and errno set.
354 See the POSIX:2001 specification
355 <http://www.opengroup.org/susv3xsh/ftruncate.html>. */
356 extern int ftruncate (int fd, off_t length);
358 #elif defined GNULIB_POSIXCHECK
360 # define ftruncate(f,l) \
361 (GL_LINK_WARNING ("ftruncate is unportable - " \
362 "use gnulib module ftruncate for portability"), \
368 /* Include the headers that might declare getcwd so that they will not
369 cause confusion if included after this file. */
371 # if @REPLACE_GETCWD@
372 /* Get the name of the current working directory, and put it in SIZE bytes
374 Return BUF if successful, or NULL if the directory couldn't be determined
375 or SIZE was too small.
376 See the POSIX:2001 specification
377 <http://www.opengroup.org/susv3xsh/getcwd.html>.
378 Additionally, the gnulib module 'getcwd' guarantees the following GNU
379 extension: If BUF is NULL, an array is allocated with 'malloc'; the array
380 is SIZE bytes long, unless SIZE == 0, in which case it is as big as
382 # define getcwd rpl_getcwd
383 extern char * getcwd (char *buf, size_t size);
385 #elif defined GNULIB_POSIXCHECK
387 # define getcwd(b,s) \
388 (GL_LINK_WARNING ("getcwd is unportable - " \
389 "use gnulib module getcwd for portability"), \
394 #if @GNULIB_GETDOMAINNAME@
395 /* Return the NIS domain name of the machine.
396 WARNING! The NIS domain name is unrelated to the fully qualified host name
397 of the machine. It is also unrelated to email addresses.
398 WARNING! The NIS domain name is usually the empty string or "(none)" when
401 Put up to LEN bytes of the NIS domain name into NAME.
402 Null terminate it if the name is shorter than LEN.
403 If the NIS domain name is longer than LEN, set errno = EINVAL and return -1.
404 Return 0 if successful, otherwise set errno and return -1. */
405 # if !@HAVE_GETDOMAINNAME@
406 extern int getdomainname(char *name, size_t len);
408 #elif defined GNULIB_POSIXCHECK
409 # undef getdomainname
410 # define getdomainname(n,l) \
411 (GL_LINK_WARNING ("getdomainname is unportable - " \
412 "use gnulib module getdomainname for portability"), \
413 getdomainname (n, l))
417 #if @GNULIB_GETDTABLESIZE@
418 # if !@HAVE_GETDTABLESIZE@
419 /* Return the maximum number of file descriptors in the current process.
420 In POSIX, this is same as sysconf (_SC_OPEN_MAX). */
421 extern int getdtablesize (void);
423 #elif defined GNULIB_POSIXCHECK
424 # undef getdtablesize
425 # define getdtablesize() \
426 (GL_LINK_WARNING ("getdtablesize is unportable - " \
427 "use gnulib module getdtablesize for portability"), \
432 #if @GNULIB_GETHOSTNAME@
433 /* Return the standard host name of the machine.
434 WARNING! The host name may or may not be fully qualified.
436 Put up to LEN bytes of the host name into NAME.
437 Null terminate it if the name is shorter than LEN.
438 If the host name is longer than LEN, set errno = EINVAL and return -1.
439 Return 0 if successful, otherwise set errno and return -1. */
440 # if @UNISTD_H_HAVE_WINSOCK2_H@
442 # define gethostname rpl_gethostname
444 # if @UNISTD_H_HAVE_WINSOCK2_H@ || !@HAVE_GETHOSTNAME@
445 extern int gethostname(char *name, size_t len);
447 #elif @UNISTD_H_HAVE_WINSOCK2_H@
449 # define gethostname gethostname_used_without_requesting_gnulib_module_gethostname
450 #elif defined GNULIB_POSIXCHECK
452 # define gethostname(n,l) \
453 (GL_LINK_WARNING ("gethostname is unportable - " \
454 "use gnulib module gethostname for portability"), \
459 #if @GNULIB_GETLOGIN_R@
460 /* Copies the user's login name to NAME.
461 The array pointed to by NAME has room for SIZE bytes.
463 Returns 0 if successful. Upon error, an error number is returned, or -1 in
464 the case that the login name cannot be found but no specific error is
465 provided (this case is hopefully rare but is left open by the POSIX spec).
467 See <http://www.opengroup.org/susv3xsh/getlogin.html>.
469 # if !@HAVE_DECL_GETLOGIN_R@
470 extern int getlogin_r (char *name, size_t size);
472 #elif defined GNULIB_POSIXCHECK
474 # define getlogin_r(n,s) \
475 (GL_LINK_WARNING ("getlogin_r is unportable - " \
476 "use gnulib module getlogin_r for portability"), \
481 #if @GNULIB_GETPAGESIZE@
482 # if @REPLACE_GETPAGESIZE@
483 # define getpagesize rpl_getpagesize
484 extern int getpagesize (void);
485 # elif !@HAVE_GETPAGESIZE@
486 /* This is for POSIX systems. */
487 # if !defined getpagesize && defined _SC_PAGESIZE
488 # if ! (defined __VMS && __VMS_VER < 70000000)
489 # define getpagesize() sysconf (_SC_PAGESIZE)
492 /* This is for older VMS. */
493 # if !defined getpagesize && defined __VMS
495 # define getpagesize() 8192
497 # define getpagesize() 512
500 /* This is for BeOS. */
501 # if !defined getpagesize && @HAVE_OS_H@
503 # if defined B_PAGE_SIZE
504 # define getpagesize() B_PAGE_SIZE
507 /* This is for AmigaOS4.0. */
508 # if !defined getpagesize && defined __amigaos4__
509 # define getpagesize() 2048
511 /* This is for older Unix systems. */
512 # if !defined getpagesize && @HAVE_SYS_PARAM_H@
513 # include <sys/param.h>
514 # ifdef EXEC_PAGESIZE
515 # define getpagesize() EXEC_PAGESIZE
521 # define getpagesize() (NBPG * CLSIZE)
524 # define getpagesize() NBPC
530 #elif defined GNULIB_POSIXCHECK
532 # define getpagesize() \
533 (GL_LINK_WARNING ("getpagesize is unportable - " \
534 "use gnulib module getpagesize for portability"), \
539 #if @GNULIB_GETUSERSHELL@
540 # if !@HAVE_GETUSERSHELL@
541 /* Return the next valid login shell on the system, or NULL when the end of
542 the list has been reached. */
543 extern char *getusershell (void);
544 /* Rewind to pointer that is advanced at each getusershell() call. */
545 extern void setusershell (void);
546 /* Free the pointer that is advanced at each getusershell() call and
547 associated resources. */
548 extern void endusershell (void);
550 #elif defined GNULIB_POSIXCHECK
552 # define getusershell() \
553 (GL_LINK_WARNING ("getusershell is unportable - " \
554 "use gnulib module getusershell for portability"), \
557 # define setusershell() \
558 (GL_LINK_WARNING ("setusershell is unportable - " \
559 "use gnulib module getusershell for portability"), \
562 # define endusershell() \
563 (GL_LINK_WARNING ("endusershell is unportable - " \
564 "use gnulib module getusershell for portability"), \
570 # if @REPLACE_LCHOWN@
571 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
572 to GID (if GID is not -1). Do not follow symbolic links.
573 Return 0 if successful, otherwise -1 and errno set.
574 See the POSIX:2001 specification
575 <http://www.opengroup.org/susv3xsh/lchown.html>. */
576 # define lchown rpl_lchown
577 extern int lchown (char const *file, uid_t owner, gid_t group);
579 #elif defined GNULIB_POSIXCHECK
581 # define lchown(f,u,g) \
582 (GL_LINK_WARNING ("lchown is unportable to pre-POSIX.1-2001 " \
583 "systems - use gnulib module lchown for portability"), \
589 /* Create a new hard link for an existing file.
590 Return 0 if successful, otherwise -1 and errno set.
591 See POSIX:2001 specification
592 <http://www.opengroup.org/susv3xsh/link.html>. */
594 extern int link (const char *path1, const char *path2);
596 #elif defined GNULIB_POSIXCHECK
598 # define link(path1,path2) \
599 (GL_LINK_WARNING ("link is unportable - " \
600 "use gnulib module link for portability"), \
607 /* Set the offset of FD relative to SEEK_SET, SEEK_CUR, or SEEK_END.
608 Return the new offset if successful, otherwise -1 and errno set.
609 See the POSIX:2001 specification
610 <http://www.opengroup.org/susv3xsh/lseek.html>. */
611 # define lseek rpl_lseek
612 extern off_t lseek (int fd, off_t offset, int whence);
614 #elif defined GNULIB_POSIXCHECK
616 # define lseek(f,o,w) \
617 (GL_LINK_WARNING ("lseek does not fail with ESPIPE on pipes on some " \
618 "systems - use gnulib module lseek for portability"), \
624 /* Create a pipe, applying the given flags when opening the read-end of the
625 pipe and the write-end of the pipe.
626 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
627 and O_TEXT, O_BINARY (defined in "binary-io.h").
628 Store the read-end as fd[0] and the write-end as fd[1].
629 Return 0 upon success, or -1 with errno set upon failure.
630 See also the Linux man page at
631 <http://www.kernel.org/doc/man-pages/online/pages/man2/pipe2.2.html>. */
633 # define pipe2 rpl_pipe2
635 extern int pipe2 (int fd[2], int flags);
636 #elif defined GNULIB_POSIXCHECK
638 # define pipe2(f,o) \
639 (GL_LINK_WARNING ("pipe2 is unportable - " \
640 "use gnulib module pipe2 for portability"), \
645 #if @GNULIB_READLINK@
646 /* Read the contents of the symbolic link FILE and place the first BUFSIZE
647 bytes of it into BUF. Return the number of bytes placed into BUF if
648 successful, otherwise -1 and errno set.
649 See the POSIX:2001 specification
650 <http://www.opengroup.org/susv3xsh/readlink.html>. */
651 # if !@HAVE_READLINK@
652 extern int readlink (const char *file, char *buf, size_t bufsize);
654 #elif defined GNULIB_POSIXCHECK
656 # define readlink(f,b,s) \
657 (GL_LINK_WARNING ("readlink is unportable - " \
658 "use gnulib module readlink for portability"), \
664 /* Pause the execution of the current thread for N seconds.
665 Returns the number of seconds left to sleep.
666 See the POSIX:2001 specification
667 <http://www.opengroup.org/susv3xsh/sleep.html>. */
669 extern unsigned int sleep (unsigned int n);
671 #elif defined GNULIB_POSIXCHECK
674 (GL_LINK_WARNING ("sleep is unportable - " \
675 "use gnulib module sleep for portability"), \
680 #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
681 /* Write up to COUNT bytes starting at BUF to file descriptor FD.
682 See the POSIX:2001 specification
683 <http://www.opengroup.org/susv3xsh/write.html>. */
685 # define write rpl_write
686 extern ssize_t write (int fd, const void *buf, size_t count);
695 #endif /* _GL_UNISTD_H */
696 #endif /* _GL_UNISTD_H */