Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1 │ #include <stdio.h>
- 2 │ #include <unistd.h>
- 3 │ #include <sys/types.h>
- 4 │ #include <sys/stat.h>
- 5 │ #include <sys/wait.h>
- 6 │ #include <fcntl.h>
- 7 │ #include <errno.h>
- 8 │ #include <string.h>
- 9 │
- 10 │ long long ans = 0;
- 11 │
- 12 │ int mysys(const char* str) {
- 13 │ int pid = fork();
- 14 │ if (pid < 0) {
- 15 │ return 1;
- 16 │ }
- 17 │ if (!pid) {
- 18 │ execlp(str, str, NULL);
- 19 │ _exit(1);
- 20 │ }
- 21 │ int status = 0;
- 22 │ while (waitpid(pid, &status, 0) > 0) {}
- 23 │ if (WIFEXITED(status)) {
- 24 │ ans += WEXITSTATUS(status) == 0;
- 25 │ return WEXITSTATUS(status) != 0;
- 26 │ }
- 27 │ return 1;
- 28 │ }
- 29 │
- 30 │ int mysys2(const char* str) {
- 31 │ int pid = fork();
- 32 │ if (pid < 0) {
- 33 │ return 1;
- 34 │ }
- 35 │ if (!pid) {
- 36 │ execlp(str, str, NULL);
- 37 │ _exit(1);
- 38 │ }
- 39 │ return 0;
- 40 │ }
- 41 │
- 42 │ int main(int argc, char* argv[]) {
- 43 │ while (mysys(argv[1]) ? !mysys(argv[2]) : 1) {
- 44 │ for (int i = 3; i < argc; ++i) {
- 45 │ mysys2(argv[i]);
- 46 │ }
- 47 │ int status;
- 48 │ while (wait(&status) > 0) {
- 49 │ if (WIFEXITED(status)) {
- 50 │ ans += WEXITSTATUS(status) == 0;
- 51 │ }
- 52 │ }
- 53 │ }
- 54 │ printf("%lld\n", ans);
- 55 │ }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement