Commit bcd47a9f authored by Matthew Burket's avatar Matthew Burket

Work on getting piping working

parent b922752f
coms352_shell
\ No newline at end of file
......@@ -40,6 +40,10 @@ int main() {
exit(EXIT_SUCCESS);
}
char *doMore = strchr(readBuff, '|');
if (doMore != NULL) {
readBuff = strtok(readBuff, "|");
}
char **tokens = malloc(sizeof(char) * 5000);
int numTokens = tokenize(readBuff, tokens);
char *lastToken = tokens[numTokens - 2];
......@@ -114,13 +118,29 @@ int main() {
storeHist(readBuff, &historyPos, historyBuff);
int runInBackground = 0;
int doMoreCheck = 0;
if (doMore != NULL) {
doMoreCheck = 1;
}
if (strcmp(lastToken, "&") == TRUE) {
runInBackground = 1;
tokens[numTokens - 2] = NULL;
}
int thePipe[2];
if (pipe(thePipe) != 0) {
fprintf(stderr, "Unable to create the pipe. Aborting.");
exit(EXIT_FAILURE);
}
if (fork() == 0) {
if (doMoreCheck != 0) {
close(STDOUT_FILENO);
dup(thePipe[1]);
close(thePipe[0]);
}
int returnCode = execvp(tokens[0], tokens);
close(thePipe[1]);
if (returnCode == -1) {
fprintf(stdout, "bsh: Command not found\n");
}
......@@ -129,6 +149,21 @@ int main() {
wait(NULL);
}
}
// Do if more fork, otherwise short cricut and move on with life
if (doMoreCheck != 0 && fork() == 0) {
close(STDIN_FILENO);
dup(thePipe[0]);
close(thePipe[1]);
char* moreCmd[] = {"more"};
execvp(moreCmd[0], moreCmd);
close(thePipe[0]);
exit(EXIT_SUCCESS);
} else if (doMoreCheck != 0) {
// wait(NULL);
}
free(tokens);
}
}
......
......@@ -7,7 +7,8 @@
/// Takes in the user input and tokenizes it
///
/// \param string user input
/// \param output
/// \param output tokens
/// \param pipe does the command need to be piped to more
/// \return number of tokens
int tokenize(char *string, char **output) {
// Make strtok thread safe
......@@ -26,5 +27,7 @@ int tokenize(char *string, char **output) {
output[i] = token;
i++;
}
// Return token count
return i;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment