/*-------------------------------------------------------------------*/ /* textforkexec.c */ /* The fork and exec system calls. */ /*-------------------------------------------------------------------*/ #include #include #include #include #define MAX_COMMAND_SIZE 1024 int main(int argc, char *argv[]) { int iPid; char pcCommandName[MAX_COMMAND_SIZE]; printf("%% "); scanf("%s", pcCommandName); while (strcmp(pcCommandName, "exit") != 0) { fflush(NULL); iPid = fork(); if (iPid == -1) {perror(argv[0]); return 1;} if (iPid == 0) { /* This code is executed by only the child process. */ char *ppcArgv[2]; ppcArgv[0] = pcCommandName; ppcArgv[1] = NULL; execvp(ppcArgv[0], ppcArgv); perror(argv[0]); exit(1); } printf("%% "); scanf("%s", pcCommandName); } return 0; } /* Sample execution (malformed): $ gcc -Wall -ansi -pedantic -o hello hello.c $ gcc -Wall -ansi -pedantic -o testforkexec testforkexec.c $ testforkexec % hello % hello process (29667) date % Tue Apr 20 21:46:28 EDT 2004 xxx % testforkexec: No such file or directory exit $ */