Skip to content

Commit

Permalink
Draft of DupShell
Browse files Browse the repository at this point in the history
  • Loading branch information
cnphil committed Apr 5, 2013
1 parent 4c1ab8c commit 0398ff0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
Binary file added DupShell
Binary file not shown.
60 changes: 60 additions & 0 deletions DupShell.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "cse356header.h"
#include "readline.c"
#define MAX_ARGS 50
#define MAX_PIPES 50
const char * delim_no_pipe = " \t\n";

void parse_argv(char *buffer, char *argv[])
{
char *cp = buffer;
for(int argc = 0; argc < MAX_ARGS; argc++) {
if((argv[argc] = strtok(cp, delim_no_pipe)) == NULL)
break;
cp = NULL;
}
}

void parse_pipe(char *buffer, char *argv[])
{
char *cp = buffer;
for(int argc = 0; argc < MAX_PIPES; argc++) {
if((argv[argc] = strtok(cp, "|")) == NULL)
break;
cp = NULL;
}
}

main()
{
while (true) {
int argc;
char *argv[MAX_ARGS], *comm[MAX_PIPES];

write(1, "$ ", 2);
char buffer[256], buffer2[256];
readline(0, buffer, 255);
buffer[strlen(buffer) - 1] = '\0'; // get rid of cr
strcpy(buffer2, buffer);
parse_argv(buffer, argv);
if(strcmp("exit", argv[0]) == 0) exit(0);

int pipefd[2][2];
parse_pipe(buffer2, comm);
for(int i = 0; comm[i] != NULL; i++) {
pipe(pipefd[i % 2]);
write(1, "\"", 1);
write(1, comm[i], strlen(comm[i]));
write(1, "\"", 1);
if(fork() == 0) {
if(comm[i + 1] != NULL) {dup2(pipefd[i % 2][1], STDOUT_FILENO); close(pipefd[i % 2][0]);}
if(i != 0) {dup2(pipefd[(i + 1) % 2][0], STDIN_FILENO); close(pipefd[(i + 1) % 2][1]);}
parse_argv(comm[i], argv);
execvp(argv[0], argv);
perror("exec failed");
exit(1);
}
wait(NULL);
}
}
exit(0);
}
6 changes: 5 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
all: MyCopy ForkCopy PipeCopy timer MyShell MoreShell
all: MyCopy ForkCopy PipeCopy timer MyShell MoreShell DupShell
clear:
rm *.o
MyCopy: MyCopy.o
Expand All @@ -25,3 +25,7 @@ MoreShell: MoreShell.o
g++ MoreShell.o -o MoreShell
MoreShell.o:
g++ -c MoreShell.c
DupShell: DupShell.o
g++ DupShell.o -o DupShell
DupShell.o:
g++ -c DupShell.c

0 comments on commit 0398ff0

Please sign in to comment.