Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <limits.h>
- #include <sys/wait.h>
- #include <string.h>
- enum { INTEGER_BASE = 10 };
- void
- operate(char *path, char *buf)
- {
- //char buf[PATH_MAX] = {0};
- FILE *f = fopen(path, "r");
- fgets(buf, sizeof(buf), f);
- buf[strlen(buf) - 1] = '\0';
- fclose(f);
- }
- void
- execute(char *path, char *buf)
- {
- FILE *f = fopen(path, "r");
- fgets(buf, sizeof(buf), f);
- buf[strlen(buf) - 1] = '\0';
- fclose(f);
- pid_t pid = fork();
- if (pid == -1) {
- _exit(1);
- } else if (!pid) {
- execlp(buf, buf, NULL);
- _exit(1);
- }
- return;
- }
- int main(int argc, char *argv[])
- {
- //FILE *f;
- int ans = 0, status;
- int n = strtol(argv[1], NULL, INTEGER_BASE);
- for (int i = 0; i < n; ++i) {
- if (2+i >= argc) {
- break;
- }
- /*
- char buf[PATH_MAX] = {0};
- f = fopen(argv[2+i], "r");
- fgets(buf, sizeof(buf), f);
- buf[strlen(buf) - 1] = '\0';
- fclose(f);
- */
- char buf[PATH_MAX] = {0};
- execute(argv[2+i], buf);
- }
- while (wait(&status) > 0)
- {
- ans += (WIFEXITED(status) && !WEXITSTATUS(status));
- }
- for (int i = n + 2; i < argc; ++i) {
- /*
- char buf[PATH_MAX] = {0};
- f = fopen(argv[i], "r");
- fgets(buf, sizeof(buf), f);
- buf[strlen(buf) - 1] = '\0';
- fclose(f);
- */
- char buf[PATH_MAX] = {0};
- execute(argv[i], buf);
- wait(&status);
- ans += (WIFEXITED(status) && !WEXITSTATUS(status));
- }
- printf("%d\n", ans);
- fflush(stdout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement