Question: Code for these functions: void process_input (int argc, char **argv) { call handle_redir passing it argc and argv call execvp passing in argv[0] and argv
Code for these functions:
void process_input (int argc, char **argv)
{ call handle_redir passing it argc and argv call execvp passing in argv[0] and argv and return a value to an integer variable (Example: returned_value) if (returned_value == -1) error check and do _exit(EXIT_FAILURE) } void handle_redir(int count, char *agrv[]) { You need two integer variables to keep track of the location in the string of the redirection symbols, (one for out_redir (>), one for in_redir (<) ). Initialize them to zero. for loop from 0 to < count if ( > == 0) // use strcmp function if out_redir not equal 0 Cannot output to more than one file. print error. _exit failure. else if loop_counter compares equal 0 No command entered. print error. _exit failure. set out_redir to the current loop_counter. else if (< == 0) // use strcmp function if (in_redir not equal 0) Cannot input from more than one file. print error. _exit failure. else if loop_counter compares equal 0 No command entered. print error. _exit failure. set in_redir to the current loop_counter. // end of the if // end of the for loop more pseudo code on next page CSC 60. Fall 2018. Lab 10. Write your own UNIX Shell, part 2. Page 4 of 6. if(out_redir != 0) if argv (indexed by out_redir+1) contains a NULL There is no file, so print an error, and _exit in failure. Open the file using name from argv, indexed by out_redir+1, and assign returned value to fd. [See 9-Unix, slides 6-10] use flags: to read/write; to create file if needed; to truncate existing file to zero length use permission bits for: user-read; user-write Error check the open. _exit Call dup2 to switch standard-out to the value of the file descriptor. Close the file Set things up for the future exec call by setting argv[out_redir] to NULL // end of if(out_redir != 0) if(in_redir != 0) if argv (indexed by in_redir+1) contains a NULL There is no file, so print an error, and _exit in failure. Open the file using name from argv, indexed by in_redir+1 and assign returned value to fd. use flags; for read only Error check the open. _exit Call dup2 to switch standard-in to the value of the file descriptor. Close the file Set things up for the future exec call by setting argv[in_redir] to NULL //end of if(in_redir != 0) This is the provided code for Lab10:
#include
#define MAXLINE 80 #define MAXARGS 20 #define MAX_PATH_LENGTH 50 #define TRUE 1
/* function prototypes */ int parseline(char *cmdline, char **argv);
void process_input(int argc, char **argv); void handle_redir(int count, char *argv[]);
/* ----------------------------------------------------------------- */ /* The main program starts here */ /* ----------------------------------------------------------------- */ int main(void) { char cmdline[MAXLINE]; char *argv[MAXARGS]; int argc; int status; pid_t pid; int i;
/* Loop forever to wait and process commands */ while (TRUE) { /* Print your shell name: csc60mshell (m for mini shell) */ printf("csc60mshell> ");
/* Read the command line */ fgets(cmdline, MAXLINE, stdin);
/* Call parseline to build argc/argv */ argc = parseline(cmdline, argv); printf("Argc = %i ", argc); for (i = 0; i < argc; i++) { printf("Argv %i = %s ", i, argv[i]); }
if (argc == 0) continue;
if (strcmp(cmdline, "exit") == 0) { exit(EXIT_SUCCESS); } else if (strcmp(cmdline, "pwd") == 0) { char path[MAX_PATH_LENGTH]; if (getcwd(path, MAX_PATH_LENGTH) != NULL) { printf("Working Directory Path: %s ", path); } else { perror("Path Not Found"); } continue; } else if (strcmp(cmdline, "cd") == 0) { char *dir; if (argc == 1) { dir = getenv("HOME"); } else { dir = argv[1]; } if (chdir(dir) != 0) { perror("Error Changing Directory"); } continue; } else { printf("Command not supported. "); continue; }
/* Else, fork off a process */ else { pid = fork(); switch (pid) { case -1: perror("Shell Program fork error"); exit(EXIT_FAILURE); case 0: /* I am child process. I will execute the command, */ /* and call: execvp */ process_input(argc, argv); break; default: /* I am parent process */ if (wait(&status) == -1) perror("Parent Process error"); else printf("Child returned status: %d ", status); break; } /* end of the switch */
} /* end of the if-else-if */ } /* end of the while */ } /* end of main */
/* ----------------------------------------------------------------- */ /* parseline */ /* ----------------------------------------------------------------- */ /* parse input line into argc/argv format */
int parseline(char *cmdline, char **argv) { int count = 0; char *separator = " \t"; /* Includes space, Enter, Tab */ /* strtok searches for the characters listed in separator */ argv[count] = strtok(cmdline, separator);
while ((argv[count] != NULL) && (count+1 < MAXARGS)) argv[++count] = strtok((char *) 0, separator); return count; //This becomes "argc" back in main. } /* ----------------------------------------------------------------- */ /* process_input */ /* ----------------------------------------------------------------- */ void process_input(int argc, char **argv) {
/* Step 1: Call handle_redir to deal with operators: */ /* < , or >, or both */
/* Step 2: perform system call execvp to execute command */ /* Hint: Please be sure to review execvp.c sample program */ if (........ == -1) { fprintf(stderr, "Error on the exec call "); _exit(EXIT_FAILURE); } } /* ----------------------------------------------------------------- */ void handle_redir(int count, char *argv[]) /* ----------------------------------------------------------------- */
/* ----------------------------------------------------------------- */
...
Provided code for execvp.c: /* execvp.c Program to execute a command using an argument from argv */ #include
#include
int main (void) { int fileId; fileId = creat( "x.lis",0640 ); if( fileId < 0 ) { printf("error creating x.lis " ); exit (EXIT_FAILURE); } dup2( fileId, 1); /* copy fileID to stdout */ close( fileId ); execl( "/bin/ls", "ls", 0 ); exit (EXIT_SUCCESS); } ..
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
