1 /* Substitute for and wrapper around <unistd.h>.
2 Copyright (C) 2003-2008 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_* macros in <unistd.h>. */
33 #if !(defined SEEK_CUR && defined SEEK_END && defined SEEK_SET)
37 /* mingw fails to declare _exit in <unistd.h>. */
40 #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
42 # include <sys/types.h>
45 #if @GNULIB_GETHOSTNAME@
46 /* Get all possible declarations of gethostname(). */
47 # if @UNISTD_H_HAVE_WINSOCK2_H@
48 # include <winsock2.h>
49 # if !defined _GL_SYS_SOCKET_H
51 # define socket socket_used_without_including_sys_socket_h
53 # define connect connect_used_without_including_sys_socket_h
55 # define accept accept_used_without_including_sys_socket_h
57 # define bind bind_used_without_including_sys_socket_h
59 # define getpeername getpeername_used_without_including_sys_socket_h
61 # define getsockname getsockname_used_without_including_sys_socket_h
63 # define getsockopt getsockopt_used_without_including_sys_socket_h
65 # define listen listen_used_without_including_sys_socket_h
67 # define recv recv_used_without_including_sys_socket_h
69 # define send send_used_without_including_sys_socket_h
71 # define recvfrom recvfrom_used_without_including_sys_socket_h
73 # define sendto sendto_used_without_including_sys_socket_h
75 # define setsockopt setsockopt_used_without_including_sys_socket_h
77 # define shutdown shutdown_used_without_including_sys_socket_h
79 # if !defined _GL_SYS_SELECT_H
81 # define select select_used_without_including_sys_select_h
86 /* The definition of GL_LINK_WARNING is copied here. */
89 /* Declare overridden functions. */
98 # ifndef REPLACE_CHOWN
99 # define REPLACE_CHOWN 1
102 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
103 to GID (if GID is not -1). Follow symbolic links.
104 Return 0 if successful, otherwise -1 and errno set.
105 See the POSIX:2001 specification
106 <http://www.opengroup.org/susv3xsh/chown.html>. */
107 # define chown rpl_chown
108 extern int chown (const char *file, uid_t uid, gid_t gid);
111 #elif defined GNULIB_POSIXCHECK
113 # define chown(f,u,g) \
114 (GL_LINK_WARNING ("chown fails to follow symlinks on some systems and " \
115 "doesn't treat a uid or gid of -1 on some systems - " \
116 "use gnulib module chown for portability"), \
122 # if @UNISTD_H_HAVE_WINSOCK2_H@
123 /* Need a gnulib internal function. */
124 # define HAVE__GL_CLOSE_FD_MAYBE_SOCKET 1
127 /* Automatically included by modules that need a replacement for close. */
129 # define close rpl_close
130 extern int close (int);
132 #elif @UNISTD_H_HAVE_WINSOCK2_H@
134 # define close close_used_without_requesting_gnulib_module_close
135 #elif defined GNULIB_POSIXCHECK
138 (GL_LINK_WARNING ("close does not portably work on sockets - " \
139 "use gnulib module close for portability"), \
146 /* Copy the file descriptor OLDFD into file descriptor NEWFD. Do nothing if
147 NEWFD = OLDFD, otherwise close NEWFD first if it is open.
148 Return 0 if successful, otherwise -1 and errno set.
149 See the POSIX:2001 specification
150 <http://www.opengroup.org/susv3xsh/dup2.html>. */
151 extern int dup2 (int oldfd, int newfd);
153 #elif defined GNULIB_POSIXCHECK
156 (GL_LINK_WARNING ("dup2 is unportable - " \
157 "use gnulib module dup2 for portability"), \
163 # if !@HAVE_DECL_ENVIRON@
164 /* Set of environment variables and values. An array of strings of the form
165 "VARIABLE=VALUE", terminated with a NULL. */
166 # if defined __APPLE__ && defined __MACH__
167 # include <crt_externs.h>
168 # define environ (*_NSGetEnviron ())
170 extern char **environ;
173 #elif defined GNULIB_POSIXCHECK
176 (GL_LINK_WARNING ("environ is unportable - " \
177 "use gnulib module environ for portability"), \
182 #if @GNULIB_EUIDACCESS@
183 # if !@HAVE_EUIDACCESS@
184 /* Like access(), except that is uses the effective user id and group id of
185 the current process. */
186 extern int euidaccess (const char *filename, int mode);
188 #elif defined GNULIB_POSIXCHECK
190 # define euidaccess(f,m) \
191 (GL_LINK_WARNING ("euidaccess is unportable - " \
192 "use gnulib module euidaccess for portability"), \
198 # if @REPLACE_FCHDIR@
200 /* Change the process' current working directory to the directory on which
201 the given file descriptor is open.
202 Return 0 if successful, otherwise -1 and errno set.
203 See the POSIX:2001 specification
204 <http://www.opengroup.org/susv3xsh/fchdir.html>. */
205 extern int fchdir (int /*fd*/);
208 extern int dup (int);
209 # define dup2 rpl_dup2
210 extern int dup2 (int, int);
213 #elif defined GNULIB_POSIXCHECK
216 (GL_LINK_WARNING ("fchdir is unportable - " \
217 "use gnulib module fchdir for portability"), \
223 /* Synchronize changes to a file.
224 Return 0 if successful, otherwise -1 and errno set.
225 See POSIX:2001 specification
226 <http://www.opengroup.org/susv3xsh/fsync.html>. */
228 extern int fsync (int fd);
230 #elif defined GNULIB_POSIXCHECK
233 (GL_LINK_WARNING ("fsync is unportable - " \
234 "use gnulib module fsync for portability"), \
239 #if @GNULIB_FTRUNCATE@
240 # if !@HAVE_FTRUNCATE@
241 /* Change the size of the file to which FD is opened to become equal to LENGTH.
242 Return 0 if successful, otherwise -1 and errno set.
243 See the POSIX:2001 specification
244 <http://www.opengroup.org/susv3xsh/ftruncate.html>. */
245 extern int ftruncate (int fd, off_t length);
247 #elif defined GNULIB_POSIXCHECK
249 # define ftruncate(f,l) \
250 (GL_LINK_WARNING ("ftruncate is unportable - " \
251 "use gnulib module ftruncate for portability"), \
257 /* Include the headers that might declare getcwd so that they will not
258 cause confusion if included after this file. */
260 # if @REPLACE_GETCWD@
261 /* Get the name of the current working directory, and put it in SIZE bytes
263 Return BUF if successful, or NULL if the directory couldn't be determined
264 or SIZE was too small.
265 See the POSIX:2001 specification
266 <http://www.opengroup.org/susv3xsh/getcwd.html>.
267 Additionally, the gnulib module 'getcwd' guarantees the following GNU
268 extension: If BUF is NULL, an array is allocated with 'malloc'; the array
269 is SIZE bytes long, unless SIZE == 0, in which case it is as big as
271 # define getcwd rpl_getcwd
272 extern char * getcwd (char *buf, size_t size);
274 #elif defined GNULIB_POSIXCHECK
276 # define getcwd(b,s) \
277 (GL_LINK_WARNING ("getcwd is unportable - " \
278 "use gnulib module getcwd for portability"), \
283 #if @GNULIB_GETDOMAINNAME@
284 /* Return the NIS domain name of the machine.
285 WARNING! The NIS domain name is unrelated to the fully qualified host name
286 of the machine. It is also unrelated to email addresses.
287 WARNING! The NIS domain name is usually the empty string or "(none)" when
290 Put up to LEN bytes of the NIS domain name into NAME.
291 Null terminate it if the name is shorter than LEN.
292 If the NIS domain name is longer than LEN, set errno = EINVAL and return -1.
293 Return 0 if successful, otherwise set errno and return -1. */
294 # if !@HAVE_GETDOMAINNAME@
295 extern int getdomainname(char *name, size_t len);
297 #elif defined GNULIB_POSIXCHECK
298 # undef getdomainname
299 # define getdomainname(n,l) \
300 (GL_LINK_WARNING ("getdomainname is unportable - " \
301 "use gnulib module getdomainname for portability"), \
302 getdomainname (n, l))
306 #if @GNULIB_GETDTABLESIZE@
307 # if !@HAVE_GETDTABLESIZE@
308 /* Return the maximum number of file descriptors in the current process. */
309 extern int getdtablesize (void);
311 #elif defined GNULIB_POSIXCHECK
312 # undef getdtablesize
313 # define getdtablesize() \
314 (GL_LINK_WARNING ("getdtablesize is unportable - " \
315 "use gnulib module getdtablesize for portability"), \
320 #if @GNULIB_GETHOSTNAME@
321 /* Return the standard host name of the machine.
322 WARNING! The host name may or may not be fully qualified.
324 Put up to LEN bytes of the host name into NAME.
325 Null terminate it if the name is shorter than LEN.
326 If the host name is longer than LEN, set errno = EINVAL and return -1.
327 Return 0 if successful, otherwise set errno and return -1. */
328 # if @UNISTD_H_HAVE_WINSOCK2_H@
330 # define gethostname rpl_gethostname
332 # if @UNISTD_H_HAVE_WINSOCK2_H@ || !@HAVE_GETHOSTNAME@
333 extern int gethostname(char *name, size_t len);
335 #elif @UNISTD_H_HAVE_WINSOCK2_H@
337 # define gethostname gethostname_used_without_requesting_gnulib_module_gethostname
338 #elif defined GNULIB_POSIXCHECK
340 # define gethostname(n,l) \
341 (GL_LINK_WARNING ("gethostname is unportable - " \
342 "use gnulib module gethostname for portability"), \
347 #if @GNULIB_GETLOGIN_R@
348 /* Copies the user's login name to NAME.
349 The array pointed to by NAME has room for SIZE bytes.
351 Returns 0 if successful. Upon error, an error number is returned, or -1 in
352 the case that the login name cannot be found but no specific error is
353 provided (this case is hopefully rare but is left open by the POSIX spec).
355 See <http://www.opengroup.org/susv3xsh/getlogin.html>.
357 # if !@HAVE_DECL_GETLOGIN_R@
359 extern int getlogin_r (char *name, size_t size);
361 #elif defined GNULIB_POSIXCHECK
363 # define getlogin_r(n,s) \
364 (GL_LINK_WARNING ("getlogin_r is unportable - " \
365 "use gnulib module getlogin_r for portability"), \
370 #if @GNULIB_GETPAGESIZE@
371 # if @REPLACE_GETPAGESIZE@
372 # define getpagesize rpl_getpagesize
373 extern int getpagesize (void);
374 # elif !@HAVE_GETPAGESIZE@
375 /* This is for POSIX systems. */
376 # if !defined getpagesize && defined _SC_PAGESIZE
377 # if ! (defined __VMS && __VMS_VER < 70000000)
378 # define getpagesize() sysconf (_SC_PAGESIZE)
381 /* This is for older VMS. */
382 # if !defined getpagesize && defined __VMS
384 # define getpagesize() 8192
386 # define getpagesize() 512
389 /* This is for BeOS. */
390 # if !defined getpagesize && @HAVE_OS_H@
392 # if defined B_PAGE_SIZE
393 # define getpagesize() B_PAGE_SIZE
396 /* This is for AmigaOS4.0. */
397 # if !defined getpagesize && defined __amigaos4__
398 # define getpagesize() 2048
400 /* This is for older Unix systems. */
401 # if !defined getpagesize && @HAVE_SYS_PARAM_H@
402 # include <sys/param.h>
403 # ifdef EXEC_PAGESIZE
404 # define getpagesize() EXEC_PAGESIZE
410 # define getpagesize() (NBPG * CLSIZE)
413 # define getpagesize() NBPC
419 #elif defined GNULIB_POSIXCHECK
421 # define getpagesize() \
422 (GL_LINK_WARNING ("getpagesize is unportable - " \
423 "use gnulib module getpagesize for portability"), \
428 #if @GNULIB_GETUSERSHELL@
429 # if !@HAVE_GETUSERSHELL@
430 /* Return the next valid login shell on the system, or NULL when the end of
431 the list has been reached. */
432 extern char *getusershell (void);
433 /* Rewind to pointer that is advanced at each getusershell() call. */
434 extern void setusershell (void);
435 /* Free the pointer that is advanced at each getusershell() call and
436 associated resources. */
437 extern void endusershell (void);
439 #elif defined GNULIB_POSIXCHECK
441 # define getusershell() \
442 (GL_LINK_WARNING ("getusershell is unportable - " \
443 "use gnulib module getusershell for portability"), \
446 # define setusershell() \
447 (GL_LINK_WARNING ("setusershell is unportable - " \
448 "use gnulib module getusershell for portability"), \
451 # define endusershell() \
452 (GL_LINK_WARNING ("endusershell is unportable - " \
453 "use gnulib module getusershell for portability"), \
459 # if @REPLACE_LCHOWN@
460 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
461 to GID (if GID is not -1). Do not follow symbolic links.
462 Return 0 if successful, otherwise -1 and errno set.
463 See the POSIX:2001 specification
464 <http://www.opengroup.org/susv3xsh/lchown.html>. */
465 # define lchown rpl_lchown
466 extern int lchown (char const *file, uid_t owner, gid_t group);
468 #elif defined GNULIB_POSIXCHECK
470 # define lchown(f,u,g) \
471 (GL_LINK_WARNING ("lchown is unportable to pre-POSIX.1-2001 " \
472 "systems - use gnulib module lchown for portability"), \
479 /* Set the offset of FD relative to SEEK_SET, SEEK_CUR, or SEEK_END.
480 Return the new offset if successful, otherwise -1 and errno set.
481 See the POSIX:2001 specification
482 <http://www.opengroup.org/susv3xsh/lseek.html>. */
483 # define lseek rpl_lseek
484 extern off_t lseek (int fd, off_t offset, int whence);
486 #elif defined GNULIB_POSIXCHECK
488 # define lseek(f,o,w) \
489 (GL_LINK_WARNING ("lseek does not fail with ESPIPE on pipes on some " \
490 "systems - use gnulib module lseek for portability"), \
495 #if @GNULIB_READLINK@
496 /* Read the contents of the symbolic link FILE and place the first BUFSIZE
497 bytes of it into BUF. Return the number of bytes placed into BUF if
498 successful, otherwise -1 and errno set.
499 See the POSIX:2001 specification
500 <http://www.opengroup.org/susv3xsh/readlink.html>. */
501 # if !@HAVE_READLINK@
503 extern int readlink (const char *file, char *buf, size_t bufsize);
505 #elif defined GNULIB_POSIXCHECK
507 # define readlink(f,b,s) \
508 (GL_LINK_WARNING ("readlink is unportable - " \
509 "use gnulib module readlink for portability"), \
515 /* Pause the execution of the current thread for N seconds.
516 Returns the number of seconds left to sleep.
517 See the POSIX:2001 specification
518 <http://www.opengroup.org/susv3xsh/sleep.html>. */
520 extern unsigned int sleep (unsigned int n);
522 #elif defined GNULIB_POSIXCHECK
525 (GL_LINK_WARNING ("sleep is unportable - " \
526 "use gnulib module sleep for portability"), \
531 #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
532 /* Write up to COUNT bytes starting at BUF to file descriptor FD.
533 See the POSIX:2001 specification
534 <http://www.opengroup.org/susv3xsh/write.html>. */
536 # define write rpl_write
537 extern ssize_t write (int fd, const void *buf, size_t count);
541 #ifdef FCHDIR_REPLACEMENT
542 /* gnulib internal function. */
543 extern void _gl_unregister_fd (int fd);
552 #endif /* _GL_UNISTD_H */
553 #endif /* _GL_UNISTD_H */