How To Get The Owner Of A Unix File Using C

c, file-ownership

Solution

Unix file permissions work by using the UID. Usernames can't own files. Only UIDs can. So if a file belongs to a specific UID, than all users with that UID own the file.

So it doesn't matter which username you get, since all users with that UID own the file.

Problem

I'm trying the get owner's name of a Unix file using C. The only method I have found is to use stat() and then getpwuid(stat.st_uid). But, it only returns the first user name with that uid, where users in the password file can have the same uid. Obviously, this is unacceptable and cannot be trusted. References: Owner is recievd from password file: http://pubs.opengroup.org/onlinepubs/007904875/functions/getpwuid.html Uid is found in password file: http://www.cyberciti.biz/faq/understanding-etcpasswd-file-format/ Unix allow multiple users to have same uid: http://www.e-reading.org.ua/htmbook.php/orelly/networking/puis/ch04_01.htm Is there an accurate way or a lower level way, some kind of look up table that would guarantee me accurate results?

Original source