linux - How to display groupID (GID) correctly in C? -
i'm trying display userid , groupid through 1 of functions in c program. userid displaying correctly (501) groupid isn't. when check groupid using command "id -g" 20 when run through program using function value 1.
this code.
int registerpw(char **args){ register struct passwd *pw; register uid_t uid; int c; register gid_t gid; register struct group *grp; grp = getgrgid(gid); uid = geteuid(); pw = getpwuid(uid); if (pw) { printf("%d,",uid); // userid printf("%d,", gid); //groupid puts (pw->pw_name); puts(grp->gr_name); } else{ printf("failed\n"); } return 1; }
my output
501,1,username daemon
your code merely declared & defined variable of gid
, left value uninitialized. shall assign correct value it:
register gid_t gid; gid = getgid();
or simply:
register gid_t gid = getgid();
Comments
Post a Comment