Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <utmpx.h>
  4. #include <pwd.h>
  5. #include <grp.h>
  6. #include <ctype.h>
  7. #include <unistd.h>
  8. #include <sys/types.h>
  9.  
  10.  
  11. int main(int argc, char **argv)
  12. {
  13.  
  14. // Tutaj sobie tworzysz wszystkie struktury
  15. struct utmpx *a=getutxent();
  16. struct group *gr;
  17. struct passwd *pass;
  18. gid_t *groups;
  19.  
  20. char *user;
  21. int j;
  22. int ngroups=0;
  23. int gflag=0;
  24. int iflag=0;
  25.  
  26. int c;
  27. while((c = getopt (argc, argv, "ig"))!= -1) // getopt zbiera nam argument
  28. {
  29. if(c=='g') gflag=1;
  30. if(c=='i') iflag=1;
  31. }
  32.  
  33.  
  34. /*kazde wywolanie zwraca nam kolejnego uzytkownika, no chyba, ze
  35. juz nie bedzie zadnego
  36.  
  37. */
  38. while(a=getutxent())
  39. {
  40. if(a->ut_type==7) //sprawdza nam grupe uzytkownika, 7 to byl uzytkownik
  41. {
  42. user=a->ut_user;
  43.  
  44. if(c==-1) // bez przelacznika, po prostu nam zwraca liste uzytkownikow
  45. {
  46. printf("%s" , a->ut_user);
  47. printf("\n");
  48. }
  49. if(iflag) // tu zwraca uzytkownika i hosta
  50. {
  51. printf("%s (%s)" , a->ut_user, a->ut_host);
  52. printf("\n");
  53. // printf("\nI\n");
  54. }
  55. if(gflag)
  56. {
  57. pass=getpwnam(a->ut_user); //do struktury passwd zbieramy informacje o uzytkowniku
  58. ngroups = 0; //zerujemy liczbe grup za kazdym razem jak chcemy wypisac grupy uzxytkownika
  59.  
  60. //TO BEDZIE DOBRE
  61. getgrouplist(a->ut_user, pass->pw_gid, groups, &ngroups); // tutaj wywolujemy getgroupa po to, zeby zwrocil nam liczbe "ngroups"
  62. groups = malloc(ngroups * sizeof (gid_t)); // zwrocone "ngroups" wykorzystujemy do zaalokowania sobie pamieci
  63. getgrouplist(a->ut_user, pass->pw_gid, groups, &ngroups); // i tutaj nam do zaalokowanej pamieci "groups" cos nam wpisywalo
  64.  
  65. printf("%s [", a->ut_user);
  66. for (j = 0; j < ngroups; j++)
  67. {
  68. gr = getgrgid(groups[j]);
  69. if (gr != NULL)
  70. printf("%s", gr->gr_name);
  71. if(j < ngroups - 1) printf(", "); //to do wyswietlania przecinkow, sprawdza czy wyswielil linijke wyzej ostatni element, jezeli nie to dodaje przecinek po nim
  72. //printf("\n");
  73. }
  74. printf("]\n");
  75. free(groups); //zwalniamy pamiec
  76. }
  77. }
  78. } // WHILE
  79. endutxent(); //zamykamy endutxent
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement