Unix Shell Cpre 308

Unix Shell Cpre 308

Unix Shell CprE 308 Project 1 Neh Batwara Contents 1 Summary 2 2 Code: 2 3 Included Files: 5 1 Summary In this project we created our own version of the UNIX shell. A UNIX shell is simply an interactive interface between the OS and the user. It repeatedly accepts input from the user and initiates processes on the OS based on that input. A challenging part of this project was string parsing since that was how the commands that the user inputs are executed by the shell.The shell uses fork( ) to call a program which creates a child process. To execute the command the child process calls the execvp( ) which exeutes the child process. I also used system calls to process the following process/commands: 1.chdir( ) to change the working directory with the string that gets passed in by the user inpu with cd 2.getcwd( ) to get the current working directory 3.getenv/setenv to retrieve and set environment variables. 4.waitpid( ) to wait for the child process to exit or to get exit status 2 Code: The following code is for the main Unix Shell. #include <s t d i o . h> #include <unistd . h> #include " p1 Unix Shell . h" #include <s t r i n g . h> #include <sys/types.h> #include <unistd . h> #include <sys / wait . h> #include <errno . h> #include <s t d l i b . h> /∗Maximum legnth of input allowed to the shell. ∗/ #define INPUT LENTH 100 /∗Maximum number of argument allowed to be passed to the shell. ∗/ #define ARGUMENT BUFFER 100 #define CHAR BUFFER 100 /∗ ∗ This function should parse the user input into an array. ∗/ int p a r s e i n p u t ( char ∗ cmd , char ∗ argv [ ] ) f int i = 1 ; argv[0] = strtok(cmd, " nn" ) ; while ( argv [ i −1] != NULL && i < (ARGUMENT BUFFER − 1 ) ) f argv[i] = strtok(NULL, " nn" ) ; i ++; g return i −1; g /∗ ∗ This function parses the arguments passed from the command line to the shell.. ∗/ char ∗ parse argument ( int argc , char ∗∗ argv ) f int arg ; o p t e r r = 0 ; char ∗ prompt = NULL; while ((arg = getopt(argc, argv, "p:")) != −1) f i f ( arg == ' p ' )f prompt = optarg; 2 g else f abort ( ) ; g g i f (prompt == NULL) f prompt = "308sh"; g return prompt ; g /∗ ∗ This is the main function with the while loop that executes the shell. ∗ It take the user imput from the command line and parses it to the shell program. ∗/ int main ( int argc , char ∗∗ argv ) f char ∗ prompt = parse argument(argc , argv); char input [INPUT LENTH ] ; while ( 1 ) f char ∗ argv [ARGUMENT BUFFER] ; p r i n t f ( ">>>%s> " , prompt ) ; fgets(input , INPUT LENTH, s t d i n ) ; int lastIndex = parse input(input, argv); i f (lastIndex != 0) f i f (!strcmp(argv[0] , "exit")) f break ; g else i f (!strcmp(argv[0] , "cd")) f cd ( argv ) ; g else i f (!strcmp(argv[0] , "cwd")) f p r i n t f ( " Current Working Directory : %s nn" , getcwd (NULL,CHAR BUFFER) ) ; g else i f (!strcmp(argv[0] , "pid")) f printf("PID = %i nn", getpid()); g else i f (!strcmp(argv[0] , "ppid")) f printf ("PPID = %i nn", getppid()); g else i f (!strcmp(argv[lastIndex −1] , "&" ) ) f argv[lastIndex −1] = NULL; execute(argv); g else f 3 e x e c u t e block(argv); g g int s t a t u s ; p i d t p r o c e s s child = waitpid(−1 , &status , WNOHANG) ; i f ( p r o c e s s c h i l d > 0) f printf("process %i e x i t e d nn" , p r o c e s s c h i l d ) ; processStatus(process child , status); g g return 0 ; g /∗ ∗ Fucntion when cd is called. ∗/ void cd ( char ∗ argv [ ] ) f i f (argv[1] == NULL) f p r i n t f ( "No paramaters s u p p l i e d to cdnn" ) ; g else f i f (chdir(argv[1]) == −1) f p r i n t f ( "Cd f a i l e d − %s nn", strerror(errno)); g g g /∗ ∗ This function prints the status so the user can see it. ∗/ void processStatus(pid t childPid , int s t a t u s ) f i f (WIFSIGNALED( s t a t u s ) ) f printf("Child %i e x i t e d with s i g n a l %dnn", childPid , WTERMSIG(status )); g else i f (WIFEXITED( s t a t u s ) ) f printf("Child %i e x i t e d with return code %dnn", childPid , WEXITSTATUS(status )); g g /∗ ∗ e x e c u t e block waits for the process to finish. ∗/ void e x e c u t e b l o c k ( char ∗ argv [ ] ) f p i d t pid = fork(); i f ( pid == 0) f printf("pid: %i nn", getpid()); execvp (∗ argv, argv); printf ("Unknown command . n n" ) ; 4 e x i t ( 0 ) ; g else f int s t a t u s ; waitpid(pid, &status , 0); processStatus(pid, status); g g /∗ ∗ Exceute does not block the main process and waits for the child process to finish. ∗/ void execute ( char ∗ argv [ ] ) f p i d t pid = fork(); i f ( pid == 0) f p r i n t f ( "nn PID : %i nn", getpid()); execvp (∗ argv, argv); printf("PID: %i f a i l e d − %s nn", getpid(), strerror(errno)); g g 3 Included Files: 1. makefile_cmake 2.p1 Unix Shell_c[MainC File] 3.p1 Unix Shellh_ [Header File] 4.p1 Unix Shell_o[Object File] 5.READMEtxt_ [text file for instructions] 5.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    6 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us