Just so I can get this out of the way, yes it's for class. But no, I'm not asking you to do all of it. I just need help with this problem.
I'm basically creating a shell that coincides with my OS class, and it should have the basic features of a regular shell. The only problem I'm having with is running the the following commands:
< /home/ma/cs/Data1/input1 > your.outputb tr a-z A-Z &
>your.outputf</home/ma/cs/Data1/input1 tr a-z A-Z
This is injected into my program as a typical input redirect (i.e., p2 < input). My program does the first line correctly. The second part? Not so much. It leaves a blank file half the time. Sometimes it works, sometimes it doesn't. But I need this to work all the time. My execution part of the code is as follows (the redirects work so I know it can't be that)
Code:
//Fork shell and execute command
if( (kidpid = fork ()) == 0) //CHILD PROCESS
{
for( i = 0; i < c ; i ++)
{
if(compare(argv[i], "\0"))
break;
newargv[i] = argv[i];
}
newargv[c] = NULL; //Last argv should be nulled
if((execvp(newargv[0], newargv))<0)
{
perror("Could not execute");
}
exit(1);
}
else if (kidpid == -1) //FORK ERROR
{
perror("Cannot fork\n");
}
else //PARENT PROCESS
{
if(*background == 0)
{
wait(&child_pid);
dup2(std_in, STDIN_FILENO);
dup2(std_out, STDOUT_FILENO);
}
else
{
dup2(std_in, STDIN_FILENO);
dup2(std_out, STDOUT_FILENO);
x = 255;
int directory = 0;
for(i = 0; i < STORAGE ; i++)
{
if ( argv[0][i] == '\0')
break;
else if(argv[0][i] == '/')
{
directory = 1;
x = i+1;
}
}
for(i = 0; i < STORAGE ; i++)
{
if(x+i == STORAGE)
break;
else if ( directory == 1)
argv[0][i] = argv[0][x+i];
}
printf("[ %s ]\t[ %d ]\n",argv[0],getppid());
}
}
}
Apparently this program is very finnicky depending on where its run. I developed this in Ubuntu, only to realize that once it's uploaded to the server that it needs to run on, it works 90% correctly. So if you notice anything strange, that's why.
Also the part of the code that starts with x = 255; was a design spec... Unless someone else can tell me a quick and dirty way to get the process name.