Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a mini-shell using the template code. Implement function type_prompt() can

ID: 3833005 • Letter: W

Question

Write a mini-shell using the template code. Implement function type_prompt() can be very straightforward, as simple as one line of printf() or cout code. Making read_command() work could be a little more involved. The key is to properly prepare the parameters parameter. You can find example program on usage of execve() in its man page (man execve). Note that command contains the first argument, the file name; while parameters contains all information of the input line in an array of strings, including the executable file name, same as in the command, as its first string. Do not forget to initialize a dynamic string before filling it with characters.
Some of the most commonly used commands are built-in command handled by shell, for example, exit. You will not be able to test these commands with your shell unless you implement them within your shell program. Built-in commands is processed by the shell program without invoking the fork-exec() loop.

Explanation / Answer

#include #include #include #include #include #include #include extern int errno; #define CommandNumber 6 #define MaxCommandLine 128 #define MaxParamNumber 2 char* promptStr="minishell>"; char* delim=" "; char* helpStr= "Syntax: exit Syntax: cd directory Syntax: dir [directory] Syntax: del file Syntax: ren srcfile dstfile Syntax: copy srcfile dstfile "; enum CommandType { ExitType, CDType, DirType, DelType, RenType, CopyType }; char* commandStr[CommandNumber]= { "exit", "cd", "dir", "del", "ren", "copy" }; void exitShell(char* params[], int paramNumber); void changeDir(char* params[], int paramNumber); void listDir(char* params[], int paramNumber); void delFile(char* params[], int paramNumber); void renFile(char* params[], int paramNumber) ; void copyFile(char* params[], int paramNumber); void (*commandArray[CommandNumber])(char* params[], int paramNumber)= { exitShell, changeDir, listDir, delFile, renFile, copyFile }; int parseCommand(char* cmdStr, char* params[], int* paramNumber); void dodir(char* path); void printPrompt(); void errhandle(char* msg); int main(int argc, char* argv[]) { char buf[MaxCommandLine]; int n, paramNumber; int commandType; char* params[4]; printPrompt(); while ((n=read(STDIN_FILENO, buf, MaxCommandLine))>0) { buf[n]=''; commandType=parseCommand(buf, params, ¶mNumber); if (commandType==-1) { printf("illegal command %s"); } else { commandArray[commandType](params, paramNumber); } printPrompt(); } return 0; } int parseCommand(char* buf, char* params[], int* paramNumber) { int i; *paramNumber=0; if ((params[*paramNumber]=strtok(buf, delim))!=NULL) { for (i=CommandNumber-1; i>=0; i--) { if (strcmp(params[*paramNumber], commandStr[i])==0) { break; } } //when not found, i==-1 if (i==-1) { return i; } } else { return -1; } (*paramNumber)++; //the maximum param number is only 2, so I test for 3 to see if strtok return NULL while (1) { if ((params[*paramNumber]=strtok(NULL, delim))==NULL) { break; } (*paramNumber)++; if (*paramNumber==4) { //this means the param number is more than 2 and it is wrong return -1; } } return i; } void exitShell(char* params[], int paramNumber) { exit(0); } void renFile(char* params[], int paramNumber) { if (paramNumber!=3) { printf(helpStr); } else { if (strcmp(params[1], params[2])==0) { printf("The source file %s and the destination file %s can not be the same. ", params[1], params[2]); } else { if (access(params[1], F_OK)
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote