Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. 1)Program wywoływany ./prog delta plik1 plik2 plikN. Wczytuje N plików. Co delta sekund sprawdza czy dokonano modyfikacji w którymś z plików. Jeśli dokonano to wyświetla czas ostatniej modyfikacji i UID właściciela pliku, w którym nastąpiła modyfikacja.
  2.  
  3. Kod:
  4.  
  5. #include <sys>
  6. #include <stdio>
  7. #include <dirent>
  8. #include <unistd>
  9. #include <fcntl>
  10. #include <stdlib>
  11. #include <string>
  12.  
  13. int main (int argc, char* argv[])
  14. {
  15. struct stat pocz;
  16. struct stat staty;
  17.  
  18. int i,j;
  19. int tab_wielk1[argc-2];
  20. int tab_zmian1[argc-2];
  21. int tab_wielk2[argc-2];
  22. int tab_zmian2[argc-2];
  23. for(i=0; i<argc-2; i++)
  24. {
  25. if ( stat(argv[i+2], &pocz) == -1)
  26. {
  27. perror("blad stat \n");
  28. exit(2);
  29. }
  30. tab_wielk1[i]= pocz.st_size;
  31. tab_zmian1[i]= pocz.st_mtime;
  32. // printf("wielkosc: %d, modyfikacja: %d \n ", pocz.st_size, pocz.st_mtime);
  33. }
  34. stat(argv[3], &pocz);
  35.  
  36. while(1)
  37. {
  38. int fd1,fd2;
  39.  
  40. for(j=0; j<argc-2; j++)
  41. {
  42. if( stat(argv[j+2], &staty) == -1)
  43. {
  44. perror("blad stat \n");
  45. exit(1);
  46. }
  47. tab_wielk2[j]= staty.st_size;
  48. tab_zmian2[j]= staty.st_mtime;
  49.  
  50. if(tab_wielk1[j]!=tab_wielk2[j] || tab_zmian1[j]!=tab_zmian2[j])
  51. {
  52. printf("plik : %s , wielkosc: %d, UID: %d \n ",argv[j+2], (int)staty.st_size, (int)staty.st_uid);
  53.  
  54. }
  55.  
  56. tab_wielk1[j]=tab_wielk2[j];
  57. tab_zmian1[j]=tab_zmian2[j];
  58. }
  59. sleep(atoi(argv[1]));
  60. }
  61. return 0;
  62. }
  63.  
  64.  
  65.  
  66. 2) modyfikacja programu pierwszego, tak, że sprawdza czy plik został otwarty(a nie jak wyzej zmodyfikowany), dodatkowo sprawdzanie jest tylko dla plików, a nie katalogów, coś takiego było.
  67. Kod:
  68.  
  69. #include <sys>
  70. #include <stdio>
  71. #include <dirent>
  72. #include <unistd>
  73. #include <fcntl>
  74. #include <stdlib>
  75. #include <string>
  76.  
  77. int main (int argc, char* argv[])
  78. {
  79. struct stat pocz;
  80. struct stat staty;
  81.  
  82. int i,j;
  83. int tab_zmian1[argc-2];
  84. int tab_zmian2[argc-2];
  85. for(i=0; i<argc-2; i++)
  86. {
  87. if ( stat(argv[i+2], &pocz) == -1)
  88. {
  89. perror("blad stat \n");
  90. exit(2);
  91. }
  92. tab_zmian1[i]= pocz.st_atime; //tutaj przypisanie do tablicy, tego parametru do sprawdzenia, czyli czy był otwierany plik
  93. // printf("wielkosc: %d, modyfikacja: %d \n ", pocz.st_size, pocz.st_mtime);
  94. }
  95. stat(argv[3], &pocz);
  96.  
  97. while(1)
  98. {
  99.  
  100. for(j=0; j<argc-2; j++)
  101. {
  102. if( stat(argv[j+2], &staty) == -1)
  103. {
  104. perror("blad stat \n");
  105. exit(1);
  106. }
  107. tab_zmian2[j]= staty.st_atime;
  108.  
  109.  
  110. if(S_ISREG(staty.st_mode)==1) // tutaj użycie makra do sprawdzania, czy jest to plik regularny
  111. {
  112. if(tab_zmian1[j]!=tab_zmian2[j]) // sprawdzanie czy plik byl otwarty
  113. {
  114. printf("plik : %s , wielkosc: %d, UID: %d \n ",argv[j+2], (int)staty.st_size, (int)staty.st_uid);
  115.  
  116. }
  117. }
  118. else printf("katalog\n");
  119. tab_zmian1[j]=tab_zmian2[j];
  120. }
  121. sleep(atoi(argv[1]));
  122. }
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement