Recap
Recap

Reputation: 178

How to display groupID (GID) correctly in C?

I'm trying to display my userID and groupID through one of my functions in my C program. The userID is displaying correctly (501) but my groupID isn't. When I check my groupID using the command "id -g" I get 20 but when I run it through my program using my function I get the value 1.

This is my 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 is

501,1,USERNAME
daemon

Upvotes: 1

Views: 5975

Answers (2)

Anusha
Anusha

Reputation: 1

i just simply declare variables using int datatype,and use getgid() and geteuid() method to display userid and groupid.

int gid,uid;
uid=geteuid();
gid=getgid();

Upvotes: 0

starrify
starrify

Reputation: 14751

Your code merely declared & defined the variable of gid, but left its value uninitialized. You shall assign a correct value to it:

register gid_t gid;
gid = getgid();

Or simply:

register gid_t gid = getgid();

Upvotes: 4

Related Questions