Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

================================================================================== This is the original commandserver.c #include #include #include #include #include #include #include #include #include #include #include #include void ctrlCHandler(int signum) { fprintf(stderr,Command server terminated

image text in transcribed ================================================================================== This is the original commandserver.c

#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 * f1() { return "Command 'one' 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"} ; char *(*methods[10])()={f1,f2,f3,f4,f2}; //Alternate declaration struct CMDSTRUCT { char *cmd; char *(*method)(); } cmdStruct[]={{"fred",f1},{"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; }

==================================================================================== This the code after modify it. image text in transcribed

This is the second code for the interepter from the commandserver.c after modify it. image text in transcribed

3. Implement a 2nd version of your program which implements the following using C functions a. Instead of "command not found", if your interpret routine does not find the command have the C system call execute it and use the return code from the system call to display the status of the command. (2 marks) b. export name=value (2 marks) unset name Set the environment variable name to the value. Verify that name is set by issuing a echo command which, since it's not in the list, will be executed by system in a sub shell. An unset variable will display nothing when you attempt to echo it. c. chdir dirname (3) Change your cmdline prompt so that it always shows the current directory. (1) Have your function use the C chdir function to change directories and test by issuing a command to (1) Testing: i. Change to an absolute directory path ii. Change to a relative directory iii. Attempt to change to a directory that doesn't exist. iv. Have your program use perror to display an error message if chdir fails. (1) d. access file1 file2 file3 ... (2) Use the access function to test and report each of the 4 access properties separately for each file. If the file does not exist then only do that test and ignore the others. e. chmod octalPermission file1 file2 file3.... (3) Use sscanf to convert the octalPermission string to a number. Use the C chmod function to change the permissions of the files. If chmod fails use strerror to report the problem. Verify that the changes took place. f. path file1 file2 file3 (2) Display the realpath (full name), dirname and basename of each file. Your test cases should have g. touch m t1 -a t2 file1 file2 file3 .... (4 marks) i. Implement a small getopt loop in your function to implement this command. Set the default times for the modify and access flags to the current time seconds since the epoch. Use the getopt loop to capture mt1 and at2, if they are present where t1 and t2 are large integers representing ii. Verify that each file exists. 1. If it does use utimes to set both times 2. If it doesn't," use creat to create it, verify that you were successful, use futimes to set both times and then close the file. 3. Verify using the stat command that you were successful. h. Link -5 -f file1 file2 (4) In your function write a getopt loop to capture the 5 and/or -f flags if they are present. If the f (force) option is present use the appropriate "little" function to delete file2. If the -s (symbolic) option is present use the appropriate "little " function to create a hard link. Itemize your test cases and report on how successful you are

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Show Work Please

Answered: 1 week ago