Advertisement
Guest User

Untitled

a guest
May 1st, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.24 KB | None | 0 0
  1. void write_command(int *w, char *argv[], char *string[]) {
  2. for (int r = 0; argv[r] != NULL; r++) {
  3. string[r] = strdup(argv[r]);
  4. }
  5. }
  6.  
  7. void write_argument(int *argc, char *argv[], char *string[]) {
  8. int j = 0;
  9. for (j = 0; j < *argc; j++) {
  10. string[j] = strdup(argv[j]);
  11. }
  12. }
  13.  
  14. static int runCmd(const char *cmd) {
  15. const char *cp;
  16. pid_t pid;
  17. int status;
  18. struct command structcommand[15];
  19. char **argv = 0;
  20. int argc = 1;
  21. bool pipe = false;
  22. char *string[z][z];
  23. char *pString3[40];
  24. char *pString2[40];
  25. int n = 0;
  26. char **ptr1;
  27. char string1[z];
  28. bool keep = false;
  29. char *pString1[z];
  30. char *pString[z];
  31. *pString1 = "";
  32. *pString = "";
  33. char *temp = {''};
  34. int w = 0;
  35. bool b = false;
  36. int j = 0;
  37. int i;
  38. int p = 0;
  39. char **ptr;
  40. char *tmpchar;
  41. char *cmdtmp;
  42. bool b1 = false;
  43. int y = 0;
  44. i = 0;
  45. int h = 0;
  46. nullterminate(string);
  47. if (cmd) {
  48. for (cp = cmd; *cp; cp++) {
  49. if ((*cp >= 'a') && (*cp <= 'z')) {
  50. continue;
  51. }
  52. if ((*cp >= 'A') && (*cp <= 'Z')) {
  53. continue;
  54. }
  55. if (isDecimal(*cp)) {
  56. continue;
  57. }
  58. if (isBlank(*cp)) {
  59. continue;
  60. }
  61. if ((*cp == '.') || (*cp == '/') || (*cp == '-') ||
  62. (*cp == '+') || (*cp == '=') || (*cp == '_') ||
  63. (*cp == ':') || (*cp == ',') || (*cp == ''') ||
  64. (*cp == '"')) {
  65. continue;
  66. }
  67. }
  68. }
  69. if (cmd) {
  70. cmdtmp = malloc(sizeof(char *) * strlen(cmd) + 1);
  71. strcpy(cmdtmp, cmd);
  72. tmpchar = malloc(sizeof(char *) * strlen(cmd) + 1);
  73. if (tmpchar == NULL) {
  74. printf("Error allocating memory!n"); /* print an error message */
  75. return 1; /* return with failure */
  76. }
  77. strcpy(tmpchar, cmd);
  78. ptr1 = str_split(pString3, cmdtmp, '|');
  79. if (strstr(cmd, "|") == NULL) { /* not a pipeline */
  80.  
  81. makeArgs(cmd, &argc, (const char ***) &argv, pipe, 0, 0);
  82. write_argument(&argc, argv, string[0]);
  83. n++;
  84. }
  85. else {
  86. for (i = 0; *(ptr1 + i); i++) { /* tokenize the input string for each pipeline*/
  87. n++; /* save number of pipelines */
  88. int e = 0; /* a counter */
  89. *pString = ""; /* should malloc and free this? */
  90. strcpy(string1, *(ptr1 + i));
  91. if ((string1[0] != '') && !isspace(string1[0])) { /* this is neither the end nor a new argument */
  92. ptr = str_split(pString2, *(&string1), ' '); /* split the string at the arguments */
  93. h = 0;
  94. for (j = 0; *(ptr + j); j++) { /* step through the arguments */
  95. /* the pipeline is in cmdtmp and the argument/program is in ptr[i] */
  96. if (ptr + j && !b && strstr(*(ptr + j), "'")) {
  97. b = true;
  98. strcpy(temp, *(ptr + j));
  99. if (y < 1) {
  100. y++;
  101. }
  102. }
  103. while (b) {
  104. if (*(ptr + j) && strstr(*(ptr + j), "'")) { /* end of quote */
  105. b = false;
  106. if (y < 1) {
  107. string[i][j] = strcpy(temp, *(ptr + j));
  108. }
  109. y = 0;
  110. }
  111. else if (*(ptr + j)) { /* read until end of quote */
  112. string[i][j] = temp;
  113. continue;
  114. } else {
  115. b = false;
  116. break;
  117. }
  118. }
  119. if (ptr + j) {
  120. if (*(ptr + j)[0] == '{') {
  121. keep = true;
  122. }
  123. if (testFn(*(ptr + j))) { /* test for last char */
  124. string[i][j - p] = concat(*pString1, *(ptr + j));
  125. keep = false;
  126. free(*pString1);
  127. goto mylabel;
  128. }
  129. if (keep) {
  130. *pString1 = concat(*pString1, *(ptr + j));
  131. *pString1 = concat(*pString1, " ");
  132. p++;
  133. } else {
  134. b1 = false;
  135. int q = j;
  136. for (e = 0; *(ptr + q + e); e++) { /* step through the string */
  137. b1 = true;
  138. if (*(ptr + e + q)) {
  139. *pString = concat(*pString, *(ptr + e + q));
  140. *pString = concat(*pString, " ");
  141. }
  142. j = e;
  143. }
  144. if (makeArgs(*pString, &argc, (const char ***) &argv, pipe, i, h)) {
  145. write_command(&w, argv, string[w]);
  146. w++;
  147.  
  148. } else {
  149. if (!b1) { /* no args (?) */
  150. for (int r = 0; argv[r] != NULL; r++) {
  151. string[i][r] = argv[r];
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. mylabel:
  159. free(ptr);
  160. dump_argv((const char *) "d", argc, argv);
  161. }
  162. }
  163. free(ptr1);
  164. free(cmdtmp);
  165. free(tmpchar);
  166. }
  167. for (i = 0; i < n; i++) {
  168. for (j = 0; DEBUG && string[i][j] != NULL; j++) {
  169. if (i == 0 && j == 0) printf("n");
  170. printf("p[%d][%d] %sn", i, j, string[i][j]);
  171. }
  172. structcommand[i].argv = string[i];
  173. }
  174. fflush(NULL);
  175. pid = fork();
  176. if (pid < 0) {
  177. perror("fork failed");
  178. return -1;
  179. }
  180. /* If we are the child process, then go execute the string.*/
  181. if (pid == 0) {
  182. /* spawn(cmd);*/
  183. fork_pipes(n, structcommand);
  184. }
  185. /*
  186. * We are the parent process.
  187. * Wait for the child to complete.
  188. */
  189. status = 0;
  190. while (((pid = waitpid(pid, &status, 0)) < 0) && (errno == EINTR));
  191. if (pid < 0) {
  192. fprintf(stderr, "Error from waitpid: %s", strerror(errno));
  193. return -1;
  194. }
  195. if (WIFSIGNALED(status)) {
  196. fprintf(stderr, "pid %ld: killed by signal %dn",
  197. (long) pid, WTERMSIG(status));
  198.  
  199. return -1;
  200. }
  201. }
  202. return WEXITSTATUS(status);
  203.  
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement