Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 2nd, 2012  |  syntax: None  |  size: 1.05 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. after fork/execvp control does not return to parent
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5.  
  6. int main(int argc, char* argv[]){
  7.     while(1){
  8.         print_the_prompt();
  9.         char user_text[100];
  10.         if( fgets(user_text, sizeof(user_text), stdin) != NULL ){
  11.             char* nl_char = strchr(user_text, 'n');
  12.             if( nl_char != NULL ){
  13.                 *nl_char = '';
  14.             }
  15.         }
  16.  
  17.     //printf("user_text = "%s"n", user_text);
  18.  
  19.         if( is_command_built_in(user_text) ){
  20.             //run built in command
  21.         }
  22.         else{
  23.             //run regular command
  24.             execute_new_command(user_text);
  25.         }
  26.     }
  27.  
  28.     return 0;
  29. }
  30.  
  31. void print_the_prompt(){
  32.         printf("!:  ");
  33. }
  34.  
  35. int is_command_built_in(char* command){
  36.     return 0;
  37. }
  38.  
  39. void execute_new_command(char* command){
  40.     pid_t pID = fork();
  41.     if(pID == 0){
  42.         //is child
  43.         char* execv_arguments[] = { command, (char*)0 };
  44.         execvp(command, execv_arguments);
  45.     }
  46.     else{
  47.         //is parent
  48.         printf("im done");
  49.     }
  50. }