Skip to content

Instantly share code, notes, and snippets.

@jonpryor
Created October 10, 2012 14:55
Show Gist options
  • Save jonpryor/3866137 to your computer and use it in GitHub Desktop.
Save jonpryor/3866137 to your computer and use it in GitHub Desktop.
+ public static Group [] getgrouplist (string username)
+ {
+ // Syscall to getpwnam to retrieve user uid
+ Passwd pw = Syscall.getpwnam (username);
+ if (pw == null)
+ return new Group [0];
+ // initialising the lngroups by 1 to get the group count
+ int ngroups = 1;
+ // allocating buffer to store group uid's
+ uint [] groups = new uint [ngroups];
+ int res = sys_getgrouplist (username, pw.pw_gid, groups, ref ngroups);
+ if (res == -1) {
+ // using value ngroups to resize buffer.
+ Array.Resize(ref groups, ngroups);
+ res = sys_getgrouplist (username, pw.pw_gid, groups, ref ngroups);
+ if (res == -1)
+ return new Group [0];
+ }
+ Group [] result = new Group [ngroups];
+ for (int i = 0; i < res; i++)
+ result [i] = Syscall.getgrgid (groups [i]);
+ return result;
+ }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment