Combine the two replacements of 'close'.
[gnulib.git] / lib / unistd.in.h
1 /* Substitute for and wrapper around <unistd.h>.
2    Copyright (C) 2004-2008 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 @PRAGMA_SYSTEM_HEADER@
21
22 /* The include_next requires a split double-inclusion guard.  */
23 #if @HAVE_UNISTD_H@
24 # @INCLUDE_NEXT@ @NEXT_UNISTD_H@
25 #endif
26
27 #ifndef _GL_UNISTD_H
28 #define _GL_UNISTD_H
29
30 /* mingw doesn't define the SEEK_* macros in <unistd.h>.  */
31 #if !(defined SEEK_CUR && defined SEEK_END && defined SEEK_SET)
32 # include <stdio.h>
33 #endif
34
35 /* mingw fails to declare _exit in <unistd.h>.  */
36 #include <stdlib.h>
37
38 #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
39 /* Get ssize_t.  */
40 # include <sys/types.h>
41 #endif
42
43 /* The definition of GL_LINK_WARNING is copied here.  */
44
45
46 /* Declare overridden functions.  */
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51
52
53 #if @GNULIB_CHOWN@
54 # if @REPLACE_CHOWN@
55 #  ifndef REPLACE_CHOWN
56 #   define REPLACE_CHOWN 1
57 #  endif
58 #  if REPLACE_CHOWN
59 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
60    to GID (if GID is not -1).  Follow symbolic links.
61    Return 0 if successful, otherwise -1 and errno set.
62    See the POSIX:2001 specification
63    <http://www.opengroup.org/susv3xsh/chown.html>.  */
64 #   define chown rpl_chown
65 extern int chown (const char *file, uid_t uid, gid_t gid);
66 #  endif
67 # endif
68 #elif defined GNULIB_POSIXCHECK
69 # undef chown
70 # define chown(f,u,g) \
71     (GL_LINK_WARNING ("chown fails to follow symlinks on some systems and " \
72                       "doesn't treat a uid or gid of -1 on some systems - " \
73                       "use gnulib module chown for portability"), \
74      chown (f, u, g))
75 #endif
76
77
78 #if @GNULIB_CLOSE@
79 # if @REPLACE_CLOSE@
80 /* Automatically included by modules that need a replacement for close.  */
81 #  undef close
82 #  define close rpl_close
83 extern int close (int);
84 # endif
85 #elif @UNISTD_H_HAVE_WINSOCK2_H@
86 # undef close
87 # define close close_used_without_requesting_gnulib_module_close
88 #elif defined GNULIB_POSIXCHECK
89 # undef close
90 # define close(f) \
91     (GL_LINK_WARNING ("close does not portably work on sockets - " \
92                       "use gnulib module close for portability"), \
93      close (f))
94 #endif
95
96
97 #if @GNULIB_DUP2@
98 # if !@HAVE_DUP2@
99 /* Copy the file descriptor OLDFD into file descriptor NEWFD.  Do nothing if
100    NEWFD = OLDFD, otherwise close NEWFD first if it is open.
101    Return 0 if successful, otherwise -1 and errno set.
102    See the POSIX:2001 specification
103    <http://www.opengroup.org/susv3xsh/dup2.html>.  */
104 extern int dup2 (int oldfd, int newfd);
105 # endif
106 #elif defined GNULIB_POSIXCHECK
107 # undef dup2
108 # define dup2(o,n) \
109     (GL_LINK_WARNING ("dup2 is unportable - " \
110                       "use gnulib module dup2 for portability"), \
111      dup2 (o, n))
112 #endif
113
114
115 #if @GNULIB_ENVIRON@
116 # if !@HAVE_DECL_ENVIRON@
117 /* Set of environment variables and values.  An array of strings of the form
118    "VARIABLE=VALUE", terminated with a NULL.  */
119 #  if defined __APPLE__ && defined __MACH__
120 #   include <crt_externs.h>
121 #   define environ (*_NSGetEnviron ())
122 #  else
123 extern char **environ;
124 #  endif
125 # endif
126 #elif defined GNULIB_POSIXCHECK
127 # undef environ
128 # define environ \
129     (GL_LINK_WARNING ("environ is unportable - " \
130                       "use gnulib module environ for portability"), \
131      environ)
132 #endif
133
134
135 #if @GNULIB_FCHDIR@
136 # if @REPLACE_FCHDIR@
137
138 /* Change the process' current working directory to the directory on which
139    the given file descriptor is open.
140    Return 0 if successful, otherwise -1 and errno set.
141    See the POSIX:2001 specification
142    <http://www.opengroup.org/susv3xsh/fchdir.html>.  */
143 extern int fchdir (int /*fd*/);
144
145 #  define dup rpl_dup
146 extern int dup (int);
147 #  define dup2 rpl_dup2
148 extern int dup2 (int, int);
149
150 # endif
151 #elif defined GNULIB_POSIXCHECK
152 # undef fchdir
153 # define fchdir(f) \
154     (GL_LINK_WARNING ("fchdir is unportable - " \
155                       "use gnulib module fchdir for portability"), \
156      fchdir (f))
157 #endif
158
159
160 #if @GNULIB_FSYNC@
161 /* Synchronize changes to a file.
162    Return 0 if successful, otherwise -1 and errno set.
163    See POSIX:2001 specification
164    <http://www.opengroup.org/susv3xsh/fsync.html>.  */
165 # if !@HAVE_FSYNC@
166 extern int fsync (int fd);
167 # endif
168 #elif defined GNULIB_POSIXCHECK
169 # undef fsync
170 # define fsync(fd) \
171     (GL_LINK_WARNING ("fsync is unportable - " \
172                       "use gnulib module fsync for portability"), \
173      fsync (fd))
174 #endif
175
176
177 #if @GNULIB_FTRUNCATE@
178 # if !@HAVE_FTRUNCATE@
179 /* Change the size of the file to which FD is opened to become equal to LENGTH.
180    Return 0 if successful, otherwise -1 and errno set.
181    See the POSIX:2001 specification
182    <http://www.opengroup.org/susv3xsh/ftruncate.html>.  */
183 extern int ftruncate (int fd, off_t length);
184 # endif
185 #elif defined GNULIB_POSIXCHECK
186 # undef ftruncate
187 # define ftruncate(f,l) \
188     (GL_LINK_WARNING ("ftruncate is unportable - " \
189                       "use gnulib module ftruncate for portability"), \
190      ftruncate (f, l))
191 #endif
192
193
194 #if @GNULIB_GETCWD@
195 /* Include the headers that might declare getcwd so that they will not
196    cause confusion if included after this file.  */
197 # include <stdlib.h>
198 # if @REPLACE_GETCWD@
199 /* Get the name of the current working directory, and put it in SIZE bytes
200    of BUF.
201    Return BUF if successful, or NULL if the directory couldn't be determined
202    or SIZE was too small.
203    See the POSIX:2001 specification
204    <http://www.opengroup.org/susv3xsh/getcwd.html>.
205    Additionally, the gnulib module 'getcwd' guarantees the following GNU
206    extension: If BUF is NULL, an array is allocated with 'malloc'; the array
207    is SIZE bytes long, unless SIZE == 0, in which case it is as big as
208    necessary.  */
209 #  define getcwd rpl_getcwd
210 extern char * getcwd (char *buf, size_t size);
211 # endif
212 #elif defined GNULIB_POSIXCHECK
213 # undef getcwd
214 # define getcwd(b,s) \
215     (GL_LINK_WARNING ("getcwd is unportable - " \
216                       "use gnulib module getcwd for portability"), \
217      getcwd (b, s))
218 #endif
219
220
221 #if @GNULIB_GETDTABLESIZE@
222 # if !@HAVE_GETDTABLESIZE@
223 /* Return the maximum number of file descriptors in the current process.  */
224 extern int getdtablesize (void);
225 # endif
226 #elif defined GNULIB_POSIXCHECK
227 # undef getdtablesize
228 # define getdtablesize() \
229     (GL_LINK_WARNING ("getdtablesize is unportable - " \
230                       "use gnulib module getdtablesize for portability"), \
231      getdtablesize ())
232 #endif
233
234
235 #if @GNULIB_GETLOGIN_R@
236 /* Copies the user's login name to NAME.
237    The array pointed to by NAME has room for SIZE bytes.
238
239    Returns 0 if successful.  Upon error, an error number is returned, or -1 in
240    the case that the login name cannot be found but no specific error is
241    provided (this case is hopefully rare but is left open by the POSIX spec).
242
243    See <http://www.opengroup.org/susv3xsh/getlogin.html>.
244  */
245 # if !@HAVE_DECL_GETLOGIN_R@
246 #  include <stddef.h>
247 extern int getlogin_r (char *name, size_t size);
248 # endif
249 #elif defined GNULIB_POSIXCHECK
250 # undef getlogin_r
251 # define getlogin_r(n,s) \
252     (GL_LINK_WARNING ("getlogin_r is unportable - " \
253                       "use gnulib module getlogin_r for portability"), \
254      getlogin_r (n, s))
255 #endif
256
257
258 #if @GNULIB_GETPAGESIZE@
259 # if @REPLACE_GETPAGESIZE@
260 #  define getpagesize rpl_getpagesize
261 extern int getpagesize (void);
262 # elif !@HAVE_GETPAGESIZE@
263 /* This is for POSIX systems.  */
264 #  if !defined getpagesize && defined _SC_PAGESIZE
265 #   if ! (defined __VMS && __VMS_VER < 70000000)
266 #    define getpagesize() sysconf (_SC_PAGESIZE)
267 #   endif
268 #  endif
269 /* This is for older VMS.  */
270 #  if !defined getpagesize && defined __VMS
271 #   ifdef __ALPHA
272 #    define getpagesize() 8192
273 #   else
274 #    define getpagesize() 512
275 #   endif
276 #  endif
277 /* This is for BeOS.  */
278 #  if !defined getpagesize && @HAVE_OS_H@
279 #   include <OS.h>
280 #   if defined B_PAGE_SIZE
281 #    define getpagesize() B_PAGE_SIZE
282 #   endif
283 #  endif
284 /* This is for AmigaOS4.0.  */
285 #  if !defined getpagesize && defined __amigaos4__
286 #   define getpagesize() 2048
287 #  endif
288 /* This is for older Unix systems.  */
289 #  if !defined getpagesize && @HAVE_SYS_PARAM_H@
290 #   include <sys/param.h>
291 #   ifdef EXEC_PAGESIZE
292 #    define getpagesize() EXEC_PAGESIZE
293 #   else
294 #    ifdef NBPG
295 #     ifndef CLSIZE
296 #      define CLSIZE 1
297 #     endif
298 #     define getpagesize() (NBPG * CLSIZE)
299 #    else
300 #     ifdef NBPC
301 #      define getpagesize() NBPC
302 #     endif
303 #    endif
304 #   endif
305 #  endif
306 # endif
307 #elif defined GNULIB_POSIXCHECK
308 # undef getpagesize
309 # define getpagesize() \
310     (GL_LINK_WARNING ("getpagesize is unportable - " \
311                       "use gnulib module getpagesize for portability"), \
312      getpagesize ())
313 #endif
314
315
316 #if @GNULIB_LCHOWN@
317 # if @REPLACE_LCHOWN@
318 /* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
319    to GID (if GID is not -1).  Do not follow symbolic links.
320    Return 0 if successful, otherwise -1 and errno set.
321    See the POSIX:2001 specification
322    <http://www.opengroup.org/susv3xsh/lchown.html>.  */
323 #  define lchown rpl_lchown
324 extern int lchown (char const *file, uid_t owner, gid_t group);
325 # endif
326 #elif defined GNULIB_POSIXCHECK
327 # undef lchown
328 # define lchown(f,u,g) \
329     (GL_LINK_WARNING ("lchown is unportable to pre-POSIX.1-2001 " \
330                       "systems - use gnulib module lchown for portability"), \
331      lchown (f, u, g))
332 #endif
333
334
335 #if @GNULIB_LSEEK@
336 # if @REPLACE_LSEEK@
337 /* Set the offset of FD relative to SEEK_SET, SEEK_CUR, or SEEK_END.
338    Return the new offset if successful, otherwise -1 and errno set.
339    See the POSIX:2001 specification
340    <http://www.opengroup.org/susv3xsh/lseek.html>.  */
341 #  define lseek rpl_lseek
342    extern off_t lseek (int fd, off_t offset, int whence);
343 # endif
344 #elif defined GNULIB_POSIXCHECK
345 # undef lseek
346 # define lseek(f,o,w) \
347     (GL_LINK_WARNING ("lseek does not fail with ESPIPE on pipes on some " \
348                       "systems - use gnulib module lseek for portability"), \
349      lseek (f, o, w))
350 #endif
351
352
353 #if @GNULIB_READLINK@
354 /* Read the contents of the symbolic link FILE and place the first BUFSIZE
355    bytes of it into BUF.  Return the number of bytes placed into BUF if
356    successful, otherwise -1 and errno set.
357    See the POSIX:2001 specification
358    <http://www.opengroup.org/susv3xsh/readlink.html>.  */
359 # if !@HAVE_READLINK@
360 #  include <stddef.h>
361 extern int readlink (const char *file, char *buf, size_t bufsize);
362 # endif
363 #elif defined GNULIB_POSIXCHECK
364 # undef readlink
365 # define readlink(f,b,s) \
366     (GL_LINK_WARNING ("readlink is unportable - " \
367                       "use gnulib module readlink for portability"), \
368      readlink (f, b, s))
369 #endif
370
371
372 #if @GNULIB_SLEEP@
373 /* Pause the execution of the current thread for N seconds.
374    Returns the number of seconds left to sleep.
375    See the POSIX:2001 specification
376    <http://www.opengroup.org/susv3xsh/sleep.html>.  */
377 # if !@HAVE_SLEEP@
378 extern unsigned int sleep (unsigned int n);
379 # endif
380 #elif defined GNULIB_POSIXCHECK
381 # undef sleep
382 # define sleep(n) \
383     (GL_LINK_WARNING ("sleep is unportable - " \
384                       "use gnulib module sleep for portability"), \
385      sleep (n))
386 #endif
387
388
389 #if @GNULIB_WRITE@ && @REPLACE_WRITE@ && @GNULIB_UNISTD_H_SIGPIPE@
390 /* Write up to COUNT bytes starting at BUF to file descriptor FD.
391    See the POSIX:2001 specification
392    <http://www.opengroup.org/susv3xsh/write.html>.  */
393 # undef write
394 # define write rpl_write
395 extern ssize_t write (int fd, const void *buf, size_t count);
396 #endif
397
398
399 #ifdef FCHDIR_REPLACEMENT
400 /* gnulib internal function.  */
401 extern void _gl_unregister_fd (int fd);
402 #endif
403
404
405 #ifdef __cplusplus
406 }
407 #endif
408
409
410 #endif /* _GL_UNISTD_H */
411 #endif /* _GL_UNISTD_H */