Question

Design and implement C/C++ program (myshell5.c) to process command (to tokenize and parse the command, and...

Design and implement C/C++ program (myshell5.c) to process command (to tokenize and parse the command, and print its components correctly). Your C/C++ program should be able to parse each command from user (to process one command after the other in a loop), until the user's command entered is "exit" to terminate the program.

Examples (You may create your own output format or template to show the command(s) being parsed.)

Run your program for each of the following examples, to show it is working correctly.

#3. Two commands with pipe.

            For example, ls | wc should be parsed and display

           Command: ls

            Pipe

            Command: wc

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code is given below :

#include <stdio.h>
#include <sys/types.h>

void parse(char *line, char **argv)
{
while (*line != '\0') { /* if not the end of line ....... */
while (*line == ' ' || *line == '\t' || *line == '\n')
*line++ = '\0'; /* replace white spaces with 0 */
*argv++ = line; /* save the argument position */
while (*line != '\0' && *line != ' ' &&
*line != '\t' && *line != '\n')
line++; /* skip the argument until ... */
}
*argv = '\0'; /* mark the end of argument list */
}

void execute(char **argv)
{
pid_t pid;
int status;

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

void 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 */
gets(line); /* read in the command line */
printf("\n");
parse(line, argv); /* parse the line */
if (strcmp(argv[0], "exit") == 0) /* is it an "exit"? */
exit(0); /* exit if it is */
execute(argv); /* otherwise, execute the command */
}
}

Add a comment
Know the answer?
Add Answer to:
Design and implement C/C++ program (myshell5.c) to process command (to tokenize and parse the command, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program that parses a string (command line) and tokenize it by breaking the...

    Write a C program that parses a string (command line) and tokenize it by breaking the string characters into words that are separated by delimiters that can be white spaces (space and/or tab characters). It also must report how many commands in the input. This program will be reused for your next C programming assignment that is a simple shell interpreter/program called "tech shell". % a.out please enter a string: this is a test 1: this 2: is 3: a...

  • Design a c++program to Implement a bash-like shell to show the following comands: pwd ls touch...

    Design a c++program to Implement a bash-like shell to show the following comands: pwd ls touch cat rm mkdir rmdir wc egrep cp mv date echo exit as well as pipes (>, <, |) it has to be in one program and whatever you enter it should give an answer even if its wrong . for example if i entered "adedaeew" and it wasnt recognised it should give feedback that the entry wasn't recognized so it's more like a loop

  • DO NOT COPY PASTE THE SOLUTION FROM ANOTHER QUESTION! IT IS NOT THE RIGHT ONE, ANSWER...

    DO NOT COPY PASTE THE SOLUTION FROM ANOTHER QUESTION! IT IS NOT THE RIGHT ONE, ANSWER WILL BE REPORTED IF NOT A UNIQUE SOLUTION. Write a C program called myshell.c, which, when compiled and run, will do what the shell does, namely, it executes in a loop (until user types exit on the keyboard), prints a prompt on the screen, reads the command typed on the keyboard (terminated by \n), creates a new process and lets the child execute the...

  • Java program Program: Grade Stats In this program you will create a utility to calculate and...

    Java program Program: Grade Stats In this program you will create a utility to calculate and display various statistics about the grades of a class. In particular, you will read a CSV file (comma separated value) that stores the grades for a class, and then print out various statistics, either for the whole class, individual assignments, or individual students Things you will learn Robustly parsing simple text files Defining your own objects and using them in a program Handling multiple...

  • Project 1: Implementing a Shell 1 Overview In this individual project you will have to design...

    Project 1: Implementing a Shell 1 Overview In this individual project you will have to design and implement a simple shell command interpreter called mysh. The basic function of a shell is to accept lines of text as input and execute programs in response. The shell must be able to execute built-in commands in a process different from the one executing mysh. 2 Requirements When first started, your shell should initialize any necessary data structures and then enter a loop...

  • Design and implement a simple, interactive shell program that prompt the user for a command, parser...

    Design and implement a simple, interactive shell program that prompt the user for a command, parser the command (you do not need to write a parser) and then execute it. The commands are: attrib file. To make the file read only. copy fileA fileB To copy fileA into fileB. delete file To delete the file. dir name or just dir The listing of the directory name is displayed. In case of just dir, the list of the      items in the current...

  • Update the program in the bottom using C++ to fit the requirements specified in the assignment....

    Update the program in the bottom using C++ to fit the requirements specified in the assignment. Description For this assignment, you will be writing a single program that enters a loop in which each iteration prompts the user for two, single-line inputs. If the text of either one of the inputs is “quit”, the program should immediately exit. If “quit” is not found, each of these lines of input will be treated as a command line to be executed. These...

  • In some cases, people who are not strong in Linux may need to run specific Linux...

    In some cases, people who are not strong in Linux may need to run specific Linux commands. To make this easier, you can create a menu-driven program that runs these Linux commands. Such a program allows users to execute the commands without having to actually know about how to execute them. For this scenario, create a menu-driven program that has the following options: 1. List users who are logged in (using the who command) 2. List system information (using the...

  • Overview Writing in the C language, implement a very basic shell, called smash. In this project,...

    Overview Writing in the C language, implement a very basic shell, called smash. In this project, we will work on processing strings and using the appropriate system calls. Build System setup Before we can start writing code we need to get our build system all setup. This will involve writing a very simple makefile. You should leverage your Makefile project for this part of the assignment! Write a Makefile that provides the three targets listed below all - The default...

  • C program To develop a C program to implement a process system call. PROBLEM You are...

    C program To develop a C program to implement a process system call. PROBLEM You are to use the Ubuntu operating system to write a C program that creates a process to determine the identification of the current user of your computer. I mean if joe is the login of the current user of your computer your solution should return joe. Your solution must also provide the pid of both the parent and the child processes. You may use the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT