I'm trying to create a program that runs commands from user input.
At the moment it works for multiple word commands but I'm trying to implement redirections.
I started with taking input from a file and it's not working but I'm not getting any error (I'm testing using the "wc -l < text.txt" command, the text.txt file is the the same dir as the program.)
Here is the code:
- input is the str with the user's input
- before coming to this method I already checked that it has a redirection on it
redirect(int proc, char * input){
char * comm;
if(proc == 1){ //in
comm = strsep(&input, "<");
}
else{ //out
comm = strsep(&input, ">");
}
int proc2 = check(input);
if(proc2 == 0){ //only one redirection
if(proc == 1){ //in
input = trim(input);
int fd = open(input, O_RDWR);
close(0);
dup2(fd, 0);
close(fd);
comm = trim(comm);
char ** words = parse(comm);
char str[105];
strcpy(str, "/bin/");
strcat(str, words[0]);
shrink(str);
if(!execvp(str, words)){ /*exec failed */
exit(1);
}
}
else{ //out
}
}
else{ //more than one redirection/pipe
}
}
edit
I need to use the execvp command to run the user input.
The user command "<" needs to change the stdin to be the file after it.
I changed the stdin to be the text.txt but I don't know how to pass it as an arg so the execvp can run it.