/* * Assignment Three * * Write a program to prompt for a second program name * with optional arguments. Then execute the program * from a child of your program. * * Use the script program to show the program source code, * its compilation, and run time behavior. Show success * and failure of executing the command argument. * * Here is a test program that forks and executes the ls * program. Begin by testing it to see if it works. */ #include /* Defines printf() and other fxxxxx() I/O services */ #include /* Defines exit() and other library support routines */ #include /* Defines exec() family of services */ char command[255]; main (int argc, char *argv[], char *envp[]) { register char **p; int pid, status; char *arg="-ls"; char *dir="/bin"; char *prog="/bin/ls"; for (p = argv; *p ; p++) { printf ("%s\n", *p); } if (fork() == 0) { /* Child code */ gets (command); /* Gcc will complain - ignore */ if (execl (prog, prog, arg, dir, 0)) /* -1 return means error */ printf ("Cannot find file\n"); exit(222); } else { /* Parent code */ pid = wait (&status); printf ("Statushigh = %d, Statuslow = %d, PID = %d\n", ((status & 0xff00) >> 8), (status & 0xff), pid); } } =============================================== /* * This second example prompts the user and reads the typed string * into a buffer. String conditioning assumes the ls command. * You have to make it more general for all commands. */ #include /* Defines printf() and other fxxxxx() I/O services */ #include /* Defines exit() and other library support routines */ #include /* Defines exec() family of services */ char command[255]; main (int argc, char *argv[], char *envp[]) { register char **p; int pid, status; for (p = argv; *p ; p++) { printf ("%s\n", *p); } if (!fork()) { /* Child code */ printf ("Yes master?\n"); gets (command); printf ("%s\n", command); /* Assume /bin/ls -l was typed */ command[7] = 0; command[13] = 0; if (execl (command, command, &command[8], 0)) /* -1 return means error */ printf ("Cannot find file\n"); exit(123); } else { /* Parent code */ pid = wait (&status); printf ("Statushigh = %d, Statuslow = %d, PID = %d\n", ((status & 0xff00) >> 8), (status & 0xff), pid); } } =============================================== /* * A string processing sample is shown in this third example. */ #include /* Defines printf() and other fxxxxx() I/O services */ #include /* Defines exit() and other library support routines */ #include /* Defines exec() family of services */ #include /* Defines isxxxx() family of services */ #include /* Defines strxxx() family of services */ #define TRUE 1 #define FALSE 0 #define NULL '\0' char command[255]; main (int argc, char *argv[], char *envp[]) { register char **p; int count, pid, status; char *cp, *prog, *cmdptr, *arg1; if (fork() == FALSE) { // Child code - Concise version: if (!fork()) printf ("Yes master?\n"); // If fgets() reads in the newline and stores it into the buffer. // Also, null ('\0') character is placed after the last character in the buffer. count = strlen(command); printf ("String Length: %d\n", count); cp = command; // Initialize working pointer prog = cp; // Assume 1st char in command buffer is program name while (!isspace(*cp)) cp++; // Skip over program name until white space found if (*cp == '\n') goto error; // Reached end of string too soon, no argument *cp++ = NULL; // Terminate string and skip to next char in string printf ("ProgName: %s\n", prog); while (isspace(*cp)) cp++; // Skip white space at beginning of 1st argument arg1 = cp; // Save pointer to start of 1st argument while (!isspace(*cp)) cp++; // Skip over 1st argument until white space found *cp = NULL; // Null terminates string for C library routines printf ("Arg1: %s\n", arg1); // A loop is needed here to capture all arguments until end of command buffer found. // See "man strtok" for another (easier) way of doing string processing. printf ("Reached end of string\n"); printf ("Ready to exec\n"); fflush(stdout); if (execl (prog, prog, arg1, 0)) { /* -1 return means error */ printf ("Exec service failed\n"); fflush(stdout); error: printf ("Cannot find file\n"); fflush(stdout); exit(123); } // End if (exec) } else { // Parent code pid = wait (&status); printf ("Statushigh = %d, Statuslow = %d, PID = %d\n", ((status & 0xff00) >> 8), (status & 0xff), pid); } } ===============================================