Created
February 28, 2013 06:22
-
-
Save CocoaBeans/5054672 to your computer and use it in GitHub Desktop.
List logged in *console* users on MacOS X
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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