Shell of OS Class
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Riley Spahn: Shell Assignment

To Build: make
To Run: ./make
To Clean: make clean

Source Code Files:
rogram.c/h - Contains structures representing command line programs that are to be executed, their inputs and their outputs. The program structure is is used to represent a single program or node int the linked program list.  ProgramList maintains a list of programs that are to be executed and the redirects for stdin, stdout and stderr.

libshell.c/h - Contains various helper functions for sting manipulation for path and executable concatenation, setting the standard redirects and pipes.

parser.c/h - Contains the function used for parsing the command line. The parser uses a state machine with states for the beginning, parsing commands, arguments redirects and pipes.  This simplified the work a lot but it would have been better to use tools like Flex and BisonIn future I would have designed this to be more portable and less of a behemoth.

path.c/h - Structures to maintain a linked list of directories that should be in the path. Handles adding, removing and checking that final slashes in the path are correct.

myshell.c - Contains that main logic for the program and handles execution. The main structure of executing an input line is that the main loop will for a process that will control execution. That control process will set the redirects for standard in/out/err. It will also handle all pipe file descriptors for the unix filters. The control process will then fork a process for each of the programs that it needs to execute in reverse order that they appear on the command line. For "ls | grep" grep would begin executing and then ls.  This is needed because grep must be listening on the pipe before ls starts outputting. The subprocess for each of the programs handles calling dup for the file descriptors used in the pipes.  The control process waits for the subprocesses to finish executing before exiting itself.

I used Richard Stevens' Advanced Programming in the Unix Environment as a reference.


/home/riley/os_shell>> path + /bin
/home/riley/os_shell>> path + /usr/bin
/home/riley/os_shell>> path + /usr/local/bin
/home/riley/os_shell>> path
/home/riley/os_shell>> path - /usr/local/bin
/home/riley/os_shell>> ls
libshell.c  libshell.o	myshell    parser.c  parser.o  path.h  pipeOut.txt  program.h
libshell.h  makefile	myshell.c  parser.h  path.c    path.o  program.c    program.o  redirect.txt
/home/riley/os_shell>> make clean
rm -f myshell *.o
/home/riley/os_shell>> ls
libshell.c  makefile   parser.c  path.c  pipeOut.txt  program.h  redirect.txt
libshell.h  myshell.c  parser.h  path.h  program.c
/home/riley/os_shell>> ls -l
total 60
-rw------- 1 riley phd 4795 Feb 10 21:24 libshell.c
-rw------- 1 riley phd 1329 Feb 10 21:24 libshell.h
-rw------- 1 riley phd  410 Feb  9 19:53 makefile
-rw------- 1 riley phd 6686 Feb 10 21:35 myshell.c
-rw------- 1 riley phd 7996 Feb 10 21:31 parser.c
-rw------- 1 riley phd  144 Feb  5 17:38 parser.h
-rw------- 1 riley phd 2274 Feb 10 21:24 path.c
-rw------- 1 riley phd 1471 Feb  9 19:53 path.h
-rw------- 1 riley phd  266 Feb 10 21:33 pipeOut.txt
-rw------- 1 riley phd 3091 Feb  9 19:53 program.c
-rw------- 1 riley phd 1429 Feb  9 19:53 program.h
-rw------- 1 riley phd   36 Jan 27 23:34
-rw------- 1 riley phd   99 Feb 10 21:34 redirect.txt
/home/riley/os_shell>> rm pipeOut.txt
/home/riley/os_shell>> cat myshell.c | grep char | sort > pipeOut.txt
/home/riley/os_shell>> cat pipeOut.txt
	char cdcmd[] = "cd";
	char errmsg[] = "Expected: path [(+|-) <path to modify>]\n";
	char ***execs;
	char exitcmd[] = "exit";
	char line[MAXLINE];
	char pathcmd[] = "path";
	char prefix[] = ">> ";
	char *pwd;
	execs = (char ***)malloc(sizeof(char **) * pgl->count);
/home/riley/os_shell>> grep ex pipeOut.txt
	char ***execs;
	char exitcmd[] = "exit";
	execs = (char ***)malloc(sizeof(char **) * pgl->count);
/home/riley/os_shell>> grep ex <pipeOut.txt
	char ***execs;
	char exitcmd[] = "exit";
	execs = (char ***)malloc(sizeof(char **) * pgl->count);
/home/riley/os_shell>> rm redirect.out
/bin/rm: cannot remove `redirect.out': No such file or directory
/home/riley/os_shell>> rm redirect.txt
/home/riley/os_shell>> ls
libshell.c  makefile   parser.c  path.c  pipeOut.txt  program.h
libshell.h  myshell.c  parser.h  path.h  program.c
/home/riley/os_shell>> grep ex <pipeOut.txt >redirect.txt
/home/riley/os_shell>> cat redirect.txt
	char ***execs;
	char exitcmd[] = "exit";
	execs = (char ***)malloc(sizeof(char **) * pgl->count);
/home/riley/os_shell>> cd ../
/home/riley>> ls os_shell
libshell.c  makefile   parser.c  path.c  pipeOut.txt  program.h  redirect.txt
libshell.h  myshell.c  parser.h  path.h  program.c
/home/riley>> exit