Print to stdout a generic shell prompt \">> \" and wait for user input (a string
ID: 3914176 • Letter: P
Question
Print to stdout a generic shell prompt ">> " and wait for user input (a string) by calling getchar(). The input entered by the user is treated as a command to a generic Linux shell (e.g., tcsh, bash) and executed using the function system(). The app you are writing can be viewed as your customizable shell that takes commands (binary executables or interpreted shell commands) as input and executes them. As such, your code must follow the structure of an infinite loop within which user input and execution of user requested command is performed using system(). This is the design of a canonical client/server app. Code, compile, and test the shell, call it vsh, and verify that it works correctly.
To make it a tad more useful, add the following custom features to vsh:
First, when the user enters "nprompt <string>" then make vsh change its prompt from its default ">> " to whatever <string> has been entered followed by a space.
Second, if the user hits the return key without providing any input, go to a new line, print "What do you want? ", and wait for user input.
Third, if the user enters "Q" or "q" then vsh should exit, that is, terminate after printing a suitable message of your choice to stdout. Note that when vsh terminates, your terminal returns to the control of the shell program under which you ran vsh.
Fourth, when the user enters "secure" then lock the terminal by printing the string "secret key: " on stdout and waiting on a secret string to be entered to unlock. Hardcode the secret string by using the C preprocessor command #define. Set the secret key as "purduepete". If the secret string is correctly entered, unlock by printing the prompt and resuming normal operation. On any other input, print "secret key: " on a new line and keep waiting for the correct secret string to be entered. Up to three attempts are allowed. If the third attempt fails, exist vsh with a suitable message printed to stdout.
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SECRET "purduepete" // using C preprocess command #define to set secret key
int main()
{
char prompt[100] = ">>"; // string for prompt
char input[100]; // string for user input
while (1) // infinite loop
{
printf("%s ",prompt); // display prompt
char c;
int i = 0;
while ((c = getchar()) != ' ') // read characters using getchar() until ' ' is encountered
{
input[i++] = c; // append read character into input
}
input[i] = 0; // terminate input string
if (strncmp(input, "nprompt ", 8)==0) // check if first word of input is "nprompt", first feature
{
int j = 0;
for (int i = 8; i < strlen(input); i++) // iterate over the rest of the input after "nprompt "
prompt[j++] = input[i]; // copy the characters from the input to prompt
prompt[j] = 0; // terminate prompt string
}
else if(strlen(input)==0) // check is input string is empty, second feature
{
printf("What do you want? "); // display the required statement
}
else if(strcmp(input,"q")==0||strcmp(input,"Q")==0) // check if input string is "q" or "Q", third feature
{
printf("Thank you for using vsh! "); // display exit message
break; // break infinite loop to exit
}
else if(strcmp(input,"secure")==0) // check if input string is "secure", fourth feature
{
int attempts = 0; // keeps track of incorrect attempts
char secret[100]; // string for secret key entered by user
while (attempts < 3)
{
int i = 0;
printf("secret key: "); // display
while((c=getchar())!=' ') // read characters using getchar() until ' ' is encountered
secret[i++] = c; // append read character to secret
secret[i] = 0; // terminate secret string
if(strcmp(secret,SECRET)==0) // if secret string matches with SECRET
break; // successfully unlock
else
attempts++; // incorrect secret key, add 1 to attempts
}
if(attempts==3) // if key is incorrectly entered for 3 times
{
printf("You failed to enter the secret key in 3 attempts! Exiting. "); // print appropriate exit message
break; // break infinite loop to exit
}
}
else
system(input); // execute input string by using the system() command
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.