/* * prompting shell version 2 * Solves the 'one-shot' problem of version 1 * Uses execvp(), but fork()s first so that the * shell waits around to perform another command * New problem: shell catches signals. Run vi, press ^c */
intmain() { char *arglist[MAXARGS + 1]; // an array of ptrs int numargs; // index into array char argbuf[ARGLEN]; // read stuff here char * makestring(); // malloc etc
numargs = 0; while (numargs < MAXARGS) { printf("Arg[%d]?", numargs); if (fgets(argbuf, ARGLEN, stdin) && *argbuf != '\n') arglist[numargs ++] = makestring(argbuf); else { if (numargs > 0) { arglist[numargs] = NULL; // close list execute(arglist); numargs = 0; } } } return0; }
intexecute(char *arglist[]) /* * use fork and execvp and wait to do it */ { int pid, exitstatus;