Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <utmpx.h>
- #include <pwd.h>
- #include <grp.h>
- #include <ctype.h>
- #include <unistd.h>
- #include <sys/types.h>
- int main(int argc, char **argv)
- {
- // Tutaj sobie tworzysz wszystkie struktury
- struct utmpx *a=getutxent();
- struct group *gr;
- struct passwd *pass;
- gid_t *groups;
- char *user;
- int j;
- int ngroups=0;
- int gflag=0;
- int iflag=0;
- int c;
- while((c = getopt (argc, argv, "ig"))!= -1) // getopt zbiera nam argument
- {
- if(c=='g') gflag=1;
- if(c=='i') iflag=1;
- }
- /*kazde wywolanie zwraca nam kolejnego uzytkownika, no chyba, ze
- juz nie bedzie zadnego
- */
- while(a=getutxent())
- {
- if(a->ut_type==7) //sprawdza nam grupe uzytkownika, 7 to byl uzytkownik
- {
- user=a->ut_user;
- if(c==-1) // bez przelacznika, po prostu nam zwraca liste uzytkownikow
- {
- printf("%s" , a->ut_user);
- printf("\n");
- }
- if(iflag) // tu zwraca uzytkownika i hosta
- {
- printf("%s (%s)" , a->ut_user, a->ut_host);
- printf("\n");
- // printf("\nI\n");
- }
- if(gflag)
- {
- pass=getpwnam(a->ut_user); //do struktury passwd zbieramy informacje o uzytkowniku
- ngroups = 0; //zerujemy liczbe grup za kazdym razem jak chcemy wypisac grupy uzxytkownika
- //TO BEDZIE DOBRE
- getgrouplist(a->ut_user, pass->pw_gid, groups, &ngroups); // tutaj wywolujemy getgroupa po to, zeby zwrocil nam liczbe "ngroups"
- groups = malloc(ngroups * sizeof (gid_t)); // zwrocone "ngroups" wykorzystujemy do zaalokowania sobie pamieci
- getgrouplist(a->ut_user, pass->pw_gid, groups, &ngroups); // i tutaj nam do zaalokowanej pamieci "groups" cos nam wpisywalo
- printf("%s [", a->ut_user);
- for (j = 0; j < ngroups; j++)
- {
- gr = getgrgid(groups[j]);
- if (gr != NULL)
- printf("%s", gr->gr_name);
- if(j < ngroups - 1) printf(", "); //to do wyswietlania przecinkow, sprawdza czy wyswielil linijke wyzej ostatni element, jezeli nie to dodaje przecinek po nim
- //printf("\n");
- }
- printf("]\n");
- free(groups); //zwalniamy pamiec
- }
- }
- } // WHILE
- endutxent(); //zamykamy endutxent
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement