Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. I want to create a command line interpreter with more than one comamnd per line.
  2.  
  3. #define MAX_SIZE 512
  4.  
  5. //Clean function
  6. void Clean(char* cmd[],char* pin[]) { //Clean Array
  7.  
  8. int a;
  9. for(a=0; a < 40; a++){
  10. cmd[a] = NULL;
  11. pin[a] = NULL;
  12. }
  13.  
  14. }
  15.  
  16. pid_t pid;
  17. pid = fork();
  18.  
  19. switch(pid) {
  20. case -1:
  21. printf("DEBUG:Fork Failuren");
  22. exit(-1);
  23. case 0:
  24. execvp(*cmd, pinakas);
  25.  
  26. if(execvp(cmd, pinakas) == -1) {
  27. printf("Command Not Foundn");
  28. exit(0);
  29. }
  30.  
  31. default:
  32. wait(NULL);
  33. printf("DEBUG:Child Finishedn");
  34.  
  35. }
  36.  
  37.  
  38. }
  39.  
  40. int main() {
  41. char* cnd;
  42. char* cmd[40];
  43. char* pin[40];
  44. char* array;
  45. int how_much;
  46.  
  47.  
  48. char *input = malloc (MAX_SIZE);
  49.  
  50. if (input == NULL) {
  51. printf ("No memoryn");
  52. return 1;
  53. }
  54.  
  55.  
  56. Clean(cmd,pin);
  57.  
  58.  
  59.  
  60. printf("shell> ");
  61.  
  62. fgets (input, MAX_SIZE, stdin);
  63.  
  64. if ((strlen(input)>0) && (input[strlen (input) - 1] == 'n')) {
  65. input[strlen (input) - 1] = '';
  66. }
  67.  
  68.  
  69. printf("INPUT: %sn", input);
  70.  
  71. int i = 0;
  72.  
  73. while(cnd != NULL) {
  74. cmd[i] = cnd;
  75. i++;
  76. cnd = strtok(NULL, ";");
  77.  
  78. }
  79.  
  80. cmd[i] = NULL;
  81. how_much = i;
  82.  
  83. array = strtok(input, " ");
  84.  
  85. int k=0;
  86.  
  87. while(array != NULL) {
  88. pin[k] = array;
  89. k++;
  90. array = strtok(NULL, " ");
  91. }
  92.  
  93. pin[k] = NULL;
  94.  
  95. Execute(pin[0],pin);
  96.  
  97. }
  98.  
  99. free (input);
  100.  
  101. return 1;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement