+DEFUN("user-group-name", Fuser_group_name, 0, 1, 0, /*
+Return the group name under which the user logged in, as a string.
+This is based on the effective gid, not the real gid.
+If the optional argument GID is present, then this function returns
+the group name for that UID, or nil.
+*/
+ (gid))
+{
+ char *returned_name;
+ uid_t local_gid;
+
+ if (!NILP(gid)) {
+ CHECK_INT(gid);
+ local_gid = XINT(gid);
+ returned_name = user_group_name(&local_gid);
+ } else {
+ returned_name = user_group_name(NULL);
+ }
+ /* #### - I believe this should return nil instead of "unknown" when pw==0
+ pw=0 is indicated by a null return from user_login_name
+ */
+ return returned_name ? build_string(returned_name) : Qnil;
+}
+
+/* This function may be called from other C routines when a
+ character string representation of the user_group_name is
+ needed but a Lisp Object is not. The GID is passed by
+ reference. If GID == NULL, then the group for
+ for the user running XEmacs will be returned. This
+ corresponds to a nil argument to Fuser_group_name.
+*/
+char *user_group_name(gid_t * gid)
+{
+ /* gid == NULL to return the group of this user */
+ struct group * grp = getgrgid( gid ? *gid : getegid());
+ if (grp == NULL) {
+ return NULL;
+ }
+ return grp->gr_name;
+}
+