Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
613 | 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: WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE |
||
28 | * DESCRIBE IT HERE! |
||
29 | * |
||
30 | ****************************************************************************/ |
||
31 | |||
32 | |||
33 | #include "variety.h" |
||
34 | #include |
||
35 | #include |
||
36 | #include |
||
37 | #include |
||
38 | #include "rtdata.h" |
||
39 | #include "liballoc.h" |
||
40 | #include "fileacc.h" |
||
41 | #include "rtinit.h" |
||
42 | #include "seterrno.h" |
||
43 | #include "iomode.h" |
||
44 | #include "handleio.h" |
||
45 | |||
46 | #ifdef DLHEAP |
||
47 | |||
48 | void* _cdecl dlmalloc(size_t); |
||
49 | void _cdecl dlfree(void*); |
||
50 | void _cdecl mf_init(); |
||
51 | |||
52 | #define malloc dlmalloc |
||
53 | #define free dlfree |
||
54 | #define realloc dlrealloc |
||
55 | |||
56 | #define lib_malloc dlmalloc |
||
57 | #define lib_free dlfree |
||
58 | #define lib_realloc dlrealloc |
||
59 | |||
60 | #endif |
||
61 | |||
62 | #if defined(__NETWARE__) |
||
63 | #error NO IO MODE MANAGER UNDER NETWARE |
||
64 | #endif |
||
65 | |||
66 | extern unsigned _HUGEDATA __init_mode[_NFILES]; |
||
67 | extern unsigned __NFiles; /* maximum # of files we can open */ |
||
68 | |||
69 | static unsigned _init_NFiles; // original __NFiles value; |
||
70 | |||
71 | void __grow_iomode( int num ) |
||
72 | { |
||
73 | unsigned *new; |
||
74 | |||
75 | _AccessIOB(); |
||
76 | if( __io_mode == __init_mode ) |
||
77 | { |
||
78 | _init_NFiles = __NFiles; |
||
79 | new = (unsigned *) lib_malloc( num * sizeof( unsigned ) ); |
||
80 | if( new != NULL ) { |
||
81 | memcpy( new, __init_mode, __NFiles * sizeof(unsigned) ); |
||
82 | } |
||
83 | } |
||
84 | else |
||
85 | { |
||
86 | #if defined(__NETWARE__) |
||
87 | #else |
||
88 | new = (unsigned *) lib_realloc( __io_mode, num * sizeof( unsigned ) ); |
||
89 | #endif |
||
90 | } |
||
91 | if( new == NULL ) |
||
92 | { |
||
93 | __set_errno( ENOMEM ); |
||
94 | } |
||
95 | else |
||
96 | { |
||
97 | memset( &new[__NFiles], 0, (num-__NFiles)*sizeof(unsigned) ); |
||
98 | __io_mode = new; |
||
99 | __NFiles = num; |
||
100 | } |
||
101 | _ReleaseIOB(); |
||
102 | } |
||
103 | |||
104 | void __shrink_iomode( void ) |
||
105 | { |
||
106 | _AccessIOB(); |
||
107 | // free any malloc'd iomode array |
||
108 | if( __io_mode != __init_mode ) |
||
109 | { |
||
110 | lib_free( __io_mode ); |
||
111 | __io_mode = __init_mode; |
||
112 | __NFiles = _init_NFiles; |
||
113 | } |
||
114 | _ReleaseIOB(); |
||
115 | } |
||
116 | |||
117 | AYI(__shrink_iomode,INIT_PRIORITY_IOSTREAM); |
||
118 | |||
119 | |||
120 | #if defined(__WARP__) |
||
121 | |||
122 | extern unsigned __NHandles; |
||
123 | |||
124 | static void __preinit_iomode_os2(void) |
||
125 | { |
||
126 | LONG req_count; |
||
127 | ULONG curr_max_fh; |
||
128 | APIRET rc; |
||
129 | |||
130 | // Ensure that the clib and OS file handle limits match |
||
131 | req_count = 0; |
||
132 | rc = DosSetRelMaxFH( &req_count, &curr_max_fh ); |
||
133 | if( rc == 0 ) { |
||
134 | __grow_iomode( curr_max_fh ); |
||
135 | __NHandles = curr_max_fh; // same as __set_handles |
||
136 | } |
||
137 | |||
138 | } |
||
139 | |||
140 | AXI( __preinit_iomode_os2, INIT_PRIORITY_RUNTIME ); |
||
141 | |||
142 | #endif |
||
143 | |||
144 | #define _INITIALIZED _DYNAMIC |
||
145 | |||
146 | signed __SetIOMode( int handle, unsigned value ) |
||
147 | { |
||
148 | int i; |
||
149 | |||
150 | if( handle >= __NFiles ) |
||
151 | { |
||
152 | i = __NFiles; // 20 -> (20+10+1) -> 31 |
||
153 | // 31 -> (31+15+1) -> 47 |
||
154 | // 47 -> (47+23+1) -> 71 |
||
155 | __grow_iomode( i + (i > 1) + 1 ); |
||
156 | } |
||
157 | if( handle >= __NFiles ) |
||
158 | { |
||
159 | // return an error indication (errno should be set to ENOMEM) |
||
160 | return( -1 ); |
||
161 | } |
||
162 | else |
||
163 | { |
||
164 | if( value != 0 ) |
||
165 | { |
||
166 | __ChkTTYIOMode( handle ); |
||
167 | __io_mode[handle] = value | _INITIALIZED; |
||
168 | } |
||
169 | else |
||
170 | { |
||
171 | __io_mode[handle] = value; /* we're closing it; smite _INITIALIZED */ |
||
172 | } |
||
173 | return( handle ); |
||
174 | } |
||
175 | } |