Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/wait.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. char **parse_cmdline( const char *cmdlin );
  7. int main(int argc,char **argv){
  8. int check_for_ending = 0;
  9. char string[2];
  10. string[0] = '$';
  11. string[1] = ' ';
  12.  
  13. char new_line[1];
  14. new_line[0] = '\n';
  15.  
  16. while(1){
  17.  
  18. write(1,string,2);
  19. //printf("$ ");
  20. int t=1,i=0;
  21. char buffer[1];
  22. char *suffer = malloc(t*sizeof(char));
  23. //int size=0;
  24.  
  25. /* int vladi = getline(&suffer,&size,stdin);
  26. if( feof(stdin) ) {
  27. break;
  28. }
  29. suffer[vladi-1]='\0';
  30. */
  31. while(1){
  32. int g = read(STDIN_FILENO,buffer,1);
  33. if(g==0){
  34. check_for_ending = 1;
  35. break;
  36. }
  37. if(buffer[0]=='\n'){
  38. suffer[i] = '\0';
  39. break;
  40. }
  41. suffer[i] = buffer[0];
  42. i++;
  43. if(i==t)
  44. {
  45. t=t+1;
  46. suffer = realloc(suffer,t*sizeof(char));
  47. }
  48. }
  49. if(check_for_ending){
  50. //write(STDIN_FILENO,new_line,1);
  51. break;
  52. }
  53. char **masivche = parse_cmdline(suffer);
  54. /*char *pointer = masivche[0];
  55. char *pointer1 = masivche[1];
  56. char *pointer2 = masivche[2];
  57. char *pointer3 = masivche[3];
  58. write(STDIN_FILENO,pointer,strlen(masivche[0]));
  59. write(STDIN_FILENO,pointer1,strlen(masivche[1]));
  60. write(STDIN_FILENO,pointer2,strlen(masivche[2]));
  61. return 0;*/
  62. char *pointer = masivche[0];
  63. if(pointer==NULL){continue;}
  64. pid_t pid = fork();
  65. if(pid < 0){
  66. perror("fork");
  67. continue;
  68. }
  69. if(pid == 0){
  70. int p = execv(masivche[0],masivche);
  71. if(p < 0){
  72. perror(masivche[0]);
  73. exit(0);
  74. }
  75. exit(0);
  76. }else{
  77. waitpid(pid, 0 ,0);
  78. }
  79. free(masivche[0]);
  80. free(masivche);
  81. }
  82. return 0;
  83. }
  84. char **parse_cmdline( const char *cmdlin ){
  85. char *string = malloc(strlen(cmdlin)*sizeof(char));
  86. strcpy(string,cmdlin);
  87. int op=0;
  88. char **array_of_commands = NULL;
  89. char *token = strtok(string, " ");
  90. while(token != NULL )
  91. {
  92. op++;
  93. array_of_commands = realloc(array_of_commands,(op+1)*sizeof(char*));
  94. array_of_commands[op-1] = token;
  95. token = strtok(NULL, " ");
  96. }
  97. array_of_commands[op]=NULL;
  98. return array_of_commands;
  99.  
  100.  
  101. /*int i=2,j,y=0,help=0,k=2,array_counter=0;
  102. char **array_of_commands = malloc(k*sizeof(char*));
  103. char *command = malloc(i*sizeof(char*));
  104. for(j=0;;j++){
  105. if(cmdlin[j]!=' '){break;}
  106. }
  107. for(;;j++){
  108. if(cmdlin[j]=='\0'){break;}
  109. while(cmdlin[j]!=' '){
  110. command[y] = cmdlin[j];
  111. j++;y++;
  112. if(y == i){
  113. i*=2;
  114. command = realloc(command,i*sizeof(char));
  115. }
  116. help=1;
  117. }
  118. if(help == 1){
  119. command[y]='\0';
  120. y=0;
  121. array_of_commands[array_counter] = command;
  122. array_counter++;
  123. if(array_counter + 1 == k){
  124. k*=2;
  125. array_of_commands = realloc(array_of_commands,k*sizeof(char*));
  126. }
  127. }
  128. help = 0;
  129. }*/
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement