Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. #define _XOPEN_SOURCE 700
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6.  
  7. #include <pwd.h>
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stdbool.h>
  12. #include <stdint.h>
  13.  
  14. /* makro maskira neupotrebljene promenljive */
  15. #define UNUSED(x) ((void)x)
  16.  
  17. /* poruka koja definise pravilno pozivanje programa */
  18. static const char* os_Usage = "Usage: ./1 filePath";
  19.  
  20. /* funkcija vraca ukupan broj korisnika sistema u vlasnickoj grupi fajla */
  21. int osNumberOfUsersInGroup(const char* filePath);
  22.  
  23. int main(int argc, char** argv) {
  24.  
  25. /* provera broja argumenata komandne linije */
  26. if (argc != 2) {
  27. perror(os_Usage);
  28. exit(EXIT_FAILURE);
  29. }
  30.  
  31. /* pronalazi se ukupan broj korisnika sistema u vlasnickoj grupi fajla */
  32. int users = osNumberOfUsersInGroup(argv[1]);
  33.  
  34. /* stampa se pronadjeni broj korisnika */
  35. printf("%d\n", users);
  36.  
  37. /* postavlja se exit kod */
  38. exit(EXIT_SUCCESS);
  39. }
  40. /* funkcija vraca ukupan broj korisnika sistema u vlasnickoj grupi fajla */
  41. int osNumberOfUsersInGroup(const char* filePath) {
  42.  
  43. /* dobijanje informacija o fajlu */
  44. struct stat fInfo;
  45. if (stat(filePath, &fInfo) == -1) {
  46. perror("File stat failed");
  47. exit(EXIT_FAILURE);
  48. }
  49. /* inicijalno, broj korisnika u vlasnickoj grupi fajla je nula */
  50. int users = 0;
  51.  
  52. /* passwd fajl se premotava na pocetak */
  53. setpwent();
  54. struct passwd* entry = NULL;
  55. /* dok se ne dodje do kraja fajla */
  56. while((entry = getpwent()) != NULL) {
  57.  
  58. /* poredi se gid korisnika sa gid-om vlasnicke grupe */
  59. if (entry->pw_gid == fInfo.st_gid)
  60. /* ako su isti, uvecava se brojac korisnika */
  61. users++;
  62. }
  63. /* zatvara se passwd fajl */
  64. endpwent();
  65.  
  66. /* vraca se ukupan broj korisnika u vlasnickoj grupi fajla */
  67. return users;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement