process.c 15.9 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * msvcrt.dll spawn/exec functions
 *
 * Copyright 1996,1998 Marcus Meissner
 * Copyright 1996 Jukka Iivonen
 * Copyright 1997,2000 Uwe Bonnes
 * Copyright 2000 Jon Griffiths
 *
9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
23 24 25 26 27
 * FIXME:
 * -File handles need some special handling. Sometimes children get
 *  open file handles, sometimes not. The docs are confusing
 * -No check for maximum path/argument/environment size is done
 */
28 29 30 31
#include "config.h"

#include <stdarg.h>

32
#include "msvcrt.h"
33 34 35
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
36 37

/* INTERNAL: Spawn a child process */
38
static int msvcrt_spawn(int flags, const char* exe, char* cmdline, char* env)
39 40 41 42 43 44 45
{
  STARTUPINFOA si;
  PROCESS_INFORMATION pi;

  if (sizeof(HANDLE) != sizeof(int))
    WARN("This call is unsuitable for your architecture\n");

46
  if ((unsigned)flags > MSVCRT__P_DETACH)
47
  {
48
    *MSVCRT__errno() = MSVCRT_EINVAL;
49 50 51 52 53
    return -1;
  }

  memset(&si, 0, sizeof(si));
  si.cb = sizeof(si);
54
  msvcrt_create_io_inherit_block(&si);
55
  if (!CreateProcessA(exe, cmdline, NULL, NULL, TRUE,
56
                     flags == MSVCRT__P_DETACH ? DETACHED_PROCESS : 0,
57 58
                     env, NULL, &si, &pi))
  {
59
    msvcrt_set_errno(GetLastError());
60
    MSVCRT_free(si.lpReserved2);
61 62 63
    return -1;
  }

64
  MSVCRT_free(si.lpReserved2);
65 66
  switch(flags)
  {
67
  case MSVCRT__P_WAIT:
68
    WaitForSingleObject(pi.hProcess, INFINITE);
69 70 71 72
    GetExitCodeProcess(pi.hProcess,&pi.dwProcessId);
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
    return (int)pi.dwProcessId;
73
  case MSVCRT__P_DETACH:
74 75 76
    CloseHandle(pi.hProcess);
    pi.hProcess = 0;
    /* fall through */
77 78
  case MSVCRT__P_NOWAIT:
  case MSVCRT__P_NOWAITO:
79 80
    CloseHandle(pi.hThread);
    return (int)pi.hProcess;
81
  case  MSVCRT__P_OVERLAY:
82 83 84 85 86
    MSVCRT__exit(0);
  }
  return -1; /* can't reach here */
}

87 88 89
/* INTERNAL: Convert argv list to a single 'delim'-separated string, with an
 * extra '\0' to terminate it
 */
90
static char* msvcrt_argvtos(const char* const* arg, char delim)
91
{
92 93 94 95
  const char* const* a;
  long size;
  char* p;
  char* ret;
96 97

  if (!arg && !delim)
98 99 100 101
  {
      /* Return NULL for an empty environment list */
      return NULL;
  }
102 103

  /* get length */
104 105 106
  a = arg;
  size = 0;
  while (*a)
107
  {
108 109
    size += strlen(*a) + 1;
    a++;
110 111
  }

112 113
  ret = (char*)MSVCRT_malloc(size + 1);
  if (!ret)
114 115 116
    return NULL;

  /* fill string */
117 118 119
  a = arg;
  p = ret;
  while (*a)
120
  {
121
    int len = strlen(*a);
122
    memcpy(p,*a,len);
123 124 125
    p += len;
    *p++ = delim;
    a++;
126
  }
127 128
  if (delim && p > ret) p[-1] = 0;
  else *p = 0;
129 130 131
  return ret;
}

132 133 134 135
/* INTERNAL: Convert va_list to a single 'delim'-separated string, with an
 * extra '\0' to terminate it
 */
static char* msvcrt_valisttos(const char* arg0, va_list alist, char delim)
136
{
137
  va_list alist2;
138
  long size;
139 140
  const char *arg;
  char* p;
141 142
  char *ret;

143
#ifdef HAVE_VA_COPY
144 145
  va_copy(alist2,alist);
#else
146
# ifdef HAVE___VA_COPY
147 148 149 150 151 152
  __va_copy(alist2,alist);
# else
  alist2 = alist;
# endif
#endif

153 154
  if (!arg0 && !delim)
  {
155 156
      /* Return NULL for an empty environment list */
      return NULL;
157 158
  }

159 160 161 162 163 164 165 166 167 168
  /* get length */
  arg = arg0;
  size = 0;
  do {
      size += strlen(arg) + 1;
      arg = va_arg(alist, char*);
  } while (arg != NULL);

  ret = (char*)MSVCRT_malloc(size + 1);
  if (!ret)
169 170 171
    return NULL;

  /* fill string */
172 173 174 175 176 177 178 179 180
  arg = arg0;
  p = ret;
  do {
      int len = strlen(arg);
      memcpy(p,arg,len);
      p += len;
      *p++ = delim;
      arg = va_arg(alist2, char*);
  } while (arg != NULL);
181 182
  if (delim && p > ret) p[-1] = 0;
  else *p = 0;
183 184 185
  return ret;
}

186 187 188
/*********************************************************************
 *		_cwait (MSVCRT.@)
 */
189
int _cwait(int *status, int pid, int action)
190 191 192 193 194 195
{
  HANDLE hPid = (HANDLE)pid;
  int doserrno;

  action = action; /* Remove warning */

196
  if (!WaitForSingleObject(hPid, INFINITE))
197 198 199 200 201 202 203 204 205 206 207 208 209
  {
    if (status)
    {
      DWORD stat;
      GetExitCodeProcess(hPid, &stat);
      *status = (int)stat;
    }
    return (int)pid;
  }
  doserrno = GetLastError();

  if (doserrno == ERROR_INVALID_HANDLE)
  {
210
    *MSVCRT__errno() =  MSVCRT_ECHILD;
211
    *MSVCRT___doserrno() = doserrno;
212 213
  }
  else
214
    msvcrt_set_errno(doserrno);
215 216 217 218

  return status ? *status = -1 : -1;
}

219 220
/*********************************************************************
 *		_execl (MSVCRT.@)
221
 *
222
 * Like on Windows, this function does not handle arguments with spaces
223
 * or double-quotes.
224 225 226 227 228 229 230 231 232 233 234
 */
int _execl(const char* name, const char* arg0, ...)
{
  va_list ap;
  char * args;
  int ret;

  va_start(ap, arg0);
  args = msvcrt_valisttos(arg0, ap, ' ');
  va_end(ap);

235
  ret = msvcrt_spawn(MSVCRT__P_OVERLAY, name, args, NULL);
236 237 238 239 240
  MSVCRT_free(args);

  return ret;
}

241 242 243 244 245 246 247 248 249
/*********************************************************************
 *		_execle (MSVCRT.@)
 */
int _execle(const char* name, const char* arg0, ...)
{
    FIXME("stub\n");
    return -1;
}

250 251
/*********************************************************************
 *		_execlp (MSVCRT.@)
252
 *
253
 * Like on Windows, this function does not handle arguments with spaces
254
 * or double-quotes.
255 256 257 258 259 260 261 262 263 264 265 266 267 268
 */
int _execlp(const char* name, const char* arg0, ...)
{
  va_list ap;
  char * args;
  int ret;
  char fullname[MAX_PATH];

  _searchenv(name, "PATH", fullname);

  va_start(ap, arg0);
  args = msvcrt_valisttos(arg0, ap, ' ');
  va_end(ap);

269
  ret = msvcrt_spawn(MSVCRT__P_OVERLAY, fullname[0] ? fullname : name, args, NULL);
270 271 272 273 274
  MSVCRT_free(args);

  return ret;
}

275 276 277 278 279 280 281 282 283
/*********************************************************************
 *		_execlpe (MSVCRT.@)
 */
int _execlpe(const char* name, const char* arg0, ...)
{
    FIXME("stub\n");
    return -1;
}

284 285
/*********************************************************************
 *		_execv (MSVCRT.@)
286
 *
287
 * Like on Windows, this function does not handle arguments with spaces
288
 * or double-quotes.
289 290 291
 */
int _execv(const char* name, char* const* argv)
{
292
  return _spawnve(MSVCRT__P_OVERLAY, name, (const char* const*) argv, NULL);
293 294 295 296
}

/*********************************************************************
 *		_execve (MSVCRT.@)
297
 *
298
 * Like on Windows, this function does not handle arguments with spaces
299
 * or double-quotes.
300 301 302
 */
int _execve(const char* name, char* const* argv, const char* const* envv)
{
303
  return _spawnve(MSVCRT__P_OVERLAY, name, (const char* const*) argv, envv);
304 305 306 307
}

/*********************************************************************
 *		_execvpe (MSVCRT.@)
308
 *
309
 * Like on Windows, this function does not handle arguments with spaces
310
 * or double-quotes.
311 312 313 314 315 316
 */
int _execvpe(const char* name, char* const* argv, const char* const* envv)
{
  char fullname[MAX_PATH];

  _searchenv(name, "PATH", fullname);
317
  return _spawnve(MSVCRT__P_OVERLAY, fullname[0] ? fullname : name,
318 319 320 321 322
                  (const char* const*) argv, envv);
}

/*********************************************************************
 *		_execvp (MSVCRT.@)
323
 *
324
 * Like on Windows, this function does not handle arguments with spaces
325
 * or double-quotes.
326 327 328 329 330 331
 */
int _execvp(const char* name, char* const* argv)
{
  return _execvpe(name, argv, NULL);
}

332 333
/*********************************************************************
 *		_spawnl (MSVCRT.@)
334
 *
335
 * Like on Windows, this function does not handle arguments with spaces
336
 * or double-quotes.
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
 */
int _spawnl(int flags, const char* name, const char* arg0, ...)
{
  va_list ap;
  char * args;
  int ret;

  va_start(ap, arg0);
  args = msvcrt_valisttos(arg0, ap, ' ');
  va_end(ap);

  ret = msvcrt_spawn(flags, name, args, NULL);
  MSVCRT_free(args);

  return ret;
}

354 355 356 357 358
/*********************************************************************
 *		_spawnle (MSVCRT.@)
 */
int _spawnle(int flags, const char* name, const char* arg0, ...)
{
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    va_list ap;
    char *args, *envs = NULL;
    const char * const *envp;
    int ret;

    va_start(ap, arg0);
    args = msvcrt_valisttos(arg0, ap, ' ');
    va_end(ap);

    va_start(ap, arg0);
    while (va_arg( ap, char * ) != NULL) /*nothing*/;
    envp = va_arg( ap, const char * const * );
    if (envp) envs = msvcrt_argvtos(envp, 0);
    va_end(ap);

    ret = msvcrt_spawn(flags, name, args, envs);

    MSVCRT_free(args);
    if (envs) MSVCRT_free(envs);
    return ret;
379 380 381
}


382
/*********************************************************************
383
 *		_spawnlp (MSVCRT.@)
384
 *
385
 * Like on Windows, this function does not handle arguments with spaces
386
 * or double-quotes.
387 388 389 390 391 392 393 394 395 396 397 398 399 400
 */
int _spawnlp(int flags, const char* name, const char* arg0, ...)
{
  va_list ap;
  char * args;
  int ret;
  char fullname[MAX_PATH];

  _searchenv(name, "PATH", fullname);

  va_start(ap, arg0);
  args = msvcrt_valisttos(arg0, ap, ' ');
  va_end(ap);

401
  ret = msvcrt_spawn(flags, fullname[0] ? fullname : name, args, NULL);
402 403 404 405 406
  MSVCRT_free(args);

  return ret;
}

407 408 409 410 411
/*********************************************************************
 *		_spawnlpe (MSVCRT.@)
 */
int _spawnlpe(int flags, const char* name, const char* arg0, ...)
{
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    va_list ap;
    char *args, *envs = NULL;
    const char * const *envp;
    int ret;
    char fullname[MAX_PATH];

    _searchenv(name, "PATH", fullname);

    va_start(ap, arg0);
    args = msvcrt_valisttos(arg0, ap, ' ');
    va_end(ap);

    va_start(ap, arg0);
    while (va_arg( ap, char * ) != NULL) /*nothing*/;
    envp = va_arg( ap, const char * const * );
    if (envp) envs = msvcrt_argvtos(envp, 0);
    va_end(ap);

    ret = msvcrt_spawn(flags, fullname[0] ? fullname : name, args, envs);

    MSVCRT_free(args);
    if (envs) MSVCRT_free(envs);
    return ret;
435 436
}

437 438
/*********************************************************************
 *		_spawnve (MSVCRT.@)
439
 *
440
 * Like on Windows, this function does not handle arguments with spaces
441
 * or double-quotes.
442
 */
443 444
int _spawnve(int flags, const char* name, const char* const* argv,
                            const char* const* envv)
445
{
446 447
  char * args = msvcrt_argvtos(argv,' ');
  char * envs = msvcrt_argvtos(envv,0);
448
  const char *fullname = name;
449 450 451
  int ret = -1;

  FIXME(":not translating name %s to locate program\n",fullname);
452 453
  TRACE(":call (%s), params (%s), env (%s)\n",debugstr_a(name),debugstr_a(args),
   envs?"Custom":"Null");
454 455 456

  if (args)
  {
457
    ret = msvcrt_spawn(flags, fullname, args, envs);
458 459 460 461 462 463 464 465 466 467
    MSVCRT_free(args);
  }
  if (envs)
    MSVCRT_free(envs);

  return ret;
}

/*********************************************************************
 *		_spawnv (MSVCRT.@)
468
 *
469
 * Like on Windows, this function does not handle arguments with spaces
470
 * or double-quotes.
471
 */
472
int _spawnv(int flags, const char* name, const char* const* argv)
473
{
474
  return _spawnve(flags, name, argv, NULL);
475 476 477 478
}

/*********************************************************************
 *		_spawnvpe (MSVCRT.@)
479
 *
480
 * Like on Windows, this function does not handle arguments with spaces
481
 * or double-quotes.
482
 */
483 484
int _spawnvpe(int flags, const char* name, const char* const* argv,
                            const char* const* envv)
485 486
{
  char fullname[MAX_PATH];
487 488
  _searchenv(name, "PATH", fullname);
  return _spawnve(flags, fullname[0] ? fullname : name, argv, envv);
489 490 491 492
}

/*********************************************************************
 *		_spawnvp (MSVCRT.@)
493
 *
494
 * Like on Windows, this function does not handle arguments with spaces
495
 * or double-quotes.
496
 */
497
int _spawnvp(int flags, const char* name, const char* const* argv)
498
{
499
  return _spawnvpe(flags, name, argv, NULL);
500 501
}

502 503
/*********************************************************************
 *		_popen (MSVCRT.@)
504 505 506
 * FIXME: convert to _wpopen and call that from here instead?  But it
 * would have to convert the command back to ANSI to call msvcrt_spawn,
 * less than ideal.
507
 */
508
MSVCRT_FILE* MSVCRT__popen(const char* command, const char* mode)
509
{
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
  static const char wcmd[] = "wcmd", cmdFlag[] = " /C ", comSpec[] = "COMSPEC";
  MSVCRT_FILE *ret;
  BOOL readPipe = TRUE;
  int textmode, fds[2], fdToDup, fdToOpen, fdStdHandle = -1, fdStdErr = -1;
  const char *p;
  char *cmdcopy;
  DWORD comSpecLen;

  TRACE("(command=%s, mode=%s)\n", debugstr_a(command), debugstr_a(mode));

  if (!command || !mode)
    return NULL;

  textmode = *__p__fmode() & (MSVCRT__O_BINARY | MSVCRT__O_TEXT);
  for (p = mode; *p; p++)
  {
    switch (*p)
    {
      case 'W':
      case 'w':
        readPipe = FALSE;
        break;
      case 'B':
      case 'b':
        textmode |= MSVCRT__O_BINARY;
        textmode &= ~MSVCRT__O_TEXT;
        break;
      case 'T':
      case 't':
        textmode |= MSVCRT__O_TEXT;
        textmode &= ~MSVCRT__O_BINARY;
        break;
    }
  }
  textmode |= MSVCRT__O_NOINHERIT;
  if (_pipe(fds, 0, textmode) == -1)
    return NULL;

  fdToDup = readPipe ? 1 : 0;
  fdToOpen = readPipe ? 0 : 1;

  if ((fdStdHandle = _dup(fdToDup)) == -1)
    goto error;
  if (_dup2(fds[fdToDup], fdToDup) != 0)
    goto error;
  if (readPipe)
  {
    if ((fdStdErr = _dup(MSVCRT_STDERR_FILENO)) == -1)
      goto error;
    if (_dup2(fds[fdToDup], MSVCRT_STDERR_FILENO) != 0)
      goto error;
  }

  _close(fds[fdToDup]);

  comSpecLen = GetEnvironmentVariableA(comSpec, NULL, 0);
  if (!comSpecLen)
    comSpecLen = strlen(wcmd) + 1;
568
  cmdcopy = HeapAlloc(GetProcessHeap(), 0, comSpecLen + strlen(cmdFlag)
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
   + strlen(command));
  if (!GetEnvironmentVariableA(comSpec, cmdcopy, comSpecLen))
    strcpy(cmdcopy, wcmd);
  strcat(cmdcopy, cmdFlag);
  strcat(cmdcopy, command);
  if (msvcrt_spawn(MSVCRT__P_NOWAIT, NULL, cmdcopy, NULL) == -1)
  {
    _close(fds[fdToOpen]);
    ret = NULL;
  }
  else
  {
    ret = MSVCRT__fdopen(fds[fdToOpen], mode);
    if (!ret)
      _close(fds[fdToOpen]);
  }
  HeapFree(GetProcessHeap(), 0, cmdcopy);
  _dup2(fdStdHandle, fdToDup);
  _close(fdStdHandle);
  if (readPipe)
  {
    _dup2(fdStdErr, MSVCRT_STDERR_FILENO);
    _close(fdStdErr);
  }
  return ret;

error:
  if (fdStdHandle != -1) _close(fdStdHandle);
  if (fdStdErr != -1)    _close(fdStdErr);
  _close(fds[0]);
  _close(fds[1]);
600 601 602 603 604 605
  return NULL;
}

/*********************************************************************
 *		_wpopen (MSVCRT.@)
 */
606
MSVCRT_FILE* MSVCRT__wpopen(const MSVCRT_wchar_t* command, const MSVCRT_wchar_t* mode)
607 608 609 610 611 612 613 614
{
  FIXME("(command=%s, mode=%s): stub\n", debugstr_w(command), debugstr_w(mode));
  return NULL;
}

/*********************************************************************
 *		_pclose (MSVCRT.@)
 */
615
int MSVCRT__pclose(MSVCRT_FILE* file)
616
{
617
  return MSVCRT_fclose(file);
618 619
}

620 621 622
/*********************************************************************
 *		system (MSVCRT.@)
 */
623
int MSVCRT_system(const char* cmd)
624
{
625 626 627 628 629 630
    char* cmdcopy;
    int res;

    /* Make a writable copy for CreateProcess */
    cmdcopy=_strdup(cmd);
    /* FIXME: should probably launch cmd interpreter in COMSPEC */
631
    res=msvcrt_spawn(MSVCRT__P_WAIT, NULL, cmdcopy, NULL);
632 633
    MSVCRT_free(cmdcopy);
    return res;
634 635 636 637 638
}

/*********************************************************************
 *		_loaddll (MSVCRT.@)
 */
639
int _loaddll(const char* dllname)
640
{
641
  return (int)LoadLibraryA(dllname);
642 643 644 645 646
}

/*********************************************************************
 *		_unloaddll (MSVCRT.@)
 */
647
int _unloaddll(int dll)
648
{
649
  if (FreeLibrary((HMODULE)dll))
650 651 652 653
    return 0;
  else
  {
    int err = GetLastError();
654
    msvcrt_set_errno(err);
655 656 657
    return err;
  }
}
658 659 660 661 662 663 664 665 666 667 668 669 670 671

/*********************************************************************
 *		_getdllprocaddr (MSVCRT.@)
 */
void *_getdllprocaddr(int dll, const char *name, int ordinal)
{
    if (name)
    {
        if (ordinal != -1) return NULL;
        return GetProcAddress( (HMODULE)dll, name );
    }
    if (HIWORD(ordinal)) return NULL;
    return GetProcAddress( (HMODULE)dll, (LPCSTR)(ULONG_PTR)ordinal );
}