Told you so.
Here's my full code:
Code:
#include "shell2.h"
int main (int argc, char **argv)
{
char *argvNew[8];
char *cmd; // Command that is read
int pid; // Holds PID after fork
int i, j, background;
i = 0;
int *status; // Used to loop
char *token; // Temporarily holds each token
struct timeval t1, t2;
struct rusage usage;
while (1) {
cmd = readline("$");
/*if (cmd[127] != '\0') {
printf ("Error: Too many characters, try again.\n");
goto end;
}*/
if (strcmp (cmd, "exit") == 0) // End if exit or EOF is used as input
exit(0);
else if (cmd[0] == EOF)
exit(0);
if (cmd[strlen(cmd)-1] == '&') {
cmd[strlen(cmd)-1] = '\0';
background == 1;
}
token = strtok (cmd, " ");
while (token != NULL && i < 8) { // Copy the arguments into argvNew with the exception of the 0th element
argvNew[i] = token;
token = strtok (NULL, " ");
i++;
}
if (strcmp (argvNew[0], "cd") == 0) {
chdir(argvNew[1]);
goto end;
}
gettimeofday(&t1, NULL);
pid = fork();
if (pid < 0) {
printf ("Error forking!\n");
exit(1);
}
else if (pid == 0) { // If child, execute the commands
int status = execvp (argvNew[0], argvNew);
if (status == -1) {
printf ("execvp error!\n");
exit (1);
}
exit (0);
}
else {
if (background == 1)
wait3(status, WNOHANG, &usage);
else {
wait (status);
gettimeofday(&t2, NULL);
getrusage(RUSAGE_CHILDREN, &usage);
printstats(usage, t1, t2);
}
}
end:
cmd = NULL;
for (j = 0; j < i; j++)
argvNew[j] = NULL;
i = j = 0;
}
}
Every time I run execvp(), I get an error returned. Looking at the argvNew, it appears to have the correct values:
Code:
Starting program: /home/devin/Dropbox/My Documents/School/CS 3013/Assignment 1/shell2 shell2
$ls /usr
execvp error!
Breakpoint 5, main (argc=2, argv=0x7fffffffe2a8) at shell2.c:50
50 else if (pid == 0) { // If child, execute the commands
6: argvNew = {0x615ef0 "ls", 0x615ef3 "/usr",
0xc2 <Address 0xc2 out of bounds>, 0x7fffffffe17e "", 0x7fffffffe17f "",
0x1 <Address 0x1 out of bounds>, 0x0,
0x7ffff7661748 "H\205\300u[L\211\351L\211\342D\211\376\211\357\350\023\376\377\377H\205\300uF\213t$\bL\211\351L\211\342\211\357\350\375\375\377\377H\205\300u0D9t$\fv\tA\203\306\001\353\210\353\001\220\201", <incomplete sequence \355\277>}