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 /* mingw doesn't define the SEEK_* or *_FILENO macros in <unistd.h>. */
33 #if !(defined SEEK_CUR && defined SEEK_END && defined SEEK_SET)
37 /* mingw fails to declare _exit in <unistd.h>. */
38 /* mingw, BeOS, Haiku declare environ in <stdlib.h>, not in <unistd.h>. */
41 #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
43 # include <sys/types.h>
46 /* Get getopt(), optarg, optind, opterr, optopt. */
47 #if @GNULIB_UNISTD_H_GETOPT@
51 #if @GNULIB_GETHOSTNAME@
52 /* Get all possible declarations of gethostname(). */
53 # if @UNISTD_H_HAVE_WINSOCK2_H@
54 # include <winsock2.h>
55 # if !defined _GL_SYS_SOCKET_H
57 # define socket socket_used_without_including_sys_socket_h
59 # define connect connect_used_without_including_sys_socket_h
61 # define accept accept_used_without_including_sys_socket_h
63 # define bind bind_used_without_including_sys_socket_h
65 # define getpeername getpeername_used_without_including_sys_socket_h
67 # define getsockname getsockname_used_without_including_sys_socket_h
69 # define getsockopt getsockopt_used_without_including_sys_socket_h
71 # define listen listen_used_without_including_sys_socket_h
73 # define recv recv_used_without_including_sys_socket_h
75 # define send send_used_without_including_sys_socket_h
77 # define recvfrom recvfrom_used_without_including_sys_socket_h
79 # define sendto sendto_used_without_including_sys_socket_h
81 # define setsockopt setsockopt_used_without_including_sys_socket_h
83 # define shutdown shutdown_used_without_including_sys_socket_h
85 # if !defined _GL_SYS_SELECT_H
87 # define select select_used_without_including_sys_select_h
92 /* The definition of GL_LINK_WARNING is copied here. */
95 /* OS/2 EMX lacks these macros. */
97 # define STDIN_FILENO 0
100 # define STDOUT_FILENO 1
102 #ifndef STDERR_FILENO
103 # define STDERR_FILENO 2
106 /* Declare overridden functions. */
115 # ifndef REPLACE_CHOWN
116 # define REPLACE_CHOWN 1
119 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
120 to GID (if GID is not -1). Follow symbolic links.
121 Return 0 if successful, otherwise -1 and errno set.
122 See the POSIX:2001 specification
123 <http://www.opengroup.org/susv3xsh/chown.html>. */
124 # define chown rpl_chown
125 extern int chown (const char *file, uid_t uid, gid_t gid);
128 #elif defined GNULIB_POSIXCHECK
130 # define chown(f,u,g) \
131 (GL_LINK_WARNING ("chown fails to follow symlinks on some systems and " \
132 "doesn't treat a uid or gid of -1 on some systems - " \
133 "use gnulib module chown for portability"), \
140 /* Automatically included by modules that need a replacement for close. */
142 # define close rpl_close
143 extern int close (int);
145 #elif @UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS@
147 # define close close_used_without_requesting_gnulib_module_close
148 #elif defined GNULIB_POSIXCHECK
151 (GL_LINK_WARNING ("close does not portably work on sockets - " \
152 "use gnulib module close for portability"), \
159 # define dup2 rpl_dup2
161 # if !@HAVE_DUP2@ || @REPLACE_DUP2@
162 /* Copy the file descriptor OLDFD into file descriptor NEWFD. Do nothing if
163 NEWFD = OLDFD, otherwise close NEWFD first if it is open.
164 Return newfd if successful, otherwise -1 and errno set.
165 See the POSIX:2001 specification
166 <http://www.opengroup.org/susv3xsh/dup2.html>. */
167 extern int dup2 (int oldfd, int newfd);
169 #elif defined GNULIB_POSIXCHECK
172 (GL_LINK_WARNING ("dup2 is unportable - " \
173 "use gnulib module dup2 for portability"), \
179 # if !@HAVE_DECL_ENVIRON@
180 /* Set of environment variables and values. An array of strings of the form
181 "VARIABLE=VALUE", terminated with a NULL. */
182 # if defined __APPLE__ && defined __MACH__
183 # include <crt_externs.h>
184 # define environ (*_NSGetEnviron ())
186 extern char **environ;
189 #elif defined GNULIB_POSIXCHECK
192 (GL_LINK_WARNING ("environ is unportable - " \
193 "use gnulib module environ for portability"), \
198 #if @GNULIB_EUIDACCESS@
199 # if !@HAVE_EUIDACCESS@
200 /* Like access(), except that is uses the effective user id and group id of
201 the current process. */
202 extern int euidaccess (const char *filename, int mode);
204 #elif defined GNULIB_POSIXCHECK
206 # define euidaccess(f,m) \
207 (GL_LINK_WARNING ("euidaccess is unportable - " \
208 "use gnulib module euidaccess for portability"), \
214 # if @REPLACE_FCHDIR@
216 /* Change the process' current working directory to the directory on which
217 the given file descriptor is open.
218 Return 0 if successful, otherwise -1 and errno set.
219 See the POSIX:2001 specification
220 <http://www.opengroup.org/susv3xsh/fchdir.html>. */
221 extern int fchdir (int /*fd*/);
224 extern int dup (int);
229 # define dup2 rpl_dup2_fchdir
230 extern int dup2 (int, int);
233 #elif defined GNULIB_POSIXCHECK
236 (GL_LINK_WARNING ("fchdir is unportable - " \
237 "use gnulib module fchdir for portability"), \
243 /* Synchronize changes to a file.
244 Return 0 if successful, otherwise -1 and errno set.
245 See POSIX:2001 specification
246 <http://www.opengroup.org/susv3xsh/fsync.html>. */
248 extern int fsync (int fd);
250 #elif defined GNULIB_POSIXCHECK
253 (GL_LINK_WARNING ("fsync is unportable - " \
254 "use gnulib module fsync for portability"), \
259 #if @GNULIB_FTRUNCATE@
260 # if !@HAVE_FTRUNCATE@
261 /* Change the size of the file to which FD is opened to become equal to LENGTH.
262 Return 0 if successful, otherwise -1 and errno set.
263 See the POSIX:2001 specification
264 <http://www.opengroup.org/susv3xsh/ftruncate.html>. */
265 extern int ftruncate (int fd, off_t length);
267 #elif defined GNULIB_POSIXCHECK
269 # define ftruncate(f,l) \
270 (GL_LINK_WARNING ("ftruncate is unportable - " \
271 "use gnulib module ftruncate for portability"), \
277 /* Include the headers that might declare getcwd so that they will not
278 cause confusion if included after this file. */
280 # if @REPLACE_GETCWD@
281 /* Get the name of the current working directory, and put it in SIZE bytes
283 Return BUF if successful, or NULL if the directory couldn't be determined
284 or SIZE was too small.
285 See the POSIX:2001 specification
286 <http://www.opengroup.org/susv3xsh/getcwd.html>.
287 Additionally, the gnulib module 'getcwd' guarantees the following GNU
288 extension: If BUF is NULL, an array is allocated with 'malloc'; the array
289 is SIZE bytes long, unless SIZE == 0, in which case it is as big as
291 # define getcwd rpl_getcwd
292 extern char * getcwd (char *buf, size_t size);
294 #elif defined GNULIB_POSIXCHECK
296 # define getcwd(b,s) \
297 (GL_LINK_WARNING ("getcwd is unportable - " \
298 "use gnulib module getcwd for portability"), \
303 #if @GNULIB_GETDOMAINNAME@
304 /* Return the NIS domain name of the machine.
305 WARNING! The NIS domain name is unrelated to the fully qualified host name
306 of the machine. It is also unrelated to email addresses.
307 WARNING! The NIS domain name is usually the empty string or "(none)" when
310 Put up to LEN bytes of the NIS domain name into NAME.
311 Null terminate it if the name is shorter than LEN.
312 If the NIS domain name is longer than LEN, set errno = EINVAL and return -1.
313 Return 0 if successful, otherwise set errno and return -1. */
314 # if !@HAVE_GETDOMAINNAME@
315 extern int getdomainname(char *name, size_t len);
317 #elif defined GNULIB_POSIXCHECK
318 # undef getdomainname
319 # define getdomainname(n,l) \
320 (GL_LINK_WARNING ("getdomainname is unportable - " \
321 "use gnulib module getdomainname for portability"), \
322 getdomainname (n, l))
326 #if @GNULIB_GETDTABLESIZE@
327 # if !@HAVE_GETDTABLESIZE@
328 /* Return the maximum number of file descriptors in the current process. */
329 extern int getdtablesize (void);
331 #elif defined GNULIB_POSIXCHECK
332 # undef getdtablesize
333 # define getdtablesize() \
334 (GL_LINK_WARNING ("getdtablesize is unportable - " \
335 "use gnulib module getdtablesize for portability"), \
340 #if @GNULIB_GETHOSTNAME@
341 /* Return the standard host name of the machine.
342 WARNING! The host name may or may not be fully qualified.
344 Put up to LEN bytes of the host name into NAME.
345 Null terminate it if the name is shorter than LEN.
346 If the host name is longer than LEN, set errno = EINVAL and return -1.
347 Return 0 if successful, otherwise set errno and return -1. */
348 # if @UNISTD_H_HAVE_WINSOCK2_H@
350 # define gethostname rpl_gethostname
352 # if @UNISTD_H_HAVE_WINSOCK2_H@ || !@HAVE_GETHOSTNAME@
353 extern int gethostname(char *name, size_t len);
355 #elif @UNISTD_H_HAVE_WINSOCK2_H@
357 # define gethostname gethostname_used_without_requesting_gnulib_module_gethostname
358 #elif defined GNULIB_POSIXCHECK
360 # define gethostname(n,l) \
361 (GL_LINK_WARNING ("gethostname is unportable - " \
362 "use gnulib module gethostname for portability"), \
367 #if @GNULIB_GETLOGIN_R@
368 /* Copies the user's login name to NAME.
369 The array pointed to by NAME has room for SIZE bytes.
371 Returns 0 if successful. Upon error, an error number is returned, or -1 in
372 the case that the login name cannot be found but no specific error is
373 provided (this case is hopefully rare but is left open by the POSIX spec).
375 See <http://www.opengroup.org/susv3xsh/getlogin.html>.
377 # if !@HAVE_DECL_GETLOGIN_R@
379 extern int getlogin_r (char *name, size_t size);
381 #elif defined GNULIB_POSIXCHECK
383 # define getlogin_r(n,s) \
384 (GL_LINK_WARNING ("getlogin_r is unportable - " \
385 "use gnulib module getlogin_r for portability"), \
390 #if @GNULIB_GETPAGESIZE@
391 # if @REPLACE_GETPAGESIZE@
392 # define getpagesize rpl_getpagesize
393 extern int getpagesize (void);
394 # elif !@HAVE_GETPAGESIZE@
395 /* This is for POSIX systems. */
396 # if !defined getpagesize && defined _SC_PAGESIZE
397 # if ! (defined __VMS && __VMS_VER < 70000000)
398 # define getpagesize() sysconf (_SC_PAGESIZE)
401 /* This is for older VMS. */
402 # if !defined getpagesize && defined __VMS
404 # define getpagesize() 8192
406 # define getpagesize() 512
409 /* This is for BeOS. */
410 # if !defined getpagesize && @HAVE_OS_H@
412 # if defined B_PAGE_SIZE
413 # define getpagesize() B_PAGE_SIZE
416 /* This is for AmigaOS4.0. */
417 # if !defined getpagesize && defined __amigaos4__
418 # define getpagesize() 2048
420 /* This is for older Unix systems. */
421 # if !defined getpagesize && @HAVE_SYS_PARAM_H@
422 # include <sys/param.h>
423 # ifdef EXEC_PAGESIZE
424 # define getpagesize() EXEC_PAGESIZE
430 # define getpagesize() (NBPG * CLSIZE)
433 # define getpagesize() NBPC
439 #elif defined GNULIB_POSIXCHECK
441 # define getpagesize() \
442 (GL_LINK_WARNING ("getpagesize is unportable - " \
443 "use gnulib module getpagesize for portability"), \
448 #if @GNULIB_GETUSERSHELL@
449 # if !@HAVE_GETUSERSHELL@
450 /* Return the next valid login shell on the system, or NULL when the end of
451 the list has been reached. */
452 extern char *getusershell (void);
453 /* Rewind to pointer that is advanced at each getusershell() call. */
454 extern void setusershell (void);
455 /* Free the pointer that is advanced at each getusershell() call and
456 associated resources. */
457 extern void endusershell (void);
459 #elif defined GNULIB_POSIXCHECK
461 # define getusershell() \
462 (GL_LINK_WARNING ("getusershell is unportable - " \
463 "use gnulib module getusershell for portability"), \
466 # define setusershell() \
467 (GL_LINK_WARNING ("setusershell is unportable - " \
468 "use gnulib module getusershell for portability"), \
471 # define endusershell() \
472 (GL_LINK_WARNING ("endusershell is unportable - " \
473 "use gnulib module getusershell for portability"), \
479 # if @REPLACE_LCHOWN@
480 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
481 to GID (if GID is not -1). Do not follow symbolic links.
482 Return 0 if successful, otherwise -1 and errno set.
483 See the POSIX:2001 specification
484 <http://www.opengroup.org/susv3xsh/lchown.html>. */
485 # define lchown rpl_lchown
486 extern int lchown (char const *file, uid_t owner, gid_t group);
488 #elif defined GNULIB_POSIXCHECK
490 # define lchown(f,u,g) \
491 (GL_LINK_WARNING ("lchown is unportable to pre-POSIX.1-2001 " \
492 "systems - use gnulib module lchown for portability"), \
498 /* Create a new hard link for an existing file.
499 Return 0 if successful, otherwise -1 and errno set.
500 See POSIX:2001 specification
501 <http://www.opengroup.org/susv3xsh/link.html>. */
503 extern int link (const char *path1, const char *path2);
505 #elif defined GNULIB_POSIXCHECK
507 # define link(path1,path2) \
508 (GL_LINK_WARNING ("link is unportable - " \
509 "use gnulib module link for portability"), \
516 /* Set the offset of FD relative to SEEK_SET, SEEK_CUR, or SEEK_END.
517 Return the new offset if successful, otherwise -1 and errno set.
518 See the POSIX:2001 specification
519 <http://www.opengroup.org/susv3xsh/lseek.html>. */
520 # define lseek rpl_lseek
521 extern off_t lseek (int fd, off_t offset, int whence);
523 #elif defined GNULIB_POSIXCHECK
525 # define lseek(f,o,w) \
526 (GL_LINK_WARNING ("lseek does not fail with ESPIPE on pipes on some " \
527 "systems - use gnulib module lseek for portability"), \
532 #if @GNULIB_READLINK@
533 /* Read the contents of the symbolic link FILE and place the first BUFSIZE
534 bytes of it into BUF. Return the number of bytes placed into BUF if
535 successful, otherwise -1 and errno set.
536 See the POSIX:2001 specification
537 <http://www.opengroup.org/susv3xsh/readlink.html>. */
538 # if !@HAVE_READLINK@
540 extern int readlink (const char *file, char *buf, size_t bufsize);
542 #elif defined GNULIB_POSIXCHECK
544 # define readlink(f,b,s) \
545 (GL_LINK_WARNING ("readlink is unportable - " \
546 "use gnulib module readlink for portability"), \
552 /* Pause the execution of the current thread for N seconds.
553 Returns the number of seconds left to sleep.
554 See the POSIX:2001 specification
555 <http://www.opengroup.org/susv3xsh/sleep.html>. */
557 extern unsigned int sleep (unsigned int n);
559 #elif defined GNULIB_POSIXCHECK
562 (GL_LINK_WARNING ("sleep is unportable - " \
563 "use gnulib module sleep for portability"), \
568 #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
569 /* Write up to COUNT bytes starting at BUF to file descriptor FD.
570 See the POSIX:2001 specification
571 <http://www.opengroup.org/susv3xsh/write.html>. */
573 # define write rpl_write
574 extern ssize_t write (int fd, const void *buf, size_t count);
578 #ifdef FCHDIR_REPLACEMENT
579 /* gnulib internal function. */
580 extern void _gl_unregister_fd (int fd);
589 #endif /* _GL_UNISTD_H */
590 #endif /* _GL_UNISTD_H */