Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
List logged in *console* users on MacOS X
#import <Foundation/Foundation.h>
#import <utmpx.h>
// CCFLAGS="-std=c99 -framework Foundation -arch i386 -mmacosx-version-min=10.6"
NSSet *ConsoleUsers(void)
{
NSMutableSet *userList = [NSMutableSet set];
struct utmpx *userTmp = getutxent();
while ( userTmp != NULL) {
NSLog(@"\n========= Username: %s =========", userTmp->ut_user);
NSLog(@"userTmp->ut_user: %s", userTmp->ut_user);
NSLog(@"userTmp->ut_id: %s", userTmp->ut_id);
NSLog(@"userTmp->ut_line: %s", userTmp->ut_line);
NSLog(@"userTmp->ut_pid: %d", userTmp->ut_pid);
NSLog(@"userTmp->ut_type: %d", userTmp->ut_type);
NSString *username = [NSString stringWithUTF8String:userTmp->ut_user];
BOOL isConsoleUser = [[NSString stringWithUTF8String:userTmp->ut_line] isEqualToString:@"console"];
if ([username length] > 0 && isConsoleUser && userTmp->ut_type != DEAD_PROCESS )
[userList addObject:username];
userTmp = getutxent();
}
endutxent();
return userList;
}
int main(int argc, char *argv[]) {
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
NSSet *users = ConsoleUsers();
NSLog(@"Console users: %@", users);
[p release];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment