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 Lesser 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser 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 /* Copy the file descriptor OLDFD into file descriptor NEWFD. Do nothing if
155 NEWFD = OLDFD, otherwise close NEWFD first if it is open.
156 Return 0 if successful, otherwise -1 and errno set.
157 See the POSIX:2001 specification
158 <http://www.opengroup.org/susv3xsh/dup2.html>. */
159 extern int dup2 (int oldfd
, int newfd
);
161 #elif defined GNULIB_POSIXCHECK
164 (GL_LINK_WARNING ("dup2 is unportable - " \
165 "use gnulib module dup2 for portability"), \
171 # if !@HAVE_DECL_ENVIRON@
172 /* Set of environment variables and values. An array of strings of the form
173 "VARIABLE=VALUE", terminated with a NULL. */
174 # if defined __APPLE__ && defined __MACH__
175 # include <crt_externs.h>
176 # define environ (*_NSGetEnviron ())
178 extern char **environ
;
181 #elif defined GNULIB_POSIXCHECK
184 (GL_LINK_WARNING ("environ is unportable - " \
185 "use gnulib module environ for portability"), \
190 #if @GNULIB_EUIDACCESS@
191 # if !@HAVE_EUIDACCESS@
192 /* Like access(), except that is uses the effective user id and group id of
193 the current process. */
194 extern int euidaccess (const char *filename
, int mode
);
196 #elif defined GNULIB_POSIXCHECK
198 # define euidaccess(f,m) \
199 (GL_LINK_WARNING ("euidaccess is unportable - " \
200 "use gnulib module euidaccess for portability"), \
206 # if @REPLACE_FCHDIR@
208 /* Change the process' current working directory to the directory on which
209 the given file descriptor is open.
210 Return 0 if successful, otherwise -1 and errno set.
211 See the POSIX:2001 specification
212 <http://www.opengroup.org/susv3xsh/fchdir.html>. */
213 extern int fchdir (int /*fd*/);
216 extern int dup (int);
217 # define dup2 rpl_dup2
218 extern int dup2 (int, int);
221 #elif defined GNULIB_POSIXCHECK
224 (GL_LINK_WARNING ("fchdir is unportable - " \
225 "use gnulib module fchdir for portability"), \
231 /* Synchronize changes to a file.
232 Return 0 if successful, otherwise -1 and errno set.
233 See POSIX:2001 specification
234 <http://www.opengroup.org/susv3xsh/fsync.html>. */
236 extern int fsync (int fd
);
238 #elif defined GNULIB_POSIXCHECK
241 (GL_LINK_WARNING ("fsync is unportable - " \
242 "use gnulib module fsync for portability"), \
247 #if @GNULIB_FTRUNCATE@
248 # if !@HAVE_FTRUNCATE@
249 /* Change the size of the file to which FD is opened to become equal to LENGTH.
250 Return 0 if successful, otherwise -1 and errno set.
251 See the POSIX:2001 specification
252 <http://www.opengroup.org/susv3xsh/ftruncate.html>. */
253 extern int ftruncate (int fd
, off_t length
);
255 #elif defined GNULIB_POSIXCHECK
257 # define ftruncate(f,l) \
258 (GL_LINK_WARNING ("ftruncate is unportable - " \
259 "use gnulib module ftruncate for portability"), \
265 /* Include the headers that might declare getcwd so that they will not
266 cause confusion if included after this file. */
268 # if @REPLACE_GETCWD@
269 /* Get the name of the current working directory, and put it in SIZE bytes
271 Return BUF if successful, or NULL if the directory couldn't be determined
272 or SIZE was too small.
273 See the POSIX:2001 specification
274 <http://www.opengroup.org/susv3xsh/getcwd.html>.
275 Additionally, the gnulib module 'getcwd' guarantees the following GNU
276 extension: If BUF is NULL, an array is allocated with 'malloc'; the array
277 is SIZE bytes long, unless SIZE == 0, in which case it is as big as
279 # define getcwd rpl_getcwd
280 extern char * getcwd (char *buf
, size_t size
);
282 #elif defined GNULIB_POSIXCHECK
284 # define getcwd(b,s) \
285 (GL_LINK_WARNING ("getcwd is unportable - " \
286 "use gnulib module getcwd for portability"), \
291 #if @GNULIB_GETDOMAINNAME@
292 /* Return the NIS domain name of the machine.
293 WARNING! The NIS domain name is unrelated to the fully qualified host name
294 of the machine. It is also unrelated to email addresses.
295 WARNING! The NIS domain name is usually the empty string or "(none)" when
298 Put up to LEN bytes of the NIS domain name into NAME.
299 Null terminate it if the name is shorter than LEN.
300 If the NIS domain name is longer than LEN, set errno = EINVAL and return -1.
301 Return 0 if successful, otherwise set errno and return -1. */
302 # if !@HAVE_GETDOMAINNAME@
303 extern int getdomainname(char *name
, size_t len
);
305 #elif defined GNULIB_POSIXCHECK
306 # undef getdomainname
307 # define getdomainname(n,l) \
308 (GL_LINK_WARNING ("getdomainname is unportable - " \
309 "use gnulib module getdomainname for portability"), \
310 getdomainname (n, l))
314 #if @GNULIB_GETDTABLESIZE@
315 # if !@HAVE_GETDTABLESIZE@
316 /* Return the maximum number of file descriptors in the current process. */
317 extern int getdtablesize (void);
319 #elif defined GNULIB_POSIXCHECK
320 # undef getdtablesize
321 # define getdtablesize() \
322 (GL_LINK_WARNING ("getdtablesize is unportable - " \
323 "use gnulib module getdtablesize for portability"), \
328 #if @GNULIB_GETHOSTNAME@
329 /* Return the standard host name of the machine.
330 WARNING! The host name may or may not be fully qualified.
332 Put up to LEN bytes of the host name into NAME.
333 Null terminate it if the name is shorter than LEN.
334 If the host name is longer than LEN, set errno = EINVAL and return -1.
335 Return 0 if successful, otherwise set errno and return -1. */
336 # if @UNISTD_H_HAVE_WINSOCK2_H@
338 # define gethostname rpl_gethostname
340 # if @UNISTD_H_HAVE_WINSOCK2_H@ || !@HAVE_GETHOSTNAME@
341 extern int gethostname(char *name
, size_t len
);
343 #elif @UNISTD_H_HAVE_WINSOCK2_H@
345 # define gethostname gethostname_used_without_requesting_gnulib_module_gethostname
346 #elif defined GNULIB_POSIXCHECK
348 # define gethostname(n,l) \
349 (GL_LINK_WARNING ("gethostname is unportable - " \
350 "use gnulib module gethostname for portability"), \
355 #if @GNULIB_GETLOGIN_R@
356 /* Copies the user's login name to NAME.
357 The array pointed to by NAME has room for SIZE bytes.
359 Returns 0 if successful. Upon error, an error number is returned, or -1 in
360 the case that the login name cannot be found but no specific error is
361 provided (this case is hopefully rare but is left open by the POSIX spec).
363 See <http://www.opengroup.org/susv3xsh/getlogin.html>.
365 # if !@HAVE_DECL_GETLOGIN_R@
367 extern int getlogin_r (char *name
, size_t size
);
369 #elif defined GNULIB_POSIXCHECK
371 # define getlogin_r(n,s) \
372 (GL_LINK_WARNING ("getlogin_r is unportable - " \
373 "use gnulib module getlogin_r for portability"), \
378 #if @GNULIB_GETPAGESIZE@
379 # if @REPLACE_GETPAGESIZE@
380 # define getpagesize rpl_getpagesize
381 extern int getpagesize (void);
382 # elif !@HAVE_GETPAGESIZE@
383 /* This is for POSIX systems. */
384 # if !defined getpagesize && defined _SC_PAGESIZE
385 # if ! (defined __VMS && __VMS_VER < 70000000)
386 # define getpagesize() sysconf (_SC_PAGESIZE)
389 /* This is for older VMS. */
390 # if !defined getpagesize && defined __VMS
392 # define getpagesize() 8192
394 # define getpagesize() 512
397 /* This is for BeOS. */
398 # if !defined getpagesize && @HAVE_OS_H@
400 # if defined B_PAGE_SIZE
401 # define getpagesize() B_PAGE_SIZE
404 /* This is for AmigaOS4.0. */
405 # if !defined getpagesize && defined __amigaos4__
406 # define getpagesize() 2048
408 /* This is for older Unix systems. */
409 # if !defined getpagesize && @HAVE_SYS_PARAM_H@
410 # include <sys/param.h>
411 # ifdef EXEC_PAGESIZE
412 # define getpagesize() EXEC_PAGESIZE
418 # define getpagesize() (NBPG * CLSIZE)
421 # define getpagesize() NBPC
427 #elif defined GNULIB_POSIXCHECK
429 # define getpagesize() \
430 (GL_LINK_WARNING ("getpagesize is unportable - " \
431 "use gnulib module getpagesize for portability"), \
436 #if @GNULIB_GETUSERSHELL@
437 # if !@HAVE_GETUSERSHELL@
438 /* Return the next valid login shell on the system, or NULL when the end of
439 the list has been reached. */
440 extern char *getusershell (void);
441 /* Rewind to pointer that is advanced at each getusershell() call. */
442 extern void setusershell (void);
443 /* Free the pointer that is advanced at each getusershell() call and
444 associated resources. */
445 extern void endusershell (void);
447 #elif defined GNULIB_POSIXCHECK
449 # define getusershell() \
450 (GL_LINK_WARNING ("getusershell is unportable - " \
451 "use gnulib module getusershell for portability"), \
454 # define setusershell() \
455 (GL_LINK_WARNING ("setusershell is unportable - " \
456 "use gnulib module getusershell for portability"), \
459 # define endusershell() \
460 (GL_LINK_WARNING ("endusershell is unportable - " \
461 "use gnulib module getusershell for portability"), \
467 # if @REPLACE_LCHOWN@
468 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
469 to GID (if GID is not -1). Do not follow symbolic links.
470 Return 0 if successful, otherwise -1 and errno set.
471 See the POSIX:2001 specification
472 <http://www.opengroup.org/susv3xsh/lchown.html>. */
473 # define lchown rpl_lchown
474 extern int lchown (char const *file
, uid_t owner
, gid_t group
);
476 #elif defined GNULIB_POSIXCHECK
478 # define lchown(f,u,g) \
479 (GL_LINK_WARNING ("lchown is unportable to pre-POSIX.1-2001 " \
480 "systems - use gnulib module lchown for portability"), \
486 /* Create a new hard link for an existing file.
487 Return 0 if successful, otherwise -1 and errno set.
488 See POSIX:2001 specification
489 <http://www.opengroup.org/susv3xsh/link.html>. */
491 extern int link (const char *path1
, const char *path2
);
493 #elif defined GNULIB_POSIXCHECK
495 # define link(path1,path2) \
496 (GL_LINK_WARNING ("link is unportable - " \
497 "use gnulib module link for portability"), \
504 /* Set the offset of FD relative to SEEK_SET, SEEK_CUR, or SEEK_END.
505 Return the new offset if successful, otherwise -1 and errno set.
506 See the POSIX:2001 specification
507 <http://www.opengroup.org/susv3xsh/lseek.html>. */
508 # define lseek rpl_lseek
509 extern off_t
lseek (int fd
, off_t offset
, int whence
);
511 #elif defined GNULIB_POSIXCHECK
513 # define lseek(f,o,w) \
514 (GL_LINK_WARNING ("lseek does not fail with ESPIPE on pipes on some " \
515 "systems - use gnulib module lseek for portability"), \
520 #if @GNULIB_READLINK@
521 /* Read the contents of the symbolic link FILE and place the first BUFSIZE
522 bytes of it into BUF. Return the number of bytes placed into BUF if
523 successful, otherwise -1 and errno set.
524 See the POSIX:2001 specification
525 <http://www.opengroup.org/susv3xsh/readlink.html>. */
526 # if !@HAVE_READLINK@
528 extern int readlink (const char *file
, char *buf
, size_t bufsize
);
530 #elif defined GNULIB_POSIXCHECK
532 # define readlink(f,b,s) \
533 (GL_LINK_WARNING ("readlink is unportable - " \
534 "use gnulib module readlink for portability"), \
540 /* Pause the execution of the current thread for N seconds.
541 Returns the number of seconds left to sleep.
542 See the POSIX:2001 specification
543 <http://www.opengroup.org/susv3xsh/sleep.html>. */
545 extern unsigned int sleep (unsigned int n
);
547 #elif defined GNULIB_POSIXCHECK
550 (GL_LINK_WARNING ("sleep is unportable - " \
551 "use gnulib module sleep for portability"), \
556 #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
557 /* Write up to COUNT bytes starting at BUF to file descriptor FD.
558 See the POSIX:2001 specification
559 <http://www.opengroup.org/susv3xsh/write.html>. */
561 # define write rpl_write
562 extern ssize_t
write (int fd
, const void *buf
, size_t count
);
566 #ifdef FCHDIR_REPLACEMENT
567 /* gnulib internal function. */
568 extern void _gl_unregister_fd (int fd
);
577 #endif /* _GL_UNISTD_H */
578 #endif /* _GL_UNISTD_H */