Annotation of coherent/b/lib/libc/sys/execvpe.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * libc/sys/execvpe.c
        !             3:  * Sys exec with an array of arguments and environment,
        !             4:  * searching directories specified in PATH.
        !             5:  */
        !             6: 
        !             7: #include <errno.h>
        !             8: #include <path.h>
        !             9: #include <string.h>
        !            10: #include <stdlib.h>
        !            11: 
        !            12: #define        SHELL   "/bin/sh"       /* not DEFSHELL, which is just "sh" */
        !            13: 
        !            14: 
        !            15: /* execvpe(name, argv, env) */
        !            16: int
        !            17: execvpe(name, argv, env) char *name; char *argv[]; char **env;
        !            18: {
        !            19:        register char *p1, *p2, *sp;
        !            20:        char fname[MAXPATH];
        !            21:        int isabs;
        !            22: 
        !            23:        if ((sp = getenv("PATH")) == NULL)              /* find PATH */
        !            24:                sp = DEFPATH;                           /* or take default */
        !            25:        isabs = (strchr(name, PATHSEP) != NULL);        /* iff name is absolute */
        !            26:        for (;;) {
        !            27:                if (isabs)
        !            28:                        strcpy(fname, name);            /* use given name */
        !            29:                else {
        !            30:                        for (p2 = fname; *sp != '\0' && *sp != LISTSEP; )
        !            31:                                *p2++ = *sp++;          /* copy pathname component */
        !            32:                        if (p2 != fname)
        !            33:                                *p2++ = PATHSEP;        /* append PATHSEP if nonempty */
        !            34:                        for (p1 = name; *p1 != '\0'; )
        !            35:                                *p2++ = *p1++;          /* append name */
        !            36:                        *p2 = '\0';                     /* and NUL-terminate */
        !            37:                }
        !            38:                execve(fname, argv, env);               /* go for it */
        !            39:                if (isabs && errno != ENOEXEC)
        !            40:                        break;                          /* failed */
        !            41:                if (errno == ENOEXEC) {                 /* try again with sh */
        !            42:                        p1 = argv[-1];
        !            43:                        p2 = argv[0];
        !            44:                        argv[-1] = SHELL;
        !            45:                        argv[0] = fname;
        !            46:                        execve(argv[-1], argv-1, env);
        !            47:                        argv[-1] = p1;
        !            48:                        argv[0] = p2;
        !            49:                        break;                          /* failed */
        !            50:                }
        !            51:                if (*sp == '\0')
        !            52:                        break;                          /* end of PATH, failed */
        !            53:                if (*sp == LISTSEP)
        !            54:                        sp++;
        !            55:        }
        !            56:        return -1;                                      /* failed */
        !            57: }
        !            58: 
        !            59: /* end of libc/sys/execvpe.c */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.