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@
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);
221 # define dup2 rpl_dup2
222 extern int dup2 (int, int);
226 #elif defined GNULIB_POSIXCHECK
229 (GL_LINK_WARNING ("fchdir is unportable - " \
230 "use gnulib module fchdir for portability"), \
236 /* Synchronize changes to a file.
237 Return 0 if successful, otherwise -1 and errno set.
238 See POSIX:2001 specification
239 <http://www.opengroup.org/susv3xsh/fsync.html>. */
241 extern int fsync (int fd);
243 #elif defined GNULIB_POSIXCHECK
246 (GL_LINK_WARNING ("fsync is unportable - " \
247 "use gnulib module fsync for portability"), \
252 #if @GNULIB_FTRUNCATE@
253 # if !@HAVE_FTRUNCATE@
254 /* Change the size of the file to which FD is opened to become equal to LENGTH.
255 Return 0 if successful, otherwise -1 and errno set.
256 See the POSIX:2001 specification
257 <http://www.opengroup.org/susv3xsh/ftruncate.html>. */
258 extern int ftruncate (int fd, off_t length);
260 #elif defined GNULIB_POSIXCHECK
262 # define ftruncate(f,l) \
263 (GL_LINK_WARNING ("ftruncate is unportable - " \
264 "use gnulib module ftruncate for portability"), \
270 /* Include the headers that might declare getcwd so that they will not
271 cause confusion if included after this file. */
273 # if @REPLACE_GETCWD@
274 /* Get the name of the current working directory, and put it in SIZE bytes
276 Return BUF if successful, or NULL if the directory couldn't be determined
277 or SIZE was too small.
278 See the POSIX:2001 specification
279 <http://www.opengroup.org/susv3xsh/getcwd.html>.
280 Additionally, the gnulib module 'getcwd' guarantees the following GNU
281 extension: If BUF is NULL, an array is allocated with 'malloc'; the array
282 is SIZE bytes long, unless SIZE == 0, in which case it is as big as
284 # define getcwd rpl_getcwd
285 extern char * getcwd (char *buf, size_t size);
287 #elif defined GNULIB_POSIXCHECK
289 # define getcwd(b,s) \
290 (GL_LINK_WARNING ("getcwd is unportable - " \
291 "use gnulib module getcwd for portability"), \
296 #if @GNULIB_GETDOMAINNAME@
297 /* Return the NIS domain name of the machine.
298 WARNING! The NIS domain name is unrelated to the fully qualified host name
299 of the machine. It is also unrelated to email addresses.
300 WARNING! The NIS domain name is usually the empty string or "(none)" when
303 Put up to LEN bytes of the NIS domain name into NAME.
304 Null terminate it if the name is shorter than LEN.
305 If the NIS domain name is longer than LEN, set errno = EINVAL and return -1.
306 Return 0 if successful, otherwise set errno and return -1. */
307 # if !@HAVE_GETDOMAINNAME@
308 extern int getdomainname(char *name, size_t len);
310 #elif defined GNULIB_POSIXCHECK
311 # undef getdomainname
312 # define getdomainname(n,l) \
313 (GL_LINK_WARNING ("getdomainname is unportable - " \
314 "use gnulib module getdomainname for portability"), \
315 getdomainname (n, l))
319 #if @GNULIB_GETDTABLESIZE@
320 # if !@HAVE_GETDTABLESIZE@
321 /* Return the maximum number of file descriptors in the current process. */
322 extern int getdtablesize (void);
324 #elif defined GNULIB_POSIXCHECK
325 # undef getdtablesize
326 # define getdtablesize() \
327 (GL_LINK_WARNING ("getdtablesize is unportable - " \
328 "use gnulib module getdtablesize for portability"), \
333 #if @GNULIB_GETHOSTNAME@
334 /* Return the standard host name of the machine.
335 WARNING! The host name may or may not be fully qualified.
337 Put up to LEN bytes of the host name into NAME.
338 Null terminate it if the name is shorter than LEN.
339 If the host name is longer than LEN, set errno = EINVAL and return -1.
340 Return 0 if successful, otherwise set errno and return -1. */
341 # if @UNISTD_H_HAVE_WINSOCK2_H@
343 # define gethostname rpl_gethostname
345 # if @UNISTD_H_HAVE_WINSOCK2_H@ || !@HAVE_GETHOSTNAME@
346 extern int gethostname(char *name, size_t len);
348 #elif @UNISTD_H_HAVE_WINSOCK2_H@
350 # define gethostname gethostname_used_without_requesting_gnulib_module_gethostname
351 #elif defined GNULIB_POSIXCHECK
353 # define gethostname(n,l) \
354 (GL_LINK_WARNING ("gethostname is unportable - " \
355 "use gnulib module gethostname for portability"), \
360 #if @GNULIB_GETLOGIN_R@
361 /* Copies the user's login name to NAME.
362 The array pointed to by NAME has room for SIZE bytes.
364 Returns 0 if successful. Upon error, an error number is returned, or -1 in
365 the case that the login name cannot be found but no specific error is
366 provided (this case is hopefully rare but is left open by the POSIX spec).
368 See <http://www.opengroup.org/susv3xsh/getlogin.html>.
370 # if !@HAVE_DECL_GETLOGIN_R@
372 extern int getlogin_r (char *name, size_t size);
374 #elif defined GNULIB_POSIXCHECK
376 # define getlogin_r(n,s) \
377 (GL_LINK_WARNING ("getlogin_r is unportable - " \
378 "use gnulib module getlogin_r for portability"), \
383 #if @GNULIB_GETPAGESIZE@
384 # if @REPLACE_GETPAGESIZE@
385 # define getpagesize rpl_getpagesize
386 extern int getpagesize (void);
387 # elif !@HAVE_GETPAGESIZE@
388 /* This is for POSIX systems. */
389 # if !defined getpagesize && defined _SC_PAGESIZE
390 # if ! (defined __VMS && __VMS_VER < 70000000)
391 # define getpagesize() sysconf (_SC_PAGESIZE)
394 /* This is for older VMS. */
395 # if !defined getpagesize && defined __VMS
397 # define getpagesize() 8192
399 # define getpagesize() 512
402 /* This is for BeOS. */
403 # if !defined getpagesize && @HAVE_OS_H@
405 # if defined B_PAGE_SIZE
406 # define getpagesize() B_PAGE_SIZE
409 /* This is for AmigaOS4.0. */
410 # if !defined getpagesize && defined __amigaos4__
411 # define getpagesize() 2048
413 /* This is for older Unix systems. */
414 # if !defined getpagesize && @HAVE_SYS_PARAM_H@
415 # include <sys/param.h>
416 # ifdef EXEC_PAGESIZE
417 # define getpagesize() EXEC_PAGESIZE
423 # define getpagesize() (NBPG * CLSIZE)
426 # define getpagesize() NBPC
432 #elif defined GNULIB_POSIXCHECK
434 # define getpagesize() \
435 (GL_LINK_WARNING ("getpagesize is unportable - " \
436 "use gnulib module getpagesize for portability"), \
441 #if @GNULIB_GETUSERSHELL@
442 # if !@HAVE_GETUSERSHELL@
443 /* Return the next valid login shell on the system, or NULL when the end of
444 the list has been reached. */
445 extern char *getusershell (void);
446 /* Rewind to pointer that is advanced at each getusershell() call. */
447 extern void setusershell (void);
448 /* Free the pointer that is advanced at each getusershell() call and
449 associated resources. */
450 extern void endusershell (void);
452 #elif defined GNULIB_POSIXCHECK
454 # define getusershell() \
455 (GL_LINK_WARNING ("getusershell is unportable - " \
456 "use gnulib module getusershell for portability"), \
459 # define setusershell() \
460 (GL_LINK_WARNING ("setusershell is unportable - " \
461 "use gnulib module getusershell for portability"), \
464 # define endusershell() \
465 (GL_LINK_WARNING ("endusershell is unportable - " \
466 "use gnulib module getusershell for portability"), \
472 # if @REPLACE_LCHOWN@
473 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
474 to GID (if GID is not -1). Do not follow symbolic links.
475 Return 0 if successful, otherwise -1 and errno set.
476 See the POSIX:2001 specification
477 <http://www.opengroup.org/susv3xsh/lchown.html>. */
478 # define lchown rpl_lchown
479 extern int lchown (char const *file, uid_t owner, gid_t group);
481 #elif defined GNULIB_POSIXCHECK
483 # define lchown(f,u,g) \
484 (GL_LINK_WARNING ("lchown is unportable to pre-POSIX.1-2001 " \
485 "systems - use gnulib module lchown for portability"), \
491 /* Create a new hard link for an existing file.
492 Return 0 if successful, otherwise -1 and errno set.
493 See POSIX:2001 specification
494 <http://www.opengroup.org/susv3xsh/link.html>. */
496 extern int link (const char *path1, const char *path2);
498 #elif defined GNULIB_POSIXCHECK
500 # define link(path1,path2) \
501 (GL_LINK_WARNING ("link is unportable - " \
502 "use gnulib module link for portability"), \
509 /* Set the offset of FD relative to SEEK_SET, SEEK_CUR, or SEEK_END.
510 Return the new offset if successful, otherwise -1 and errno set.
511 See the POSIX:2001 specification
512 <http://www.opengroup.org/susv3xsh/lseek.html>. */
513 # define lseek rpl_lseek
514 extern off_t lseek (int fd, off_t offset, int whence);
516 #elif defined GNULIB_POSIXCHECK
518 # define lseek(f,o,w) \
519 (GL_LINK_WARNING ("lseek does not fail with ESPIPE on pipes on some " \
520 "systems - use gnulib module lseek for portability"), \
525 #if @GNULIB_READLINK@
526 /* Read the contents of the symbolic link FILE and place the first BUFSIZE
527 bytes of it into BUF. Return the number of bytes placed into BUF if
528 successful, otherwise -1 and errno set.
529 See the POSIX:2001 specification
530 <http://www.opengroup.org/susv3xsh/readlink.html>. */
531 # if !@HAVE_READLINK@
533 extern int readlink (const char *file, char *buf, size_t bufsize);
535 #elif defined GNULIB_POSIXCHECK
537 # define readlink(f,b,s) \
538 (GL_LINK_WARNING ("readlink is unportable - " \
539 "use gnulib module readlink for portability"), \
545 /* Pause the execution of the current thread for N seconds.
546 Returns the number of seconds left to sleep.
547 See the POSIX:2001 specification
548 <http://www.opengroup.org/susv3xsh/sleep.html>. */
550 extern unsigned int sleep (unsigned int n);
552 #elif defined GNULIB_POSIXCHECK
555 (GL_LINK_WARNING ("sleep is unportable - " \
556 "use gnulib module sleep for portability"), \
561 #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
562 /* Write up to COUNT bytes starting at BUF to file descriptor FD.
563 See the POSIX:2001 specification
564 <http://www.opengroup.org/susv3xsh/write.html>. */
566 # define write rpl_write
567 extern ssize_t write (int fd, const void *buf, size_t count);
571 #ifdef FCHDIR_REPLACEMENT
572 /* gnulib internal function. */
573 extern void _gl_unregister_fd (int fd);
582 #endif /* _GL_UNISTD_H */
583 #endif /* _GL_UNISTD_H */