[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#33195: 27.0.50; user-login-name has no group-login-name
From: |
Jules Tamagnan |
Subject: |
bug#33195: 27.0.50; user-login-name has no group-login-name |
Date: |
Tue, 30 Oct 2018 11:25:33 -0700 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) |
First off I want to say thank you for your review and your time
Eli Zaretskii <eliz@gnu.org> writes:
>> From: Jules Tamagnan <jtamagnan@gmail.com>
>> Date: Mon, 29 Oct 2018 11:52:17 -0700
>>
>> I've found it difficult to go from an integer group id to the name of
>> that group. I've looked at the source for `user-login-name` and
>> `system-groups` and could not find a way to do what I needed.
>>
>> As such I think it would be great to add a function to do that.
>
> Thanks. Please allow me a few comments:
>
> Please always provide a ChangeLog-style commit log message describing
> your changes. The file CONTRIBUTE has more about that.
>
>> I'm unsure how to make an argument not be optional.
>
> You want the argument NOT to be optional? If so, make this:
>
>> +DEFUN ("group-name", Fgroup_name, Sgroup_name, 0, 1, 0,
>
> say this instead:
>
>> +DEFUN ("group-name", Fgroup_name, Sgroup_name, 1, 1, 0,
>
> See the node "Writing Emacs Primitives" in the ELisp manual, which
> describes how to use the DEFUN macro.
Turns out there is a word for not optional... required :)
>> +DEFUN ("group-name", Fgroup_name, Sgroup_name, 0, 1, 0,
>> + doc: /* If argument GID is an integer or a float, return the login
>> name
>> +of the group with that GID, or nil if there is no such GID. */)
>> + (Lisp_Object gid)
>> +{
>> + struct group *gr;
>> + gid_t id;
>> +
>> + if (NILP (gid))
>> + return Qnil;
>
> Wouldn't it be more useful to return user's group name when the
> argument is nil or omitted?
I always thought that user could have many groups, for example if I run
$ groups $(id -un)
I get a long list of groups. I'm not sure if I misunderstood your
statement or the desired behavior but for the moment I think a separate
function would be best for this. Or maybe the `system-groups` function
could be modified to take in a UID and then list groups for that UID,
then if no argument is passed in, it would return the full list of
system-groups as it does now.
> Also, would you please write a couple of simple tests for this new
> function?
I've tried my hand at writing a test for this but am dubious it will
pass on all machines, I believe it should work on gnu/linux
> Finally, this function should be announced in NEWS and documented in
> the ELisp manual.
I've added a short entry in NEWS under "Lisp Changes in Emacs 27.1" and
in os.texi
>From 671c8b55d80743a13d6fda99f5181558cd56eea6 Mon Sep 17 00:00:00 2001
From: Jules Tamagnan <jtamagnan@gmail.com>
Date: Tue, 30 Oct 2018 10:22:03 -0700
Subject: [PATCH] src/editfns.c (group-name): New function.
---
doc/lispref/os.texi | 5 +++++
etc/NEWS | 3 +++
src/editfns.c | 16 ++++++++++++++++
test/src/editfns-tests.el | 7 +++++++
4 files changed, 31 insertions(+)
diff --git a/doc/lispref/os.texi b/doc/lispref/os.texi
index cb33757..dc32958 100644
--- a/doc/lispref/os.texi
+++ b/doc/lispref/os.texi
@@ -1230,6 +1230,11 @@ User Identification
return value is @code{nil}.
@end defun
+@defun user-login-name &optional gid
+This runction returns the group name that corresponds to @var{gid},
+or @code{nil} if there is no such group.
+@end defun
+
@node Time of Day
@section Time of Day
diff --git a/etc/NEWS b/etc/NEWS
index 226ae1e..a0bcbb5 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1228,6 +1228,9 @@ where there's no better alternative. We believe that the
incorrect
uses of this function all but disappeared by now, so we are
un-obsoleting it.
++++
+** New function 'group-name' returns a group name based on a group-GID
+
* Changes in Emacs 27.1 on Non-Free Operating Systems
diff --git a/src/editfns.c b/src/editfns.c
index e995b38..15a0fa7 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -1143,6 +1143,21 @@ of the user with that uid, or nil if there is no such
user. */)
return (pw ? build_string (pw->pw_name) : Qnil);
}
+DEFUN ("group-name", Fgroup_name, Sgroup_name, 1, 1, 0,
+ doc: /* If argument GID is an integer or a float, return the login name
+of the group with that gid, or nil if there is no such GID. */)
+ (Lisp_Object gid)
+{
+ struct group *gr;
+ gid_t id;
+
+ CONS_TO_INTEGER (gid, gid_t, id);
+ block_input ();
+ gr = getgrgid (id);
+ unblock_input ();
+ return (gr ? build_string (gr->gr_name) : Qnil);
+}
+
DEFUN ("user-real-login-name", Fuser_real_login_name, Suser_real_login_name,
0, 0, 0,
doc: /* Return the name of the user's real uid, as a string.
@@ -4487,6 +4502,7 @@ it to be non-nil. */);
defsubr (&Sinsert_byte);
defsubr (&Suser_login_name);
+ defsubr (&Sgroup_name);
defsubr (&Suser_real_login_name);
defsubr (&Suser_uid);
defsubr (&Suser_real_uid);
diff --git a/test/src/editfns-tests.el b/test/src/editfns-tests.el
index 17b2c51..6ee0ab0 100644
--- a/test/src/editfns-tests.el
+++ b/test/src/editfns-tests.el
@@ -351,4 +351,11 @@ transpose-test-get-byte-positions
(should (equal (format "%-#50.40x" v3)
"-0x000000003ffffffffffffffe000000000000000 "))))
+(ert-deftest group-name ()
+ (let ((list `((0 . "root")
+ (1000 . ,(user-login-name 1000))
+ (1212345 . nil))))
+ (dolist (test list)
+ (should (equal (group-name (car test)) (cdr test))))))
+
;;; editfns-tests.el ends here
--
2.7.4