Advertisement
DanFloyd

Dummyshell

May 21st, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/wait.h>
  7.  
  8. #define ARGLIMIT 64
  9. #define ARGLENGH 256
  10. #define TRUE 1
  11. #define FALSE 0
  12.  
  13. #define ETMARGS \
  14. {printf("Fatal error: only 8 arguments are allowed\n"); return 1;}
  15.  
  16. static int cmd_counter, st;
  17. char *argv[ARGLIMIT];
  18.  
  19. void init(){
  20.     cmd_counter=0;
  21.     printf("Welcome in Dummyshell!\n");
  22.     fflush(stdout);
  23. }
  24.  
  25. void leave_shell(void){
  26.     int i;
  27.     if(errno == 0) for(i=0;i<ARGLIMIT-1;i++) free(argv[i]);
  28.     printf("You're leaving Dummyshell...\n");
  29.     fflush(stdout);
  30. }
  31.  
  32. void type_prompt(){
  33.     printf("[cmd # %d][stat: %d] @ Dush £: ",cmd_counter,st);
  34.     fflush(stdout);
  35.     cmd_counter++;
  36. }
  37.  
  38. int read_cmd_line(char *argv[]){
  39.    
  40.     char *arg_buffer = (char*) malloc(sizeof(char)*ARGLENGH), *it;
  41.     int i=0, l1=0, l2=0;
  42.  
  43.     fgets(arg_buffer,ARGLENGH,stdin);
  44.     fflush(stdin);
  45.  
  46.     for(it=arg_buffer; it != '\0'; it++){
  47.         if(i >= ARGLIMIT -1) ETMARGS
  48.         argv[i] = (char*) malloc(sizeof(char)*ARGLENGH);
  49.         l1 = strlen(it);
  50.         strncpy(argv[i],it,l1);
  51.         argv[i][l1-1] = '\0';
  52.         it = strchr(it,' ');
  53.         if (it == NULL) break;
  54.         l2 = strlen(it);
  55.         argv[i][l1-l2] = '\0';
  56.         i++;
  57.     }
  58.     argv[i+1] = NULL;
  59.     return 0;
  60. }
  61.  
  62. int cmd_exit(char *argv[]){
  63.     if(strcmp(argv[0],"exit")==0) return 1;
  64.     return 0;
  65. }
  66.  
  67. static void execute(int *st, char *argv[]){
  68.     pid_t pid;
  69.     switch(pid=fork()){
  70.         case -1 :{
  71.             perror("Couldn't fork!\n");
  72.             break;
  73.         }
  74.         case 0 :{
  75.             execvp(argv[0],argv);
  76.             perror("Couldn't execute!\n");
  77.             break;
  78.         }
  79.         default :{
  80.             if(waitpid(pid,st,0)==-1){
  81.                 perror("Waitpid: ");
  82.                 exit(errno);
  83.             }
  84.         }
  85.     }
  86. }  
  87.  
  88. int main(){
  89.    
  90.     if(atexit(leave_shell)) perror("Atexit failed! Possible memory leak\n");
  91.    
  92.     init();
  93.     while(TRUE){
  94.         type_prompt();
  95.         if(read_cmd_line(argv)!=-1){
  96.             if(cmd_exit(argv)) exit(EXIT_SUCCESS);
  97.             execute(&st,argv);
  98.         }
  99.         else fprintf(stderr,"Invalid command!\n");
  100.     }
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement