Question
Making a basic shell in C language. There will be an incomplete shell to work off of. Your basic shell should prompt the user to
Making a basic shell in C language. There will be an incomplete shell to work off of.
Your basic shell should prompt the user to enter a command, read in a text string from the user, parse the text string to determine a command, execute the command, and repeat these steps forever. Your shell should be able to execute a variety of commands. Your shell should also be able to recognize incorrect commands and give the appropriate error message. Your shell should also accomplish the following:
• It should accept one command per line. It can execute any valid command typed in with arguments using the execvp() (and not system) system call. The separation of multiple commands on a single line by semicolons will not be implemented.
• If the user presses the [Enter] key without a command, the shell displays another prompt.
Your shell must support the following internal commands:
• C file1 file2 Copy; create file2, copy all bytes of file1 to file2 without deleting file1.
• D file Delete the named file.
• E comment Echo; display comment on screen followed by a new line (multiple spaces/tabs may be reduced to a single space); if no argument simply issue a new prompt.
• H Help; display the user manual, described below.
• L List the contents of the current directory; see below.
• M file Make; create the named text file by launching a text editor.
• P file Print; display the contents of the named file on screen.
• Q Quit the shell.
• S Surf the web by launching a browser as a background process.
• W Wipe; clear the screen.
• X program Execute the named program.
All commands are case sensitive. Any command not part of this list should just be passed to execvp() and normal execution attempted.
MUST USE THE FOLLOWING CODE:
/* * This is a very minimal shell. It finds an executable in the * PATH, then loads it and executes it (using execv). Since * it uses "." (dot) as a separator, it cannot handle file * names like "minishell.h" * * The focus on this exercise is to use fork, PATH variables, * and execv. */ #include #include #include #define MAX_ARGS 64#define MAX_ARG_LEN 16#define MAX_LINE_LEN 80#define WHITESPACE " ,t" struct command_t { char *name; int argc; char *argv[MAX_ARGS];}; /* Function prototypes */int parseCommand(char *, struct command_t *);void printPrompt();void readCommand(char *); int main(int argc, char *argv[]) { int pid; int status; char cmdLine[MAX_LINE_LEN]; struct command_t command; while (TRUE) { printPrompt(); /* Read the command line and parse it */ readCommand(cmdLine); parseCommand(cmdLine, &command); command.argv[command.argc] = NULL; /* TODO: if the command is one of the shortcuts you're testing for either execute it directly or build a new command structure to execute next */ /* Create a child process to execute the command */ if ((pid = fork()) == 0) { /* Child executing command */ execvp(command.name, command.argv); } /* Wait for the child to terminate */ wait(&status); /* EDIT THIS LINE */ } /* Shell termination */ printf(" shell: Terminating successfully"); return 0;} /* End basic shell */ /* Parse Command function */ /* Determine command name and construct the parameter list. * This function will build argv[] and set the argc value. * argc is the number of "tokens" or words on the command line * argv[] is an array of strings (pointers to char *). The last * element in argv[] must be NULL. As we scan the command line * from the left, the first token goes in argv[0], the second in * argv[1], and so on. Each time we add a token to argv[], * we increment argc. */int parseCommand(char *cLine, struct command_t *cmd) { int argc; char **clPtr; /* Initialization */ clPtr = &cLine; /* cLine is the command line */ argc = 0; cmd->argv[argc] = (char *) malloc(MAX_ARG_LEN); /* Fill argv[] */ while ((cmd->argv[argc] = strsep(clPtr, WHITESPACE)) != NULL) { cmd->argv[++argc] = (char *) malloc(MAX_ARG_LEN); } /* Set the command name and argc */ cmd->argc = argc-1; cmd->name = (char *) malloc(sizeof(cmd->argv[0])); strcpy(cmd->name, cmd->argv[0]); return 1;} /* End parseCommand function */ /* Print prompt and read command functions - Nutt pp. 79-80 */ void printPrompt() { /* Build the prompt string to have the machine name, * current directory, or other desired information */ promptString = ...; printf("%s ", promptString);} void readCommand(char *buffer) { /* This code uses any set of I/O functions, such as those in * the stdio library to read the entire command line into * the buffer. This implementation is greatly simplified, * but it does the job. */ fgets(buffer, 80, stdin);} /* End printPrompt and readCommand */Step by Step Solution
3.40 Rating (156 Votes )
There are 3 Steps involved in it
Step: 1
include stdioh include stdlibh include stringh include unistdh include syswaith define MAXARGS 64 de...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started