Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- Kod:
- #include <sys>
- #include <stdio>
- #include <dirent>
- #include <unistd>
- #include <fcntl>
- #include <stdlib>
- #include <string>
- int main (int argc, char* argv[])
- {
- struct stat pocz;
- struct stat staty;
- int i,j;
- int tab_wielk1[argc-2];
- int tab_zmian1[argc-2];
- int tab_wielk2[argc-2];
- int tab_zmian2[argc-2];
- for(i=0; i<argc-2; i++)
- {
- if ( stat(argv[i+2], &pocz) == -1)
- {
- perror("blad stat \n");
- exit(2);
- }
- tab_wielk1[i]= pocz.st_size;
- tab_zmian1[i]= pocz.st_mtime;
- // printf("wielkosc: %d, modyfikacja: %d \n ", pocz.st_size, pocz.st_mtime);
- }
- stat(argv[3], &pocz);
- while(1)
- {
- int fd1,fd2;
- for(j=0; j<argc-2; j++)
- {
- if( stat(argv[j+2], &staty) == -1)
- {
- perror("blad stat \n");
- exit(1);
- }
- tab_wielk2[j]= staty.st_size;
- tab_zmian2[j]= staty.st_mtime;
- if(tab_wielk1[j]!=tab_wielk2[j] || tab_zmian1[j]!=tab_zmian2[j])
- {
- printf("plik : %s , wielkosc: %d, UID: %d \n ",argv[j+2], (int)staty.st_size, (int)staty.st_uid);
- }
- tab_wielk1[j]=tab_wielk2[j];
- tab_zmian1[j]=tab_zmian2[j];
- }
- sleep(atoi(argv[1]));
- }
- return 0;
- }
- 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.
- Kod:
- #include <sys>
- #include <stdio>
- #include <dirent>
- #include <unistd>
- #include <fcntl>
- #include <stdlib>
- #include <string>
- int main (int argc, char* argv[])
- {
- struct stat pocz;
- struct stat staty;
- int i,j;
- int tab_zmian1[argc-2];
- int tab_zmian2[argc-2];
- for(i=0; i<argc-2; i++)
- {
- if ( stat(argv[i+2], &pocz) == -1)
- {
- perror("blad stat \n");
- exit(2);
- }
- tab_zmian1[i]= pocz.st_atime; //tutaj przypisanie do tablicy, tego parametru do sprawdzenia, czyli czy był otwierany plik
- // printf("wielkosc: %d, modyfikacja: %d \n ", pocz.st_size, pocz.st_mtime);
- }
- stat(argv[3], &pocz);
- while(1)
- {
- for(j=0; j<argc-2; j++)
- {
- if( stat(argv[j+2], &staty) == -1)
- {
- perror("blad stat \n");
- exit(1);
- }
- tab_zmian2[j]= staty.st_atime;
- if(S_ISREG(staty.st_mode)==1) // tutaj użycie makra do sprawdzania, czy jest to plik regularny
- {
- if(tab_zmian1[j]!=tab_zmian2[j]) // sprawdzanie czy plik byl otwarty
- {
- printf("plik : %s , wielkosc: %d, UID: %d \n ",argv[j+2], (int)staty.st_size, (int)staty.st_uid);
- }
- }
- else printf("katalog\n");
- tab_zmian1[j]=tab_zmian2[j];
- }
- sleep(atoi(argv[1]));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement