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 #if @GNULIB_GETHOSTNAME@
47 /* Get all possible declarations of gethostname(). */
48 # if @UNISTD_H_HAVE_WINSOCK2_H@
49 # include <winsock2.h>
50 # if !defined _GL_SYS_SOCKET_H
52 # define socket socket_used_without_including_sys_socket_h
54 # define connect connect_used_without_including_sys_socket_h
56 # define accept accept_used_without_including_sys_socket_h
58 # define bind bind_used_without_including_sys_socket_h
60 # define getpeername getpeername_used_without_including_sys_socket_h
62 # define getsockname getsockname_used_without_including_sys_socket_h
64 # define getsockopt getsockopt_used_without_including_sys_socket_h
66 # define listen listen_used_without_including_sys_socket_h
68 # define recv recv_used_without_including_sys_socket_h
70 # define send send_used_without_including_sys_socket_h
72 # define recvfrom recvfrom_used_without_including_sys_socket_h
74 # define sendto sendto_used_without_including_sys_socket_h
76 # define setsockopt setsockopt_used_without_including_sys_socket_h
78 # define shutdown shutdown_used_without_including_sys_socket_h
80 # if !defined _GL_SYS_SELECT_H
82 # define select select_used_without_including_sys_select_h
87 /* The definition of GL_LINK_WARNING is copied here. */
90 /* OS/2 EMX lacks these macros. */
92 # define STDIN_FILENO 0
95 # define STDOUT_FILENO 1
98 # define STDERR_FILENO 2
101 /* Declare overridden functions. */
110 # ifndef REPLACE_CHOWN
111 # define REPLACE_CHOWN 1
114 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
115 to GID (if GID is not -1). Follow symbolic links.
116 Return 0 if successful, otherwise -1 and errno set.
117 See the POSIX:2001 specification
118 <http://www.opengroup.org/susv3xsh/chown.html>. */
119 # define chown rpl_chown
120 extern int chown (const char *file, uid_t uid, gid_t gid);
123 #elif defined GNULIB_POSIXCHECK
125 # define chown(f,u,g) \
126 (GL_LINK_WARNING ("chown fails to follow symlinks on some systems and " \
127 "doesn't treat a uid or gid of -1 on some systems - " \
128 "use gnulib module chown for portability"), \
135 /* Automatically included by modules that need a replacement for close. */
137 # define close rpl_close
138 extern int close (int);
140 #elif @UNISTD_H_HAVE_WINSOCK2_H_AND_USE_SOCKETS@
142 # define close close_used_without_requesting_gnulib_module_close
143 #elif defined GNULIB_POSIXCHECK
146 (GL_LINK_WARNING ("close does not portably work on sockets - " \
147 "use gnulib module close for portability"), \
154 # define dup2 rpl_dup2
156 # if !@HAVE_DUP2@ || @REPLACE_DUP2@
157 /* Copy the file descriptor OLDFD into file descriptor NEWFD. Do nothing if
158 NEWFD = OLDFD, otherwise close NEWFD first if it is open.
159 Return newfd if successful, otherwise -1 and errno set.
160 See the POSIX:2001 specification
161 <http://www.opengroup.org/susv3xsh/dup2.html>. */
162 extern int dup2 (int oldfd, int newfd);
164 #elif defined GNULIB_POSIXCHECK
167 (GL_LINK_WARNING ("dup2 is unportable - " \
168 "use gnulib module dup2 for portability"), \
174 # if !@HAVE_DECL_ENVIRON@
175 /* Set of environment variables and values. An array of strings of the form
176 "VARIABLE=VALUE", terminated with a NULL. */
177 # if defined __APPLE__ && defined __MACH__
178 # include <crt_externs.h>
179 # define environ (*_NSGetEnviron ())
181 extern char **environ;
184 #elif defined GNULIB_POSIXCHECK
187 (GL_LINK_WARNING ("environ is unportable - " \
188 "use gnulib module environ for portability"), \
193 #if @GNULIB_EUIDACCESS@
194 # if !@HAVE_EUIDACCESS@
195 /* Like access(), except that is uses the effective user id and group id of
196 the current process. */
197 extern int euidaccess (const char *filename, int mode);
199 #elif defined GNULIB_POSIXCHECK
201 # define euidaccess(f,m) \
202 (GL_LINK_WARNING ("euidaccess is unportable - " \
203 "use gnulib module euidaccess for portability"), \
209 # if @REPLACE_FCHDIR@
211 /* Change the process' current working directory to the directory on which
212 the given file descriptor is open.
213 Return 0 if successful, otherwise -1 and errno set.
214 See the POSIX:2001 specification
215 <http://www.opengroup.org/susv3xsh/fchdir.html>. */
216 extern int fchdir (int /*fd*/);
219 extern int dup (int);
224 # define dup2 rpl_dup2_fchdir
225 extern int dup2 (int, int);
228 #elif defined GNULIB_POSIXCHECK
231 (GL_LINK_WARNING ("fchdir is unportable - " \
232 "use gnulib module fchdir for portability"), \
238 /* Synchronize changes to a file.
239 Return 0 if successful, otherwise -1 and errno set.
240 See POSIX:2001 specification
241 <http://www.opengroup.org/susv3xsh/fsync.html>. */
243 extern int fsync (int fd);
245 #elif defined GNULIB_POSIXCHECK
248 (GL_LINK_WARNING ("fsync is unportable - " \
249 "use gnulib module fsync for portability"), \
254 #if @GNULIB_FTRUNCATE@
255 # if !@HAVE_FTRUNCATE@
256 /* Change the size of the file to which FD is opened to become equal to LENGTH.
257 Return 0 if successful, otherwise -1 and errno set.
258 See the POSIX:2001 specification
259 <http://www.opengroup.org/susv3xsh/ftruncate.html>. */
260 extern int ftruncate (int fd, off_t length);
262 #elif defined GNULIB_POSIXCHECK
264 # define ftruncate(f,l) \
265 (GL_LINK_WARNING ("ftruncate is unportable - " \
266 "use gnulib module ftruncate for portability"), \
272 /* Include the headers that might declare getcwd so that they will not
273 cause confusion if included after this file. */
275 # if @REPLACE_GETCWD@
276 /* Get the name of the current working directory, and put it in SIZE bytes
278 Return BUF if successful, or NULL if the directory couldn't be determined
279 or SIZE was too small.
280 See the POSIX:2001 specification
281 <http://www.opengroup.org/susv3xsh/getcwd.html>.
282 Additionally, the gnulib module 'getcwd' guarantees the following GNU
283 extension: If BUF is NULL, an array is allocated with 'malloc'; the array
284 is SIZE bytes long, unless SIZE == 0, in which case it is as big as
286 # define getcwd rpl_getcwd
287 extern char * getcwd (char *buf, size_t size);
289 #elif defined GNULIB_POSIXCHECK
291 # define getcwd(b,s) \
292 (GL_LINK_WARNING ("getcwd is unportable - " \
293 "use gnulib module getcwd for portability"), \
298 #if @GNULIB_GETDOMAINNAME@
299 /* Return the NIS domain name of the machine.
300 WARNING! The NIS domain name is unrelated to the fully qualified host name
301 of the machine. It is also unrelated to email addresses.
302 WARNING! The NIS domain name is usually the empty string or "(none)" when
305 Put up to LEN bytes of the NIS domain name into NAME.
306 Null terminate it if the name is shorter than LEN.
307 If the NIS domain name is longer than LEN, set errno = EINVAL and return -1.
308 Return 0 if successful, otherwise set errno and return -1. */
309 # if !@HAVE_GETDOMAINNAME@
310 extern int getdomainname(char *name, size_t len);
312 #elif defined GNULIB_POSIXCHECK
313 # undef getdomainname
314 # define getdomainname(n,l) \
315 (GL_LINK_WARNING ("getdomainname is unportable - " \
316 "use gnulib module getdomainname for portability"), \
317 getdomainname (n, l))
321 #if @GNULIB_GETDTABLESIZE@
322 # if !@HAVE_GETDTABLESIZE@
323 /* Return the maximum number of file descriptors in the current process. */
324 extern int getdtablesize (void);
326 #elif defined GNULIB_POSIXCHECK
327 # undef getdtablesize
328 # define getdtablesize() \
329 (GL_LINK_WARNING ("getdtablesize is unportable - " \
330 "use gnulib module getdtablesize for portability"), \
335 #if @GNULIB_GETHOSTNAME@
336 /* Return the standard host name of the machine.
337 WARNING! The host name may or may not be fully qualified.
339 Put up to LEN bytes of the host name into NAME.
340 Null terminate it if the name is shorter than LEN.
341 If the host name is longer than LEN, set errno = EINVAL and return -1.
342 Return 0 if successful, otherwise set errno and return -1. */
343 # if @UNISTD_H_HAVE_WINSOCK2_H@
345 # define gethostname rpl_gethostname
347 # if @UNISTD_H_HAVE_WINSOCK2_H@ || !@HAVE_GETHOSTNAME@
348 extern int gethostname(char *name, size_t len);
350 #elif @UNISTD_H_HAVE_WINSOCK2_H@
352 # define gethostname gethostname_used_without_requesting_gnulib_module_gethostname
353 #elif defined GNULIB_POSIXCHECK
355 # define gethostname(n,l) \
356 (GL_LINK_WARNING ("gethostname is unportable - " \
357 "use gnulib module gethostname for portability"), \
362 #if @GNULIB_GETLOGIN_R@
363 /* Copies the user's login name to NAME.
364 The array pointed to by NAME has room for SIZE bytes.
366 Returns 0 if successful. Upon error, an error number is returned, or -1 in
367 the case that the login name cannot be found but no specific error is
368 provided (this case is hopefully rare but is left open by the POSIX spec).
370 See <http://www.opengroup.org/susv3xsh/getlogin.html>.
372 # if !@HAVE_DECL_GETLOGIN_R@
374 extern int getlogin_r (char *name, size_t size);
376 #elif defined GNULIB_POSIXCHECK
378 # define getlogin_r(n,s) \
379 (GL_LINK_WARNING ("getlogin_r is unportable - " \
380 "use gnulib module getlogin_r for portability"), \
385 #if @GNULIB_GETPAGESIZE@
386 # if @REPLACE_GETPAGESIZE@
387 # define getpagesize rpl_getpagesize
388 extern int getpagesize (void);
389 # elif !@HAVE_GETPAGESIZE@
390 /* This is for POSIX systems. */
391 # if !defined getpagesize && defined _SC_PAGESIZE
392 # if ! (defined __VMS && __VMS_VER < 70000000)
393 # define getpagesize() sysconf (_SC_PAGESIZE)
396 /* This is for older VMS. */
397 # if !defined getpagesize && defined __VMS
399 # define getpagesize() 8192
401 # define getpagesize() 512
404 /* This is for BeOS. */
405 # if !defined getpagesize && @HAVE_OS_H@
407 # if defined B_PAGE_SIZE
408 # define getpagesize() B_PAGE_SIZE
411 /* This is for AmigaOS4.0. */
412 # if !defined getpagesize && defined __amigaos4__
413 # define getpagesize() 2048
415 /* This is for older Unix systems. */
416 # if !defined getpagesize && @HAVE_SYS_PARAM_H@
417 # include <sys/param.h>
418 # ifdef EXEC_PAGESIZE
419 # define getpagesize() EXEC_PAGESIZE
425 # define getpagesize() (NBPG * CLSIZE)
428 # define getpagesize() NBPC
434 #elif defined GNULIB_POSIXCHECK
436 # define getpagesize() \
437 (GL_LINK_WARNING ("getpagesize is unportable - " \
438 "use gnulib module getpagesize for portability"), \
443 #if @GNULIB_GETUSERSHELL@
444 # if !@HAVE_GETUSERSHELL@
445 /* Return the next valid login shell on the system, or NULL when the end of
446 the list has been reached. */
447 extern char *getusershell (void);
448 /* Rewind to pointer that is advanced at each getusershell() call. */
449 extern void setusershell (void);
450 /* Free the pointer that is advanced at each getusershell() call and
451 associated resources. */
452 extern void endusershell (void);
454 #elif defined GNULIB_POSIXCHECK
456 # define getusershell() \
457 (GL_LINK_WARNING ("getusershell is unportable - " \
458 "use gnulib module getusershell for portability"), \
461 # define setusershell() \
462 (GL_LINK_WARNING ("setusershell is unportable - " \
463 "use gnulib module getusershell for portability"), \
466 # define endusershell() \
467 (GL_LINK_WARNING ("endusershell is unportable - " \
468 "use gnulib module getusershell for portability"), \
474 # if @REPLACE_LCHOWN@
475 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
476 to GID (if GID is not -1). Do not follow symbolic links.
477 Return 0 if successful, otherwise -1 and errno set.
478 See the POSIX:2001 specification
479 <http://www.opengroup.org/susv3xsh/lchown.html>. */
480 # define lchown rpl_lchown
481 extern int lchown (char const *file, uid_t owner, gid_t group);
483 #elif defined GNULIB_POSIXCHECK
485 # define lchown(f,u,g) \
486 (GL_LINK_WARNING ("lchown is unportable to pre-POSIX.1-2001 " \
487 "systems - use gnulib module lchown for portability"), \
493 /* Create a new hard link for an existing file.
494 Return 0 if successful, otherwise -1 and errno set.
495 See POSIX:2001 specification
496 <http://www.opengroup.org/susv3xsh/link.html>. */
498 extern int link (const char *path1, const char *path2);
500 #elif defined GNULIB_POSIXCHECK
502 # define link(path1,path2) \
503 (GL_LINK_WARNING ("link is unportable - " \
504 "use gnulib module link for portability"), \
511 /* Set the offset of FD relative to SEEK_SET, SEEK_CUR, or SEEK_END.
512 Return the new offset if successful, otherwise -1 and errno set.
513 See the POSIX:2001 specification
514 <http://www.opengroup.org/susv3xsh/lseek.html>. */
515 # define lseek rpl_lseek
516 extern off_t lseek (int fd, off_t offset, int whence);
518 #elif defined GNULIB_POSIXCHECK
520 # define lseek(f,o,w) \
521 (GL_LINK_WARNING ("lseek does not fail with ESPIPE on pipes on some " \
522 "systems - use gnulib module lseek for portability"), \
527 #if @GNULIB_READLINK@
528 /* Read the contents of the symbolic link FILE and place the first BUFSIZE
529 bytes of it into BUF. Return the number of bytes placed into BUF if
530 successful, otherwise -1 and errno set.
531 See the POSIX:2001 specification
532 <http://www.opengroup.org/susv3xsh/readlink.html>. */
533 # if !@HAVE_READLINK@
535 extern int readlink (const char *file, char *buf, size_t bufsize);
537 #elif defined GNULIB_POSIXCHECK
539 # define readlink(f,b,s) \
540 (GL_LINK_WARNING ("readlink is unportable - " \
541 "use gnulib module readlink for portability"), \
547 /* Pause the execution of the current thread for N seconds.
548 Returns the number of seconds left to sleep.
549 See the POSIX:2001 specification
550 <http://www.opengroup.org/susv3xsh/sleep.html>. */
552 extern unsigned int sleep (unsigned int n);
554 #elif defined GNULIB_POSIXCHECK
557 (GL_LINK_WARNING ("sleep is unportable - " \
558 "use gnulib module sleep for portability"), \
563 #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
564 /* Write up to COUNT bytes starting at BUF to file descriptor FD.
565 See the POSIX:2001 specification
566 <http://www.opengroup.org/susv3xsh/write.html>. */
568 # define write rpl_write
569 extern ssize_t write (int fd, const void *buf, size_t count);
573 #ifdef FCHDIR_REPLACEMENT
574 /* gnulib internal function. */
575 extern void _gl_unregister_fd (int fd);
584 #endif /* _GL_UNISTD_H */
585 #endif /* _GL_UNISTD_H */