xref: /csrg-svn/old/dbx/tests/noenv.c (revision 44053)
1*44053Sbostic /*
2*44053Sbostic  * Exec a program with no environment.
3*44053Sbostic  */
4*44053Sbostic 
5*44053Sbostic #include <stdio.h>
6*44053Sbostic 
7*44053Sbostic extern int errno;
8*44053Sbostic extern char *getenv(), *index();
9*44053Sbostic 
main(argc,argv)10*44053Sbostic main (argc, argv)
11*44053Sbostic int argc;
12*44053Sbostic char *argv[];
13*44053Sbostic {
14*44053Sbostic     execvep(argv[1], &argv[1], 0);
15*44053Sbostic     printf("exec failed, errno %d\n", errno);
16*44053Sbostic }
17*44053Sbostic 
execvep(name,argv,envp)18*44053Sbostic execvep (name, argv, envp)
19*44053Sbostic char *name, *argv[], *envp[];
20*44053Sbostic {
21*44053Sbostic     char *path;
22*44053Sbostic     register char *cp;
23*44053Sbostic     char fullname[1000];
24*44053Sbostic 
25*44053Sbostic     path = getenv("PATH");
26*44053Sbostic     if (path == NULL) {
27*44053Sbostic 	path = "";
28*44053Sbostic 	cp = NULL;
29*44053Sbostic     } else {
30*44053Sbostic 	cp = index(path, ':');
31*44053Sbostic     }
32*44053Sbostic     for (;;) {
33*44053Sbostic 	if (cp != NULL) {
34*44053Sbostic 	    *cp = '\0';
35*44053Sbostic 	}
36*44053Sbostic 	sprintf(fullname, "%s/%s", path, name);
37*44053Sbostic 	execve(fullname, argv, envp);
38*44053Sbostic 	if (cp != NULL) {
39*44053Sbostic 	    path = cp + 1;
40*44053Sbostic 	    cp = index(path, ':');
41*44053Sbostic 	} else {
42*44053Sbostic 	    break;
43*44053Sbostic 	}
44*44053Sbostic     }
45*44053Sbostic     return -1;
46*44053Sbostic }
47