Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <utmp.h>
  4. #include <getopt.h>
  5. #include <sys/types.h>
  6. #include <pwd.h>
  7. #include <grp.h>
  8.  
  9. int main(int argc, char **argv) {
  10. struct utmp *handler;
  11. struct passwd *passwdRecord;
  12. struct group *groupNameRecord;
  13. int groupsCount;
  14. int gFlag = 0;
  15. int hFlag = 0;
  16. int c;
  17. while((c = getopt (argc, argv, "hg")) != -1) {
  18. switch(c) {
  19. case 'h':
  20. hFlag = 1;
  21. break;
  22. case 'g':
  23. gFlag = 1;
  24. break;
  25. default:
  26. abort();
  27. }
  28. }
  29. handler = getutent();
  30. while(handler != NULL) {
  31. if(handler->ut_type == 7) {
  32. passwdRecord = getpwnam(handler->ut_user);
  33. printf("%s ", handler->ut_user);
  34. if(hFlag) printf("(%s) ", handler->ut_host);
  35. if(gFlag) {
  36. groupsCount = 0;
  37. getgrouplist(handler->ut_user, passwdRecord->pw_gid, NULL, &groupsCount);
  38. gid_t *userGroups = (gid_t*)malloc(groupsCount * sizeof(gid_t));
  39. getgrouplist(handler->ut_user, passwdRecord->pw_gid, userGroups, &groupsCount);
  40. printf("[");
  41. for (int i=0; i<groupsCount; i++) {
  42. groupNameRecord= getgrgid(userGroups[i]);
  43. printf("%s ", groupNameRecord->gr_name);
  44. }
  45. printf("]");
  46. free(userGroups);
  47. }
  48. }
  49. }
  50. return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement