Added stubs for AccessCheckByType, AddAuditAccessAce,
[wine] / dlls / msvcrt / process.c
1 /*
2  * msvcrt.dll spawn/exec functions
3  *
4  * Copyright 1996,1998 Marcus Meissner
5  * Copyright 1996 Jukka Iivonen
6  * Copyright 1997,2000 Uwe Bonnes
7  * Copyright 2000 Jon Griffiths
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  * FIXME:
24  * -File handles need some special handling. Sometimes children get
25  *  open file handles, sometimes not. The docs are confusing
26  * -No check for maximum path/argument/environment size is done
27  */
28 #include "config.h"
29
30 #include <stdarg.h>
31
32 #include "msvcrt.h"
33 #include "msvcrt/errno.h"
34
35 #include "msvcrt/stdio.h"
36 #include "msvcrt/process.h"
37 #include "msvcrt/stdlib.h"
38 #include "msvcrt/string.h"
39
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
43
44 /* INTERNAL: Spawn a child process */
45 static int msvcrt_spawn(int flags, const char* exe, char* cmdline, char* env)
46 {
47   STARTUPINFOA si;
48   PROCESS_INFORMATION pi;
49
50   if (sizeof(HANDLE) != sizeof(int))
51     WARN("This call is unsuitable for your architecture\n");
52
53   if ((unsigned)flags > _P_DETACH)
54   {
55     *MSVCRT__errno() = MSVCRT_EINVAL;
56     return -1;
57   }
58
59   FIXME(":must dup/kill streams for child process\n");
60
61   memset(&si, 0, sizeof(si));
62   si.cb = sizeof(si);
63
64   if (!CreateProcessA(exe, cmdline, NULL, NULL, TRUE,
65                      flags == _P_DETACH ? DETACHED_PROCESS : 0,
66                      env, NULL, &si, &pi))
67   {
68     MSVCRT__set_errno(GetLastError());
69     return -1;
70   }
71
72   switch(flags)
73   {
74   case _P_WAIT:
75     WaitForSingleObject(pi.hProcess,-1); /* wait forvever */
76     GetExitCodeProcess(pi.hProcess,&pi.dwProcessId);
77     CloseHandle(pi.hProcess);
78     CloseHandle(pi.hThread);
79     return (int)pi.dwProcessId;
80   case _P_DETACH:
81     CloseHandle(pi.hProcess);
82     pi.hProcess = 0;
83     /* fall through */
84   case _P_NOWAIT:
85   case _P_NOWAITO:
86     CloseHandle(pi.hThread);
87     return (int)pi.hProcess;
88   case  _P_OVERLAY:
89     MSVCRT__exit(0);
90   }
91   return -1; /* can't reach here */
92 }
93
94 /* INTERNAL: Convert argv list to a single 'delim'-separated string, with an
95  * extra '\0' to terminate it
96  */
97 static char* msvcrt_argvtos(const char* const* arg, char delim)
98 {
99   const char* const* a;
100   long size;
101   char* p;
102   char* ret;
103
104   if (!arg && !delim)
105   {
106       /* Return NULL for an empty environment list */
107       return NULL;
108   }
109
110   /* get length */
111   a = arg;
112   size = 0;
113   while (*a)
114   {
115     size += strlen(*a) + 1;
116     a++;
117   }
118
119   ret = (char*)MSVCRT_malloc(size + 1);
120   if (!ret)
121     return NULL;
122
123   /* fill string */
124   a = arg;
125   p = ret;
126   while (*a)
127   {
128     int len = strlen(*a);
129     memcpy(p,*a,len);
130     p += len;
131     *p++ = delim;
132     a++;
133   }
134   if (delim && p > ret) p[-1] = 0;
135   else *p = 0;
136   return ret;
137 }
138
139 /* INTERNAL: Convert va_list to a single 'delim'-separated string, with an
140  * extra '\0' to terminate it
141  */
142 static char* msvcrt_valisttos(const char* arg0, va_list alist, char delim)
143 {
144   va_list alist2;
145   long size;
146   const char *arg;
147   char* p;
148   char *ret;
149
150 #ifdef HAVE_VA_COPY
151   va_copy(alist2,alist);
152 #else
153 # ifdef HAVE___VA_COPY
154   __va_copy(alist2,alist);
155 # else
156   alist2 = alist;
157 # endif
158 #endif
159
160   if (!arg0 && !delim)
161   {
162       /* Return NULL for an empty environment list */
163       return NULL;
164   }
165
166   /* get length */
167   arg = arg0;
168   size = 0;
169   do {
170       size += strlen(arg) + 1;
171       arg = va_arg(alist, char*);
172   } while (arg != NULL);
173
174   ret = (char*)MSVCRT_malloc(size + 1);
175   if (!ret)
176     return NULL;
177
178   /* fill string */
179   arg = arg0;
180   p = ret;
181   do {
182       int len = strlen(arg);
183       memcpy(p,arg,len);
184       p += len;
185       *p++ = delim;
186       arg = va_arg(alist2, char*);
187   } while (arg != NULL);
188   if (delim && p > ret) p[-1] = 0;
189   else *p = 0;
190   return ret;
191 }
192
193 /*********************************************************************
194  *              _cwait (MSVCRT.@)
195  */
196 int _cwait(int *status, int pid, int action)
197 {
198   HANDLE hPid = (HANDLE)pid;
199   int doserrno;
200
201   action = action; /* Remove warning */
202
203   if (!WaitForSingleObject(hPid, -1)) /* wait forever */
204   {
205     if (status)
206     {
207       DWORD stat;
208       GetExitCodeProcess(hPid, &stat);
209       *status = (int)stat;
210     }
211     return (int)pid;
212   }
213   doserrno = GetLastError();
214
215   if (doserrno == ERROR_INVALID_HANDLE)
216   {
217     *MSVCRT__errno() =  MSVCRT_ECHILD;
218     *__doserrno() = doserrno;
219   }
220   else
221     MSVCRT__set_errno(doserrno);
222
223   return status ? *status = -1 : -1;
224 }
225
226 /*********************************************************************
227  *              _execl (MSVCRT.@)
228  *
229  * Like on Windows, this function does not handle arguments with spaces
230  * or double-quotes.
231  */
232 int _execl(const char* name, const char* arg0, ...)
233 {
234   va_list ap;
235   char * args;
236   int ret;
237
238   va_start(ap, arg0);
239   args = msvcrt_valisttos(arg0, ap, ' ');
240   va_end(ap);
241
242   ret = msvcrt_spawn(_P_OVERLAY, name, args, NULL);
243   MSVCRT_free(args);
244
245   return ret;
246 }
247
248 /*********************************************************************
249  *              _execlp (MSVCRT.@)
250  *
251  * Like on Windows, this function does not handle arguments with spaces
252  * or double-quotes.
253  */
254 int _execlp(const char* name, const char* arg0, ...)
255 {
256   va_list ap;
257   char * args;
258   int ret;
259   char fullname[MAX_PATH];
260
261   _searchenv(name, "PATH", fullname);
262
263   va_start(ap, arg0);
264   args = msvcrt_valisttos(arg0, ap, ' ');
265   va_end(ap);
266
267   ret = msvcrt_spawn(_P_OVERLAY, fullname[0] ? fullname : name, args, NULL);
268   MSVCRT_free(args);
269
270   return ret;
271 }
272
273 /*********************************************************************
274  *              _execv (MSVCRT.@)
275  *
276  * Like on Windows, this function does not handle arguments with spaces
277  * or double-quotes.
278  */
279 int _execv(const char* name, char* const* argv)
280 {
281   return _spawnve(_P_OVERLAY, name, (const char* const*) argv, NULL);
282 }
283
284 /*********************************************************************
285  *              _execve (MSVCRT.@)
286  *
287  * Like on Windows, this function does not handle arguments with spaces
288  * or double-quotes.
289  */
290 int _execve(const char* name, char* const* argv, const char* const* envv)
291 {
292   return _spawnve(_P_OVERLAY, name, (const char* const*) argv, envv);
293 }
294
295 /*********************************************************************
296  *              _execvpe (MSVCRT.@)
297  *
298  * Like on Windows, this function does not handle arguments with spaces
299  * or double-quotes.
300  */
301 int _execvpe(const char* name, char* const* argv, const char* const* envv)
302 {
303   char fullname[MAX_PATH];
304
305   _searchenv(name, "PATH", fullname);
306   return _spawnve(_P_OVERLAY, fullname[0] ? fullname : name,
307                   (const char* const*) argv, envv);
308 }
309
310 /*********************************************************************
311  *              _execvp (MSVCRT.@)
312  *
313  * Like on Windows, this function does not handle arguments with spaces
314  * or double-quotes.
315  */
316 int _execvp(const char* name, char* const* argv)
317 {
318   return _execvpe(name, argv, NULL);
319 }
320
321 /*********************************************************************
322  *              _spawnl (MSVCRT.@)
323  *
324  * Like on Windows, this function does not handle arguments with spaces
325  * or double-quotes.
326  */
327 int _spawnl(int flags, const char* name, const char* arg0, ...)
328 {
329   va_list ap;
330   char * args;
331   int ret;
332
333   va_start(ap, arg0);
334   args = msvcrt_valisttos(arg0, ap, ' ');
335   va_end(ap);
336
337   ret = msvcrt_spawn(flags, name, args, NULL);
338   MSVCRT_free(args);
339
340   return ret;
341 }
342
343 /*********************************************************************
344  *              _spawnlp (MSVCRT.@)
345  *
346  * Like on Windows, this function does not handle arguments with spaces
347  * or double-quotes.
348  */
349 int _spawnlp(int flags, const char* name, const char* arg0, ...)
350 {
351   va_list ap;
352   char * args;
353   int ret;
354   char fullname[MAX_PATH];
355
356   _searchenv(name, "PATH", fullname);
357
358   va_start(ap, arg0);
359   args = msvcrt_valisttos(arg0, ap, ' ');
360   va_end(ap);
361
362   ret = msvcrt_spawn(flags, fullname[0] ? fullname : name, args, NULL);
363   MSVCRT_free(args);
364
365   return ret;
366 }
367
368 /*********************************************************************
369  *              _spawnve (MSVCRT.@)
370  *
371  * Like on Windows, this function does not handle arguments with spaces
372  * or double-quotes.
373  */
374 int _spawnve(int flags, const char* name, const char* const* argv,
375                             const char* const* envv)
376 {
377   char * args = msvcrt_argvtos(argv,' ');
378   char * envs = msvcrt_argvtos(envv,0);
379   const char *fullname = name;
380   int ret = -1;
381
382   FIXME(":not translating name %s to locate program\n",fullname);
383   TRACE(":call (%s), params (%s), env (%s)\n",name,args,envs?"Custom":"Null");
384
385   if (args)
386   {
387     ret = msvcrt_spawn(flags, fullname, args, envs);
388     MSVCRT_free(args);
389   }
390   if (envs)
391     MSVCRT_free(envs);
392
393   return ret;
394 }
395
396 /*********************************************************************
397  *              _spawnv (MSVCRT.@)
398  *
399  * Like on Windows, this function does not handle arguments with spaces
400  * or double-quotes.
401  */
402 int _spawnv(int flags, const char* name, const char* const* argv)
403 {
404   return _spawnve(flags, name, argv, NULL);
405 }
406
407 /*********************************************************************
408  *              _spawnvpe (MSVCRT.@)
409  *
410  * Like on Windows, this function does not handle arguments with spaces
411  * or double-quotes.
412  */
413 int _spawnvpe(int flags, const char* name, const char* const* argv,
414                             const char* const* envv)
415 {
416   char fullname[MAX_PATH];
417   _searchenv(name, "PATH", fullname);
418   return _spawnve(flags, fullname[0] ? fullname : name, argv, envv);
419 }
420
421 /*********************************************************************
422  *              _spawnvp (MSVCRT.@)
423  *
424  * Like on Windows, this function does not handle arguments with spaces
425  * or double-quotes.
426  */
427 int _spawnvp(int flags, const char* name, const char* const* argv)
428 {
429   return _spawnvpe(flags, name, argv, NULL);
430 }
431
432 /*********************************************************************
433  *              _popen (MSVCRT.@)
434  */
435 MSVCRT_FILE* _popen(const char* command, const char* mode)
436 {
437   FIXME("(command=%s, mode=%s): stub\n", debugstr_a(command), debugstr_a(mode));
438   return NULL;
439 }
440
441 /*********************************************************************
442  *              _wpopen (MSVCRT.@)
443  */
444 MSVCRT_FILE* _wpopen(const MSVCRT_wchar_t* command, const MSVCRT_wchar_t* mode)
445 {
446   FIXME("(command=%s, mode=%s): stub\n", debugstr_w(command), debugstr_w(mode));
447   return NULL;
448 }
449
450 /*********************************************************************
451  *              _pclose (MSVCRT.@)
452  */
453 int _pclose(MSVCRT_FILE* file)
454 {
455   FIXME("(file=%p): stub\n", file);
456   return 0;
457 }
458
459 /*********************************************************************
460  *              system (MSVCRT.@)
461  */
462 int MSVCRT_system(const char* cmd)
463 {
464     char* cmdcopy;
465     int res;
466
467     /* Make a writable copy for CreateProcess */
468     cmdcopy=_strdup(cmd);
469     /* FIXME: should probably launch cmd interpreter in COMSPEC */
470     res=msvcrt_spawn(_P_WAIT, NULL, cmdcopy, NULL);
471     MSVCRT_free(cmdcopy);
472     return res;
473 }
474
475 /*********************************************************************
476  *              _loaddll (MSVCRT.@)
477  */
478 int _loaddll(const char* dllname)
479 {
480   return (int)LoadLibraryA(dllname);
481 }
482
483 /*********************************************************************
484  *              _unloaddll (MSVCRT.@)
485  */
486 int _unloaddll(int dll)
487 {
488   if (FreeLibrary((HMODULE)dll))
489     return 0;
490   else
491   {
492     int err = GetLastError();
493     MSVCRT__set_errno(err);
494     return err;
495   }
496 }
497
498 /*********************************************************************
499  *              _getdllprocaddr (MSVCRT.@)
500  */
501 void *_getdllprocaddr(int dll, const char *name, int ordinal)
502 {
503     if (name)
504     {
505         if (ordinal != -1) return NULL;
506         return GetProcAddress( (HMODULE)dll, name );
507     }
508     if (HIWORD(ordinal)) return NULL;
509     return GetProcAddress( (HMODULE)dll, (LPCSTR)(ULONG_PTR)ordinal );
510 }