Directions: Modify your code below so that it implements the following internal
ID: 3938149 • Letter: D
Question
Directions:
Modify your code below so that it implements the following internal commands:
1. pwd Print current working directory.
2. cd [dir] Change directories.
3. exit Terminates the shell
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <curses.h>
#include <string.h>
extern char **environ;
int main(int argc, char **argv)
{
char *s;
char *token;
int count = 0;
int command = -1;
char *name;
while ((s=readline("prompt>")) != NULL) // reads from the command line
{
token = strtok(s, " "); // calls the strtoks function
while(token != NULL) //Running through the loop
{
if (count == 0)
{
if (strcmp (token, "set") == 0) //set command
{
command = 1;
}
else if (strcmp (token, "delete") == 0) //delete command
{
command = 2;
}
else if (strcmp (token, "print") == 0) //print command
{
command = 3;
}
}
else if (count == 1) // Goes through the while loop
{
if (command == 1) //set the value of of the enviornment variable with varname
{
name = token;
}
else if (command == 2) //removes the enviorment variable
{
unsetenv(token);
}
else if (command == 3)//prints the environment variable
{
printf("%s = %s ",token,getenv(token));
}
}
else if (count == 2)
{
}
else if (count == 3) //sets the value of the variable name
{
setenv(name, token, 1);
}
token = strtok(NULL, " ");
count++;
}
count = 0;
add_history(s); /* adds the line to the readline history buffer */
free(s); /* clean up! */
}
return(0);
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <curses.h>
#include <string.h>
extern char **environ;
int main(int argc, char **argv)
{
char *s;
char *token;
int count = 0;
int command = -1;
char *name;
//Added these lines.
chdir("/path/to/change/directory/to");
getcwd(cwd, sizeof(cwd));
while ((s=readline("prompt>")) != NULL) // reads from the command line
{
token = strtok(s, " "); // calls the strtoks function
while(token != NULL) //Running through the loop
{
if (count == 0)
{
if (strcmp (token, "set") == 0) //set command
{
command = 1;
}
else if (strcmp (token, "delete") == 0) //delete command
{
command = 2;
}
else if (strcmp (token, "print") == 0) //print command
{
command = 3;
}
}
else if (count == 1) // Goes through the while loop
{
if (command == 1) //set the value of of the enviornment variable with varname
{
name = token;
}
else if (command == 2) //removes the enviorment variable
{
unsetenv(token);
}
else if (command == 3)//prints the environment variable
{
printf("%s = %s ",token,getenv(token));
}
}
else if (count == 2)
{
}
else if (count == 3) //sets the value of the variable name
{
setenv(name, token, 1);
}
token = strtok(NULL, " ");
count++;
}
count = 0;
add_history(s); /* adds the line to the readline history buffer */
free(s); /* clean up! */
}
return(0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.