Created
June 30, 2015 06:20
-
-
Save mdippery/f9fad5c21a5b1a5df959 to your computer and use it in GitHub Desktop.
Get file's group name in Cocoa
This file contains 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> | |
#include <grp.h> | |
#include <string.h> | |
#include <sys/stat.h> | |
@interface FileOwnerManager : NSObject | |
- (BOOL)isWheelPresent:(NSString *)path; | |
@end | |
@implementation FileOwnerManager | |
- (BOOL)isWheelPresent:(NSString *)path | |
{ | |
struct stat f_stat; | |
int res = stat([path UTF8String], &f_stat); | |
if (res != 0) { | |
NSLog(@"Error!"); | |
return NO; | |
} | |
struct group *group = getgrgid(f_stat.st_gid); | |
NSLog(@"%@: %s", path, group->gr_name); | |
return strcmp(group->gr_name, "wheel") == 0; | |
} | |
@end | |
int main(int argc, char **argv) | |
{ | |
BOOL res = NO; | |
@autoreleasepool { | |
FileOwnerManager *fom = [[FileOwnerManager alloc] init]; | |
res = [fom isWheelPresent:[NSString stringWithUTF8String:argv[1]]]; | |
[fom release]; | |
} | |
return res ? 0 : 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment