Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <sys/wait.h>
  6. #include <sys/file.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <limits.h>
  10. #include <ctype.h>
  11. #include <dirent.h>
  12. #include <string.h>
  13.  
  14. char buf[10000];
  15. int ans;
  16.  
  17. int
  18. main(int argc, char **argv)
  19. {
  20. FILE *f;
  21. char c;
  22. int cnt;
  23. int N = atoi(argv[1]), i;
  24. for (i = 0; i + 2 < argc && i < N; i++) {
  25. f = fopen(argv[i + 2], "r");
  26. cnt = 0;
  27. while (fscanf(f, "%c", &c) > 0) {
  28. if (c == '\n') {
  29. buf[cnt] = '\0';
  30. break;
  31. }
  32. buf[cnt++] = c;
  33. }
  34. fclose(f);
  35. if (!fork()) {
  36. execlp(buf, buf, NULL);
  37. exit(1);
  38. }
  39. }
  40. int status;
  41. while (wait(&status) > 0) {
  42. if (WIFEXITED(status) && !WEXITSTATUS(status)) {
  43. ans++;
  44. }
  45. }
  46. for (i = N + 2; i < argc; i++) {
  47. f = fopen(argv[i], "r");
  48. cnt = 0;
  49. while (fscanf(f, "%c", &c) > 0) {
  50. if (c == '\n') {
  51. buf[cnt] = '\0';
  52. break;
  53. }
  54. buf[cnt++] = c;
  55. }
  56. fclose(f);
  57. if (!fork()) {
  58. execlp(buf, buf, NULL);
  59. exit(1);
  60. } else {
  61. wait(&status);
  62. if (WIFEXITED(status) && !WEXITSTATUS(status)) {
  63. ans++;
  64. }
  65. }
  66. }
  67. printf("%d\n", ans);
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement