#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <readline/re
ID: 3852525 • Letter: #
Question
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <readline/readline.h>
#include<string.h>
#include <readline/history.h>
#include <limits.h>
/* Simple example of using gnu readline to get lines of input from a user.
Needs to be linked with -lreadline -lcurses
add_history tells the readline library to add the line to it's
internal history, so that using up-arrow (or ^p) will allows the user
to see/edit previous lines.
*/
int main(int argc, char **argv) {
char *s;
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);}
Task 3: A Basic Shell – External Commands
If the user input does not match any internal commands, your shell must attempt to find an executable file
with the same name as the command, and to run the command. If the command name starts with either "/"
or "." the user is specifying a complete path to the executable file. If the command name doesn't specify a
complete path - your shell should assume it is a file name. Use the proper form of exec to search the path.
Your shell must support any number of command line arguments. For example, the user could type "ls -al
foo.c blah.h." As such, you need to make sure the ls command receives the command line arguments. Your
shell does not need to support directly any special characters like "*" or "?". As such, if a user types in "ls *.c",
just pass along the "*.c" to the ls command (a real shell would replace this with the name of all files that end
in .c).
For external commands (everything that is not an internal command), the program will create (fork) a child
process. The child process will use a member of the exec family of system calls to execute the external
command. Be sure to use fork and exec to create a new process to run the external command. The use of the
system function is NOT permitted.
Your shell should wait for the child process to complete before accepting more input from the user. Therefore,
you should use waitpid to wait for the child process (technically, wait can also be used as we are dealing with a
single child process).
If a command fails because the shell could not find the command to run, then the child process of shell should
print an error message and set the exit status to 1. This exit status can be collected by the parent process
waiting for the child process.
Explanation / Answer
compilation info
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.