Rev 359 | Go to most recent revision | Details | Compare with Previous | 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 | #undef __getOSHandle |
||
52 | |||
53 | extern unsigned __NFiles; // the size of the iomode array |
||
54 | extern void __grow_iomode( int num ); |
||
55 | unsigned __NHandles = 0; |
||
56 | |||
57 | #if defined(__NT__) |
||
58 | |||
59 | HANDLE *__OSHandles = NULL; |
||
60 | |||
61 | unsigned __growPOSIXHandles( unsigned num ) |
||
62 | { |
||
63 | HANDLE *new2; |
||
64 | unsigned i; |
||
65 | |||
66 | if( num > __NHandles ) |
||
67 | { |
||
68 | _AccessFList(); |
||
69 | if( __OSHandles == NULL ) |
||
70 | { |
||
71 | new2 = lib_malloc( num * sizeof( int ) ); |
||
72 | } |
||
73 | else |
||
74 | { |
||
75 | new2 = lib_realloc( __OSHandles, num * sizeof( int ) ); |
||
76 | } |
||
77 | if( new2 == NULL ) |
||
78 | { |
||
79 | __set_errno( ENOMEM ); |
||
80 | num = __NHandles; |
||
81 | } |
||
82 | else |
||
83 | { |
||
84 | for( i = __NHandles; i < num; i++ ) |
||
85 | { |
||
86 | new2[ i ] = NULL_HANDLE; |
||
87 | } |
||
88 | __OSHandles = new2; |
||
89 | __NHandles = num; |
||
90 | } |
||
91 | _ReleaseFList(); |
||
92 | } |
||
93 | return( __NHandles ); |
||
94 | } |
||
95 | |||
96 | int __allocPOSIXHandle( HANDLE hdl ) |
||
97 | { |
||
98 | int i; |
||
99 | |||
100 | _AccessFList(); |
||
101 | for( i = 0; i < __NHandles; i++ ) |
||
102 | { |
||
103 | if( __OSHandles[i] == NULL_HANDLE ) break; |
||
104 | } |
||
105 | if( i >= __NHandles ) |
||
106 | { |
||
107 | // 20 -> (20+10+1) -> 31 |
||
108 | // 31 -> (31+15+1) -> 47 |
||
109 | // 47 -> (47+23+1) -> 71 |
||
110 | __growPOSIXHandles( i + (i >> 1) + 1 ); |
||
111 | // keep iomode array in sync |
||
112 | if( __NHandles > __NFiles ) __grow_iomode( __NHandles ); |
||
113 | for( ; i < __NHandles; i++ ) |
||
114 | { |
||
115 | if( __OSHandles[i] == NULL_HANDLE ) break; |
||
116 | } |
||
117 | } |
||
118 | if( i >= __NHandles ) |
||
119 | { |
||
120 | i = -1; |
||
121 | } else { |
||
122 | __OSHandles[i] = hdl; |
||
123 | } |
||
124 | _ReleaseFList(); |
||
125 | return( i ); |
||
126 | } |
||
127 | |||
128 | void __freePOSIXHandle( int hid ) |
||
129 | { |
||
130 | __OSHandles[ hid ] = NULL_HANDLE; |
||
131 | } |
||
132 | |||
133 | |||
134 | HANDLE __getOSHandle( int hid ) |
||
135 | { |
||
136 | return( __OSHandles[ hid ] ); |
||
137 | } |
||
138 | |||
548 | serge | 139 | |
359 | serge | 140 | int __setOSHandle( unsigned hid, HANDLE hdl ) |
141 | { |
||
142 | // call the Win32 API for a standard file handle |
||
143 | switch( hid ) { |
||
144 | case STDIN_FILENO: |
||
145 | // SetStdHandle( STD_INPUT_HANDLE, hdl ); |
||
146 | break; |
||
147 | case STDOUT_FILENO: |
||
148 | // SetStdHandle( STD_OUTPUT_HANDLE, hdl ); |
||
149 | break; |
||
150 | case STDERR_FILENO: |
||
151 | // SetStdHandle( STD_ERROR_HANDLE, hdl ); |
||
152 | break; |
||
153 | } |
||
154 | if( hid < __NHandles ) |
||
155 | { |
||
156 | __OSHandles[ hid ] = hdl; |
||
157 | } |
||
158 | else |
||
159 | { |
||
160 | hid = (unsigned)-1; // this should never happen |
||
161 | } |
||
162 | return( hid ); |
||
163 | } |
||
164 | |||
165 | HANDLE *__FakeHandles = 0; |
||
166 | static int __topFakeHandle = 0; |
||
167 | |||
168 | HANDLE __NTGetFakeHandle( void ) |
||
169 | { |
||
170 | HANDLE os_handle; |
||
548 | serge | 171 | static DWORD fakeHandle = 0x80000000L; |
359 | serge | 172 | _AccessFList(); |
548 | serge | 173 | |
174 | fakeHandle++; |
||
175 | os_handle = (HANDLE)fakeHandle; |
||
359 | serge | 176 | _ReleaseFList(); |
177 | return( os_handle ); |
||
178 | } |
||
179 | |||
180 | // called from library startup code |
||
181 | |||
182 | void __initPOSIXHandles( void ) |
||
183 | { |
||
184 | HANDLE h; |
||
185 | |||
186 | // __OSHandles = NULL; |
||
187 | // __NHandles = 0; |
||
188 | |||
189 | __growPOSIXHandles( __NFiles ); |
||
190 | h = 0; //GetStdHandle( STD_INPUT_HANDLE ); |
||
191 | if( h == 0 || h == INVALID_HANDLE_VALUE ) { |
||
192 | h = (HANDLE)__NTGetFakeHandle(); |
||
193 | } |
||
194 | __allocPOSIXHandle( h ); // should return 0==STDIN_FILENO |
||
195 | h = 0; //GetStdHandle( STD_OUTPUT_HANDLE ); |
||
196 | if( h == 0 || h == INVALID_HANDLE_VALUE ) { |
||
197 | h = (HANDLE)__NTGetFakeHandle(); |
||
198 | } |
||
199 | __allocPOSIXHandle( h ); // should return 1==STDOUT_FILENO |
||
200 | h = 0; //GetStdHandle( STD_ERROR_HANDLE ); |
||
201 | if( h == 0 || h == INVALID_HANDLE_VALUE ) { |
||
202 | h = (HANDLE)__NTGetFakeHandle(); |
||
203 | } |
||
204 | __allocPOSIXHandle( h ); // should return 3==STDERR_FILENO |
||
205 | } |
||
206 | |||
207 | static void __finiPOSIXHandles( void ) |
||
208 | { |
||
209 | if( __OSHandles != NULL ) { |
||
210 | lib_free( __OSHandles ); |
||
211 | __OSHandles = NULL; |
||
212 | } |
||
213 | if( __FakeHandles != NULL ) |
||
214 | { |
||
215 | int i; |
||
216 | for( i = 0 ; i < __topFakeHandle ; i++ ) |
||
217 | { |
||
218 | // CloseHandle( __FakeHandles[i] ); |
||
219 | } |
||
220 | lib_free( __FakeHandles ); |
||
221 | __FakeHandles = 0; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | AYI( __finiPOSIXHandles, INIT_PRIORITY_LIBRARY-1 ) |
||
226 | |||
227 | #endif |
||
228 | |||
229 | |||
230 | void __set_handles( int num ) |
||
231 | { |
||
232 | __NHandles = num; |
||
233 | } |
||
234 | |||
235 | _WCRTLINK int _grow_handles( int num ) |
||
236 | { |
||
237 | if( num > __NHandles ) |
||
238 | { |
||
239 | #if defined(MSDOS) |
||
240 | #elif defined( __OS2_286__ ) |
||
241 | #elif defined( __WARP__ ) |
||
242 | #elif defined(__WINDOWS__) |
||
243 | #elif defined(__NT__) |
||
244 | { |
||
245 | num = __growPOSIXHandles( num ); |
||
246 | } |
||
247 | #elif defined(__NETWARE__) |
||
248 | #elif defined(__UNIX__) |
||
249 | #endif |
||
250 | |||
251 | if( num > __NFiles ) { |
||
252 | __grow_iomode( num ); // sets new __NFiles if successful |
||
253 | } |
||
254 | __NHandles = num; |
||
255 | } |
||
256 | return( __NHandles ); |
||
257 | }>>>>> |