update for HEAD-2003021201
[reactos.git] / lib / crtdll / process / spawnve.c
index 1b698b9..5e2c193 100644 (file)
@@ -3,15 +3,13 @@
 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
 
 #include <windows.h>
-
-#include <crtdll/stdio.h>
-#include <crtdll/stdlib.h>
-#include <crtdll/string.h>
-#include <crtdll/errno.h>
-//#include <crtdll/limits.h>
-#include <crtdll/process.h>
-#include <crtdll/ctype.h>
-#include <crtdll/io.h>
+#include <msvcrt/stdio.h>
+#include <msvcrt/stdlib.h>
+#include <msvcrt/string.h>
+#include <msvcrt/errno.h>
+#include <msvcrt/process.h>
+#include <msvcrt/ctype.h>
+#include <msvcrt/io.h>
 
 
 #ifndef F_OK
 int _fileinfo_dll = 0;
 
 static int
-direct_exec_tail(const char *program, const char *args,
-                char * const envp[],
-               PROCESS_INFORMATION *ProcessInformation)
+direct_exec_tail(const char* program, const char* args,
+                 char* const envp[],
+                 PROCESS_INFORMATION* ProcessInformation)
 {
-
-       static STARTUPINFO StartupInfo;
-
-       StartupInfo.cb = sizeof(STARTUPINFO);
-       StartupInfo.lpReserved= NULL;
-       StartupInfo.dwFlags = 0;
-       StartupInfo.wShowWindow = SW_SHOWDEFAULT; 
-       StartupInfo.lpReserved2 = NULL;
-       StartupInfo.cbReserved2 = 0; 
-
-
-       if (! CreateProcessA((char *)program,(char *)args,NULL,NULL,FALSE,0,(char **)envp,NULL,&StartupInfo,ProcessInformation) ) 
-       {
-         __set_errno( GetLastError() );
-          return -1;
-       }
-
-       return (int)ProcessInformation->hProcess;
+    static STARTUPINFO StartupInfo;
+
+    StartupInfo.cb = sizeof(STARTUPINFO);
+    StartupInfo.lpReserved= NULL;
+    StartupInfo.dwFlags = 0;
+    StartupInfo.wShowWindow = SW_SHOWDEFAULT; 
+    StartupInfo.lpReserved2 = NULL;
+    StartupInfo.cbReserved2 = 0; 
+    if (!CreateProcessA((char*)program,(char*)args,NULL,NULL,FALSE,0,(char**)envp,NULL,&StartupInfo,ProcessInformation)) {
+        __set_errno( GetLastError() );
+        return -1;
+    }
+    return (int)ProcessInformation->hProcess;
 }
 
-static int vdm_exec(const char *program, char **argv, char **envp,
-       PROCESS_INFORMATION *ProcessInformation)
+static int vdm_exec(const char* program, char** argv, char** envp,
+                    PROCESS_INFORMATION* ProcessInformation)
 {
-       static char args[1024];
-       int i = 0;
-       args[0] = 0;
-
-       strcpy(args,"vdm.exe ");
-       while(argv[i] != NULL ) {
-        strcat(args,argv[i]);
-        strcat(args," ");
+    static char args[1024];
+    int i = 0;
+    args[0] = 0;
+
+    strcpy(args, "vdm.exe ");
+    while (argv[i] != NULL) {
+        strcat(args, argv[i]);
+        strcat(args, " ");
         i++; 
-       }
-  
-       return direct_exec_tail(program,args,envp,ProcessInformation);
+    }
+    return direct_exec_tail(program,args,envp,ProcessInformation);
 }
 
-static int go32_exec(const char *program, char **argv, char **envp,
-       PROCESS_INFORMATION *ProcessInformation)
+static int go32_exec(const char* program, char** argv, char** envp,
+                     PROCESS_INFORMATION* ProcessInformation)
 {
-
-
-       static char args[1024];
-       static char envblock[2048];
-       char * penvblock;
-       int i = 0;
-
-
-       envblock[0] = 0;
-       penvblock=envblock;
-
-       while(envp[i] != NULL ) {
+    static char args[1024];
+    static char envblock[2048];
+    char* penvblock;
+    int i = 0;
+
+    envblock[0] = 0;
+    penvblock=envblock;
+    while(envp[i] != NULL ) {
           strcat(penvblock,envp[i]);
-         penvblock+=strlen(envp[i])+1;
+          penvblock+=strlen(envp[i])+1;
           i++; 
-       }
-       penvblock[0]=0;
-
-       args[0] = 0;
-        i = 0;
-       while(argv[i] != NULL ) {
+    }
+    penvblock[0]=0;
+    args[0] = 0;
+    i = 0;
+    while(argv[i] != NULL ) {
           strcat(args,argv[i]);
           strcat(args," ");
           i++; 
-       }
-  
-       return direct_exec_tail(program,args,envp,ProcessInformation);
+    }
+    return direct_exec_tail(program,args,envp,ProcessInformation);
 }
 
-int
-command_exec(const char *program, char **argv, char **envp,
-       PROCESS_INFORMATION *ProcessInformation)
+int command_exec(const char* program, char** argv, char** envp,
+                 PROCESS_INFORMATION* ProcessInformation)
 {
-       static char args[1024];
-       int i = 0;
-
-
+    static char args[1024];
+    int i = 0;
 
-       args[0] = 0;
-
-       strcpy(args,"cmd.exe  /c ");
-       while(argv[i] != NULL ) {
+    args[0] = 0;
+    strcpy(args,"cmd.exe  /c ");
+    while(argv[i] != NULL ) {
         strcat(args,argv[i]);
         strcat(args," ");
         i++; 
-       }
-  
-       return direct_exec_tail(program,args,envp,ProcessInformation);
-
+    }
+    return direct_exec_tail(program,args,envp,ProcessInformation);
 }
 
-static int script_exec(const char *program, char **argv, char **envp,
-       PROCESS_INFORMATION *ProcessInformation)
+static int script_exec(const char* program, char** argv, char** envp,
+                       PROCESS_INFORMATION* ProcessInformation)
 {
-       return 0;
+    return 0;
 }
 
 
@@ -141,23 +121,22 @@ static int script_exec(const char *program, char **argv, char **envp,
    those extensions that can be *omitted* when you invoke the
    executable from one of the shells used on MSDOS.  */
 static struct {
-  const char *extension;
-  int (*interp)(const char *, char **, char **,
-       PROCESS_INFORMATION *);
+    const char* extension;
+    int (*interp)(const char*, char**, char**, PROCESS_INFORMATION*);
 } interpreters[] = {
-       { ".com", vdm_exec },
-       { ".exe", go32_exec },
-       { ".dll", go32_exec },
-       { ".cmd", command_exec },
-       { ".bat", command_exec },
-       { ".btm", command_exec },
-       { ".sh",  script_exec },  /* for compatibility with ms_sh */
-       { ".ksh", script_exec },
-       { ".pl", script_exec },   /* Perl */
-       { ".sed", script_exec },
-       { "",     go32_exec },
-       { 0,      script_exec },  /* every extension not mentioned above calls it */
-       { 0,      0 },
+    { ".com", vdm_exec },
+    { ".exe", go32_exec },
+    { ".dll", go32_exec },
+    { ".cmd", command_exec },
+    { ".bat", command_exec },
+    { ".btm", command_exec },
+    { ".sh",  script_exec },   /* for compatibility with ms_sh */
+    { ".ksh", script_exec },
+    { ".pl",  script_exec },   /* Perl */
+    { ".sed", script_exec },
+    { "",     go32_exec },
+    { 0,      script_exec },   /* every extension not mentioned above calls it */
+    { 0,      0 },
 };
 
 /* This is the index into the above array of the interpreter
@@ -166,173 +145,143 @@ static struct {
 
 /*-------------------------------------------------*/
 
-
-
-
-int _spawnve(int mode, const char *path, char *const argv[], char *const envp[])
+int _spawnve(int mode, const char* path, char* const argv[], char* const envp[])
 {
-  /* This is the one that does the work! */
-  PROCESS_INFORMATION ProcessInformation;
-  union { char *const *x; char **p; } u;
-  int i = -1;
-  char **argvp;
-  char **envpp;
-  char rpath[FILENAME_MAX], *rp, *rd=0;
-  int e = errno;
-  int is_dir = 0;
-  int found = 0;
-  DWORD ExitCode;
-
-  if (path == 0 || argv[0] == 0)
-  {
-    errno = EINVAL;
-    return -1;
-  }
-  if (strlen(path) > FILENAME_MAX - 1)
-  {
-    errno = ENAMETOOLONG;
-    return -1;
-  }
-
-  u.x = argv; argvp = u.p;
-  u.x = envp; envpp = u.p;
-
-  fflush(stdout); /* just in case */
-  for (rp=rpath; *path; *rp++ = *path++)
-  {
+    /* This is the one that does the work! */
+    PROCESS_INFORMATION ProcessInformation;
+    union { char* const* x; char** p; } u;
+    int i = -1;
+    char** argvp;
+    char** envpp;
+    char rpath[FILENAME_MAX], *rp, *rd = 0;
+    int e = errno;
+    int is_dir = 0;
+    int found = 0;
+    DWORD ExitCode;
+
+    if (path == 0 || argv[0] == 0) {
+        errno = EINVAL;
+        return -1;
+    }
+    if (strlen(path) > FILENAME_MAX - 1) {
+        errno = ENAMETOOLONG;
+        return -1;
+    }
+    u.x = argv; argvp = u.p;
+    u.x = envp; envpp = u.p;
+    fflush(stdout); /* just in case */
+    for (rp=rpath; *path; *rp++ = *path++) {
     if (*path == '.')
-      rd = rp;
-    if (*path == '\\' || *path == '/')
-      rd = 0;
-  }
-  *rp = 0;
-
-  /* If LFN is supported on the volume where rpath resides, we
-     might have something like foo.bar.exe or even foo.exe.com.
-     If so, look for RPATH.ext before even trying RPATH itself. */
-  if (!rd)
-  {
-    for (i=0; interpreters[i].extension; i++)
-    {
-      strcpy(rp, interpreters[i].extension);
-      if (_access(rpath, F_OK) == 0 && !(is_dir = (_access(rpath, D_OK) == 0)))
-      {
-       found = 1;
-       break;
-      }
+        rd = rp;
+        if (*path == '\\' || *path == '/')
+            rd = 0;
+    }
+    *rp = 0;
+
+    /* If LFN is supported on the volume where rpath resides, we
+       might have something like foo.bar.exe or even foo.exe.com.
+       If so, look for RPATH.ext before even trying RPATH itself. */
+    if (!rd) {
+        for (i=0; interpreters[i].extension; i++) {
+            strcpy(rp, interpreters[i].extension);
+            if (_access(rpath, F_OK) == 0 && !(is_dir = (_access(rpath, D_OK) == 0))) {
+                found = 1;
+                break;
+            }
+        }
     }
-  }
 
-  if (!found)
-  {
-    const char *rpath_ext;
+    if (!found) {
+        const char *rpath_ext;
 
-    if (rd)
-    {
-      i = 0;
-      rpath_ext = rd;
+        if (rd) {
+            i = 0;
+            rpath_ext = rd;
+        } else {
+            i = INTERP_NO_EXT;
+            rpath_ext = "";
+        }
+        for ( ; interpreters[i].extension; i++)
+            if (_stricmp(rpath_ext, interpreters[i].extension) == 0
+                && _access(rpath, F_OK) == 0
+                && !(is_dir = (_access(rpath, D_OK) == 0)))
+            {
+                found = 1;
+                break;
+            }
+    }
+    if (!found) {
+        errno = is_dir ? EISDIR : ENOENT;
+        return -1;
     }
-    else
-    {
-      i = INTERP_NO_EXT;
-      rpath_ext = "";
+    errno = e;
+    i = interpreters[i].interp(rpath, argvp, envpp, &ProcessInformation);
+    if (mode == P_OVERLAY)
+        exit(i);
+    if (mode == P_WAIT) {
+        WaitForSingleObject(ProcessInformation.hProcess,INFINITE);
+        GetExitCodeProcess(ProcessInformation.hProcess,&ExitCode);
+        i = (int)ExitCode;
     }
-    for ( ; interpreters[i].extension; i++)
-      if (_stricmp(rpath_ext, interpreters[i].extension) == 0
-         && _access(rpath, F_OK) == 0
-         && !(is_dir = (_access(rpath, D_OK) == 0)))
-      {
-       found = 1;
-        break;
-      }
-  }
-  if (!found)
-  {
-    errno = is_dir ? EISDIR : ENOENT;
-    return -1;
-  }
-  errno = e;
-  i = interpreters[i].interp(rpath, argvp, envpp, &ProcessInformation);
-  if (mode == P_OVERLAY)
-    exit(i);
-  if (mode == P_WAIT)
-  {
-    WaitForSingleObject(ProcessInformation.hProcess,INFINITE);
-    GetExitCodeProcess(ProcessInformation.hProcess,&ExitCode);
-    i = (int)ExitCode;
-  }
-  return i;
+    return i;
 }
 
-
-
-
-const char * find_exec(char * path,char *rpath)
+const char* find_exec(char* path, char* rpath)
 {
- char *rp, *rd=0;
- int i;
- int is_dir = 0;
- int found = 0;
-  if (path == 0 )
-    return 0;
-  if (strlen(path) > FILENAME_MAX - 1)
-    return path;
-
-  /* copy path in rpath */
-  for (rd=path,rp=rpath; *rd; *rp++ = *rd++)
+    char *rp, *rd=0;
+    int i;
+    int is_dir = 0;
+    int found = 0;
+
+    if (path == 0 )
+        return 0;
+    if (strlen(path) > FILENAME_MAX - 1)
+        return path;
+
+    /* copy path in rpath */
+    for (rd=path,rp=rpath; *rd; *rp++ = *rd++)
     ;
-  *rp = 0;
-  /* try first with the name as is */
-  for (i=0; interpreters[i].extension; i++)
-  {
-    strcpy(rp, interpreters[i].extension);
-    if (_access(rpath, F_OK) == 0 && !(is_dir = (_access(rpath, D_OK) == 0)))
-    {
-       found = 1;
-       break;
+    *rp = 0;
+    /* try first with the name as is */
+    for (i=0; interpreters[i].extension; i++) {
+        strcpy(rp, interpreters[i].extension);
+        if (_access(rpath, F_OK) == 0 && !(is_dir = (_access(rpath, D_OK) == 0))) {
+            found = 1;
+            break;
+        }
     }
-  }
-
-  if (!found)
-  {
-    /* search in the PATH */
-    char winpath[MAX_PATH];
-    if( GetEnvironmentVariableA("PATH",winpath,MAX_PATH))
-    {
-     char *ep=winpath;
-      while( *ep)
-      {
-        if(*ep == ';') ep++;
-        rp=rpath;
-        for ( ; *ep && (*ep != ';') ; *rp++ = *ep++)
-         ;
-        *rp++='/';
-        for (rd=path ; *rd ; *rp++ = *rd++)
-         ;
-
-        for (i=0; interpreters[i].extension; i++)
-        {
-          strcpy(rp, interpreters[i].extension);
-          if (_access(rpath, F_OK) == 0 && !(is_dir = (_access(rpath, D_OK) == 0)))
-          {
-           found = 1;
-           break;
-          }
+    if (!found) {
+        /* search in the PATH */
+        char winpath[MAX_PATH];
+        if (GetEnvironmentVariableA("PATH", winpath, MAX_PATH)) {
+            char* ep = winpath;
+            while (*ep) {
+                if (*ep == ';') ep++;
+                rp = rpath;
+                for ( ; *ep && (*ep != ';') ; *rp++ = *ep++)
+                ;
+                *rp++ = '/';
+                for (rd = path ; *rd ; *rp++ = *rd++)
+                ;
+                for (i = 0; interpreters[i].extension; i++) {
+                    strcpy(rp, interpreters[i].extension);
+                    if (_access(rpath, F_OK) == 0 && !(is_dir = (_access(rpath, D_OK) == 0))) {
+                        found = 1;
+                        break;
+                    }
+                }
+                if (found) break;
+            }
         }
-       if (found) break;
-      }
     }
-  }
-  if (!found)
-    return path;
-
-  return rpath;
+    if (!found)
+        return path;
+    return rpath;
 }
 
 int _spawnvpe(int nMode, const char* szPath, char* const* szaArgv, char* const* szaEnv)
 {
- char rpath[FILENAME_MAX];
-
-  return _spawnve(nMode, find_exec((char*)szPath,rpath), szaArgv, szaEnv);
+    char rpath[FILENAME_MAX];
 
+    return _spawnve(nMode, find_exec((char*)szPath,rpath), szaArgv, szaEnv);
 }