OSDN Git Service

Eliminate some potential definition inconsistencies.
[mingw/mingw-org-wsl.git] / mingwrt / include / unistd.h
1 #ifndef _UNISTD_H
2 /*
3  * unistd.h
4  *
5  * Standard header file declaring MinGW's POSIX compatibility features.
6  *
7  * $Id$
8  *
9  * Written by Rob Savoye <rob@cygnus.com>
10  * Modified by Earnie Boyd <earnie@users.sourceforge.net>
11  *   Danny Smith <dannysmith@users.sourceforge.net>
12  *   Ramiro Polla <ramiro@lisha.ufsc.br>
13  *   Gregory McGarry  <gregorymcgarry@users.sourceforge.net>
14  *   Keith Marshall  <keithmarshall@users.sourceforge.net>
15  * Copyright (C) 1997, 1999, 2002-2004, 2007-2009, 2014, MinGW.org Project.
16  *
17  *
18  * Permission is hereby granted, free of charge, to any person obtaining a
19  * copy of this software and associated documentation files (the "Software"),
20  * to deal in the Software without restriction, including without limitation
21  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
22  * and/or sell copies of the Software, and to permit persons to whom the
23  * Software is furnished to do so, subject to the following conditions:
24  *
25  * The above copyright notice, this permission notice, and the following
26  * disclaimer shall be included in all copies or substantial portions of
27  * the Software.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
32  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF OR OTHER
35  * DEALINGS IN THE SOFTWARE.
36  *
37  */
38 #define _UNISTD_H  1
39 #pragma GCC system_header
40
41 /* All MinGW headers MUST include _mingw.h before anything else,
42  * to ensure proper initialization of feature test macros.
43  */
44 #include <_mingw.h>
45
46 /* unistd.h maps (roughly) to io.h
47  * Other headers included by unistd.h may be selectively processed;
48  * __UNISTD_H_SOURCED__ enables such selective processing.
49  */
50 #define __UNISTD_H_SOURCED__ 1
51
52 #include <io.h>
53 #include <process.h>
54 #include <getopt.h>
55
56 /* These are defined in stdio.h.  POSIX also requires that they
57  * are to be consistently defined here; don't guard against prior
58  * definitions, as this might conceal inconsistencies.
59  */
60 #define SEEK_SET   0
61 #define SEEK_CUR   1
62 #define SEEK_END   2
63
64 #if _POSIX_C_SOURCE
65 /*
66  * POSIX process/thread suspension functions; all are supported by a
67  * common MinGW API in libmingwex.a, providing for suspension periods
68  * ranging from mean values of ~7.5 milliseconds, (see comments below),
69  * extending up to a maximum of ~136 years.
70  *
71  * Note that, whereas POSIX supports early wake-up of any suspended
72  * process/thread, in response to a signal, this implementation makes
73  * no attempt to emulate this signalling behaviour, (since signals are
74  * not well supported by Windows); thus, unless impeded by an invalid
75  * argument, this implementation always returns an indication as if
76  * the sleeping period ran to completion.
77  */
78 _EXTERN_C _cdecl __MINGW_NOTHROW
79 int __mingw_sleep( unsigned long, unsigned long );
80
81 /* Structure timespec is mandated by POSIX, for specification of
82  * intervals with the greatest precision supported by the OS kernel.
83  * Although this allows for specification to nanosecond precision, do
84  * not be deluded into any false expectation that such short intervals
85  * can be realized on Windows; on Win9x derivatives, the metronome used
86  * by the process scheduler has a period of ~55 milliseconds, while for
87  * WinNT derivatives, the corresponding period is ~15 milliseconds; thus,
88  * the shortest intervals which can be realistically timed will range
89  * from 0..55 milliseconds on Win9x hosts, and from 0..15 ms on WinNT,
90  * with period values normally distributed around means of ~27.5 ms
91  * and ~7.5 ms, for the two system types respectively.
92  */
93 #define _FAKE_TIME_H_SOURCED    1
94 #define __need_struct_timespec  1
95 #include <parts/time.h>
96
97 _BEGIN_C_DECLS
98
99 /* The nanosleep() function provides the most general purpose API for
100  * process/thread suspension; it provides for specification of periods
101  * ranging from ~7.5 ms mean, (on WinNT derivatives; ~27.5 ms on Win9x),
102  * extending up to ~136 years, (effectively eternity).
103  */
104 _cdecl __MINGW_NOTHROW
105 int nanosleep( const struct timespec *, struct timespec * );
106
107 #ifndef __NO_INLINE__
108 __CRT_INLINE __LIBIMPL__(( FUNCTION = nanosleep ))
109 int nanosleep( const struct timespec *period, struct timespec *residual )
110 {
111   if( residual != (void *)(0) )
112     residual->tv_sec = (long long)(residual->tv_nsec = 0);
113   return __mingw_sleep((unsigned)(period->tv_sec), (period->tv_sec < 0LL)
114     ? (unsigned)(-1) : (unsigned)(period->tv_nsec));
115 }
116 #endif
117
118 /* The usleep() function, and its associated useconds_t type specifier
119  * were made obsolete in POSIX.1-2008; declared here, only for backward
120  * compatibility, its continued use is not recommended.  (It is limited
121  * to specification of suspension periods ranging from ~7.5 ms mean up
122  * to a maximum of 999,999 microseconds only).
123  */
124 typedef unsigned long useconds_t __MINGW_ATTRIB_DEPRECATED;
125 int _cdecl __MINGW_NOTHROW usleep( useconds_t )__MINGW_ATTRIB_DEPRECATED;
126
127 #ifndef __NO_INLINE__
128 __CRT_INLINE __LIBIMPL__(( FUNCTION = usleep ))
129 int usleep( useconds_t period ){ return __mingw_sleep( 0, 1000 * period ); }
130 #endif
131
132 /* The sleep() function is, perhaps, the most commonly used of all the
133  * process/thread suspension APIs; it provides support for specification
134  * of suspension periods ranging from 1 second to ~136 years.  (However,
135  * POSIX recommends limiting the maximum period to 65535 seconds, to
136  * maintain portability to platforms with only 16-bit ints).
137  */
138 unsigned _cdecl __MINGW_NOTHROW sleep( unsigned );
139
140 #ifndef __NO_INLINE__
141 __CRT_INLINE __LIBIMPL__(( FUNCTION = sleep ))
142 unsigned sleep( unsigned period ){ return __mingw_sleep( period, 0 ); }
143 #endif
144
145
146 /* POSIX ftruncate() function.
147  *
148  * Microsoft's _chsize() function is incorrectly described, on MSDN,
149  * as a preferred replacement for the POSIX chsize() function.  There
150  * never was any such POSIX function; the actual POSIX equivalent is
151  * the ftruncate() function.
152  */
153 int _cdecl ftruncate( int, off_t );
154
155 #ifndef __NO_INLINE__
156 __CRT_INLINE __JMPSTUB__(( FUNCTION = ftruncate, REMAPPED = _chsize ))
157 int ftruncate( int __fd, off_t __length ){ return _chsize( __fd, __length ); }
158 #endif
159
160 _END_C_DECLS
161
162 #endif /* _POSIX_C_SOURCE */
163
164 #undef __UNISTD_H_SOURCED__
165 #endif /* ! _UNISTD_H: $RCSfile$: end of file */