cocoamacosgetpwnamgetpwuiddscl

list of uids / names of System Preferences > Accounts


How can I obtain an array with uid and names?

I could iterate from 0 to 99999 and do a getpwnam(). However most machines have less than 5 accounts, so it's not optimal. I don't know what framework is responsible for this and thus I have no clue what to search for.

Is there a more optimal solution that can traverse the accounts?

Edit: Right after I posted I discovered getpwent() for traversing accounts.

setpwent();
struct passwd *pw;
while ((pw = getpwent())) printf("%d\n", pw->pw_uid);
endpwent();

However that doesn't indicate wether an account is a System Preferences account or not.

So still how does one obtain the System Preferences accounts?


Edit: I have found the commandline equivalent of this, the dscl command.

prompt> dscl . -list /Users UniqueID
_mysql                  74
_postfix                27
_spotlight              89
_sshd                   75
_windowserver           88
_www                    70
daemon                  1
johndoe                 501
nobody                  -2
root                    0

Solution

  • Use getgrnam("staff") to get a group record for the staff group. The gr_mem member, while not explained in detail by the manpage, appears to be an array of user names terminated by a NULL pointer.

    To find which users are administrators, do the same thing with the admin group.