Program description:

Unix shell with redirects and pipes The purpose of this assignment is to learn to develop multi-process programs. You are expected to extend the myshell.c program and add pipelines and I/O redirections. In particular, your shell program should recognize the following:

  1. > - Redirect standard output from a command to a file. Note: if the file already exist, it will be erased and overwritten without warning. For example,
COP4338$ ls > 1
COP4338$ sort myshell.c > 2

Note that you're not supposed to implement the unix commands (ls, sort, ...). You do need to implement the shell that invoke these commands and you need to "wire" up the standard input and output so that they "chain" up as expected.

  1. >> - Append standard output from a command to a file if the file exists; if the file does not exist, create one. For example,
COP4338$ sort myshell.c >> 1
COP4338$ echo "Add A Line" >> 1
  1. < - Redirect the standard input to be from a file, rather than the keyboard. For example,
COP4338$ sort < myshell.c
COP4338$ sort < myshell.c > 1
COP4338$ sort > 1 < myshell.c

The second and third commands are the same: the sort program reads the file named myshell.c as standard input, sorts it, and then writes to the standard output to file named 1.
4. | - Pass the standard output of one command to another for further processing. For example,

COP4338$ ls | sort
COP4338$ sort < myshell.c | grep main | cat > output

Make sure you have your parent process to fork the children and 'wire' them up using pipes accordingly Your program should be able to run with all the unix commands as expected. Don't assume your program will only be tested by the above commands. Also, there could be more than two commands chained by pipes, like the second example above.
Your program needs to provide necessary sanity-check from the user input. Prompt meaningful errors accordingly as what you'd experience with your shell.
Hint: use strtok() functions to separate the tokens and use strstr() to locate sub-strings.
One should be able to create the executable by simply 'make'. The Makefile should also contain a 'clean' target for cleaning up the directory
(removing all temporary files, object files and executable files). Make sure you don't include intermediate files: *.o, executables, *~, etc., in your submission. (There'll be a penalty for including unnecessary intermediate files).

Your code:

Make file

myshell: myshell.c
	cc myshell.c -o myshell
	
clean:
	rm -f myshell *.o *.err *~

.c File

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

#define MAX_ARGS 20
#define BUFFERSIZE 1024

//this functions will tokenize the command line
int get_args(char* cmdline, char* args[]){
	int i = 0;
	if((args[0] = strtok(cmdline, "\n\t ")) == NULL) 
		return 0; 

	while((args[++i] = strtok(NULL, "\n\t ")) != NULL){
		if(i >= MAX_ARGS){
			printf("Too many arguments!\n");
			exit(1);
		}
	}
	return i;
}

//this function handles the execution
//of the commmands that the user writes
//into the command line
void execute(char* cmdline){
	char* args[MAX_ARGS];	 //number of arugments
	int cmdstart[MAX_ARGS];	 //where to start
	int counter 	= 0;	 //argument counter
	int child 		= 1;	 
	int start 		= 0;	 //starting point
	int pcounter 	= 0;	 //pipe counter
	int pid 		= 0;	
	int status 		= 0;
	int i 			= 0;
	int j 			= 0;
	int cp 			= 0;	 //current position
	int fout	 	= 0;	 //file output	
	int fin 		= 0;	 //file input
	char* wfile;		 	 //file to write to
	char* rfile;			 //file to read from
	char* afile;			 //file to append to
	
	cmdstart[0] = 0;
	
	int nargs = get_args(cmdline, args);
	if(nargs <= 0)
		return;
	
	if(!strcmp(args[0], "quit") || !strcmp(args[0], "exit"))
		exit(0);

	while(args[counter]){
	
		if(!strcmp(args[counter], "|")){
			args[counter] = NULL;
			cmdstart[child] = counter + 1;
			child++;
		}
		counter++;
	}
	
	int fd[2*(child - 1)];	//file descriptor			
	
	for(counter = 0; counter < (child - 1); counter++)
		pipe(fd + (counter * 2));
	
	for(counter = 0; counter < child; ++counter){
		start = cmdstart[counter];	
		pid = fork();
		
		if(pid == 0){	
			while(args[start + i]){
				cp = start + i;	
				
				if(args[cp]){
					//redirects standard output from command to a file
					if(!strcmp(args[cp], ">")){
						wfile = args[cp + 1];
						args[cp] = NULL;
						fout = creat(wfile,0640);
					}
					//redirects standard input from a file
					else if(!strcmp(args[cp], "<")){
						rfile = args[cp + 1];
						args[cp] = NULL;
						fin = open(rfile, O_RDONLY);
					}
					//append standard output from command to a file
					else if(!strcmp(args[cp], ">>")){
						afile = args[cp + 1];
						args[cp] = NULL;
						fout = open(afile, O_CREAT | O_WRONLY | O_APPEND);
					}
				}
				i++;
			}
			if(fout){
				dup2(fout, 1);
				close(fout);
			}
			else{
				if(counter < (child - 1))
					dup2(fd[pcounter + 1], 1);
			}
			if(fin){
				dup2(fin, 0);
				close(fin);
			}
			else{
				if(pcounter != 0)
					dup2(fd[pcounter - 2], 0);	
			}
			for(j = 0; j < (2*(child - 1)); j++)
				close(fd[j]);
			execvp(args[start], args + start);
			perror("exec failed");	//failed to execute
			exit(-1);
		}
		else if(pid < 0){ 
			perror("fork failed");	//failed to fork
			exit(1);
		}
		pcounter += 2;
	}
	for(j = 0; j < (2*(child - 1)); j++)
		close(fd[j]); //close the file descriptor
	for(j = 0; j < (child); j++)
        wait(&status);
}

int main (int argc, char* argv []){
	char cmdline[BUFFERSIZE];
  
	for(;;) {
		printf("COP4338$ ");
		if(fgets(cmdline, BUFFERSIZE, stdin) == NULL) {
			perror("fgets failed");
			exit(1);
		}
			execute(cmdline);
	}
	return 0;
}

All the assignments will be posted. Subscribe for more! :)