Advertisement
anechka_ne_plach

7-5

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