How do I write the following code?
*Directions can be seen at the end of question
This is the FULL CODE, to help solve :)
Again, this last bit of code is what I need to be answered.
THESE ARE THE DIRECTIONS
// Redirect output to write to the given file, then execute the command void exec_cmd_red_out(char *args[], char *filename) { // YOUR CODE HERE } // Redirect input to read from the given file, then execute the command void exec_cmd_red_in(char *args[], char *filename) { // YOUR CODE HERE } // Create a pipline between the first command and the second command void exec_cmd_pipe(char *args1[], char *args[]) { // YOUR CODE HERE }
8 9 10 11 #include #include #include #include #include #include #include 12 13 14 15 16 17 18 const int MAX_LINE = 256; const int MAX_ARGS = 64; 19 int split_cmd( char *cmd, char *args[]); void process_args (char *args[], int count); 20 21 22 23 void exec_cmd( char *args[]); void exec_cmd_red_out(char *args[], char *filename); void exec_cmd_red_in(char *args[], char *filename); void exec_cmd_pipe(char *args1[], char *args[]); 24 25 26 27 28 29 30 31 32 int main() { // The command line string char cmd [MAX_LINE]; 33 // The comand line broken into arguments char *args[MAX_ARGS] ; 34 35 36 37 V 38 39 40 // Repeatedly prompt for a command and execute it while(1) { // Display prompt and read the command line printf("> "); fgets(cmd, MAX_LINE, stdin); 41 42 43 // Process the command int count = split_cmd( cmd, args); process_args (args, count); 44 } 45 46 47 return 0; 48 } 49
50 51 // Break the command into command line arguments int split_cmd(char *cmd, char *args[]) { int index @; 52 53 54 55 56 57 58 // Extract each comand line argument and store it in args char *arg = strtok(cmd, " \t\n\v\f\r"); while(arg != NULL && index < MAX_ARGS - 1) { args[index] = arg; arg = strtok (NULL, \t\n\v\f\r"); index++; } args[index] - NULL; return index; } 59 60 61 62 63 64 65 66 67 68 // Takes an array of command line arguments and an argument count and processes // the command line void process_args (char *args[], int count) { 69 72 73 74 75 76 77 // Find the location of the delimiter (I, <, or ») int i, delim = -1; for(i = 0; i < count; i++) { if(strcmp(args[i], "|") == 0 || strcmp(args[i], "<") == 0 || strcmp(args[i], ">") = 0) delim = i; } // If user entered a blank line do nothing if(count == 0) return; 80 81 82 83 // If user entered exit, then exit the shell else if(count == 1 && strcmp(args[@], "exit") == 0) { puts("[exiting smash]\n"); exit(EXIT_SUCCESS); } 89 // If there are no pipes or redirects, execute the command else if(delim < 0) exec_cmd(args); 90 91 92 93 94 95 // Pipe or redirect appears as the first or last argument else if(delim == || delim count - 1) fprintf(stderr, "smash: sytnax error\n"); ==
96 97 98 // Redirect output else if(strcmp(args[delim], ">") == ) { args[delin] = NULL; exec_cmd_red_out (args, args[delim+1]); } 99 100 101 102 103 104 105 106 // Redirect input else if(strcmp(args[delin], "<") == 0) { args[delim] = NULL; exec_cmd_red_in(args, args[delim+1]); } 107 108 109 110 // Pipe else if(strcmp(args[delin], "|") == 0) { args[delim] = NULL; exec_cmd_pipe(args, &args[delim+1]); } 111 112 113 114 115 116 117 118 // If this point is reached something went wrong with this code else { fprintf(stderr, "smash: internal error"); } } 119 120 // Execute the command void exec_cmd (char *args[]) { 121 122 123 124 // Fork a child process pid_t pid = fork(); 125 126 127 128 129 130 // Child process if(pid == e) { // Execute the command execvp(args[@], args); fprintf(stderr, "smash: %s: command not found\n", args[@]); exit(EXIT_FAILURE); } 131 132 133 134 135 136 // Parent process else { // Wait for the child to complete wait(NULL); ] } 137 138 139 140
141 142 143 144 // Redirect output to write to the given file, then execute the command void exec_cmd_red_out(char *args[], char *filename) { // YOUR CODE HERE } 145 146 147 // Redirect input to read from the given file, then execute the command void exec_cmd_red_in(char *args[], char *filename) { // YOUR CODE HERE } 148 149 150 151 152 153 154 155 // Create a pipline between the first command and the second command void exec_cmd_pipe(char *args1[], char *args2[]) { // YOUR CODE HERE }
. For this programming assignment you are going to implement a simple shell called smash in C. This shell will be able to process command lines in the following forms: command [args] command [args] > file (output redirected to write to file) command [args] < file input redirected to read from file) command1 [args1] | command2 [args2] (output of command1 piped into input of command) The shell should do the following: Display a command prompt. 2. Read a line of text from the user. Break the command line into individual arguments. 4. Process the line of text and do one of the following: (a) If the line is blank, go back to step 1. 1. 3. (b) If the line is the exit command, exit the shell. (c) Otherwise attempt to execute the command. (d) If the execution fails, display an error message and go back to step 1. (e) If the execution succeeds, go back to step 1 after the command has finished.
You will need to implement the following functions: void exec_cmd_red_out(char *args[], char *filename) 1. Create a child process using fork. 2. The child process should do the following: (a) Close standard output. (b) Create a new file with the name given by filename. c) If the file can not be created, display an error and exit the child process. (d) Execute the command. 3. (e) If the execution fails, display an error and exit the child process. The parent process should wait for the child process to complete. void exec_cmd_red_in(char *args[], char *filename) Create a child process using fork. 1. 2. The child process should do the following: (a) Close standard input. (b) Open a file for reading only with the name given by filename. (c) If the file can not be opened, display an error and exit the child process. (d) Execute the command. (e) If the execution fails, display an error and exit the child process. 3. The parent process should wait for the child process to complete.
void exec_cmd_pipe(char *args1[], char *args[]) 1. Create a child process using fork. 2. The child process should do the following: (a) Create a pipe. (b) If the pipe creation fails, diplsay an error and exit the child process. (c) Create a second child process. (d) The second child process should redirect standard output to write to the pipe input, execute the command, and display an error and exit if execution fails. (e) The first child process should redirect standard input to write to the pipe output, execute the command, and display an error and exit if execution fails. The parent process should wait for the first child process to complete. 3.