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 macros 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"), \
152 /* Automatically included by modules that need a replacement for close. */
154 # define close rpl_close
155 extern int close (int);
157 #elif @UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS@
159 # define close close_used_without_requesting_gnulib_module_close
160 #elif defined GNULIB_POSIXCHECK
163 (GL_LINK_WARNING ("close does not portably work on sockets - " \
164 "use gnulib module close for portability"), \
171 extern int dup (int);
177 # define dup2 rpl_dup2
179 # if !@HAVE_DUP2@ || @REPLACE_DUP2@
180 /* Copy the file descriptor OLDFD into file descriptor NEWFD. Do nothing if
181 NEWFD = OLDFD, otherwise close NEWFD first if it is open.
182 Return newfd if successful, otherwise -1 and errno set.
183 See the POSIX:2001 specification
184 <http://www.opengroup.org/susv3xsh/dup2.html>. */
185 extern int dup2 (int oldfd, int newfd);
187 #elif defined GNULIB_POSIXCHECK
190 (GL_LINK_WARNING ("dup2 is unportable - " \
191 "use gnulib module dup2 for portability"), \
197 /* Copy the file descriptor OLDFD into file descriptor NEWFD, with the
199 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
200 and O_TEXT, O_BINARY (defined in "binary-io.h").
201 Close NEWFD first if it is open.
202 Return newfd if successful, otherwise -1 and errno set.
203 See the Linux man page at
204 <http://www.kernel.org/doc/man-pages/online/pages/man2/dup3.2.html>. */
206 # define dup3 rpl_dup3
208 extern int dup3 (int oldfd, int newfd, int flags);
209 #elif defined GNULIB_POSIXCHECK
211 # define dup3(o,n,f) \
212 (GL_LINK_WARNING ("dup3 is unportable - " \
213 "use gnulib module dup3 for portability"), \
219 # if !@HAVE_DECL_ENVIRON@
220 /* Set of environment variables and values. An array of strings of the form
221 "VARIABLE=VALUE", terminated with a NULL. */
222 # if defined __APPLE__ && defined __MACH__
223 # include <crt_externs.h>
224 # define environ (*_NSGetEnviron ())
226 extern char **environ;
229 #elif defined GNULIB_POSIXCHECK
232 (GL_LINK_WARNING ("environ is unportable - " \
233 "use gnulib module environ for portability"), \
238 #if @GNULIB_EUIDACCESS@
239 # if !@HAVE_EUIDACCESS@
240 /* Like access(), except that is uses the effective user id and group id of
241 the current process. */
242 extern int euidaccess (const char *filename, int mode);
244 #elif defined GNULIB_POSIXCHECK
246 # define euidaccess(f,m) \
247 (GL_LINK_WARNING ("euidaccess is unportable - " \
248 "use gnulib module euidaccess for portability"), \
253 #if @GNULIB_FACCESSAT@
254 # if !@HAVE_FACCESSAT@
255 int faccessat (int fd, char const *file, int mode, int flag);
257 #elif defined GNULIB_POSIXCHECK
259 # define faccessat(d,n,m,f) \
260 (GL_LINK_WARNING ("faccessat is not portable - " \
261 "use gnulib module faccessat for portability"), \
262 faccessat (d, n, m, f))
267 # if @REPLACE_FCHDIR@
268 /* Change the process' current working directory to the directory on which
269 the given file descriptor is open.
270 Return 0 if successful, otherwise -1 and errno set.
271 See the POSIX:2001 specification
272 <http://www.opengroup.org/susv3xsh/fchdir.html>. */
273 extern int fchdir (int /*fd*/);
275 /* Gnulib internal hooks needed to maintain the fchdir metadata. */
276 extern int _gl_register_fd (int fd, const char *filename);
277 extern void _gl_unregister_fd (int fd);
278 extern int _gl_register_dup (int oldfd, int newfd);
279 extern const char *_gl_directory_name (int fd);
282 #elif defined GNULIB_POSIXCHECK
285 (GL_LINK_WARNING ("fchdir is unportable - " \
286 "use gnulib module fchdir for portability"), \
291 #if @GNULIB_FCHOWNAT@
292 # if @REPLACE_FCHOWNAT@
294 # define fchownat rpl_fchownat
296 # if !@HAVE_FCHOWNAT@ || @REPLACE_FCHOWNAT@
297 extern int fchownat (int fd, char const *file, uid_t owner, gid_t group, int flag);
299 #elif defined GNULIB_POSIXCHECK
301 # define fchownat(d,n,o,g,f) \
302 (GL_LINK_WARNING ("fchownat is not portable - " \
303 "use gnulib module openat for portability"), \
304 fchownat (d, n, o, g, f))
309 /* Synchronize changes to a file.
310 Return 0 if successful, otherwise -1 and errno set.
311 See POSIX:2001 specification
312 <http://www.opengroup.org/susv3xsh/fsync.html>. */
314 extern int fsync (int fd);
316 #elif defined GNULIB_POSIXCHECK
319 (GL_LINK_WARNING ("fsync is unportable - " \
320 "use gnulib module fsync for portability"), \
325 #if @GNULIB_FTRUNCATE@
326 # if !@HAVE_FTRUNCATE@
327 /* Change the size of the file to which FD is opened to become equal to LENGTH.
328 Return 0 if successful, otherwise -1 and errno set.
329 See the POSIX:2001 specification
330 <http://www.opengroup.org/susv3xsh/ftruncate.html>. */
331 extern int ftruncate (int fd, off_t length);
333 #elif defined GNULIB_POSIXCHECK
335 # define ftruncate(f,l) \
336 (GL_LINK_WARNING ("ftruncate is unportable - " \
337 "use gnulib module ftruncate for portability"), \
343 /* Include the headers that might declare getcwd so that they will not
344 cause confusion if included after this file. */
346 # if @REPLACE_GETCWD@
347 /* Get the name of the current working directory, and put it in SIZE bytes
349 Return BUF if successful, or NULL if the directory couldn't be determined
350 or SIZE was too small.
351 See the POSIX:2001 specification
352 <http://www.opengroup.org/susv3xsh/getcwd.html>.
353 Additionally, the gnulib module 'getcwd' guarantees the following GNU
354 extension: If BUF is NULL, an array is allocated with 'malloc'; the array
355 is SIZE bytes long, unless SIZE == 0, in which case it is as big as
357 # define getcwd rpl_getcwd
358 extern char * getcwd (char *buf, size_t size);
360 #elif defined GNULIB_POSIXCHECK
362 # define getcwd(b,s) \
363 (GL_LINK_WARNING ("getcwd is unportable - " \
364 "use gnulib module getcwd for portability"), \
369 #if @GNULIB_GETDOMAINNAME@
370 /* Return the NIS domain name of the machine.
371 WARNING! The NIS domain name is unrelated to the fully qualified host name
372 of the machine. It is also unrelated to email addresses.
373 WARNING! The NIS domain name is usually the empty string or "(none)" when
376 Put up to LEN bytes of the NIS domain name into NAME.
377 Null terminate it if the name is shorter than LEN.
378 If the NIS domain name is longer than LEN, set errno = EINVAL and return -1.
379 Return 0 if successful, otherwise set errno and return -1. */
380 # if !@HAVE_GETDOMAINNAME@
381 extern int getdomainname(char *name, size_t len);
383 #elif defined GNULIB_POSIXCHECK
384 # undef getdomainname
385 # define getdomainname(n,l) \
386 (GL_LINK_WARNING ("getdomainname is unportable - " \
387 "use gnulib module getdomainname for portability"), \
388 getdomainname (n, l))
392 #if @GNULIB_GETDTABLESIZE@
393 # if !@HAVE_GETDTABLESIZE@
394 /* Return the maximum number of file descriptors in the current process.
395 In POSIX, this is same as sysconf (_SC_OPEN_MAX). */
396 extern int getdtablesize (void);
398 #elif defined GNULIB_POSIXCHECK
399 # undef getdtablesize
400 # define getdtablesize() \
401 (GL_LINK_WARNING ("getdtablesize is unportable - " \
402 "use gnulib module getdtablesize for portability"), \
407 #if @GNULIB_GETHOSTNAME@
408 /* Return the standard host name of the machine.
409 WARNING! The host name may or may not be fully qualified.
411 Put up to LEN bytes of the host name into NAME.
412 Null terminate it if the name is shorter than LEN.
413 If the host name is longer than LEN, set errno = EINVAL and return -1.
414 Return 0 if successful, otherwise set errno and return -1. */
415 # if @UNISTD_H_HAVE_WINSOCK2_H@
417 # define gethostname rpl_gethostname
419 # if @UNISTD_H_HAVE_WINSOCK2_H@ || !@HAVE_GETHOSTNAME@
420 extern int gethostname(char *name, size_t len);
422 #elif @UNISTD_H_HAVE_WINSOCK2_H@
424 # define gethostname gethostname_used_without_requesting_gnulib_module_gethostname
425 #elif defined GNULIB_POSIXCHECK
427 # define gethostname(n,l) \
428 (GL_LINK_WARNING ("gethostname is unportable - " \
429 "use gnulib module gethostname for portability"), \
434 #if @GNULIB_GETLOGIN_R@
435 /* Copies the user's login name to NAME.
436 The array pointed to by NAME has room for SIZE bytes.
438 Returns 0 if successful. Upon error, an error number is returned, or -1 in
439 the case that the login name cannot be found but no specific error is
440 provided (this case is hopefully rare but is left open by the POSIX spec).
442 See <http://www.opengroup.org/susv3xsh/getlogin.html>.
444 # if !@HAVE_DECL_GETLOGIN_R@
445 extern int getlogin_r (char *name, size_t size);
447 #elif defined GNULIB_POSIXCHECK
449 # define getlogin_r(n,s) \
450 (GL_LINK_WARNING ("getlogin_r is unportable - " \
451 "use gnulib module getlogin_r for portability"), \
456 #if @GNULIB_GETPAGESIZE@
457 # if @REPLACE_GETPAGESIZE@
458 # define getpagesize rpl_getpagesize
459 extern int getpagesize (void);
460 # elif !@HAVE_GETPAGESIZE@
461 /* This is for POSIX systems. */
462 # if !defined getpagesize && defined _SC_PAGESIZE
463 # if ! (defined __VMS && __VMS_VER < 70000000)
464 # define getpagesize() sysconf (_SC_PAGESIZE)
467 /* This is for older VMS. */
468 # if !defined getpagesize && defined __VMS
470 # define getpagesize() 8192
472 # define getpagesize() 512
475 /* This is for BeOS. */
476 # if !defined getpagesize && @HAVE_OS_H@
478 # if defined B_PAGE_SIZE
479 # define getpagesize() B_PAGE_SIZE
482 /* This is for AmigaOS4.0. */
483 # if !defined getpagesize && defined __amigaos4__
484 # define getpagesize() 2048
486 /* This is for older Unix systems. */
487 # if !defined getpagesize && @HAVE_SYS_PARAM_H@
488 # include <sys/param.h>
489 # ifdef EXEC_PAGESIZE
490 # define getpagesize() EXEC_PAGESIZE
496 # define getpagesize() (NBPG * CLSIZE)
499 # define getpagesize() NBPC
505 #elif defined GNULIB_POSIXCHECK
507 # define getpagesize() \
508 (GL_LINK_WARNING ("getpagesize is unportable - " \
509 "use gnulib module getpagesize for portability"), \
514 #if @GNULIB_GETUSERSHELL@
515 # if !@HAVE_GETUSERSHELL@
516 /* Return the next valid login shell on the system, or NULL when the end of
517 the list has been reached. */
518 extern char *getusershell (void);
519 /* Rewind to pointer that is advanced at each getusershell() call. */
520 extern void setusershell (void);
521 /* Free the pointer that is advanced at each getusershell() call and
522 associated resources. */
523 extern void endusershell (void);
525 #elif defined GNULIB_POSIXCHECK
527 # define getusershell() \
528 (GL_LINK_WARNING ("getusershell is unportable - " \
529 "use gnulib module getusershell for portability"), \
532 # define setusershell() \
533 (GL_LINK_WARNING ("setusershell is unportable - " \
534 "use gnulib module getusershell for portability"), \
537 # define endusershell() \
538 (GL_LINK_WARNING ("endusershell is unportable - " \
539 "use gnulib module getusershell for portability"), \
545 # if @REPLACE_LCHOWN@
546 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
547 to GID (if GID is not -1). Do not follow symbolic links.
548 Return 0 if successful, otherwise -1 and errno set.
549 See the POSIX:2001 specification
550 <http://www.opengroup.org/susv3xsh/lchown.html>. */
551 # define lchown rpl_lchown
552 extern int lchown (char const *file, uid_t owner, gid_t group);
554 #elif defined GNULIB_POSIXCHECK
556 # define lchown(f,u,g) \
557 (GL_LINK_WARNING ("lchown is unportable to pre-POSIX.1-2001 " \
558 "systems - use gnulib module lchown for portability"), \
565 # define link rpl_link
567 /* Create a new hard link for an existing file.
568 Return 0 if successful, otherwise -1 and errno set.
569 See POSIX:2001 specification
570 <http://www.opengroup.org/susv3xsh/link.html>. */
571 # if !@HAVE_LINK@ || @REPLACE_LINK@
572 extern int link (const char *path1, const char *path2);
574 #elif defined GNULIB_POSIXCHECK
576 # define link(path1,path2) \
577 (GL_LINK_WARNING ("link is unportable - " \
578 "use gnulib module link for portability"), \
585 /* Set the offset of FD relative to SEEK_SET, SEEK_CUR, or SEEK_END.
586 Return the new offset if successful, otherwise -1 and errno set.
587 See the POSIX:2001 specification
588 <http://www.opengroup.org/susv3xsh/lseek.html>. */
589 # define lseek rpl_lseek
590 extern off_t lseek (int fd, off_t offset, int whence);
592 #elif defined GNULIB_POSIXCHECK
594 # define lseek(f,o,w) \
595 (GL_LINK_WARNING ("lseek does not fail with ESPIPE on pipes on some " \
596 "systems - use gnulib module lseek for portability"), \
602 /* Create a pipe, applying the given flags when opening the read-end of the
603 pipe and the write-end of the pipe.
604 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
605 and O_TEXT, O_BINARY (defined in "binary-io.h").
606 Store the read-end as fd[0] and the write-end as fd[1].
607 Return 0 upon success, or -1 with errno set upon failure.
608 See also the Linux man page at
609 <http://www.kernel.org/doc/man-pages/online/pages/man2/pipe2.2.html>. */
611 # define pipe2 rpl_pipe2
613 extern int pipe2 (int fd[2], int flags);
614 #elif defined GNULIB_POSIXCHECK
616 # define pipe2(f,o) \
617 (GL_LINK_WARNING ("pipe2 is unportable - " \
618 "use gnulib module pipe2 for portability"), \
623 #if @GNULIB_READLINK@
624 /* Read the contents of the symbolic link FILE and place the first BUFSIZE
625 bytes of it into BUF. Return the number of bytes placed into BUF if
626 successful, otherwise -1 and errno set.
627 See the POSIX:2001 specification
628 <http://www.opengroup.org/susv3xsh/readlink.html>. */
629 # if !@HAVE_READLINK@
630 extern int readlink (const char *file, char *buf, size_t bufsize);
632 #elif defined GNULIB_POSIXCHECK
634 # define readlink(f,b,s) \
635 (GL_LINK_WARNING ("readlink is unportable - " \
636 "use gnulib module readlink for portability"), \
641 #if @GNULIB_READLINKAT@
642 # if !@HAVE_READLINKAT@
643 ssize_t readlinkat (int fd, char const *file, char *buf, size_t len);
645 #elif defined GNULIB_POSIXCHECK
647 # define readlinkat(d,n,b,l) \
648 (GL_LINK_WARNING ("readlinkat is not portable - " \
649 "use gnulib module symlinkat for portability"), \
650 readlinkat (d, n, b, l))
656 # define rmdir rpl_rmdir
657 /* Remove the directory DIR. */
658 extern int rmdir (char const *name);
660 #elif defined GNULIB_POSIXCHECK
663 (GL_LINK_WARNING ("rmdir is unportable - " \
664 "use gnulib module rmdir for portability"), \
670 /* Pause the execution of the current thread for N seconds.
671 Returns the number of seconds left to sleep.
672 See the POSIX:2001 specification
673 <http://www.opengroup.org/susv3xsh/sleep.html>. */
675 extern unsigned int sleep (unsigned int n);
677 #elif defined GNULIB_POSIXCHECK
680 (GL_LINK_WARNING ("sleep is unportable - " \
681 "use gnulib module sleep for portability"), \
687 # if @REPLACE_SYMLINK@
689 # define symlink rpl_symlink
691 # if !@HAVE_SYMLINK@ || @REPLACE_SYMLINK@
692 int symlink (char const *contents, char const *file);
694 #elif defined GNULIB_POSIXCHECK
696 # define symlink(c,n) \
697 (GL_LINK_WARNING ("symlink is not portable - " \
698 "use gnulib module symlink for portability"), \
703 #if @GNULIB_SYMLINKAT@
704 # if !@HAVE_SYMLINKAT@
705 int symlinkat (char const *contents, int fd, char const *file);
707 #elif defined GNULIB_POSIXCHECK
709 # define symlinkat(c,d,n) \
710 (GL_LINK_WARNING ("symlinkat is not portable - " \
711 "use gnulib module symlinkat for portability"), \
717 # if @REPLACE_UNLINK@
719 # define unlink rpl_unlink
720 extern int unlink (char const *file);
722 #elif defined GNULIB_POSIXCHECK
725 (GL_LINK_WARNING ("unlink is not portable - " \
726 "use gnulib module unlink for portability"), \
731 #if @GNULIB_UNLINKAT@
732 # if @REPLACE_UNLINKAT@
734 # define unlinkat rpl_unlinkat
736 # if !@HAVE_UNLINKAT@ || @REPLACE_UNLINKAT@
737 extern int unlinkat (int fd, char const *file, int flag);
739 #elif defined GNULIB_POSIXCHECK
741 # define unlinkat(d,n,f) \
742 (GL_LINK_WARNING ("unlinkat is not portable - " \
743 "use gnulib module openat for portability"), \
748 #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
749 /* Write up to COUNT bytes starting at BUF to file descriptor FD.
750 See the POSIX:2001 specification
751 <http://www.opengroup.org/susv3xsh/write.html>. */
753 # define write rpl_write
754 extern ssize_t write (int fd, const void *buf, size_t count);
763 #endif /* _GL_UNISTD_H */
764 #endif /* _GL_UNISTD_H */