Question

In: Computer Science

Name your source code file sh.c Write your own simple shell. Your shell should prompt the...

Name your source code file sh.c

Write your own simple shell. Your shell should prompt the user for a command, run it, then prompt the user for their next command. If the user types "exit", then the shell should terminate. The shell should ignore Ctrl-C.

Inside the attached parse.h header file, there is a parse function that you can use to split up the command line into separate strings. Recall that execvp accepts two arguments: the name of the command, and then an array of command-line arguments as strings. The parse() function in parse.h accepts the string and returns the array of argument strings.

You can use parse.h or roll your own parse function.

Hints :

Remember the logic for the shell :

while input != exit
parse the command
fork
if parent process :
wait
else
execvp command

You'll need the chdir system call to add support for cd. This needs to be part of the shell as their is no cd system command.

man chdir, execvp, wait, fork, strtok

This may be the most challenging assignment of the quarter for some people. Don't wait to get started on it.

This assignment is a capstone of sorts for the course. Help will be limited to clarification of requirements and concepts.

// splits input into separate strings
// inputs:  input: a string to split up, args: an array of char pointers to
//    store the separate strings
// outputs: an array of strings, with null in the last element, passed back
//    through args
// preconditon: input is a valid c-string, read from the keyboard using fgets
// postcondition:  args contains the separate strings, one 
//    string per element.  Last element contains null
#ifndef PARSE_H
#define PARSE_H

#include "string.h"

static void parse( char* input, char* args[] )
{
  int i = 0;
  
  // fgets reads the \n, so overwrite it
  input[strlen(input)-1] = '\0';  
  
  // get the first token
  args[i] = strtok( input, " " );
  
  // get the rest of them
  while( args[++i] = strtok(NULL, " ") );
}

#endif

Solutions

Expert Solution

//execute.h
#ifndef EXECUTE_H
#define EXECUTE_H

#include <stdio.h>
#include <sys/types.h>
#include"parse.h"
void execute(char **argv)
{
pid_t pid;
int status;

if ((pid = fork()) < 0) { /* fork a child process */
printf("*** ERROR: forking child process failed\n");
return;
}
else if (pid == 0) { /* for the child process: */
if (execvp(*argv, argv) < 0) { /* execute the command */
printf("*** ERROR: exec failed\n");
return;
}
}
else { /* for the parent: */
while (wait(&status) != pid) /* wait for completion */
;
}
}

#endif

//parse.h
// splits input into separate strings
// inputs: input: a string to split up, args: an array of char pointers to
// store the separate strings
// outputs: an array of strings, with null in the last element, passed back
// through args
// preconditon: input is a valid c-string, read from the keyboard using fgets
// postcondition: args contains the separate strings, one
// string per element. Last element contains null
#ifndef PARSE_H
#define PARSE_H

#include "string.h"

static void parse( char* input, char* args[] )
{
int i = 0;
  
// fgets reads the \n, so overwrite it
input[strlen(input)-1] = '\0';
  
// get the first token
args[i] = strtok( input, " " );
  
// get the rest of them
while( args[++i] = strtok(NULL, " ") );
}

#endif

//sh.c
#include <stdio.h>
#include"parse.h"
#include"execute.h"
/* ----------------------------------------------------------------- */
/* The main program starts here */
/* ----------------------------------------------------------------- */

int main(void)
{
char line[1024]; /* the input line */
char *argv[64]; /* the command line argument */

while (1) { /* repeat until done .... */
printf("Shell -> "); /* display a prompt */
fgets(line,sizeof(line),stdin); /* read in the command line */
printf("\n");
parse(line, argv); /* parse the line */
if (strcmp(argv[0], "exit") == 0) /* is it an "exit"? */
return 0; /* exit if it is */
execute(argv); /* otherwise, execute the command */
}
}


Related Solutions

Write a brief shell script that will take in a specific file name, prompt the user...
Write a brief shell script that will take in a specific file name, prompt the user whether they would like to gzip, bzip2, or xz compress the file. Depending on response, the script then ought to compress the provided file with the corresponding method
C++ code a program should prompt the user for the name of the output file. The...
C++ code a program should prompt the user for the name of the output file. The file should be opened for write operations. Values calculated by the program will be written to this file. The program must verify that the file opened correctly. If the file did not open, an error message should be printed and the user should be prompted again. This should continue until the user supplies a valid filename.
Additional Requirements 1.The name of your source code fileshould be ProbEst.py All your code should be...
Additional Requirements 1.The name of your source code fileshould be ProbEst.py All your code should be within a single file. 2.You cannot import any package except for pandas.You need to use the pandas DataFrame object for storing data Requirements Y ou are to create a program in Python that performs the following: 1. Asks the user for the number of cars (i.e. data instances) . 2. F or each car , ask s the user to enter the following fields:...
(10pts) Write a shell script to display your name to the screen. (10pts) Write a shell...
(10pts) Write a shell script to display your name to the screen. (10pts) Write a shell script to take a directory as an argument and display the contents of that directory (10pts) Write a shell script that takes any command as a parameter and displays help on that command (e.g. the result of the execution of man <command>). (10pts) Write a shell script that requires two file names as parameters and copies content of one file into another without asking...
Writing your own shell in Linux with execvp Only code void cmdToArray() Program should be able...
Writing your own shell in Linux with execvp Only code void cmdToArray() Program should be able to execute ls -l. #include <stdio.h> #include <string,h>// strcmp #include <stdlib.h>// exit #include <unistd.h>// fork, exec #include <sys/wait.h>// wait #define MAXARGS 20 #define ARGLEN 256 void execute(char *cmd, char *arglist[]); void cmdToArray(char *cmd, char *arglist); int main(void) { int pid; char *arglist[MAXARGS+1]; // array of pointers char cmd[ARGLEN]; // read stuff here while (1) { printf("cmd> "); fgets(cmd, ARGLEN, stdin); // remove newline from...
Create a new file name condition_quiz.py. Add a comment with your name and the date. Prompt...
Create a new file name condition_quiz.py. Add a comment with your name and the date. Prompt the user to enter the cost. Convert the input to a float. Prompt the user for a status. Convert the status to an integer Compute the special_fee based on the status. If the status is 0, the special_fee will be 0.03 of the cost. Else if the status is 1, the special_fee will be 0.04 of the cost. Else if the status is 2,...
Python Code: Write a program to prompt the user to enter a first name, last name,...
Python Code: Write a program to prompt the user to enter a first name, last name, student ID, number of hours completed and GPA for 5 students. Use a loop to accept the data listed below. Save the records in a text file. Write a second program that reads the records from your new text file, then determines if the student qualifies for Dean’s list or probation. Display the records read from the file on screen with appropriate headings above...
Name your functions countOnesLoop() and countOnesWhere() and submit your code in a file named countOnes.py. Write...
Name your functions countOnesLoop() and countOnesWhere() and submit your code in a file named countOnes.py. Write a function that consists of a set of loops that run through an array and count the number of ones in it. Do the same thing using the where() function (use info(where) to find out how to use it) (in python)
Write a java code to demonstrate the File IO. Your code should get the following information...
Write a java code to demonstrate the File IO. Your code should get the following information from the user. • Get a file name fname for output • Get number of data (numbers) (N) you want to process from the user • Get N numbers from the users through keyboard and store them in an array • Get M (How many numbers to read from file) • (Or)You are free to use same N for M (use N for both...
Write a java code to demonstrate the File IO. Your code should get the following information...
Write a java code to demonstrate the File IO. Your code should get the following information from the user. • Get a file name fname for output • Get number of data (numbers) (N) you want to process from the user • Get N numbers from the users through keyboard and store them in an array • Get M (How many numbers to read from file) • (Or)You are free to use same N for M (use N for both...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT