|
|
1.1 root 1: /* libc/sys/execvp.c */
2:
3: #include <stdio.h>
4: #include <errno.h>
5: #include <path.h>
6: #include <string.h>
7:
8: extern char *getenv();
9:
10: #define SHELL "/bin/sh" /* shell name */
11:
12: /*
13: * execvp() is a version of execv()
14: * which mimics the actions of the shell
15: * in using PATH search rules and running a shell file.
16: */
17: execvp(name, argp) char *name; char *argp[];
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: execv(fname, argp); /* go for it */
39: if (isabs && errno != ENOEXEC)
40: break; /* failed */
41: if (errno == ENOEXEC) { /* try again with sh */
42: p1 = argp[-1];
43: p2 = argp[0];
44: argp[-1] = SHELL;
45: argp[0] = fname;
46: execv(argp[-1], argp-1);
47: argp[-1] = p1;
48: argp[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 execvp.c */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.