Question: Task 4: Input/Output Redirection Your shell must support I/O redirection. If an external command is followed by , your shell must arrange it so
Task 4: Input/Output Redirection Your shell must support I/O redirection. If an external command is followed by "<", it must read input from the named file. For example, the command line "sort < foo" means that sort should be fed the file foo via stdin. If an external command is followed by a ">", your shell must arrange it so that stdout of the command is directed to a file. For example, "ls > save" means that the ls command should save its output to the file named "save". You could have both on the same command, for example: "sort < infile > outfile".
#include
while (s=readline("prompt> ")) { add_history(s); /* adds the line to the readline history buffer */
if((strcmp(s,"pwd"))==0)//Check if input is pwd { char* cwd; char buff[PATH_MAX + 1]; cwd = getcwd( buff, PATH_MAX + 1 ); printf( "Directory is %s. ", cwd );//Print the directory if input is pwd }
else if((strcmp(s,"exit"))==0)//Check if input is exit { exit(0);//Exit } else if(s[0]='c'&& s[1]=='d')//Check if first two letters of input is c and d {
char a[50],b[3]; sscanf(s,"%s %s",b,a);//Scanf the directory in a chdir(a);//Change directory to a } free(s); /* clean up! */ } return(0);}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
