I have to do an assignment where I make a simple shell like application in C. It basically takes user input from a command line, parses it, and runs the execv() command to run it in a new process. Since we have to use the execv() and not the execvp(), I need to access and parse the users path to run commands. Unfortunately I have NEVER written a C program, and that is where I am having a problem. If anyone has any ideas or solutions, I would appreciate it. The following code uses execvp() and runs just fine. I need to figure out how to make it use execv(). I understand the concept, I need to parse the path, check to see if the command exists in any of the path locations, If it does execute it with that absolute path. Since I have never written in C before, I am having a REALLY difficult time. I already e-mailed my teacher and he said that recieving help is not a problem as long as I reference any help I got in the program. the following code is what I have so far:
this is not an attempt to get someone to do my homework, I am truly lost. I have a bit of experience in programming, so I grasp the concepts and fully understand them. I just dont know how to implement them in C. Unfortunately my professor wants a more specific question to help me. I don't even know the syntax for C and the pointer passing is confusing me a bit how they are declared. I am about to just give up and take incomplete credit, which I hate to do.Code:/* fork.c - example of a fork in a program */ /* The program asks for UNIX commands to be typed and inputted to a string*/ /* The string is then "parsed" by locating blanks etc. */ /* Each command and sorresponding arguments are put in a args array */ /* execvp is called to execute these commands in child process */ /* spawned by fork() */ /* c89 -o fork fork.c */ #include <stdio.h> main() { char buf[1024]; char *args[64]; char *pathDir{200}; parsePath(); for (;;) { /* * Prompt for and read a command. */ printf("user$"); gets(buf); parse(buf, args); execute(args); } } /* * parse--split the command in buf into * individual arguments. */ parse(buf, args) char *buf; char **args; { while (*buf != NULL) { /* * Strip whitespace. Use nulls, so * that the previous argument is terminated * automatically. */ while ((*buf == ' ') || (*buf == '\t')) *buf++ = NULL; /* * Save the argument. */ *args++ = buf; /* * Skip over the argument. */ while ((*buf != NULL) && (*buf != ' ') && (*buf != '\t')) buf++; } *args = NULL; } parsePath( char *dirs[] ){ char pathEnvVar; char thePath; int i; for( i = 0 ; i < 64 ; i++ ){ dirs[i] = NULL; } pathEnvVar = (char *) malloc(strlen(pathEnvVar) + 1); strcpy(thePath, pathEnvVar); } /* * execute--spawn a child process and execute * the program. */ execute(args) char **args; { int pid, status; /* * Get a child process. */ if ((pid = fork()) < 0) { perror("fork"); exit(1); } /* * The child executes the code inside the if. */ if (pid == 0) { execvp(*args, args); perror(*args); exit(1); } /* * The parent executes the wait. */ while (wait(&status) != pid) /* empty */ ; }
Thanks in advance for any help,
Roaming_Builder



LinkBack URL
About LinkBacks



It seems like the path is like PATH=/opt/intel/cc/9.1.042/bin:/opt/intel/fc/9.1.036/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:/usr/local/src/NAMD_2.6_Linux-i686:/opt/mpich/bin