Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include <stdbool.h>
  6. #include <errno.h>
  7. #include <semaphore.h>
  8. #include <time.h>
  9. #include <sys/types.h>
  10. #include <sys/wait.h>
  11. #include <unistd.h>
  12.  
  13.  
  14.  
  15. #define osAssert(cond, msg) osErrorFatal(cond, msg, __FILE__, __LINE__)
  16.  
  17. void osErrorFatal(bool cond, const char* msg, const char* file, int line)
  18. {
  19. if(!cond)
  20. {
  21. perror(msg);
  22. fprintf(stderr,"%s:%d\n", file,line);
  23. exit(EXIT_FAILURE);
  24. }
  25. }
  26.  
  27. int main(int argc, char** argv)
  28. {
  29. osAssert(argc > 1 , "wrong number of args");
  30.  
  31. int nFiles = argc-1;
  32. const char* cmd = "du\0";
  33. const char* arg1 = "-sch\0";
  34.  
  35. for(int i = 0; i < nFiles; i++)
  36. {
  37. int pipeFds[2];
  38. osAssert(-1 != pipe(pipeFds), "pipe failed");
  39.  
  40. const char* arg2 = argv[i+1];
  41.  
  42.  
  43. pid_t childPid = fork();
  44.  
  45. if(childPid == 0)
  46. {
  47. close(pipeFds[0]);
  48. osAssert(-1 != dup2(pipeFds[1], STDOUT_FILENO), "dup2 failed");
  49. osAssert(-1 != execlp(cmd,cmd,arg1,arg2,NULL), "exec failed");
  50. }
  51. else
  52. {
  53. close(pipeFds[1]);
  54.  
  55. int wstatus;
  56. osAssert(-1 != waitpid(childPid, &wstatus, 0), "wait failed");
  57.  
  58. if(WIFEXITED(wstatus))
  59. {
  60. if(WEXITSTATUS(wstatus) != EXIT_SUCCESS)
  61. {
  62. printf("neuspeh ");
  63. continue;
  64. }
  65. }
  66.  
  67. FILE *fp = fdopen(pipeFds[0], "r");
  68. osAssert(NULL != fp, "fdopen failed");
  69.  
  70. char velicina[256];
  71.  
  72. osAssert(1 == fscanf(fp,"%s",velicina),"fscanf failed");
  73. printf("%s ",velicina);
  74. fclose(fp);
  75. close(pipeFds[0]);
  76. }
  77. }
  78. printf("\n");
  79.  
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement