Skip to content

Instantly share code, notes, and snippets.

@mdippery
Created June 30, 2015 06:20
Show Gist options
  • Save mdippery/f9fad5c21a5b1a5df959 to your computer and use it in GitHub Desktop.
Save mdippery/f9fad5c21a5b1a5df959 to your computer and use it in GitHub Desktop.
Get file's group name in Cocoa
#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