Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
359 | serge | 1 | /**************************************************************************** |
2 | * |
||
3 | * Open Watcom Project |
||
4 | * |
||
5 | * Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved. |
||
6 | * |
||
7 | * ======================================================================== |
||
8 | * |
||
9 | * This file contains Original Code and/or Modifications of Original |
||
10 | * Code as defined in and that are subject to the Sybase Open Watcom |
||
11 | * Public License version 1.0 (the 'License'). You may not use this file |
||
12 | * except in compliance with the License. BY USING THIS FILE YOU AGREE TO |
||
13 | * ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is |
||
14 | * provided with the Original Code and Modifications, and is also |
||
15 | * available at www.sybase.com/developer/opensource. |
||
16 | * |
||
17 | * The Original Code and all software distributed under the License are |
||
18 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
||
19 | * EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM |
||
20 | * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF |
||
21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR |
||
22 | * NON-INFRINGEMENT. Please see the License for the specific language |
||
23 | * governing rights and limitations under the License. |
||
24 | * |
||
25 | * ======================================================================== |
||
26 | * |
||
27 | * Description: Handle manager routines. |
||
28 | * |
||
29 | ****************************************************************************/ |
||
30 | |||
31 | |||
32 | #include "variety.h" |
||
33 | #include |
||
34 | #include |
||
35 | #include "liballoc.h" |
||
36 | #include |
||
37 | #ifdef _M_IX86 |
||
38 | #include |
||
39 | #endif |
||
40 | #include |
||
41 | #include |
||
42 | #if defined(__OS2__) |
||
43 | #elif defined(__WINDOWS__) || defined(__NT__) |
||
44 | #endif |
||
45 | #include "iomode.h" |
||
46 | #include "fileacc.h" |
||
47 | #include "rtinit.h" |
||
48 | #include "seterrno.h" |
||
49 | #include "handleio.h" |
||
50 | |||
51 | #ifdef DLHEAP |
||
52 | |||
53 | void* _cdecl dlmalloc(size_t); |
||
54 | void _cdecl dlfree(void*); |
||
55 | void _cdecl mf_init(); |
||
56 | |||
57 | #define malloc dlmalloc |
||
58 | #define free dlfree |
||
59 | #define realloc dlrealloc |
||
60 | |||
61 | #define lib_malloc dlmalloc |
||
62 | #define lib_free dlfree |
||
63 | #define lib_realloc dlrealloc |
||
64 | |||
65 | #endif |
||
66 | |||
67 | |||
68 | #undef __getOSHandle |
||
69 | |||
70 | extern unsigned __NFiles; // the size of the iomode array |
||
71 | extern void __grow_iomode( int num ); |
||
72 | unsigned __NHandles = 0; |
||
73 | |||
74 | #if defined(__NT__) |
||
75 | |||
76 | HANDLE *__OSHandles = NULL; |
||
77 | |||
78 | unsigned __growPOSIXHandles( unsigned num ) |
||
79 | { |
||
80 | HANDLE *new2; |
||
81 | unsigned i; |
||
82 | |||
83 | if( num > __NHandles ) |
||
84 | { |
||
85 | _AccessFList(); |
||
86 | if( __OSHandles == NULL ) |
||
87 | { |
||
88 | new2 = lib_malloc( num * sizeof( int ) ); |
||
89 | } |
||
90 | else |
||
91 | { |
||
92 | new2 = lib_realloc( __OSHandles, num * sizeof( int ) ); |
||
93 | } |
||
94 | if( new2 == NULL ) |
||
95 | { |
||
96 | __set_errno( ENOMEM ); |
||
97 | num = __NHandles; |
||
98 | } |
||
99 | else |
||
100 | { |
||
101 | for( i = __NHandles; i < num; i++ ) |
||
102 | { |
||
103 | new2[ i ] = NULL_HANDLE; |
||
104 | } |
||
105 | __OSHandles = new2; |
||
106 | __NHandles = num; |
||
107 | } |
||
108 | _ReleaseFList(); |
||
109 | } |
||
110 | return( __NHandles ); |
||
111 | } |
||
112 | |||
113 | int __allocPOSIXHandle( HANDLE hdl ) |
||
114 | { |
||
115 | int i; |
||
116 | |||
117 | _AccessFList(); |
||
118 | for( i = 0; i < __NHandles; i++ ) |
||
119 | { |
||
120 | if( __OSHandles[i] == NULL_HANDLE ) break; |
||
121 | } |
||
122 | if( i >= __NHandles ) |
||
123 | { |
||
124 | // 20 -> (20+10+1) -> 31 |
||
125 | // 31 -> (31+15+1) -> 47 |
||
126 | // 47 -> (47+23+1) -> 71 |
||
127 | __growPOSIXHandles( i + (i >> 1) + 1 ); |
||
128 | // keep iomode array in sync |
||
129 | if( __NHandles > __NFiles ) __grow_iomode( __NHandles ); |
||
130 | for( ; i < __NHandles; i++ ) |
||
131 | { |
||
132 | if( __OSHandles[i] == NULL_HANDLE ) break; |
||
133 | } |
||
134 | } |
||
135 | if( i >= __NHandles ) |
||
136 | { |
||
137 | i = -1; |
||
138 | } else { |
||
139 | __OSHandles[i] = hdl; |
||
140 | } |
||
141 | _ReleaseFList(); |
||
142 | return( i ); |
||
143 | } |
||
144 | |||
145 | void __freePOSIXHandle( int hid ) |
||
146 | { |
||
147 | __OSHandles[ hid ] = NULL_HANDLE; |
||
148 | } |
||
149 | |||
150 | |||
151 | HANDLE __getOSHandle( int hid ) |
||
152 | { |
||
153 | return( __OSHandles[ hid ] ); |
||
154 | } |
||
155 | |||
156 | int __setOSHandle( unsigned hid, HANDLE hdl ) |
||
157 | { |
||
158 | // call the Win32 API for a standard file handle |
||
159 | switch( hid ) { |
||
160 | case STDIN_FILENO: |
||
161 | // SetStdHandle( STD_INPUT_HANDLE, hdl ); |
||
162 | break; |
||
163 | case STDOUT_FILENO: |
||
164 | // SetStdHandle( STD_OUTPUT_HANDLE, hdl ); |
||
165 | break; |
||
166 | case STDERR_FILENO: |
||
167 | // SetStdHandle( STD_ERROR_HANDLE, hdl ); |
||
168 | break; |
||
169 | } |
||
170 | if( hid < __NHandles ) |
||
171 | { |
||
172 | __OSHandles[ hid ] = hdl; |
||
173 | } |
||
174 | else |
||
175 | { |
||
176 | hid = (unsigned)-1; // this should never happen |
||
177 | } |
||
178 | return( hid ); |
||
179 | } |
||
180 | |||
181 | HANDLE *__FakeHandles = 0; |
||
182 | static int __topFakeHandle = 0; |
||
183 | |||
184 | HANDLE __NTGetFakeHandle( void ) |
||
185 | { |
||
186 | HANDLE os_handle; |
||
187 | |||
188 | _AccessFList(); |
||
189 | |||
190 | // os_handle = CreateEvent( 0, 0, 0, 0 ); |
||
191 | os_handle = 0; |
||
192 | if( os_handle == NULL ) |
||
193 | { |
||
194 | // win32s does not support event handles |
||
195 | static DWORD fakeHandle = 0x80000000L; |
||
196 | fakeHandle++; |
||
197 | os_handle = (HANDLE)fakeHandle; |
||
198 | } |
||
199 | else |
||
200 | { |
||
201 | __FakeHandles = lib_realloc( __FakeHandles, (__topFakeHandle+1) * sizeof( HANDLE ) ); |
||
202 | __FakeHandles[ __topFakeHandle ] = os_handle; |
||
203 | __topFakeHandle++; |
||
204 | } |
||
205 | _ReleaseFList(); |
||
206 | return( os_handle ); |
||
207 | } |
||
208 | |||
209 | // called from library startup code |
||
210 | |||
211 | void __initPOSIXHandles( void ) |
||
212 | { |
||
213 | HANDLE h; |
||
214 | |||
215 | // __OSHandles = NULL; |
||
216 | // __NHandles = 0; |
||
217 | |||
218 | __growPOSIXHandles( __NFiles ); |
||
219 | h = 0; //GetStdHandle( STD_INPUT_HANDLE ); |
||
220 | if( h == 0 || h == INVALID_HANDLE_VALUE ) { |
||
221 | h = (HANDLE)__NTGetFakeHandle(); |
||
222 | } |
||
223 | __allocPOSIXHandle( h ); // should return 0==STDIN_FILENO |
||
224 | h = 0; //GetStdHandle( STD_OUTPUT_HANDLE ); |
||
225 | if( h == 0 || h == INVALID_HANDLE_VALUE ) { |
||
226 | h = (HANDLE)__NTGetFakeHandle(); |
||
227 | } |
||
228 | __allocPOSIXHandle( h ); // should return 1==STDOUT_FILENO |
||
229 | h = 0; //GetStdHandle( STD_ERROR_HANDLE ); |
||
230 | if( h == 0 || h == INVALID_HANDLE_VALUE ) { |
||
231 | h = (HANDLE)__NTGetFakeHandle(); |
||
232 | } |
||
233 | __allocPOSIXHandle( h ); // should return 3==STDERR_FILENO |
||
234 | } |
||
235 | |||
236 | static void __finiPOSIXHandles( void ) |
||
237 | { |
||
238 | if( __OSHandles != NULL ) { |
||
239 | lib_free( __OSHandles ); |
||
240 | __OSHandles = NULL; |
||
241 | } |
||
242 | if( __FakeHandles != NULL ) |
||
243 | { |
||
244 | int i; |
||
245 | for( i = 0 ; i < __topFakeHandle ; i++ ) |
||
246 | { |
||
247 | // CloseHandle( __FakeHandles[i] ); |
||
248 | } |
||
249 | lib_free( __FakeHandles ); |
||
250 | __FakeHandles = 0; |
||
251 | } |
||
252 | } |
||
253 | |||
254 | AYI( __finiPOSIXHandles, INIT_PRIORITY_LIBRARY-1 ) |
||
255 | |||
256 | #endif |
||
257 | |||
258 | |||
259 | void __set_handles( int num ) |
||
260 | { |
||
261 | __NHandles = num; |
||
262 | } |
||
263 | |||
264 | _WCRTLINK int _grow_handles( int num ) |
||
265 | { |
||
266 | if( num > __NHandles ) |
||
267 | { |
||
268 | #if defined(MSDOS) |
||
269 | #elif defined( __OS2_286__ ) |
||
270 | #elif defined( __WARP__ ) |
||
271 | #elif defined(__WINDOWS__) |
||
272 | #elif defined(__NT__) |
||
273 | { |
||
274 | num = __growPOSIXHandles( num ); |
||
275 | } |
||
276 | #elif defined(__NETWARE__) |
||
277 | #elif defined(__UNIX__) |
||
278 | #endif |
||
279 | |||
280 | if( num > __NFiles ) { |
||
281 | __grow_iomode( num ); // sets new __NFiles if successful |
||
282 | } |
||
283 | __NHandles = num; |
||
284 | } |
||
285 | return( __NHandles ); |
||
286 | }>>>>> |