/* system.c (emx+gcc) -- Copyright (c) 1990-1993 by Eberhard Mattes */

#include <stdlib.h>
#include <string.h>
#include <process.h>
#include <io.h>
#include <errno.h>

int system (const char *name)
{
  int rc;
  const char *sh, *base, *opt;

  sh = getenv ("EMXSHELL");
  if (sh == NULL)
    sh = getenv ("COMSPEC");
  if (sh == NULL)
    {
      errno = ENOENT;
      return (-1);
    }
  if (name == NULL)   /* Check for command interpreter */
    return (access (sh, 0) == 0);
  if (*name == 0)
    rc = spawnlp (P_WAIT, sh, sh, NULL);
  else
    {
      base = _getname (sh);
      if (stricmp (base, "cmd.exe") == 0 || stricmp (base, "4os2.exe") == 0
          || stricmp (base, "command.com") == 0
          || stricmp (base, "4dos.com") == 0)
        opt = "/c";
      else
        opt = "-c";
      rc = spawnlp (P_WAIT, sh, sh, opt, name, NULL);
    }
  return (rc);
}
