-
-
Save donjajo/7589af55ed91cdee622f41801fa5a0c7 to your computer and use it in GitHub Desktop.
Simple replica of `id` command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <pwd.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <grp.h> | |
#include <string.h> | |
void main( int argc, char *argv[] ) { | |
struct passwd *user; | |
struct group *group; | |
uid_t uid = getuid(); | |
char response[1000]; | |
if( ( user = getpwuid( uid ) ) == NULL ) { | |
perror( "getpwuid" ); | |
exit(1); | |
} | |
if( ( group = getgrgid( user->pw_gid ) ) == NULL ) { | |
perror( "getgrgid" ); | |
exit(1); | |
} | |
sprintf( response, "uid=%d(%s) gid=%d(%s) groups=", uid, user->pw_name, user->pw_gid, group->gr_name ); | |
while( ( group = getgrent() ) != NULL ) { | |
char gr_data[256]; | |
while( *group->gr_mem != NULL ) { | |
if( strcmp( *group->gr_mem, user->pw_name ) == 0 ) { | |
sprintf( gr_data, "%d(%s),", group->gr_gid, group->gr_name ); | |
strcat( response, gr_data ); | |
} | |
group->gr_mem++; | |
} | |
} | |
endgrent(); | |
response[strlen( response )-1] = '\0'; // Remove last comma | |
printf("%s\n", response ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment