dup2: work around mingw and cygwin 1.5 bug
[gnulib.git] / lib / unistd.in.h
1 /* Substitute for and wrapper around <unistd.h>.
2    Copyright (C) 2003-2009 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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.  */
17
18 #ifndef _GL_UNISTD_H
19
20 #if __GNUC__ >= 3
21 @PRAGMA_SYSTEM_HEADER@
22 #endif
23
24 /* The include_next requires a split double-inclusion guard.  */
25 #if @HAVE_UNISTD_H@
26 # @INCLUDE_NEXT@ @NEXT_UNISTD_H@
27 #endif
28
29 #ifndef _GL_UNISTD_H
30 #define _GL_UNISTD_H
31
32 /* mingw doesn't define the SEEK_* or *_FILENO macros in <unistd.h>.  */
33 #if !(defined SEEK_CUR && defined SEEK_END && defined SEEK_SET)
34 # include <stdio.h>
35 #endif
36
37 /* mingw fails to declare _exit in <unistd.h>.  */
38 /* mingw, BeOS, Haiku declare environ in <stdlib.h>, not in <unistd.h>.  */
39 #include <stdlib.h>
40
41 #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
42 /* Get ssize_t.  */
43 # include <sys/types.h>
44 #endif
45
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
51 #   undef socket
52 #   define socket               socket_used_without_including_sys_socket_h
53 #   undef connect
54 #   define connect              connect_used_without_including_sys_socket_h
55 #   undef accept
56 #   define accept               accept_used_without_including_sys_socket_h
57 #   undef bind
58 #   define bind                 bind_used_without_including_sys_socket_h
59 #   undef getpeername
60 #   define getpeername          getpeername_used_without_including_sys_socket_h
61 #   undef getsockname
62 #   define getsockname          getsockname_used_without_including_sys_socket_h
63 #   undef getsockopt
64 #   define getsockopt           getsockopt_used_without_including_sys_socket_h
65 #   undef listen
66 #   define listen               listen_used_without_including_sys_socket_h
67 #   undef recv
68 #   define recv                 recv_used_without_including_sys_socket_h
69 #   undef send
70 #   define send                 send_used_without_including_sys_socket_h
71 #   undef recvfrom
72 #   define recvfrom             recvfrom_used_without_including_sys_socket_h
73 #   undef sendto
74 #   define sendto               sendto_used_without_including_sys_socket_h
75 #   undef setsockopt
76 #   define setsockopt           setsockopt_used_without_including_sys_socket_h
77 #   undef shutdown
78 #   define shutdown             shutdown_used_without_including_sys_socket_h
79 #  endif
80 #  if !defined _GL_SYS_SELECT_H
81 #   undef select
82 #   define select               select_used_without_including_sys_select_h
83 #  endif
84 # endif
85 #endif
86
87 /* The definition of GL_LINK_WARNING is copied here.  */
88
89
90 /* OS/2 EMX lacks these macros.  */
91 #ifndef STDIN_FILENO
92 # define STDIN_FILENO 0
93 #endif
94 #ifndef STDOUT_FILENO
95 # define STDOUT_FILENO 1
96 #endif
97 #ifndef STDERR_FILENO
98 # define STDERR_FILENO 2
99 #endif
100
101 /* Declare overridden functions.  */
102
103 #ifdef __cplusplus
104 extern "C" {
105 #endif
106
107
108 #if @GNULIB_CHOWN@
109 # if @REPLACE_CHOWN@
110 #  ifndef REPLACE_CHOWN
111 #   define REPLACE_CHOWN 1
112 #  endif
113 #  if REPLACE_CHOWN
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);
121 #  endif
122 # endif
123 #elif defined GNULIB_POSIXCHECK
124 # undef chown
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"), \
129      chown (f, u, g))
130 #endif
131
132
133 #if @GNULIB_CLOSE@
134 # if @REPLACE_CLOSE@
135 /* Automatically included by modules that need a replacement for close.  */
136 #  undef close
137 #  define close rpl_close
138 extern int close (int);
139 # endif
140 #elif @UNISTD_H_HAVE_WINSOCK2_H@
141 # undef close
142 # define close close_used_without_requesting_gnulib_module_close
143 #elif defined GNULIB_POSIXCHECK
144 # undef close
145 # define close(f) \
146     (GL_LINK_WARNING ("close does not portably work on sockets - " \
147                       "use gnulib module close for portability"), \
148      close (f))
149 #endif
150
151
152 #if @GNULIB_DUP2@
153 # if @REPLACE_DUP2@
154 #  define dup2 rpl_dup2
155 # endif
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);
163 # endif
164 #elif defined GNULIB_POSIXCHECK
165 # undef dup2
166 # define dup2(o,n) \
167     (GL_LINK_WARNING ("dup2 is unportable - " \
168                       "use gnulib module dup2 for portability"), \
169      dup2 (o, n))
170 #endif
171
172
173 #if @GNULIB_ENVIRON@
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 ())
180 #  else
181 extern char **environ;
182 #  endif
183 # endif
184 #elif defined GNULIB_POSIXCHECK
185 # undef environ
186 # define environ \
187     (GL_LINK_WARNING ("environ is unportable - " \
188                       "use gnulib module environ for portability"), \
189      environ)
190 #endif
191
192
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);
198 # endif
199 #elif defined GNULIB_POSIXCHECK
200 # undef euidaccess
201 # define euidaccess(f,m) \
202     (GL_LINK_WARNING ("euidaccess is unportable - " \
203                       "use gnulib module euidaccess for portability"), \
204      euidaccess (f, m))
205 #endif
206
207
208 #if @GNULIB_FCHDIR@
209 # if @REPLACE_FCHDIR@
210
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*/);
217
218 #  define dup rpl_dup
219 extern int dup (int);
220 #  if !@REPLACE_DUP2@
221 #   define dup2 rpl_dup2
222 extern int dup2 (int, int);
223 #  endif
224
225 # endif
226 #elif defined GNULIB_POSIXCHECK
227 # undef fchdir
228 # define fchdir(f) \
229     (GL_LINK_WARNING ("fchdir is unportable - " \
230                       "use gnulib module fchdir for portability"), \
231      fchdir (f))
232 #endif
233
234
235 #if @GNULIB_FSYNC@
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>.  */
240 # if !@HAVE_FSYNC@
241 extern int fsync (int fd);
242 # endif
243 #elif defined GNULIB_POSIXCHECK
244 # undef fsync
245 # define fsync(fd) \
246     (GL_LINK_WARNING ("fsync is unportable - " \
247                       "use gnulib module fsync for portability"), \
248      fsync (fd))
249 #endif
250
251
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);
259 # endif
260 #elif defined GNULIB_POSIXCHECK
261 # undef ftruncate
262 # define ftruncate(f,l) \
263     (GL_LINK_WARNING ("ftruncate is unportable - " \
264                       "use gnulib module ftruncate for portability"), \
265      ftruncate (f, l))
266 #endif
267
268
269 #if @GNULIB_GETCWD@
270 /* Include the headers that might declare getcwd so that they will not
271    cause confusion if included after this file.  */
272 # include <stdlib.h>
273 # if @REPLACE_GETCWD@
274 /* Get the name of the current working directory, and put it in SIZE bytes
275    of BUF.
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
283    necessary.  */
284 #  define getcwd rpl_getcwd
285 extern char * getcwd (char *buf, size_t size);
286 # endif
287 #elif defined GNULIB_POSIXCHECK
288 # undef getcwd
289 # define getcwd(b,s) \
290     (GL_LINK_WARNING ("getcwd is unportable - " \
291                       "use gnulib module getcwd for portability"), \
292      getcwd (b, s))
293 #endif
294
295
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
301             not using NIS.
302
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);
309 # endif
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))
316 #endif
317
318
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);
323 # endif
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"), \
329      getdtablesize ())
330 #endif
331
332
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.
336
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@
342 #  undef gethostname
343 #  define gethostname rpl_gethostname
344 # endif
345 # if @UNISTD_H_HAVE_WINSOCK2_H@ || !@HAVE_GETHOSTNAME@
346 extern int gethostname(char *name, size_t len);
347 # endif
348 #elif @UNISTD_H_HAVE_WINSOCK2_H@
349 # undef gethostname
350 # define gethostname gethostname_used_without_requesting_gnulib_module_gethostname
351 #elif defined GNULIB_POSIXCHECK
352 # undef gethostname
353 # define gethostname(n,l) \
354     (GL_LINK_WARNING ("gethostname is unportable - " \
355                       "use gnulib module gethostname for portability"), \
356      gethostname (n, l))
357 #endif
358
359
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.
363
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).
367
368    See <http://www.opengroup.org/susv3xsh/getlogin.html>.
369  */
370 # if !@HAVE_DECL_GETLOGIN_R@
371 #  include <stddef.h>
372 extern int getlogin_r (char *name, size_t size);
373 # endif
374 #elif defined GNULIB_POSIXCHECK
375 # undef getlogin_r
376 # define getlogin_r(n,s) \
377     (GL_LINK_WARNING ("getlogin_r is unportable - " \
378                       "use gnulib module getlogin_r for portability"), \
379      getlogin_r (n, s))
380 #endif
381
382
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)
392 #   endif
393 #  endif
394 /* This is for older VMS.  */
395 #  if !defined getpagesize && defined __VMS
396 #   ifdef __ALPHA
397 #    define getpagesize() 8192
398 #   else
399 #    define getpagesize() 512
400 #   endif
401 #  endif
402 /* This is for BeOS.  */
403 #  if !defined getpagesize && @HAVE_OS_H@
404 #   include <OS.h>
405 #   if defined B_PAGE_SIZE
406 #    define getpagesize() B_PAGE_SIZE
407 #   endif
408 #  endif
409 /* This is for AmigaOS4.0.  */
410 #  if !defined getpagesize && defined __amigaos4__
411 #   define getpagesize() 2048
412 #  endif
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
418 #   else
419 #    ifdef NBPG
420 #     ifndef CLSIZE
421 #      define CLSIZE 1
422 #     endif
423 #     define getpagesize() (NBPG * CLSIZE)
424 #    else
425 #     ifdef NBPC
426 #      define getpagesize() NBPC
427 #     endif
428 #    endif
429 #   endif
430 #  endif
431 # endif
432 #elif defined GNULIB_POSIXCHECK
433 # undef getpagesize
434 # define getpagesize() \
435     (GL_LINK_WARNING ("getpagesize is unportable - " \
436                       "use gnulib module getpagesize for portability"), \
437      getpagesize ())
438 #endif
439
440
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);
451 # endif
452 #elif defined GNULIB_POSIXCHECK
453 # undef getusershell
454 # define getusershell() \
455     (GL_LINK_WARNING ("getusershell is unportable - " \
456                       "use gnulib module getusershell for portability"), \
457      getusershell ())
458 # undef setusershell
459 # define setusershell() \
460     (GL_LINK_WARNING ("setusershell is unportable - " \
461                       "use gnulib module getusershell for portability"), \
462      setusershell ())
463 # undef endusershell
464 # define endusershell() \
465     (GL_LINK_WARNING ("endusershell is unportable - " \
466                       "use gnulib module getusershell for portability"), \
467      endusershell ())
468 #endif
469
470
471 #if @GNULIB_LCHOWN@
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);
480 # endif
481 #elif defined GNULIB_POSIXCHECK
482 # undef lchown
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"), \
486      lchown (f, u, g))
487 #endif
488
489
490 #if @GNULIB_LINK@
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>.  */
495 # if !@HAVE_LINK@
496 extern int link (const char *path1, const char *path2);
497 # endif
498 #elif defined GNULIB_POSIXCHECK
499 # undef link
500 # define link(path1,path2) \
501     (GL_LINK_WARNING ("link is unportable - " \
502                       "use gnulib module link for portability"), \
503      link (path1, path2))
504 #endif
505
506
507 #if @GNULIB_LSEEK@
508 # if @REPLACE_LSEEK@
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);
515 # endif
516 #elif defined GNULIB_POSIXCHECK
517 # undef lseek
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"), \
521      lseek (f, o, w))
522 #endif
523
524
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@
532 #  include <stddef.h>
533 extern int readlink (const char *file, char *buf, size_t bufsize);
534 # endif
535 #elif defined GNULIB_POSIXCHECK
536 # undef readlink
537 # define readlink(f,b,s) \
538     (GL_LINK_WARNING ("readlink is unportable - " \
539                       "use gnulib module readlink for portability"), \
540      readlink (f, b, s))
541 #endif
542
543
544 #if @GNULIB_SLEEP@
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>.  */
549 # if !@HAVE_SLEEP@
550 extern unsigned int sleep (unsigned int n);
551 # endif
552 #elif defined GNULIB_POSIXCHECK
553 # undef sleep
554 # define sleep(n) \
555     (GL_LINK_WARNING ("sleep is unportable - " \
556                       "use gnulib module sleep for portability"), \
557      sleep (n))
558 #endif
559
560
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>.  */
565 # undef write
566 # define write rpl_write
567 extern ssize_t write (int fd, const void *buf, size_t count);
568 #endif
569
570
571 #ifdef FCHDIR_REPLACEMENT
572 /* gnulib internal function.  */
573 extern void _gl_unregister_fd (int fd);
574 #endif
575
576
577 #ifdef __cplusplus
578 }
579 #endif
580
581
582 #endif /* _GL_UNISTD_H */
583 #endif /* _GL_UNISTD_H */