Difference between
modified post b9e by Anonymous on Fri 14th Oct 2011 09:04
original post 77a14 by Anonymous on Fri 14th Oct 2011 04:11
Download diff
Show
old version |
new version |
both versions
| 1 | 1 | /* CODE WILL TAKE A COMMAND, PASS IT TO CREATEPROCESS(), | |
| 2 | 2 | * FORK A CHILD AND EXECUTE COMMAND | |
| 3 | 3 | * | |
| 4 | 4 | * | |
| 5 | 5 | */ | |
| 6 | 6 | ||
| 7 | 7 | #include <unistd.h> | |
| 8 | 8 | #include <sys/types.h> | |
| 9 | 9 | #include <stdio.h> | |
| 10 | 10 | #include <stdlib.h> | |
| 11 | 11 | ||
| 12 | 12 | char* buffer; | |
| 13 | 13 | pid_t pid; | |
| 14 | 14 | ||
| 15 | 15 | void startProcess( char* ); | |
| 16 | 16 | ||
| 17 | 17 | int main () | |
| 18 | 18 | { | |
| 19 | 19 | char* input = (char*)malloc( 128 ); | |
| 20 | 20 | buffer = NULL; | |
| 21 | 21 | ||
| 22 | 22 | while( 1 ) | |
| 23 | 23 | { | |
| 24 | 24 | printf( "EECE315_Shell - %s$ ", getcwd(NULL, 0) ); | |
| 25 | 25 | gets( input ); | |
| 26 | 26 | if( strcmp(input, "exit") == 0 ) | |
| 27 | 27 | { | |
| 28 | 28 | printf( "Terminating shell...\n" ); | |
| 29 | 29 | return 0; | |
| 30 | 30 | } | |
| 31 | 31 | else if( strcmp(input, "") != 0 ) | |
| 32 | 32 | { | |
| 33 | 33 | startProcess( input ); | |
| 34 | 34 | } | |
| 35 | 35 | } | |
| 36 | 36 | } | |
| 37 | 37 | ||
| 38 | 38 | void startProcess( char* input ) | |
| 39 | 39 | { | |
| 40 | 40 | pid = fork(); | |
| 41 | 41 | ||
| 42 | 42 | if( pid == 0 ) | |
| 43 | 43 | { | |
| 44 | 44 | execvp( input, NULL ); | |
| 45 | 45 | } | |
| 46 | 46 | else if( pid > 0 ) | |
| 47 | 47 | { | |
| 48 | 48 | //parent wait | |
| 49 | 49 | } | |
| 50 | 50 | else | |
| 51 | 51 | { | |
| 52 | 52 | printf( "Fork returned error code, no child" ); | |
| 53 | 53 | } | |
| 54 | 54 | } | |
| 55 | 55 |
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
513.kpaste.net RSS