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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started