// Authors:
// Jonathan Pryor (jonpryor@vt.edu)
//
-// (C) 2004 Jonathan Pryor
+// (C) 2004-2005 Jonathan Pryor
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
//
using System;
+using System.Collections;
using System.Text;
using Mono.Unix;
public sealed class UnixGroupInfo
{
- private Group group;
+ private Native.Group group;
public UnixGroupInfo (string group)
{
- this.group = new Group ();
- Group gr;
- int r = Syscall.getgrnam_r (group, this.group, out gr);
+ this.group = new Native.Group ();
+ Native.Group gr;
+ int r = Native.Syscall.getgrnam_r (group, this.group, out gr);
if (r != 0 || gr == null)
throw new ArgumentException (Locale.GetText ("invalid group name"), "group");
}
- public UnixGroupInfo (uint group)
+ public UnixGroupInfo (long group)
{
- this.group = new Group ();
- Group gr;
- int r = Syscall.getgrgid_r (group, this.group, out gr);
+ this.group = new Native.Group ();
+ Native.Group gr;
+ int r = Native.Syscall.getgrgid_r (Convert.ToUInt32 (group), this.group, out gr);
if (r != 0 || gr == null)
throw new ArgumentException (Locale.GetText ("invalid group id"), "group");
}
- public UnixGroupInfo (Group group)
+ public UnixGroupInfo (Native.Group group)
{
- this.group = group;
+ this.group = CopyGroup (group);
+ }
+
+ private static Native.Group CopyGroup (Native.Group group)
+ {
+ Native.Group g = new Native.Group ();
+
+ g.gr_gid = group.gr_gid;
+ g.gr_mem = group.gr_mem;
+ g.gr_name = group.gr_name;
+ g.gr_passwd = group.gr_passwd;
+
+ return g;
}
public string GroupName {
get {return group.gr_passwd;}
}
- public uint GroupId {
+ public long GroupId {
get {return group.gr_gid;}
}
- public string[] Members {
- get {return group.gr_mem;}
+ public UnixUserInfo[] GetMembers ()
+ {
+ UnixUserInfo[] members = new UnixUserInfo [group.gr_mem.Length];
+ for (int i = 0; i < members.Length; ++i)
+ members [i] = new UnixUserInfo (group.gr_mem [i]);
+ return members;
+ }
+
+ public string[] GetMemberNames ()
+ {
+ return (string[]) group.gr_mem.Clone ();
}
public override int GetHashCode ()
return group.ToString();
}
- public Group ToGroup ()
+ public Native.Group ToGroup ()
+ {
+ return CopyGroup (group);
+ }
+
+ public static UnixGroupInfo[] GetLocalGroups ()
{
- return group;
+ ArrayList entries = new ArrayList ();
+ lock (Native.Syscall.grp_lock) {
+ if (Native.Syscall.setgrent () != 0)
+ UnixMarshal.ThrowExceptionForLastError ();
+ try {
+ Native.Group g;
+ while ((g = Native.Syscall.getgrent()) != null)
+ entries.Add (new UnixGroupInfo (g));
+ if (Native.Syscall.GetLastError() != (Native.Errno) 0)
+ UnixMarshal.ThrowExceptionForLastError ();
+ }
+ finally {
+ Native.Syscall.endgrent ();
+ }
+ }
+ return (UnixGroupInfo[]) entries.ToArray (typeof(UnixGroupInfo));
}
}
}