Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <signal.h>
- #include <sys/wait.h>
- #include <limits.h>
- int min(int a, int b) {
- if (a < b) {
- return a;
- }
- return b;
- }
- int
- main(int argc, char *argv[])
- {
- int n;
- int res = 0;
- sscanf(argv[1], "%d", &n);
- for (int i = 2; i < min(n + 2, argc); ++i) {
- int pid;
- if (!(pid = fork())) {
- char buf[PATH_MAX];
- FILE *f = fopen(argv[i], "r");
- fscanf(f, "%s", buf);
- fclose(f);
- execlp(buf, buf, NULL);
- exit(1);
- }
- }
- int status;
- while (wait(&status) != -1) {
- res = !(WIFEXITED(status) && WEXITSTATUS(status)) ? res + 1 : res;
- }
- for (int i = n + 2; i < argc; ++i) {
- int pid;
- if (!(pid= fork())) {
- char buf[PATH_MAX];
- FILE *f = fopen(argv[i], "r");
- fscanf(f, "%s", buf);
- fclose(f);
- execlp(buf, buf, NULL);
- exit(1);
- } else {
- wait(&status);
- res = !(WIFEXITED(status) && WEXITSTATUS(status)) ? res + 1 : res;
- }
- }
- printf("%d\n", res);
- fflush(stdout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment