Question: I am having problems with parsing lines of code in my runcmd function. I was to be able to take in commends such as ls

I am having problems with parsing lines of code in my runcmd function. I was to be able to take in commends such as ls (1 args) or echo hello which takes two args #include "types.h" #include "user.h" #include "fcntl.h" //present a prompt to the user and wait for them to type something int getcmd(char *buf, int nbuf) { printf(2, "@ "); memset(buf, 0, nbuf); gets(buf, nbuf); if(buf[0] == 0) // EOF return -1; return 0; } // parse the command, and if valid, carry out the command // Execute cmd. Never returns. void runcmd(char* buf) { printf(1,"running command! %s ",buf); char* args[10]; char * current_str=0; int i=0; int x=0; // index of parameter while (buf[i] != '\0')//while buffer not at end { //append (buf[i], args) current_str[x]=buf[i]; if (buf[i]==' '|| buf[i]=='\0'){ i++;} args[i]=current_str; } //printf(1,"executing %s, %s ",args[0],args[1]); exec(args[0],args); exit(); // call exit() if exec() failed } int main(int argc, char* argv[]) { printf(1,"starting shell "); static char buf[100]; // Read and run input commands. while(getcmd(buf, sizeof(buf)) >= 0){ if(fork() == 0) runcmd(buf); wait(); } exit(); }

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!