Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <utmp.h>
- #include <getopt.h>
- #include <sys/types.h>
- #include <pwd.h>
- #include <grp.h>
- int main(int argc, char **argv) {
- struct utmp *handler;
- struct passwd *passwdRecord;
- struct group *groupNameRecord;
- int groupsCount;
- int gFlag = 0;
- int hFlag = 0;
- int c;
- while((c = getopt (argc, argv, "hg")) != -1) {
- switch(c) {
- case 'h':
- hFlag = 1;
- break;
- case 'g':
- gFlag = 1;
- break;
- default:
- abort();
- }
- }
- handler = getutent();
- while(handler != NULL) {
- if(handler->ut_type == 7) {
- passwdRecord = getpwnam(handler->ut_user);
- printf("%s ", handler->ut_user);
- if(hFlag) printf("(%s) ", handler->ut_host);
- if(gFlag) {
- groupsCount = 0;
- getgrouplist(handler->ut_user, passwdRecord->pw_gid, NULL, &groupsCount);
- gid_t *userGroups = (gid_t*)malloc(groupsCount * sizeof(gid_t));
- getgrouplist(handler->ut_user, passwdRecord->pw_gid, userGroups, &groupsCount);
- printf("[");
- for (int i=0; i<groupsCount; i++) {
- groupNameRecord= getgrgid(userGroups[i]);
- printf("%s ", groupNameRecord->gr_name);
- }
- printf("]");
- free(userGroups);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement