retrieving Owner name of file using getpwuid() in c always throw root

1k Views Asked by At

Hi when i login as root in my Linux system and try to get owner name of the file Using getpwuid() it always display root(lie) rather than actual owner name below is my code. please help me to get the actually name rather then root.

int main(int argc, char *argv[])
{
        register struct passwd *access;
        register uid_t uid;
        int c;

        uid = geteuid ();
        access= getpwuid (uid);
        if (access)
        {
                puts (access->pw_name);
                exit (EXIT_SUCCESS);
        }
        fprintf (stderr,"%s: cannot find username for UID %u\n",
                        _PROGRAM_NAME, (unsigned) uid);
        exit (EXIT_FAILURE);

}

I always get username as root how can i get the actually name of owner?

1

There are 1 best solutions below

0
On BEST ANSWER

geteuid() gets the effective user ID that the process is currently running as. Since you're logged in as root, this will return UID 0 -- and getpwuid() will correctly tell you that this is root.

If you want to get the owner of a file, why are you using geteuid()? You need to call a function from the stat() family to get that information.