Advertisement
Guest User

Untitled

a guest
May 5th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. struct str_list {
  2. char *const *name;
  3. int argc;
  4. char *const *argv;
  5. };
  6.  
  7. int run_cmd(const char *cmd) {
  8. assert(cmd);
  9. struct str_list *chunks = str_split(cmd, '|');
  10. struct pipeline *pipe = alloc_pipeline(chunks->size);
  11. for (int i=0; i<chunks->size; i++) {
  12. pipe->command[i] = parse_command(chunks->data[i]);
  13. }
  14. int status = execute_pipeline(pipe);
  15. free_pipeline(pipe);
  16. free_str_list(chunks);
  17. return status;
  18. }
  19.  
  20. static int runCmd(const char *cmd) {
  21. int freeme_len = 0;
  22. char *pString1[z];
  23. char *pString[z];
  24. *pString1 = "";
  25. *pString = "";
  26. bool quote = false;
  27. int argc = 1;
  28. int n = 0;
  29. int status = 0;
  30. int w = 0;
  31. int i2 = 0;
  32.  
  33. struct command shellcommand[BUFFER_SIZE];
  34.  
  35. char **ptr1;
  36. char **argv = alloc_argv(BUFFER_SIZE);
  37. char **new_argv = alloc_argv(BUFFER_SIZE);
  38. char ***matrix = alloc_matrix(BUFFER_SIZE, BUFFER_SIZE);
  39. char ***pString4 = alloc_matrix(BUFFER_SIZE, BUFFER_SIZE);
  40.  
  41. char *cmdtmp;
  42. char *pString3[64];
  43. cmdtmp = strdup(cmd);
  44. ptr1 = str_split(pString3, cmdtmp, '|');
  45. for (int i = 0; ptr1[i]; i++) { /* loop for each pipeline */
  46. n++; /* save number of pipelines */
  47. char *string1[z];
  48. int e = 0;
  49. *pString = "";
  50. *string1 = strdup(ptr1[i]);
  51. if ((*string1[0] != '') &&
  52. !isspace(*string1[0])) {
  53. w = parse_command(w, new_argv, string1, pString, &i2, n, quote, i, matrix, pString1,
  54. e, &argc, argv, pString4, &freeme_len);;
  55. }
  56. free(*string1);
  57. }
  58. for (int i = 0; i < n; i++) {
  59. shellcommand[i].argv = matrix[i];
  60. }
  61. fflush(NULL);
  62. /* refactor to a function */
  63. pid_t pid;
  64. pid = fork();
  65. if (pid < 0) {
  66. perror("fork failed");
  67. return -1;
  68. }
  69. /* If we are the child process, then go execute the string.*/
  70. if (pid == 0) {
  71. /* spawn(cmd);*/
  72. fork_pipes(n, shellcommand);
  73. }
  74. /*
  75. * We are the parent process.
  76. * Wait for the child to complete.
  77. */
  78. while (((pid = waitpid(pid, &status, 0)) < 0) && (errno == EINTR));
  79. if (pid < 0) {
  80. fprintf(stderr, "Error from waitpid: %s", strerror(errno));
  81. free(cmdtmp);
  82. if (ptr1) {
  83. for (int i = 0; ptr1[i]; i++) {
  84. free(ptr1[i]);
  85. }
  86. printf("n");
  87. free(ptr1);
  88. }
  89. return -1;
  90. }
  91. if (WIFSIGNALED(status)) {
  92. fprintf(stderr, "pid %ld: killed by signal %dn",
  93. (long) pid, WTERMSIG(status));
  94. free(cmdtmp);
  95. if (ptr1) {
  96. for (int i = 0; ptr1[i]; i++) {
  97. free(ptr1[i]);
  98. }
  99. printf("n");
  100. free(ptr1);
  101. }
  102. return -1;
  103. }
  104. free(cmdtmp);
  105. if (ptr1) {
  106. for (int i = 0; ptr1[i]; i++) {
  107. free(ptr1[i]);
  108. }
  109. free(ptr1);
  110. }
  111.  
  112. free_matrix(matrix, BUFFER_SIZE, BUFFER_SIZE);
  113. free_matrix(pString4, BUFFER_SIZE, BUFFER_SIZE);
  114. free_argv(argv, BUFFER_SIZE);
  115. free_argv(new_argv, BUFFER_SIZE);
  116.  
  117. return WEXITSTATUS(status);
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement