SHARE
TWEET

crsh v2.0

B1KMusic Jan 25th, 2016 664 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <sys/wait.h>
  6.  
  7. void parse_input(char **argv, char *cmdbuf){
  8.     char argbuf[1000] = "",
  9.          *abp = argbuf,
  10.          **avp = argv,
  11.          *cbp = cmdbuf;
  12.  
  13.     while(1){
  14.         if(*cbp == ' ' || *cbp == '\n'){
  15.             *avp = calloc(sizeof(char), 100);
  16.             strcpy(*avp, argbuf);
  17.             abp = argbuf;
  18.             avp++;
  19.             cbp++;
  20.             continue;
  21.         }
  22.  
  23.         if(*cbp == 0){
  24.             *avp = 0;
  25.             break;
  26.         }
  27.  
  28.         *(abp++) = *(cbp++);
  29.     }
  30. }
  31.  
  32. void teardown_argv(char **argv){
  33.     while(*argv)
  34.         free(*(argv++));
  35. }
  36.  
  37. int run_argv(char **argv){
  38.     pid_t pid = fork();
  39.  
  40.     if(pid == 0){
  41.         execvp(*argv, argv);
  42.         perror("Error");
  43.     } else {
  44.         wait(0);
  45.         teardown_argv(argv);
  46.     }
  47. }
  48.  
  49. int main(){
  50.     char cmdbuf[1000] = "",
  51.          *argv[100];
  52.  
  53.     while (1){
  54.         printf("Crap Shell> ");
  55.         fgets(cmdbuf, 1000, stdin);
  56.         parse_input(argv, cmdbuf);
  57.         run_argv(argv);
  58.     }
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top