Question: Can you please solve this question and add the modified code to the main commandServer2.c file and make a seprate one? Thank you ================================================================================== '''

Can you please solve this question and add the modified code to the main commandServer2.c file and make a seprate one? Thank you Can you please solve this question and add the modified code

==================================================================================

'''

/* File: commandServer2.c Course: CENG320 Author: Leon King

Date: Monday Aug 31, 2020 2:40 PM

Currently working a demo.... Mar 2, 2023 I've left the modifications in

1. renamed f1 to 'displayUsers' - to illustrate that the function names should reflect their purpose.

2. sample is an example of looping through the remaining tokens and performing an action on each of them. In this case it uses access to count the # of readable files. This is similar to but is not identical to exercise 3d. It works and has been tested

3. sample2 is an example of a function that integrates a getopt loop and provides a hint for how to do 3g and 3h. It is not complete and the necessary step of counting the # of remainingTokens to determine argc is still left to you */

#include #include #include #include #include #include #include #include #include #include #include #include

void ctrlCHandler(int signum) { fprintf(stderr,"Command server terminated using C "); exit(1); }

//Examples of stub routines - the purpose here is to test commmand and control char *sample(char *cmd, char *remainingTokens[]) { int count=0; static char msg[1000]; for(int i=0;remainingTokens[i]; i++) { if(access(remainingTokens[i],R_OK)==0) count++; } sprintf(msg,"%d files are readably by me!", count); return msg; }

char *sample2(char * cmd,char * remainingTokens[]) { int argc; //this is the number of remaining tokens //Write a loop to count them! // while( (flag=getopt(argc,remainingTokens,"a:b:c")) != -1) { case 'a': //capture the flag break; case 'b': //capture the flag break; case 'c': //capture the flag break;

}

//Depending on what my flags have been set to, perform what's needed.

}

char * displayUsers() {

return "Command 'displayUsers' was received"; }

char * f2(char *cmd) { return "Command 'two' was received"; }

char * f3() { return "Command 'three' was received"; }

char * f4() { return "Command four was received"; }

//2 parallel arrays. commands is a list of commands to recognize // methods is an array of function pointers to call. char *commands[10]={"one","two","three","four", "alias","sample"} ; char *(*methods[10])()={displayUsers,f2,f3,f4,f2,sample};

//Alternate declaration struct CMDSTRUCT { char *cmd; char *(*method)(); } cmdStruct[]={{"dispU",displayUsers}, {"fred",displayUsers},{"mary",f2},{"clark",f3},{"sonia",f4},{NULL,NULL}} ;

char *interpret(char *cmdline) { char **tokens; char *cmd; int i; char *result;

tokens=history_tokenize(cmdline); //Split cmdline into individual words. if(!tokens) return "no response needed"; cmd=tokens[0];

//Detecting commands: table lookup: 2 techniques //Using the parallel arrays to look up function calls for(i=0;commands[i];i++) { if(strcasecmp(cmd,commands[i])==0) return (methods[i])(cmd,&tokens[1]); }

//Using struct CMDSTRUCT as an alternative lookup method. Pick either technique, not both //Note that its possible to create multiple aliases for the same command using either method. for(i=0;cmdStruct[i].cmd;i++) if(strcasecmp(cmd,cmdStruct[i].cmd)==0) return (cmdStruct[i].method)(cmd,&tokens[1]); return "command not found";

}

int main(int argc, char * argv[],char * envp[]) { char cmd[100]; char *cmdLine; char *expansion; time_t now=time(NULL); int nBytes; //size of msg rec'd

signal(SIGINT,ctrlCHandler); read_history("shell.log"); add_history(ctime(&now)); fprintf(stdout,"Starting the shell at: %s ",ctime(&now)); while(true) {

cmdLine=readline("Enter a command: "); if(!cmdLine) break; history_expand(cmdLine,&expansion); add_history(expansion); if(strcasecmp(cmdLine,"bye")==0) break; char *response=interpret(cmdLine); fprintf(stdout,"%s ",response); } write_history("shell.log"); system("echo Your session history is; cat -n shell.log"); fprintf(stdout,"Server is now terminated "); return 0; } '''

Create a command shell based on commandServer2.c that recognizes the following commands and calls appropriately named stub routines for each of them. Hand in a separate listing and set of tests that shows that the appropriate routine is called. Create the stubs in a separate file that is included with your main and interpret routine. DO NOT IMPLEMENT THE FUNCTIONALITY OF ANY OF THESE METHODS AT THIS STAGE. The only thing a stub routine should do is return a string "This functionName is not yet implemented" in order to identify which function was called. (4) Use a lookup table to find each command. If the command isn't in the list respond with "commandname not found"). a. export var=value unset var b. chdir dirname (3) c. access file1 file2 file3 ..... (2) d. chmod octalPermission file1 file2 file3 .... (3). e. path file1 file2 file3 .... (3) f. touch -m time1 -a time2 file1 file2 file3 ... (3). g. In -s -f file1 file2 (3) Demonstrate this program in the 1stlab

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!