103k views
3 votes
The following is a code fragment intended to handle the s execution operator ";" of a shell in programming assignment 1. Point out three errors in the code and correct them. void runcmd (struct cmd " cm ) 1. Switch ( cmd->type) case ;: : plicmd = (struct parcmd ) cmd; if ( fork ()==0 ) runcmd (plcmd->left); execute the command before ";" runcmd (plcmd->right); //execute the command after ";" break; J exit (0); 1

1 Answer

6 votes

Final answer:

The code fragment has several errors. The struct cmd parameter is not declared correctly, there is a missing colon after the case statement, and the parcmd variable is not declared correctly.

Step-by-step explanation:

The code fragment provided has several errors. Here are three errors and their corrections:The struct cmd parameter in the runcmd function is not declared correctly. It should be struct cmd *cm insteadof struct cmd " cm.There is a missing colon after the case statement. It should be case ';': instead of case ;:The parcmd variable is not declared correctly. It should be struct parcmd *plcmd instead of struct parcmd plcmd.

The code fragment provided is intended to handle the sequential execution of commands in a shell, using the execution operator ';'. There are a few errors in the provided code fragment:Incorrect casting syntax: The cast should be written with the correct data type inside parentheses followed by the variable.Improper structure field access: The '->' operator should be used to access the fields of the 'cmd' structure.Missingprocess exit: After the 'fork()' call, the child process should execute the left command and then exit to avoid running code intended for the parent process.
User Surya Rahul
by
8.1k points