--- 1.mihailo.18_test.executer.fmt.c 2020-06-08 11:39:11.624114477 +0300
+++ 1.albina.18_test.executer.fmt.c 2020-06-08 11:39:11.591114120 +0300
@@ -2,47 +2,42 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
#include <sys/wait.h>
-#include <unistd.h>
+#include <stdlib.h>
#include <string.h>
-int main(int argc, char *argv[]) {
- int num_of_commands = argc;
+int main(int argc, char **argv) {
+ int i, words, status, output;
+ char str[1000], *first;
int pipefd[2];
pipe(pipefd);
- for (int i = 1; i < num_of_commands; i++) {
- int wstatus;
- int len = 0;
- char str1[500];
- strcpy(str1, argv[i]);
- char *p = strtok(str1, " ");
- while (p != NULL) {
- len += 1;
- p = strtok(NULL, " ");
+ for (int i = 1; i < argc; i++) {
+ words = 0;
+ strcpy(str, argv[i]);
+ first = strtok(str, " ");
+ while (first) {
+ words++;
+ first = strtok(NULL, " ");
}
- char *ar[len + 1];
- ar[len] = NULL;
- strcpy(str1, argv[i]);
- p = strtok(str1, " ");
- len = 0;
- while (p != NULL) {
- ar[len] = p;
- len += 1;
- p = strtok(NULL, " ");
+ char *final_array[words + 1];
+ final_array[words] = NULL;
+ strcpy(str, argv[i]);
+ first = strtok(str, " ");
+ int j = 0;
+ while (first) {
+ final_array[j] = first;
+ j++;
+ first = strtok(NULL, " ");
}
if (fork()) {
- wait(&wstatus);
- dup(pipefd[0]);
- char str[8000];
- int n;
- n = read(pipefd[0], str, 8000);
- printf("%d\n", n);
+ wait(&status);
+ char s[10000];
+ output = read(pipefd[0], s, 10000);
+ printf("%d\n", output);
continue;
} else {
close(1);
dup(pipefd[1]);
- execvp(ar[0], ar);
+ execvp(final_array[0], &final_array[0]);
break;
}
}