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 /* Declare overridden functions. */
118 # ifndef REPLACE_CHOWN
119 # define REPLACE_CHOWN 1
122 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
123 to GID (if GID is not -1). Follow symbolic links.
124 Return 0 if successful, otherwise -1 and errno set.
125 See the POSIX:2001 specification
126 <http://www.opengroup.org/susv3xsh/chown.html>. */
127 # define chown rpl_chown
128 extern int chown (const char *file, uid_t uid, gid_t gid);
131 #elif defined GNULIB_POSIXCHECK
133 # define chown(f,u,g) \
134 (GL_LINK_WARNING ("chown fails to follow symlinks on some systems and " \
135 "doesn't treat a uid or gid of -1 on some systems - " \
136 "use gnulib module chown for portability"), \
143 /* Automatically included by modules that need a replacement for close. */
145 # define close rpl_close
146 extern int close (int);
148 #elif @UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS@
150 # define close close_used_without_requesting_gnulib_module_close
151 #elif defined GNULIB_POSIXCHECK
154 (GL_LINK_WARNING ("close does not portably work on sockets - " \
155 "use gnulib module close for portability"), \
162 # define dup2 rpl_dup2
164 # if !@HAVE_DUP2@ || @REPLACE_DUP2@
165 /* Copy the file descriptor OLDFD into file descriptor NEWFD. Do nothing if
166 NEWFD = OLDFD, otherwise close NEWFD first if it is open.
167 Return newfd if successful, otherwise -1 and errno set.
168 See the POSIX:2001 specification
169 <http://www.opengroup.org/susv3xsh/dup2.html>. */
170 extern int dup2 (int oldfd, int newfd);
172 #elif defined GNULIB_POSIXCHECK
175 (GL_LINK_WARNING ("dup2 is unportable - " \
176 "use gnulib module dup2 for portability"), \
182 # if !@HAVE_DECL_ENVIRON@
183 /* Set of environment variables and values. An array of strings of the form
184 "VARIABLE=VALUE", terminated with a NULL. */
185 # if defined __APPLE__ && defined __MACH__
186 # include <crt_externs.h>
187 # define environ (*_NSGetEnviron ())
189 extern char **environ;
192 #elif defined GNULIB_POSIXCHECK
195 (GL_LINK_WARNING ("environ is unportable - " \
196 "use gnulib module environ for portability"), \
201 #if @GNULIB_EUIDACCESS@
202 # if !@HAVE_EUIDACCESS@
203 /* Like access(), except that is uses the effective user id and group id of
204 the current process. */
205 extern int euidaccess (const char *filename, int mode);
207 #elif defined GNULIB_POSIXCHECK
209 # define euidaccess(f,m) \
210 (GL_LINK_WARNING ("euidaccess is unportable - " \
211 "use gnulib module euidaccess for portability"), \
217 # if @REPLACE_FCHDIR@
219 /* Change the process' current working directory to the directory on which
220 the given file descriptor is open.
221 Return 0 if successful, otherwise -1 and errno set.
222 See the POSIX:2001 specification
223 <http://www.opengroup.org/susv3xsh/fchdir.html>. */
224 extern int fchdir (int /*fd*/);
227 extern int dup (int);
232 # define dup2 rpl_dup2_fchdir
233 extern int dup2 (int, int);
236 #elif defined GNULIB_POSIXCHECK
239 (GL_LINK_WARNING ("fchdir is unportable - " \
240 "use gnulib module fchdir for portability"), \
246 /* Synchronize changes to a file.
247 Return 0 if successful, otherwise -1 and errno set.
248 See POSIX:2001 specification
249 <http://www.opengroup.org/susv3xsh/fsync.html>. */
251 extern int fsync (int fd);
253 #elif defined GNULIB_POSIXCHECK
256 (GL_LINK_WARNING ("fsync is unportable - " \
257 "use gnulib module fsync for portability"), \
262 #if @GNULIB_FTRUNCATE@
263 # if !@HAVE_FTRUNCATE@
264 /* Change the size of the file to which FD is opened to become equal to LENGTH.
265 Return 0 if successful, otherwise -1 and errno set.
266 See the POSIX:2001 specification
267 <http://www.opengroup.org/susv3xsh/ftruncate.html>. */
268 extern int ftruncate (int fd, off_t length);
270 #elif defined GNULIB_POSIXCHECK
272 # define ftruncate(f,l) \
273 (GL_LINK_WARNING ("ftruncate is unportable - " \
274 "use gnulib module ftruncate for portability"), \
280 /* Include the headers that might declare getcwd so that they will not
281 cause confusion if included after this file. */
283 # if @REPLACE_GETCWD@
284 /* Get the name of the current working directory, and put it in SIZE bytes
286 Return BUF if successful, or NULL if the directory couldn't be determined
287 or SIZE was too small.
288 See the POSIX:2001 specification
289 <http://www.opengroup.org/susv3xsh/getcwd.html>.
290 Additionally, the gnulib module 'getcwd' guarantees the following GNU
291 extension: If BUF is NULL, an array is allocated with 'malloc'; the array
292 is SIZE bytes long, unless SIZE == 0, in which case it is as big as
294 # define getcwd rpl_getcwd
295 extern char * getcwd (char *buf, size_t size);
297 #elif defined GNULIB_POSIXCHECK
299 # define getcwd(b,s) \
300 (GL_LINK_WARNING ("getcwd is unportable - " \
301 "use gnulib module getcwd for portability"), \
306 #if @GNULIB_GETDOMAINNAME@
307 /* Return the NIS domain name of the machine.
308 WARNING! The NIS domain name is unrelated to the fully qualified host name
309 of the machine. It is also unrelated to email addresses.
310 WARNING! The NIS domain name is usually the empty string or "(none)" when
313 Put up to LEN bytes of the NIS domain name into NAME.
314 Null terminate it if the name is shorter than LEN.
315 If the NIS domain name is longer than LEN, set errno = EINVAL and return -1.
316 Return 0 if successful, otherwise set errno and return -1. */
317 # if !@HAVE_GETDOMAINNAME@
318 extern int getdomainname(char *name, size_t len);
320 #elif defined GNULIB_POSIXCHECK
321 # undef getdomainname
322 # define getdomainname(n,l) \
323 (GL_LINK_WARNING ("getdomainname is unportable - " \
324 "use gnulib module getdomainname for portability"), \
325 getdomainname (n, l))
329 #if @GNULIB_GETDTABLESIZE@
330 # if !@HAVE_GETDTABLESIZE@
331 /* Return the maximum number of file descriptors in the current process.
332 In POSIX, this is same as sysconf (_SC_OPEN_MAX). */
333 extern int getdtablesize (void);
335 #elif defined GNULIB_POSIXCHECK
336 # undef getdtablesize
337 # define getdtablesize() \
338 (GL_LINK_WARNING ("getdtablesize is unportable - " \
339 "use gnulib module getdtablesize for portability"), \
344 #if @GNULIB_GETHOSTNAME@
345 /* Return the standard host name of the machine.
346 WARNING! The host name may or may not be fully qualified.
348 Put up to LEN bytes of the host name into NAME.
349 Null terminate it if the name is shorter than LEN.
350 If the host name is longer than LEN, set errno = EINVAL and return -1.
351 Return 0 if successful, otherwise set errno and return -1. */
352 # if @UNISTD_H_HAVE_WINSOCK2_H@
354 # define gethostname rpl_gethostname
356 # if @UNISTD_H_HAVE_WINSOCK2_H@ || !@HAVE_GETHOSTNAME@
357 extern int gethostname(char *name, size_t len);
359 #elif @UNISTD_H_HAVE_WINSOCK2_H@
361 # define gethostname gethostname_used_without_requesting_gnulib_module_gethostname
362 #elif defined GNULIB_POSIXCHECK
364 # define gethostname(n,l) \
365 (GL_LINK_WARNING ("gethostname is unportable - " \
366 "use gnulib module gethostname for portability"), \
371 #if @GNULIB_GETLOGIN_R@
372 /* Copies the user's login name to NAME.
373 The array pointed to by NAME has room for SIZE bytes.
375 Returns 0 if successful. Upon error, an error number is returned, or -1 in
376 the case that the login name cannot be found but no specific error is
377 provided (this case is hopefully rare but is left open by the POSIX spec).
379 See <http://www.opengroup.org/susv3xsh/getlogin.html>.
381 # if !@HAVE_DECL_GETLOGIN_R@
382 extern int getlogin_r (char *name, size_t size);
384 #elif defined GNULIB_POSIXCHECK
386 # define getlogin_r(n,s) \
387 (GL_LINK_WARNING ("getlogin_r is unportable - " \
388 "use gnulib module getlogin_r for portability"), \
393 #if @GNULIB_GETPAGESIZE@
394 # if @REPLACE_GETPAGESIZE@
395 # define getpagesize rpl_getpagesize
396 extern int getpagesize (void);
397 # elif !@HAVE_GETPAGESIZE@
398 /* This is for POSIX systems. */
399 # if !defined getpagesize && defined _SC_PAGESIZE
400 # if ! (defined __VMS && __VMS_VER < 70000000)
401 # define getpagesize() sysconf (_SC_PAGESIZE)
404 /* This is for older VMS. */
405 # if !defined getpagesize && defined __VMS
407 # define getpagesize() 8192
409 # define getpagesize() 512
412 /* This is for BeOS. */
413 # if !defined getpagesize && @HAVE_OS_H@
415 # if defined B_PAGE_SIZE
416 # define getpagesize() B_PAGE_SIZE
419 /* This is for AmigaOS4.0. */
420 # if !defined getpagesize && defined __amigaos4__
421 # define getpagesize() 2048
423 /* This is for older Unix systems. */
424 # if !defined getpagesize && @HAVE_SYS_PARAM_H@
425 # include <sys/param.h>
426 # ifdef EXEC_PAGESIZE
427 # define getpagesize() EXEC_PAGESIZE
433 # define getpagesize() (NBPG * CLSIZE)
436 # define getpagesize() NBPC
442 #elif defined GNULIB_POSIXCHECK
444 # define getpagesize() \
445 (GL_LINK_WARNING ("getpagesize is unportable - " \
446 "use gnulib module getpagesize for portability"), \
451 #if @GNULIB_GETUSERSHELL@
452 # if !@HAVE_GETUSERSHELL@
453 /* Return the next valid login shell on the system, or NULL when the end of
454 the list has been reached. */
455 extern char *getusershell (void);
456 /* Rewind to pointer that is advanced at each getusershell() call. */
457 extern void setusershell (void);
458 /* Free the pointer that is advanced at each getusershell() call and
459 associated resources. */
460 extern void endusershell (void);
462 #elif defined GNULIB_POSIXCHECK
464 # define getusershell() \
465 (GL_LINK_WARNING ("getusershell is unportable - " \
466 "use gnulib module getusershell for portability"), \
469 # define setusershell() \
470 (GL_LINK_WARNING ("setusershell is unportable - " \
471 "use gnulib module getusershell for portability"), \
474 # define endusershell() \
475 (GL_LINK_WARNING ("endusershell is unportable - " \
476 "use gnulib module getusershell for portability"), \
482 # if @REPLACE_LCHOWN@
483 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
484 to GID (if GID is not -1). Do not follow symbolic links.
485 Return 0 if successful, otherwise -1 and errno set.
486 See the POSIX:2001 specification
487 <http://www.opengroup.org/susv3xsh/lchown.html>. */
488 # define lchown rpl_lchown
489 extern int lchown (char const *file, uid_t owner, gid_t group);
491 #elif defined GNULIB_POSIXCHECK
493 # define lchown(f,u,g) \
494 (GL_LINK_WARNING ("lchown is unportable to pre-POSIX.1-2001 " \
495 "systems - use gnulib module lchown for portability"), \
501 /* Create a new hard link for an existing file.
502 Return 0 if successful, otherwise -1 and errno set.
503 See POSIX:2001 specification
504 <http://www.opengroup.org/susv3xsh/link.html>. */
506 extern int link (const char *path1, const char *path2);
508 #elif defined GNULIB_POSIXCHECK
510 # define link(path1,path2) \
511 (GL_LINK_WARNING ("link is unportable - " \
512 "use gnulib module link for portability"), \
519 /* Set the offset of FD relative to SEEK_SET, SEEK_CUR, or SEEK_END.
520 Return the new offset if successful, otherwise -1 and errno set.
521 See the POSIX:2001 specification
522 <http://www.opengroup.org/susv3xsh/lseek.html>. */
523 # define lseek rpl_lseek
524 extern off_t lseek (int fd, off_t offset, int whence);
526 #elif defined GNULIB_POSIXCHECK
528 # define lseek(f,o,w) \
529 (GL_LINK_WARNING ("lseek does not fail with ESPIPE on pipes on some " \
530 "systems - use gnulib module lseek for portability"), \
536 /* Create a pipe, applying the given flags when opening the read-end of the
537 pipe and the write-end of the pipe.
538 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
539 and O_TEXT, O_BINARY (defined in "binary-io.h").
540 Store the read-end as fd[0] and the write-end as fd[1].
541 Return 0 upon success, or -1 with errno set upon failure.
542 See also the Linux man page at
543 <http://www.kernel.org/doc/man-pages/online/pages/man2/pipe2.2.html>. */
545 extern int pipe2 (int fd[2], int flags);
547 #elif defined GNULIB_POSIXCHECK
549 # define pipe2(f,o) \
550 (GL_LINK_WARNING ("pipe2 is unportable - " \
551 "use gnulib module pipe2 for portability"), \
556 #if @GNULIB_READLINK@
557 /* Read the contents of the symbolic link FILE and place the first BUFSIZE
558 bytes of it into BUF. Return the number of bytes placed into BUF if
559 successful, otherwise -1 and errno set.
560 See the POSIX:2001 specification
561 <http://www.opengroup.org/susv3xsh/readlink.html>. */
562 # if !@HAVE_READLINK@
563 extern int readlink (const char *file, char *buf, size_t bufsize);
565 #elif defined GNULIB_POSIXCHECK
567 # define readlink(f,b,s) \
568 (GL_LINK_WARNING ("readlink is unportable - " \
569 "use gnulib module readlink for portability"), \
575 /* Pause the execution of the current thread for N seconds.
576 Returns the number of seconds left to sleep.
577 See the POSIX:2001 specification
578 <http://www.opengroup.org/susv3xsh/sleep.html>. */
580 extern unsigned int sleep (unsigned int n);
582 #elif defined GNULIB_POSIXCHECK
585 (GL_LINK_WARNING ("sleep is unportable - " \
586 "use gnulib module sleep for portability"), \
591 #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
592 /* Write up to COUNT bytes starting at BUF to file descriptor FD.
593 See the POSIX:2001 specification
594 <http://www.opengroup.org/susv3xsh/write.html>. */
596 # define write rpl_write
597 extern ssize_t write (int fd, const void *buf, size_t count);
601 #ifdef FCHDIR_REPLACEMENT
602 /* gnulib internal function. */
603 extern void _gl_unregister_fd (int fd);
612 #endif /* _GL_UNISTD_H */
613 #endif /* _GL_UNISTD_H */