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"), \
134 # if @UNISTD_H_HAVE_WINSOCK2_H@
135 /* Need a gnulib internal function. */
136 # define HAVE__GL_CLOSE_FD_MAYBE_SOCKET 1
139 /* Automatically included by modules that need a replacement for close. */
141 # define close rpl_close
142 extern int close (int);
144 #elif @UNISTD_H_HAVE_WINSOCK2_H@
146 # define close close_used_without_requesting_gnulib_module_close
147 #elif defined GNULIB_POSIXCHECK
150 (GL_LINK_WARNING ("close does not portably work on sockets - " \
151 "use gnulib module close for portability"), \
158 /* Copy the file descriptor OLDFD into file descriptor NEWFD. Do nothing if
159 NEWFD = OLDFD, otherwise close NEWFD first if it is open.
160 Return 0 if successful, otherwise -1 and errno set.
161 See the POSIX:2001 specification
162 <http://www.opengroup.org/susv3xsh/dup2.html>. */
163 extern int dup2 (int oldfd, int newfd);
165 #elif defined GNULIB_POSIXCHECK
168 (GL_LINK_WARNING ("dup2 is unportable - " \
169 "use gnulib module dup2 for portability"), \
175 # if !@HAVE_DECL_ENVIRON@
176 /* Set of environment variables and values. An array of strings of the form
177 "VARIABLE=VALUE", terminated with a NULL. */
178 # if defined __APPLE__ && defined __MACH__
179 # include <crt_externs.h>
180 # define environ (*_NSGetEnviron ())
182 extern char **environ;
185 #elif defined GNULIB_POSIXCHECK
188 (GL_LINK_WARNING ("environ is unportable - " \
189 "use gnulib module environ for portability"), \
194 #if @GNULIB_EUIDACCESS@
195 # if !@HAVE_EUIDACCESS@
196 /* Like access(), except that is uses the effective user id and group id of
197 the current process. */
198 extern int euidaccess (const char *filename, int mode);
200 #elif defined GNULIB_POSIXCHECK
202 # define euidaccess(f,m) \
203 (GL_LINK_WARNING ("euidaccess is unportable - " \
204 "use gnulib module euidaccess for portability"), \
210 # if @REPLACE_FCHDIR@
212 /* Change the process' current working directory to the directory on which
213 the given file descriptor is open.
214 Return 0 if successful, otherwise -1 and errno set.
215 See the POSIX:2001 specification
216 <http://www.opengroup.org/susv3xsh/fchdir.html>. */
217 extern int fchdir (int /*fd*/);
220 extern int dup (int);
221 # define dup2 rpl_dup2
222 extern int dup2 (int, int);
225 #elif defined GNULIB_POSIXCHECK
228 (GL_LINK_WARNING ("fchdir is unportable - " \
229 "use gnulib module fchdir for portability"), \
235 /* Synchronize changes to a file.
236 Return 0 if successful, otherwise -1 and errno set.
237 See POSIX:2001 specification
238 <http://www.opengroup.org/susv3xsh/fsync.html>. */
240 extern int fsync (int fd);
242 #elif defined GNULIB_POSIXCHECK
245 (GL_LINK_WARNING ("fsync is unportable - " \
246 "use gnulib module fsync for portability"), \
251 #if @GNULIB_FTRUNCATE@
252 # if !@HAVE_FTRUNCATE@
253 /* Change the size of the file to which FD is opened to become equal to LENGTH.
254 Return 0 if successful, otherwise -1 and errno set.
255 See the POSIX:2001 specification
256 <http://www.opengroup.org/susv3xsh/ftruncate.html>. */
257 extern int ftruncate (int fd, off_t length);
259 #elif defined GNULIB_POSIXCHECK
261 # define ftruncate(f,l) \
262 (GL_LINK_WARNING ("ftruncate is unportable - " \
263 "use gnulib module ftruncate for portability"), \
269 /* Include the headers that might declare getcwd so that they will not
270 cause confusion if included after this file. */
272 # if @REPLACE_GETCWD@
273 /* Get the name of the current working directory, and put it in SIZE bytes
275 Return BUF if successful, or NULL if the directory couldn't be determined
276 or SIZE was too small.
277 See the POSIX:2001 specification
278 <http://www.opengroup.org/susv3xsh/getcwd.html>.
279 Additionally, the gnulib module 'getcwd' guarantees the following GNU
280 extension: If BUF is NULL, an array is allocated with 'malloc'; the array
281 is SIZE bytes long, unless SIZE == 0, in which case it is as big as
283 # define getcwd rpl_getcwd
284 extern char * getcwd (char *buf, size_t size);
286 #elif defined GNULIB_POSIXCHECK
288 # define getcwd(b,s) \
289 (GL_LINK_WARNING ("getcwd is unportable - " \
290 "use gnulib module getcwd for portability"), \
295 #if @GNULIB_GETDOMAINNAME@
296 /* Return the NIS domain name of the machine.
297 WARNING! The NIS domain name is unrelated to the fully qualified host name
298 of the machine. It is also unrelated to email addresses.
299 WARNING! The NIS domain name is usually the empty string or "(none)" when
302 Put up to LEN bytes of the NIS domain name into NAME.
303 Null terminate it if the name is shorter than LEN.
304 If the NIS domain name is longer than LEN, set errno = EINVAL and return -1.
305 Return 0 if successful, otherwise set errno and return -1. */
306 # if !@HAVE_GETDOMAINNAME@
307 extern int getdomainname(char *name, size_t len);
309 #elif defined GNULIB_POSIXCHECK
310 # undef getdomainname
311 # define getdomainname(n,l) \
312 (GL_LINK_WARNING ("getdomainname is unportable - " \
313 "use gnulib module getdomainname for portability"), \
314 getdomainname (n, l))
318 #if @GNULIB_GETDTABLESIZE@
319 # if !@HAVE_GETDTABLESIZE@
320 /* Return the maximum number of file descriptors in the current process. */
321 extern int getdtablesize (void);
323 #elif defined GNULIB_POSIXCHECK
324 # undef getdtablesize
325 # define getdtablesize() \
326 (GL_LINK_WARNING ("getdtablesize is unportable - " \
327 "use gnulib module getdtablesize for portability"), \
332 #if @GNULIB_GETHOSTNAME@
333 /* Return the standard host name of the machine.
334 WARNING! The host name may or may not be fully qualified.
336 Put up to LEN bytes of the host name into NAME.
337 Null terminate it if the name is shorter than LEN.
338 If the host name is longer than LEN, set errno = EINVAL and return -1.
339 Return 0 if successful, otherwise set errno and return -1. */
340 # if @UNISTD_H_HAVE_WINSOCK2_H@
342 # define gethostname rpl_gethostname
344 # if @UNISTD_H_HAVE_WINSOCK2_H@ || !@HAVE_GETHOSTNAME@
345 extern int gethostname(char *name, size_t len);
347 #elif @UNISTD_H_HAVE_WINSOCK2_H@
349 # define gethostname gethostname_used_without_requesting_gnulib_module_gethostname
350 #elif defined GNULIB_POSIXCHECK
352 # define gethostname(n,l) \
353 (GL_LINK_WARNING ("gethostname is unportable - " \
354 "use gnulib module gethostname for portability"), \
359 #if @GNULIB_GETLOGIN_R@
360 /* Copies the user's login name to NAME.
361 The array pointed to by NAME has room for SIZE bytes.
363 Returns 0 if successful. Upon error, an error number is returned, or -1 in
364 the case that the login name cannot be found but no specific error is
365 provided (this case is hopefully rare but is left open by the POSIX spec).
367 See <http://www.opengroup.org/susv3xsh/getlogin.html>.
369 # if !@HAVE_DECL_GETLOGIN_R@
371 extern int getlogin_r (char *name, size_t size);
373 #elif defined GNULIB_POSIXCHECK
375 # define getlogin_r(n,s) \
376 (GL_LINK_WARNING ("getlogin_r is unportable - " \
377 "use gnulib module getlogin_r for portability"), \
382 #if @GNULIB_GETPAGESIZE@
383 # if @REPLACE_GETPAGESIZE@
384 # define getpagesize rpl_getpagesize
385 extern int getpagesize (void);
386 # elif !@HAVE_GETPAGESIZE@
387 /* This is for POSIX systems. */
388 # if !defined getpagesize && defined _SC_PAGESIZE
389 # if ! (defined __VMS && __VMS_VER < 70000000)
390 # define getpagesize() sysconf (_SC_PAGESIZE)
393 /* This is for older VMS. */
394 # if !defined getpagesize && defined __VMS
396 # define getpagesize() 8192
398 # define getpagesize() 512
401 /* This is for BeOS. */
402 # if !defined getpagesize && @HAVE_OS_H@
404 # if defined B_PAGE_SIZE
405 # define getpagesize() B_PAGE_SIZE
408 /* This is for AmigaOS4.0. */
409 # if !defined getpagesize && defined __amigaos4__
410 # define getpagesize() 2048
412 /* This is for older Unix systems. */
413 # if !defined getpagesize && @HAVE_SYS_PARAM_H@
414 # include <sys/param.h>
415 # ifdef EXEC_PAGESIZE
416 # define getpagesize() EXEC_PAGESIZE
422 # define getpagesize() (NBPG * CLSIZE)
425 # define getpagesize() NBPC
431 #elif defined GNULIB_POSIXCHECK
433 # define getpagesize() \
434 (GL_LINK_WARNING ("getpagesize is unportable - " \
435 "use gnulib module getpagesize for portability"), \
440 #if @GNULIB_GETUSERSHELL@
441 # if !@HAVE_GETUSERSHELL@
442 /* Return the next valid login shell on the system, or NULL when the end of
443 the list has been reached. */
444 extern char *getusershell (void);
445 /* Rewind to pointer that is advanced at each getusershell() call. */
446 extern void setusershell (void);
447 /* Free the pointer that is advanced at each getusershell() call and
448 associated resources. */
449 extern void endusershell (void);
451 #elif defined GNULIB_POSIXCHECK
453 # define getusershell() \
454 (GL_LINK_WARNING ("getusershell is unportable - " \
455 "use gnulib module getusershell for portability"), \
458 # define setusershell() \
459 (GL_LINK_WARNING ("setusershell is unportable - " \
460 "use gnulib module getusershell for portability"), \
463 # define endusershell() \
464 (GL_LINK_WARNING ("endusershell is unportable - " \
465 "use gnulib module getusershell for portability"), \
471 # if @REPLACE_LCHOWN@
472 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
473 to GID (if GID is not -1). Do not follow symbolic links.
474 Return 0 if successful, otherwise -1 and errno set.
475 See the POSIX:2001 specification
476 <http://www.opengroup.org/susv3xsh/lchown.html>. */
477 # define lchown rpl_lchown
478 extern int lchown (char const *file, uid_t owner, gid_t group);
480 #elif defined GNULIB_POSIXCHECK
482 # define lchown(f,u,g) \
483 (GL_LINK_WARNING ("lchown is unportable to pre-POSIX.1-2001 " \
484 "systems - use gnulib module lchown for portability"), \
490 /* Create a new hard link for an existing file.
491 Return 0 if successful, otherwise -1 and errno set.
492 See POSIX:2001 specification
493 <http://www.opengroup.org/susv3xsh/link.html>. */
495 extern int link (const char *path1, const char *path2);
497 #elif defined GNULIB_POSIXCHECK
499 # define link(path1,path2) \
500 (GL_LINK_WARNING ("link is unportable - " \
501 "use gnulib module link for portability"), \
508 /* Set the offset of FD relative to SEEK_SET, SEEK_CUR, or SEEK_END.
509 Return the new offset if successful, otherwise -1 and errno set.
510 See the POSIX:2001 specification
511 <http://www.opengroup.org/susv3xsh/lseek.html>. */
512 # define lseek rpl_lseek
513 extern off_t lseek (int fd, off_t offset, int whence);
515 #elif defined GNULIB_POSIXCHECK
517 # define lseek(f,o,w) \
518 (GL_LINK_WARNING ("lseek does not fail with ESPIPE on pipes on some " \
519 "systems - use gnulib module lseek for portability"), \
524 #if @GNULIB_READLINK@
525 /* Read the contents of the symbolic link FILE and place the first BUFSIZE
526 bytes of it into BUF. Return the number of bytes placed into BUF if
527 successful, otherwise -1 and errno set.
528 See the POSIX:2001 specification
529 <http://www.opengroup.org/susv3xsh/readlink.html>. */
530 # if !@HAVE_READLINK@
532 extern int readlink (const char *file, char *buf, size_t bufsize);
534 #elif defined GNULIB_POSIXCHECK
536 # define readlink(f,b,s) \
537 (GL_LINK_WARNING ("readlink is unportable - " \
538 "use gnulib module readlink for portability"), \
544 /* Pause the execution of the current thread for N seconds.
545 Returns the number of seconds left to sleep.
546 See the POSIX:2001 specification
547 <http://www.opengroup.org/susv3xsh/sleep.html>. */
549 extern unsigned int sleep (unsigned int n);
551 #elif defined GNULIB_POSIXCHECK
554 (GL_LINK_WARNING ("sleep is unportable - " \
555 "use gnulib module sleep for portability"), \
560 #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
561 /* Write up to COUNT bytes starting at BUF to file descriptor FD.
562 See the POSIX:2001 specification
563 <http://www.opengroup.org/susv3xsh/write.html>. */
565 # define write rpl_write
566 extern ssize_t write (int fd, const void *buf, size_t count);
570 #ifdef FCHDIR_REPLACEMENT
571 /* gnulib internal function. */
572 extern void _gl_unregister_fd (int fd);
581 #endif /* _GL_UNISTD_H */
582 #endif /* _GL_UNISTD_H */