Question: #include types.h #include user.h #include fcntl.h int getcmd(char *buf, int nbuf) { printf(2, @ ); memset(buf, 0, nbuf); gets(buf, nbuf); if(buf[0] == 0) // EOF

#include "types.h" #include "user.h" #include "fcntl.h"

int getcmd(char *buf, int nbuf) { printf(2, "@ "); memset(buf, 0, nbuf); gets(buf, nbuf); if(buf[0] == 0) // EOF return -1; return 0; }

// Execute cmd. Never returns. void runcmd(char* buf) { printf(1,"running command! %s ",buf); // int retval; char* args[10]; args[0] = "ls"; args[1] = 0; //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(); } //create function to parse buf (instead of just hardcoding the 'ls' command as i do here in runcmd //create function to implement pipes and redirection

I need help parsing simple commands like ls which only takes one argument or echo which takes two.

example: ls would display available files in folder

echo hello would display hello to the user. Problem is all that is working is ls but it is hard coded.

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!