summaryrefslogtreecommitdiffstats
path: root/private/crt32/exec/spawnve.c
blob: 00e321dd266a59fbe1fd5716157740c9005592a6 (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/***
*spawnve.c - OS/2 low level routine eventually called by all _spawnXX routines
*	also contains all code for _execve, called by _execXX routines
*
*	Copyright (c) 1985-1992, Microsoft Corporation. All rights reserved.
*
*Purpose:
*
*	This is the low level routine which is eventually invoked by all the
*	_spawnXX routines.
*
*	This is also the low-level routine which is eventually invoked by
*	all of the _execXX routines.
*
*Revision History:
*	03-??-84  RLB	created
*	05-??-84  DCW	modified to fix bug in initialization of envblock
*			pointer (used int 0 which would fail in long model) and
*			changed (char *)0 to NULL.
*	03-31-86  SKS	modified for OS/2; no OVERLAY mode,
*			new format for DOS Exec function
*			also check for Xenix or DOS style slash characters
*	10-13-86  SKS	pass program name to _cenvarg()
*	11-19-86  SKS	handle both kinds of slashes, with support for Kanji
*	01-29-87  BCM	don't try ".com" extension in protected mode (OS/2)
*	12-11-87  JCR	Added "_LOAD_DS" to declaration
*	05/31/88  SJM	Re-written to allow spawn of .CMD files, increase
*			speed. Added comexecmd routine.
*	06/01/88  SJM	added #ifdef statements for execve.obj compilation
*	10-30-88  GJF	Call _dospawn() for EXECVE, not _doexec().
*	07-21-89  GJF	Progagated fixes of 11-23-88 and 05-27-89 from CRT tree.
*	11-16-89  GJF	Added code to execve/spawnve to ensure a relative or
*			or absolute pathname is always used for the executable,
*			not just a filename (otherwise DOSEXECPGM will search
*			the PATH!). Also, cleaned up some of the erratic
*			formatting. Same as 9-15-89 change to CRT version
*	11-20-89  GJF	Added const attribute to types of appropriate args.
*	02-08-90  GJF	Fixed bug in comexecmd (must free(comname) if and only
*			if comname points to a malloc-ed block). Propagated
*			from 02-07-90 change in crt6 version.
*	03-08-90  GJF	Replaced _LOAD_DS with _CALLTYPE1, added #include
*			<cruntime.h> and removed #include <register.h>.
*	04-02-90  GJF	Made comexecmd() _CALLTYPE4. Added #include <io.h>
*			and a prototype for comexecmd() to fix compiler
*			warnings (-W3).
*	05-21-90  GJF	Fixed stack checking pragma syntax.
*	07-24-90  SBM	Removed redundant include, minor optimization
*	09-27-90  GJF	New-style function declarators.
*	12-28-90  SRW	Added _CRUISER_ conditional around check_stack pragma
*	01-17-91  GJF	ANSI naming.
*	08-21-91  JCR	Call access() in before comexecmd (bug fix)
*	01-24-92  JCR	upgraded for Win32
*	10-24-92  SKS	Remove special treatment for batch files -
*			Windows NT will spawn %COMSPEC% automatically
*	11-30-92  KRS	Ported _MBCS support from 16-bit tree.
*
*******************************************************************************/

#include <cruntime.h>
#include <io.h>
#include <process.h>
#include <errno.h>
#include <msdos.h>
#include <string.h>
#include <stdlib.h>
#include <internal.h>
#include <mbstring.h>

#ifdef	_CRUISER_
#pragma check_stack(on) 	/* turn on stack checking */
#endif  /* ndef _CRUISER_ */

#define SLASHCHAR   '\\'
#define XSLASHCHAR  '/'

#ifndef EXECVE
static int _CALLTYPE4 comexecmd(int mode, const char * name,
	const char * const * argv, const char * const * envp);
#else
static int _CALLTYPE4 comexecmd(const char * name,
	const char * const * argv, const char * const * envp);
#endif /* EXECVE */

/***
*static int comexecmd(mode, name, argv, envp) - do the exec
*	or spawn after name fixup
*
*Purpose:
*	Spawns a child process with given parameters and environment.  Either
*	overlays current process or loads in free memory while parent process
*	waits.	If the named file is a .cmd file, modifies the calling sequence
*	and prepends the /c and filename arguments into the command string
*
*	Exec doesn't take a mode; instead, the parent process goes away as
*	the child process is brought in.
*
*Entry:
*	int mode - mode to spawn (WAIT, NOWAIT, or OVERLAY)
*		    only WAIT and OVERLAY currently supported
*
*	    ****  mode is only used in the spawnve() version  ****
*
*	char *name - pathname of file to spawn.  Includes the extension
*	char **argv - vector of parameter strings
*	char **envp - vector of environment variables
*
*Exit:
*	returns exit code of child process
*	if fails, returns -1
*
*Exceptions:
*	Returns a value of (-1) to indicate an error in exec'ing the child
*	process.  errno may be set to:
*
*	E2BIG	= failed in argument/environment processing (_cenvarg)
*		  argument list or environment too big;
*	EACCESS = locking or sharing violation on file;
*	EMFILE	= too many files open;
*	ENOENT	= failed to find program name - no such file or directory;
*	ENOEXEC = failed in exec - bad executable format;
*	ENOMEM	= failed in memory allocation (during malloc, or in
*		  setting up memory for executing child process).
*
*******************************************************************************/

static int _CALLTYPE4 comexecmd (

#ifndef EXECVE
	REG3 int mode,
#endif /* EXECVE */

	REG2 const char *name,
	const char * const *argv,
	const char * const *envp
	)
{
	char *argblk;
	char *envblk;
	REG4 int rc;

	if (_cenvarg(argv, envp, &argblk, &envblk, name) == -1)
		return -1;

#ifndef EXECVE
	rc = _dospawn(mode, name, argblk, envblk);
#else
	rc = _dospawn(_P_OVERLAY, name, argblk, envblk);
#endif
	/* free memory */

	free(argblk);
	free(envblk);

	return rc;
}

/***
*int _spawnve(mode, name, argv, envp) - low level _spawnXX library function
*int _execve(name, argv, envp) - low level _execXX library function
*
*Purpose:
*	spawns or execs a child process; takes a single pointer to an argument
*	list as well as a pointer to the environment; unlike _spawnvpe,
*	_spawnve does not search the PATH= list in processing the name
*	parameter; mode specifies the parent's execution mode.
*
*Entry:
*	int mode    - parent process's execution mode:
*		      must be one of _P_OVERLAY, _P_WAIT, _P_NOWAIT;
*		      not used for _execve
*	char *name  - path name of program to spawn;
*	char **argv - pointer to array of pointers to child's arguments;
*	char **envp - pointer to array of pointers to child's environment
*		      settings.
*
*Exit:
*	Returns : (int) a status value whose meaning is as follows:
*		0	 = normal termination of child process;
*		positive = exit code of child upon error termination
*			   (abort or exit(nonzero));
*		-1	 = child process was not spawned;
*			   errno indicates what kind of error:
*			   (E2BIG, EINVAL, ENOENT, ENOEXEC, ENOMEM).
*
*Exceptions:
*	Returns a value of (-1) to indicate an error in spawning the child
*	process.  errno may be set to:
*
*	E2BIG	= failed in argument/environment processing (_cenvarg) -
*		  argument list or environment too big;
*	EINVAL	= invalid mode argument;
*	ENOENT	= failed to find program name - no such file or directory;
*	ENOEXEC = failed in spawn - bad executable format;
*	ENOMEM	= failed in memory allocation (during malloc, or in
*		  setting up memory for spawning child process).
*
*******************************************************************************/

/* Extension array - ordered in search order from right to left.

   ext_strings	= array of extensions
*/

#ifdef _CRUISER_
static char *ext_strings[] = { NULL,   ".exe", ".com" };
enum {CMD, EXE, COM, EXTFIRST=CMD, EXTLAST=COM};
#else
static char *ext_strings[] = { ".cmd", ".bat", ".exe" , ".com" };
enum {CMD, BAT, EXE, COM, EXTFIRST=CMD, EXTLAST=COM};
#endif

int _CALLTYPE1

#ifndef EXECVE

_spawnve (
	REG3 int mode,

#else

_execve (

#endif /* EXECVE */

	const char *name,
	const char * const *argv,
	const char * const *envp
	)
{
	char *ext;	/* where the extension goes if we have to add one */
	REG1 char *p;
	char *q;
	REG2 char *pathname = (char *)name;
	REG4 int rc;
	REG5 int i;

#ifdef _CRUISER_
	exstrings[CMD] = _osmode ? ".cmd" : ".bat";
#endif

	p = _mbsrchr(pathname, SLASHCHAR);
	q = strrchr(pathname, XSLASHCHAR); /* No need for _mbsrchr with "/" */

	/* ensure that pathname is an absolute or relative pathname. also,
	 * position p to point at the filename portion of pathname (i.e., just
	 * after the last occurence of a colon, slash or backslash character */

	if (!q) {
		if (!p)
			if (!(p = strchr(pathname, ':'))) {

				/* pathname is a filename only, force it to be
				 * a relative pathname. note that an extra byte
				 * is malloc-ed just in case pathname is NULL,
				 * to keep the heap from being trashed by
				 * strcpy */
				if (!(pathname = malloc(strlen(pathname) + 3)))
					return(-1);

				strcpy(pathname, ".\\");
				strcat(pathname, name);

				/* set p to point to the start of the filename
				 * (i.e., past the ".\\" prefix) */
				p = pathname + 2;
			}
			/* else pathname has drive specifier prefix and p is
			 * is pointing to the ':' */
	}
	else if (!p || q > p)	/* p == NULL or q > p */
		p = q;


	rc = -1;	/* init to error value */

	if (ext = strrchr(p, '.'))  {

		/* extension given; only do filename */

		if (_access(pathname, 0) != -1) {
#ifndef EXECVE
			rc = comexecmd(mode, pathname, argv, envp);
#else
			rc = comexecmd(pathname, argv, envp);
#endif
		}

        }
	else	{

		/* no extension; try .cmd/.bat, then .com and .exe */

		if (!(p = malloc(strlen(pathname) + 5)))
			return(-1);

		strcpy(p, pathname);
		ext = p + strlen(pathname);

		for (i = EXTLAST; i >= EXTFIRST; --i) {
			strcpy(ext, ext_strings[i]);

			if (_access(p, 0) != -1) {
#ifndef EXECVE
				rc = comexecmd(mode, p, argv, envp);
#else
				rc = comexecmd(p, argv, envp);
#endif /* EXECVE */
				break;
			}
		}
		free(p);
	}

	if (pathname != name)
		free(pathname);

	return rc;
}