summaryrefslogtreecommitdiffstats
path: root/private/crt32/lowio/ioinit.c
blob: d359f96866026f94c1371dcf2c964818ecdd3f72 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/***
*ioinit.c - Initialization for lowio functions
*
*	Copyright (c) 1992, Microsoft Corporation. All rights reserved.
*
*Purpose:
*	Contains all initialization for lowio functions. Currently, this
*	includes:
*		1. Definitions of _nhandle, _osfhnd[], _osfile[] and _pipech[],
*		   including initial values.
*		2. Processing of inherited file info from parent process.
*		3. Initialization of _osfhnd[] and _osfile[] entries for
*		   standard input/output/error.
*
*Revision History:
*	02-14-92  GJF	Module created.
*
*******************************************************************************/

#include <cruntime.h>
#include <oscalls.h>
#include <internal.h>
#include <msdos.h>
#include <stdlib.h>
#include <string.h>

/*
 * Number of allowable file handles. The static initialization of the
 * _osfhnd[] and _pipech[] arrays (below) supports only two values of
 * _NHANDLE, 64 (for single-thread) and 256 (for multi-thread)!
 */
int _nhandle = _NHANDLE_;

/*
 * File handle database
 */
char _osfile[_NHANDLE_] = { 0 };

/*
 * Array of OS file handles. Statically initialized to INVALID_HANDLE_VALUE.
 * Note that if _NHANDLE_ differs from 64 for single-thread, or differs from
 * 256 for multi-thread, this initialization needs to be revised.
 */
#define NOHNDL (long)INVALID_HANDLE_VALUE

long _osfhnd[_NHANDLE_] = {
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
#ifdef	MTHREAD
	NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
	NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL, NOHNDL,
#endif
	NOHNDL };

/*
 * Peek-ahead buffers for pipes, each initialized to a newline. Note that if
 * _NHANDLE_ differs from 64 for single-thread, or differs from 256 for
 * multi-thread, this initialization needs to be revised.
 */
char _pipech[_NHANDLE_] = {
	10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
	10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
	10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
	10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
#ifdef	MTHREAD
	10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
	10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
	10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
	10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
	10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
	10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
	10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
	10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
	10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
	10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
	10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
	10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
#endif
	10 };

/*
 * macro used to map 0, 1 and 2 to right value for call to GetStdHandle
 */
#define stdhndl(fh)  ( (fh == 0) ? STD_INPUT_HANDLE : ((fh == 1) ? \
		       STD_OUTPUT_HANDLE : STD_ERROR_HANDLE) )

/***
*_ioinit() -
*
*Purpose:
*	First, _ioinit() obtains and processes information on inherited file
*	handles from the parent process (e.g., cmd.exe)
*
*	Obtains the StartupInfo structure from the OS. The inherited file
*	handle information is pointed to by the lpReserved2 field. The format
*	of the information is as follows:
*
*	    bytes 0 thru 3	  - integer value, say N, which is the number of handles
*				    information is passed about.
*
*	    bytes 4 thru N+3	  - values for _osfile[0] thru _osfile[N-1]
*
*	    bytes N+4 thru 5*N+3  - N double-words, the N OS handle values being
*				    passed.
*
*	Next, _osfhnd[i] and _osfile[i] are initialized for i = 0, 1, 2, as
*	follows:
*
*	    If the value in _osfhnd[i] is INVALID_HANDLE_VALUE, then try to
*	    obtain a handle by calling GetStdHandle, and call GetFileType to
*	    help set _osfile[i]. Otherwise, assume _osfhndl[i] and _osfile[i]
*	    are valid, but force it to text mode (standard input/output/error
*	    are to always start out in text mode).
*
*	Notes:
*	    1. In general, not all of the passed info from the parent process
*	       will describe open handles! If, for example, only C handle 1
*	       (STDOUT) and C handle 6 are open in the parent, info for C
*	       handles 0 thru 6 is passed to the the child.
*
*	    2. Care is taken not to 'overflow' _osfhnd[] and _osfile[]. Only
*	       info on the first _NHANDLE_ handles from the parent process is
*	       used.
*
*	    3. See exec\dospawn.c for the encodes of the file handle info
*	       to be passed to a child process.
*
*Entry:
*	No parameters: reads the environment.
*
*Exit:
*	No return value.
*
*Exceptions:
*
*******************************************************************************/

void _CALLTYPE1 _ioinit (
	void
	)
{
	STARTUPINFO StartupInfo;
	int cfi_len;
	int fh;
	DWORD htype;

	/*
	 * Process inherited file handle information, if any
	 */

	GetStartupInfo( &StartupInfo );

        if ( StartupInfo.cbReserved2 != 0 && StartupInfo.lpReserved2 != NULL ) {
		/*
		 * Get the number of handles inherited.
		 */
		memcpy( &cfi_len,
			StartupInfo.lpReserved2,
			sizeof( int )
		      );
		/*
		 * Now, get one byte of info (_osfile[] entry) for each
		 * inherited handle. Be careful not to overflow the _osfile[]
		 * array.
		 */
		memcpy( _osfile,
			StartupInfo.lpReserved2 + sizeof( int ),
			__min(_NHANDLE_, cfi_len) * sizeof( char )
		      );
		/*
		 * Finally, copy each each inherited handle into _osfhnd[].
		 * Be careful not to overflow _osfile[].
		 */
		memcpy( _osfhnd,
			StartupInfo.lpReserved2 + sizeof( int ) +
			    (cfi_len * sizeof( char )),
			__min(_NHANDLE_, cfi_len) * sizeof( long )
		      );
	}

	/*
	 * If handles for standard input, output and error were not inherited,
	 * try to obtain them directly from the OS. Also, set appropriate bits
	 * in _osfile[0], _osfile[1] and _osfile[2].
	 */

	for ( fh = 0 ; fh <= 2 ; ++fh ) {

		if ( _osfhnd[fh] == (long)INVALID_HANDLE_VALUE ) {
			if ( (_osfhnd[fh] = (long)GetStdHandle( stdhndl(fh) ))
			    != (long)INVALID_HANDLE_VALUE ) {
				/*
				 * mark the stream as open in text mode.
				 * determine if it isa char device or pipe.
				 */
				_osfile[fh] = (char)(FOPEN | FTEXT);
				htype = GetFileType( (HANDLE)_osfhnd[fh] );
				if ( (htype & 0xFF) == FILE_TYPE_CHAR )
					_osfile[fh] |= FDEV;
				else if ( (htype & 0xFF) == FILE_TYPE_PIPE )
					_osfile[fh] |= FPIPE;
			}
		}
		else
			/*
			 * handle was passed to us by parent process. make
			 * sure it is text mode.
			 */
			_osfile[fh] |= FTEXT;
	}


}