You will write a Unix shell, a small command line interface to Unix. The shell will need the ability to launch applications (spawn other processes), do input and output redirection from/to a file < > , allow an application's output to be piped to another application's input |, and allow an application to be launched in the background &. You must implement the semicolon to separate commands.

The only builtin functions you need to create are exit and cd.

Simple examples follow. Note the prompt displays the process ID of the shell.

Sample Output:


Each command line will be no more than 2047 typed characters. Each symbol ( < > | ) will be surrounded on either side by at least one space ( cmd1<in|grep is not allowed). The & will be preceded by at least one space. You do NOT need to support wild cards (ls *.txt). The &, if it is present, must be the last item in a command line. A semicolon cannot follow an &.

Functions (and such) you will (probably) need: fork(), exec??(), strtok_r(), strsep(), dup2(), pipe(), waitpid(), STDOUT_FILENO, STDIN_FILENO

GitLab/GitHub (or how do I submit my work?):
You must store your source code in a Subversion repository on zeus or in GitLab/GitHub.
Git[Lab|Hub]: New repository CS460S18_Shell Name your project CS460_Shell_sche5694.
Makefile Targets
CS460_Shell: build the executable named CS460_Shell valgrind: start the executable with valgrind
valgrind -v --leak-check=yes --track-origins=yes --leak-check=full --show- leak-kinds=all ./CS460_Shell

You should not see any memory leaks or errors, even across fork()s.

debug: start the executable with the -d command line option
valgrind_debug: start the executable with the -d command line option with valgrind
clean clean

Executable Location
You should build the executable (CS460_Shell) at the root of your Project.


(10 pts) 1: Parsing the command line*, handle exit builtin, implement ;
(10 pts) 2: Launch an application (with arguments, with backgrounding, no redirection, no pipes), handle cd builtin, implement ;

(45 pts) 3: Finished Project

* See the example on the last page. For the first milestone, you will need to pick a data structure to accomplish this and you need to be able to display the parse of the command line as shown. Only display this parse if the user starts your shell with the -d command line option. When

this command line option is present you should parse and display the command only and not launch any applications. Use tabs to nest your output. Your terminal tab size may vary from this document.


Lookup the parameters that exec??() takes to guide you in building a data structure to represent the parsed command line.

Make sure the use can type either “ls” or “/usr/bin/ls” and get the same result. When in doubt, do whatever /bin/bash does!
This took me about 500 non-comment lines of code.

[chadd@arch-small CS460_Shell]$ ./CS460_Shell -d
21364> ls command:
ls arguments: none redirection:
stdin: none stdout: none
pipe: none background: no
21364> cat hint.txt > newFile.txt command: cat arguments: hint.txt redirection:
stdin: none stdout:
newFile.txt pipe: none
background: no
21364> cat hint.txt newFile.txt | grep fun & command: cat arguments: hint.txt newFile.txt redirection:
stdin: none stdout: PIPE
pipe: YES command: grep arguments: fun redirection:
stdin: PIPE stdout: none
pipe: none background: YES
21364> ls ; echo hi command: ls arguments: none redirection:
stdin: none stdout: none
pipe: none background: no command: echo arguments: hi redirection:
stdin: none stdout: none
pipe: none background: no
21364> exit
[chadd@arch-small CS460_Shell]$

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>

#define MAX_LEN 2048
#define YES    "YES"

struct Block {
char * command;
char * argv[32];
int argc;
char * in;
char * out;
char pipe[8];
char bg[8]; // background   
typedef struct Block Block;

* run when the user starts the executable with the -d command line option
* @param prompt
void debug(pid_t prompt);

* split string to array of token
* @param cmd the string
* @param len length of the array
* @return array of pointer to tokens
char ** split(char * cmd, int *len);

* turn array of token to array of block
* @param sp array of token address
* @param len length of the array
* @param bls array of empty blocks
* @return number of created block
int cmdToBlocks(char **sp, int len, Block bls[]);

* process the input and show the parse information
* @param cmd input string
* @param size size of input string
void debugProcessing(char * cmd, size_t size);


* process one command
* @param bl
* @return
int oneCommand(Block *bl);...

By purchasing this solution you'll be able to access the following files:

for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats