Question: Overview Writing in the C language, implement a very basic shell, called smash . In this project, we will work on processing strings and using

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 rule. Builds your project.

smash - Builds your smash shell. Initially only one file smash.c

Make sure to use macros!

clean - Cleans your build directory and deletes any generated files.

Implement basic string processing

Start your program by reading from standard in and splitting the input string on spaces. To test your program echo the result to standard out.

We will leverage the strtok standard library call for this part of the assignment. Below is a basic example (modified from cplusplus.com):

1: char line[] ="ls -l -a"; 2: char* token = strtok(line," "); 3: int i=0; 4: while (token != NULL){ 5: printf ("%d %s ",i++,token); 6: token = strtok (NULL, " "); 7: } 
0 ls 1 -l 2 -a 

We will also leverage the fgets standard library call to read input from standard-in.

1: #define MAXLINE 4096 2: 3: char buff[MAXLINE]; 4: while (fgets(buff, MAXLINE, stdin) != NULL) { 5: buff[strlen(buff) - 1] = '\0'; /* replace newline with null */ 6: } 7: printf("%s ",buff); 

Smash built-in commands

Once you have your program reading from standard in you will add in a few basic commands and call your first system call! A shell will typically farm out 99% of the work to other programs. There are some things such as cd and exit that are built into the shell. We will be implementing a few built in commands before we start to fork/exec.

The Exit command

Your shell should exit when the user types in exit. You will need to use strcmp to compare the string literal "exit" with the user input.

The cd command

When a user types cd some/directory you will need to use the Linux system call chdir to change the directory of the current process (your smash process) to the given directory. If the user inputs an invalid directory, you need to output the string error: some/directory does not exist. Your shell will replace some/directory with the string the user input.

In order to determine if the chdir() command worked you will need to use the getcwd system call to get the current working directory after the cddir() command. Print the result to ensure that your directory did change. NOTE: Read the documentation for getcwd very carefully! You do NOT need to use malloc for this part of the project we are going to leverage a GNU extension to the POSIX.1-2001 standard that allows us to avoid malloc.

Your should print an error under the following conditions:

chdir() returned non-zero

getcwd() returned an unknown result

If the user enters any other command your shell should print the tokens in the format specified below. The $ is your smash prompt and will be hard coded for this part of the assignment. NOTE: Make sure and print the $ to stderr NOT stdout (use fprintf for this).

Example output

bash_shell>./smash $exit bash_shell>./smash $ls -l -a [0] ls [1] -l [2] -a $cd /home /home $cd /asdfasdf error: /asdfasdf does not exist $exit bash_shell> 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!