views:

65

answers:

1

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
+1  A: 

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.

Peter Hosey
Hmm, I have 4 accounts (simon,neoneye,johndoe,testuser1), but two of them aren't listed in the staff group, even though they are in the staff group. What I'm seeing is STAFF: root, ADMIN: root, ADMIN: neoneye, ADMIN: simonI don't see my johndoe anywhere nor my testuser1. Perhaps theres is more to it?if(gr = getgrnam("staff")) { for (i=0; gr->gr_mem[i]!=0; ++i) { NSLog(@"STAFF: %s",gr->gr_mem[i]); }
neoneye
neoneye: I see that you've accepted this answer. I take it you got it working, then? What was wrong?
Peter Hosey
sorry, I accepted it, but unfortunately getgrnam("staff") doesn't seem to list all the accounts on my system, so I guess I have to look for another solution. iirc there was some user account info in the SystemConfiguration api.
neoneye