execExamples.c
/* Examples of exec's
* Of course, only the first will be executed.
* edit the file and rearrange to test others
* (if you are so possessed).
*/
#include
main(){
char *myargv[24]; /* maximum of 24 words in a cmd */
char **envp; /* environment pointer to be passed */
char **tmp; /* temporary to help build environment*/
char *strdup();
/* Simple execve */
myargv[0] = "env";
myargv[1] = NULL;
envp = (char **) malloc(15 * sizeof (char *));
tmp = envp;
*(tmp++) = strdup("TERM=vt101");
*(tmp++) = strdup("SHELL=/grad/phantom/510/mysh");
*(tmp++) = NULL;
/* Just to make sure what the environment was set to
* for(tmp = envp; *tmp != NULL; ++tmp)
* fprintf(stderr, "\n%s", *tmp);
*/
execve("/usr/bin/env", myargv, envp);
fatal("Exec Failed");
/* Simple execv */
myargv[0] = "ls";
myargv[1] = "-l";
myargv[2] = "sh.pipe.pseudo";
myargv[3] = "execExamples.c";
myargv[4] = NULL;
execv("/bin/ls", myargv);
/* Simple execvp */
myargv[0] = "ls";
myargv[1] = "-l";
myargv[2] = "sh.pipe.pseudo";
myargv[3] = "execExamples.c";
myargv[4] = NULL;
execvp("ls", myargv);
execlp("ls", "ls", " ", NULL); /* Test if blank args cause problems */
execlp("ls", "ls", "-l", NULL); /* note PATH is used to find cmd */
}
char *strdup(char * s)
{
char *tmp;
if((tmp = (char *)malloc(strlen(s))) == (char *)NULL) exit(1);
strcpy(tmp,s);
return (tmp);
}
fatal(char *s)
{
fprintf(stderr, "\nFATAL ERROR: %s", s);
exit(1);
}