Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help! need help figuring out this code! This is C++ and I'm working on a .cpp file that will create a shell program. I need

Help! need help figuring out this code! This is C++ and I'm working on a .cpp file that will create a shell program. I need to accomplish a requirement in my program called NEWNAME:
please refer to the second picture for instructions
image text in transcribed
image text in transcribed
or if you can help me with the other requirements it'd be much appreciated!
thanks
this is my code so far:
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
This is a C++ program where I'm making a shell program. This is my cpp file. I included the most important parts of code. The last picture where the code is highlighted is what I need the most help in. The "int NEWNAME (char **args)" needs to be able to manage an alias list. There's suppose to be a first option that deletes a previously define alias and there's a second option that defines an alias for a command. For example, command NEWNAME MYMOVE deletes the alias for MYMOVE. Then the command NEWNAME MYCOPY CP defines MYCOPY as the alias for the CP command. If an alias for a command already exists, then the new alias replaces the old alias. The maximum number of aliases in the list should be set to 10 as default.
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Iloption 3 below? \{ intl=0;while(k : Manages the alias another command. deletes a previously defined alias. The second option defines an alias for mymove, the command NEWle, the command NEWNAME mymove deletes the alias for command. If an alias for a command mycopy cp defines mycopy as the alias for the cp The maximum number of aliases in the alias exists, then the new alias replaces the old alias. e. (6 marks) LISTNEWNAMES: Outputs all 10 as the default. names should be shown on one line. For exall aliases that have been defined. Each pair of are shown below: mycd cd mycopy cp f. (6 marks) SAVENEWNAMES > : Stores all currently defined aliases in the file file_name>. g. (5 marks) READNEWNAMES : Reads all aliases in the file file_name . h. (2 marks) > : Executes the UNIX command UNLX command >, corresponding to any valid UNIX command. If the first token on a command line is not a built-in command, assume that it is a UNIX command. i. (2 marks) HELP: shows all built-in commands supported by myshell. Whepp > Q ishuread_line(void) sinctude esysait.hs sinclude aunisted ht Anclude estdulb, hp: sinclude estdio.hs minclude string h s cinclude csys/types,hp char sheltnase = mashelt": chare terinator =m>=3 ft. Funce int Lsh_cd(char wargs): int Lhinetptchar wargsty int lsh_exit(char warps)s int setshellngetchar wargs): Int Lsh_launch(char *iargely Int tshiexecutefchar weargsl: int Lhiteopfchar wares): int settereinaterfchar wargs): Int nemane(char wargs) Int Histinemanes(char wargs) int savenemanes (char wargs); int geadnewnestes (char wargs); Iw List of builtin comands, followed by their corresponding functions. char abuittinzstril =t ted "help", "setshelinse" "settermin "Listnewnanes" "savencknates" Int (wbuiting fune(1) (char we) = t iti sh of ticpt int (wbulttin_funct]) (char ) ={ 6tsh_cd, ctsh_help, thtshexit, Csetshetuane, Esettereinator, Cnewnene, GListnewnases, Gsavenewnanes, Greadneinanes, Ps int (sh_nua bulttins() \& return sizeof (bulttingstr) I sizeof (char )/ Buittin function inplenentations. eparan ares List of args. eres [e] is "cd", args(2] is the directery. ereturn Always returns 1 , to continue executing. iof int Lshedtchar wares) t If (args[1]=MuL)f fprintf(stderr, "Ishi expected argument to Vcodcos(n4) ) 1 etse t if (chdir(ares (1)) in te) 1 perrer ( " (skA1)s 3 return if 3 f(x) ebrief buittin conands print hetp. eparan ares List of argu. Not cukained. ereturn Always retums 1, to continue executing. it int Ish helptchar wargs) t. int it printf("stephen Brennan's Lsinn1)1s printf("Type prograe nases and argusents, asd hit enter. n/3 ecpp > ) Ish_read_line(void) int 15 printf("Stephen Brennan's LLSHM" ); printf("Type prograa nanes and argunents, and hit enter. (n)/1 printf("The following are built in: (n); for (1=0;1 3 1= eorief Launch a prograe and wait for it to terninate. eparas ares Null terninated list of argunents (including prograa). ereturn Always returns 1 , to continue execution. if Int lsh_launch (char wargs) 1 pid_t pid, vpid; int status; pid = fork(1): if ( pid =0) it Il Child process if (execvplarestol, args) =1) t perror ( "tsh"): ) exit (EXI_FAILURL): r else if (pid =. while (1) 1 Il Read a character c=getchar(); If If we hit EOF, replace it with a nutl character and return. if (c= EOF c=(n2) i buffer[position] =16; return buffer; else 4 buffer [posttion]= t) positiontiti II If we have exceeded the buffer, reallocate. if (position a bufsize) \& bufsize te LSH_R__BUFSIZE; buffer = realloc(buffer, bufsize); if (Ibutfer) \{ fprintf(stderr, "Lsht allocation errorkn"); exit(EXIT_FAILURE)/ \} 3 3 Jdefine LSH_TOK_BUFSIZE 64 *define LSH_TOK.DELIM = ItVrIAVa" f gbrief split a line into tokens (very naively). eparan Line The Uine. ereturn Null-terminated array of tokens. i= char aklsh_split_Line(char stine) t. Int bufsize = LSH_TOK_BUFSI2E, position =t char *tokens = malloc(bufsize * sizeof ( char* ) ); char wtoken: if (Itokens) \{ fprintf(stderc, "Lsht atlocation errorli":) , exit(EXIT_FAILUAE): 3 token = strtok(Line, LSH_ToK_DeLIM); while (token I= WILL) \& tokens [ position ]= token: positiont+i If (position se bufsize) \& bufsize += L.5H_TOK_eursiz; tokens = reatloc(tokens, bufsize =sizcof( chare) ) ) if (ttokens) if. hwa 2.cpp> fprintf(stderr, "Lsh: allocetion errorln"); exit(EXIT_FAILURE): b \}. token = strtok(NULL, LSH_TOK_DELIM) : f tokens [position] = NULLi return tokenst j 1 ebrlef Loop getting input and executing it. f void Ish_loop(void) t char whine; char wiargs; int status; do 1 printf("ks > ", shettnane); tine = tsh_read_tine( ): args = tsh_split_tine(tine) f status = Lh__execute(args); I/free(tine): I/free(args); \} while (status): 3 10 ebrief Main entry point. gearal arge Argusent count. eparan argv Argunent vector. ereturn status code 4 *f int main(int argc, char wargv) If Load contig files, if any. 1/ Mun coenand Loop. tsh_loop ( t) 1/ Perfore any shutdown/cleanup. return ExIT_success; List of builtin commands, followed by their corresponding functions. Iloption 3 below? \{ intl=0;while(k : Manages the alias another command. deletes a previously defined alias. The second option defines an alias for mymove, the command NEWle, the command NEWNAME mymove deletes the alias for command. If an alias for a command mycopy cp defines mycopy as the alias for the cp The maximum number of aliases in the alias exists, then the new alias replaces the old alias. e. (6 marks) LISTNEWNAMES: Outputs all 10 as the default. names should be shown on one line. For exall aliases that have been defined. Each pair of are shown below: mycd cd mycopy cp f. (6 marks) SAVENEWNAMES > : Stores all currently defined aliases in the file file_name>. g. (5 marks) READNEWNAMES : Reads all aliases in the file file_name . h. (2 marks) > : Executes the UNIX command UNLX command >, corresponding to any valid UNIX command. If the first token on a command line is not a built-in command, assume that it is a UNIX command. i. (2 marks) HELP: shows all built-in commands supported by myshell. Whepp > Q ishuread_line(void) sinctude esysait.hs sinclude aunisted ht Anclude estdulb, hp: sinclude estdio.hs minclude string h s cinclude csys/types,hp char sheltnase = mashelt": chare terinator =m>=3 ft. Funce int Lsh_cd(char wargs): int Lhinetptchar wargsty int lsh_exit(char warps)s int setshellngetchar wargs): Int Lsh_launch(char *iargely Int tshiexecutefchar weargsl: int Lhiteopfchar wares): int settereinaterfchar wargs): Int nemane(char wargs) Int Histinemanes(char wargs) int savenemanes (char wargs); int geadnewnestes (char wargs); Iw List of builtin comands, followed by their corresponding functions. char abuittinzstril =t ted "help", "setshelinse" "settermin "Listnewnanes" "savencknates" Int (wbuiting fune(1) (char we) = t iti sh of ticpt int (wbulttin_funct]) (char ) ={ 6tsh_cd, ctsh_help, thtshexit, Csetshetuane, Esettereinator, Cnewnene, GListnewnases, Gsavenewnanes, Greadneinanes, Ps int (sh_nua bulttins() \& return sizeof (bulttingstr) I sizeof (char )/ Buittin function inplenentations. eparan ares List of args. eres [e] is "cd", args(2] is the directery. ereturn Always returns 1 , to continue executing. iof int Lshedtchar wares) t If (args[1]=MuL)f fprintf(stderr, "Ishi expected argument to Vcodcos(n4) ) 1 etse t if (chdir(ares (1)) in te) 1 perrer ( " (skA1)s 3 return if 3 f(x) ebrief buittin conands print hetp. eparan ares List of argu. Not cukained. ereturn Always retums 1, to continue executing. it int Ish helptchar wargs) t. int it printf("stephen Brennan's Lsinn1)1s printf("Type prograe nases and argusents, asd hit enter. n/3 ecpp > ) Ish_read_line(void) int 15 printf("Stephen Brennan's LLSHM" ); printf("Type prograa nanes and argunents, and hit enter. (n)/1 printf("The following are built in: (n); for (1=0;1 3 1= eorief Launch a prograe and wait for it to terninate. eparas ares Null terninated list of argunents (including prograa). ereturn Always returns 1 , to continue execution. if Int lsh_launch (char wargs) 1 pid_t pid, vpid; int status; pid = fork(1): if ( pid =0) it Il Child process if (execvplarestol, args) =1) t perror ( "tsh"): ) exit (EXI_FAILURL): r else if (pid =. while (1) 1 Il Read a character c=getchar(); If If we hit EOF, replace it with a nutl character and return. if (c= EOF c=(n2) i buffer[position] =16; return buffer; else 4 buffer [posttion]= t) positiontiti II If we have exceeded the buffer, reallocate. if (position a bufsize) \& bufsize te LSH_R__BUFSIZE; buffer = realloc(buffer, bufsize); if (Ibutfer) \{ fprintf(stderr, "Lsht allocation errorkn"); exit(EXIT_FAILURE)/ \} 3 3 Jdefine LSH_TOK_BUFSIZE 64 *define LSH_TOK.DELIM = ItVrIAVa" f gbrief split a line into tokens (very naively). eparan Line The Uine. ereturn Null-terminated array of tokens. i= char aklsh_split_Line(char stine) t. Int bufsize = LSH_TOK_BUFSI2E, position =t char *tokens = malloc(bufsize * sizeof ( char* ) ); char wtoken: if (Itokens) \{ fprintf(stderc, "Lsht atlocation errorli":) , exit(EXIT_FAILUAE): 3 token = strtok(Line, LSH_ToK_DeLIM); while (token I= WILL) \& tokens [ position ]= token: positiont+i If (position se bufsize) \& bufsize += L.5H_TOK_eursiz; tokens = reatloc(tokens, bufsize =sizcof( chare) ) ) if (ttokens) if. hwa 2.cpp> fprintf(stderr, "Lsh: allocetion errorln"); exit(EXIT_FAILURE): b \}. token = strtok(NULL, LSH_TOK_DELIM) : f tokens [position] = NULLi return tokenst j 1 ebrlef Loop getting input and executing it. f void Ish_loop(void) t char whine; char wiargs; int status; do 1 printf("ks > ", shettnane); tine = tsh_read_tine( ): args = tsh_split_tine(tine) f status = Lh__execute(args); I/free(tine): I/free(args); \} while (status): 3 10 ebrief Main entry point. gearal arge Argusent count. eparan argv Argunent vector. ereturn status code 4 *f int main(int argc, char wargv) If Load contig files, if any. 1/ Mun coenand Loop. tsh_loop ( t) 1/ Perfore any shutdown/cleanup. return ExIT_success; List of builtin commands, followed by their corresponding functions<><>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Peter Robb,Carlos Coronel

5th Edition

061906269X, 9780619062699

More Books

Students also viewed these Databases questions

Question

What is the managerial capacity problem?

Answered: 1 week ago

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago