Skip to content

Instantly share code, notes, and snippets.

@CocoaBeans
Created February 28, 2013 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CocoaBeans/5054672 to your computer and use it in GitHub Desktop.
Save CocoaBeans/5054672 to your computer and use it in GitHub Desktop.
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