int main (int argc, char* argv[]) { char cmdln[MAX_LEN]; char items[MAX_ITEMS][MAX_LEN]; int nr_items; int i; while(1) { printf(">"); fgets(cmdln,MAX_LEN,stdin); nr_items = parseitems(cmdln, items); if( strcmp(items[0], "exit")==0 ) { printf("Goodbye!\n"); return 0; } if(nr_items==0) continue; printf("# of arguments: %d\n", nr_items); for(i=0; i<nr_items; i++) { printf("-argv[%d]: %s\n",i, items[i]); } } return 0; }
struct room* parseroom() { struct room* r = (struct room*) malloc(sizeof(struct room)); r->id = atoi(strtok(NULL, NEWLINE)); r->type = (char*) malloc(sizeof(char) * 16); strcpy(r->type, strtok(NULL, NEWLINE)); int textlines = atoi(strtok(NULL, NEWLINE)); r->text = (char*) malloc(sizeof(char) * 4000); int offset = 0; int i; for (i = 0; i < textlines; ++i) { char* line = strtok(NULL, NEWLINE); strcpy(r->text + offset, line); offset += strlen(line); r->text[offset] = '\n'; offset++; } r->text[offset] = 0; parseexits(r); parseitems(r); parsemobs(r); return r; }
int main (int argc, char* argv[]) { char cmdln[MAX_LEN]; char items[MAX_ITEMS][MAX_LEN]; int nr_items; int i; pid_t pid; int status; char * args[MAX_ITEMS]; while(1) { printf(">"); fgets(cmdln, MAX_LEN, stdin); nr_items = parseitems(cmdln, items); if( strcmp(items[0], "exit")==0 ) { printf("Goodbye!\n"); return 0; } if(nr_items==0) continue; for(i=0; i<MAX_ITEMS; i++) args[i]=NULL; for(i=0 ; i<nr_items; i++) args[i]=items[i]; if((pid=fork())==0) { if(execvpe(items[0], args, environ) <0) { printf("%s: Command not found.\n", items[0]); exit(0); } } else { if(waitpid(pid,&status, 0)<0) { printf("waitpid error\n"); } } } return 0; }
int main (int argc, char* argv[]) { char cmdln[MAX_LEN]; char items[MAX_ITEMS][MAX_LEN]; int nr_items; pid_t pid; int status, bg; int i; char* args[MAX_ITEMS]; signal(SIGCHLD, child_handler); // Initialize for(i=0; i<MAX_BG_ITEMS; i++) P[i].pid = -1; while(1) { usleep(1000 * 10); printf(">"); fgets(cmdln, MAX_LEN, stdin); bg=0; nr_items = parseitems ( cmdln, items ); // Check EXIT if( strcmp(items[0], "exit")==0 ) { printf("Goodbye!\n"); return 0; } // Check STATUS if( strcmp(items[0], "status")==0 ) { for(i=0; i<MAX_BG_ITEMS; i++) { if(P[i].pid!=-1) printf("[%d] %s\n", P[i].pid, P[i].cmd); } strcpy(items[0],""); continue; } // Check # of argument if(nr_items==0) continue; /* // Check Close if(strcmp(items[0], "close")==0) { pid=atoi(items[1]); for(i=0; i<MAX_BG_ITEMS; i++) { if(P[i].pid == pid) { P[i].pid=-1; waitpid(pid, &status, 0); printf("[%d] closed\n", pid); num--; } } continue; } */ if(strcmp(items[0], "killone")==0) { pid=atoi(items[1]); killone(pid); continue; } if(strcmp(items[0], "killemall")==0) { killemall(); continue; } if(strcmp(items[0], "fg")==0) { if(num==0) printf("no background process\n"); else { waitpid(stack[Spos-1], &status, 0); for(i=0; i<MAX_BG_ITEMS; i++){ if(P[i].pid==stack[Spos-1]) P[i].pid=-1; } Spos--; num--; } continue; } // Check background if(strcmp(items[nr_items-1],"&")==0) { // Check Maximum if(num>=MAX_BG_ITEMS) { printf("Failed: maximum # of background job is %d\n",MAX_BG_ITEMS); continue; } else bg=1; } if(bg) nr_items--; // Erase buffer for(i=0; i<MAX_ITEMS; i++) args[i]=NULL; for(i=0; i<nr_items; i++) args[i]=items[i]; if((pid=fork())==0) { if(bg){ printf("[%d] %s \n", getpid(), items[0]); } if(execvpe(items[0], args, environ) <0) { printf("%s: Command not found.\n", args[0]); exit(0); } } else { if(!bg) { if(waitpid(pid, &status, 0) < 0 ) printf("waitpid error\n"); } else { for(i=0; i<MAX_BG_ITEMS; i++) { if(P[i].pid==-1) { P[i].pid=pid; strcpy(P[i].cmd, args[0]); stack[Spos]=pid; break; } } Spos++; num++; } } } return 0; }