int main(int argc, char** argv) { //int i; //for(i = 0; i < argc; i++)printf("\targv[%d] = %s\n", i, argv[i]); runstat(argv+1); return 0; }
int main(int argc, char** argv) { int exit = 0; char* input = NULL; size_t buflen = 0; ssize_t inputlen = 0; while(!exit){ printf(PROMPT); inputlen = getline(&input, &buflen, stdin); //printf("READ:%s", input); if(inputlen == -1){ break; } else if (inputlen > 128){ printf("Command was too long (> 128 characters)"); } else { char* tokens[MAX_TOKENS]; char* token = NULL; int tokencount = 0; printf("INPUT:%s\n", input); input = strtok(input, "\n"); //remove endline character using strtok while((token = strtok(input, " ")) != NULL) { input = NULL; if(tokencount > MAX_TOKENS){ printf("Too many tokens in command (> 32 tokens)\n"); break; } tokens[tokencount] = token; tokencount++; } tokens[tokencount] = '\0'; /*int i; for(i = 0; i < tokencount; i++){ printf("token %d = %s\n", i, tokens[i]); }*/ if(strcmp(tokens[0], CMD_EXIT) == 0){ exit = 1; } else if (strcmp(tokens[0], CMD_CD) == 0){ int result = chdir(tokens[1]); if(result == -1){ printf("Invalid syntax for cd\n"); } } else { int childPID = fork(); if(childPID < 0){ printf("fork() failed"); } else if(childPID == 0){ runstat(tokens); exit = 1; // this branch is a child process, so exit } else { wait(childPID); } } } input = NULL; } return 0; }
/* * getstring reads a string from the keyboard, doing all necessary * output flushing. Prompting is to be done by the caller. This * routine should be called instead of os_gets. */ int getstring(char *prompt, char *buf, int bufl) { char *result; int savemoremode; int retval; /* show prompt if one was given and flush output */ savemoremode = setmore(0); if (prompt != 0) { /* display the prompt text */ outformat(prompt); /* make sure it shows up in the log file as well */ out_logfile_print(prompt, FALSE); } outflushn(0); outreset(); /* read from the command input file if we have one */ if (scrfp != 0) { int quiet = scrquiet; /* try reading from command input file */ if ((result = qasgets(buf, bufl)) == 0) { /* * End of command input file; return to reading the * keyboard. If we didn't already show the prompt, show it * now. * * Note that qasgets() will have closed the script file * before returning eof, so we won't directly read the * command here but instead handle it later when we check to * see if we need to read from the keyboard. */ if (quiet && prompt != 0) outformat(prompt); outflushn(0); outreset(); /* * Guarantee that moremode is turned back on. (moremode can * be turned off for one of two reasons: we're printing the * prompt, or we're reading from a script with no pauses. * In either case, moremode should be turned back on at this * point. -CDN) */ savemoremode = 1; /* turn off NONSTOP mode now that we're done with the script */ os_nonstop_mode(FALSE); } /* success */ retval = 0; } /* if we don't have a script file, read from the keyboard */ if (scrfp == 0) { /* update the status line */ runstat(); /* read a line from the keyboard */ result = (char *)os_gets((uchar *)buf, bufl); /* * if the result is null, we're at eof, so return a non-zero * value; otherwise, we successfully read a command, so return * zero */ retval = (result == 0); } /* restore the original "more" mode */ setmore(savemoremode); /* check the result */ if (retval != 0) { /* we got an error reading the command - return the error */ return retval; } else { char *p; /* * we got a command, or at least a partial command (if we timed * out, we may still have a partial line in the buffer) - write * the input line to the log and/or command files, as * appropriate */ out_logfile_print(buf, TRUE); if (cmdfile != 0) { os_fprintz(cmdfile, ">"); os_fprintz(cmdfile, buf); os_fprintz(cmdfile, "\n"); } /* translate the input to the internal character set */ for (p = buf ; *p != '\0' ; ++p) *p = cmap_n2i(*p); /* success */ return retval; } }