Add support for Unix lookup by name to user-uid and user-gid
authorNelson Ferreira <nelson.ferreira@ieee.org>
Sun, 16 Mar 2014 22:57:18 +0000 (18:57 -0400)
committerNelson Ferreira <nelson.ferreira@ieee.org>
Sun, 16 Mar 2014 22:57:18 +0000 (18:57 -0400)
* src/editfns.c (Fuser_uid): add optional parameter user_name. If
specified the system will lookup the user uid, return nil if that
user does not exist.

* src/editfns.c (Fuser_gid): add optional parameter group_name. If
specified the system will lookup the group gid, return nil if that
group does not exist.

* src/editfns.c (Fuser_real_uid): clarify that the uid returned is
that of the process.

* src/editfns.c (Fuser_real_gid): clarify that the uid returned is
that of the process.

Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
src/editfns.c

index a8d3fe0..c2d6a01 100644 (file)
@@ -763,24 +763,62 @@ This ignores the environment variables LOGNAME and USER, so it differs from
        return tem;
 }
 
-DEFUN("user-uid", Fuser_uid, 0, 0, 0,  /*
-Return the effective uid of Emacs, as an integer.
+DEFUN("user-uid", Fuser_uid, 0, 1, 0,  /*
+Return the effective uid of the Emacs process, as an integer.
+If the optional argument `user_name' is specified it returns the uid of
+the user with that name. Will return `nil' if there is no user with the
+specified name,
 */
-      ())
+      (user_name))
 {
-       return make_int(geteuid());
+       if (!NILP(user_name)) {
+               const char * user_name_ext = NULL;
+
+               CHECK_STRING(user_name);
+
+               TO_EXTERNAL_FORMAT(LISP_STRING, user_name,
+                                  C_STRING_ALLOCA, user_name_ext, Qnative);
+
+               struct passwd *pw = getpwnam(user_name_ext);
+               if (pw) {
+                       return make_int(pw->pw_uid);
+               } else {
+                       return Qnil;
+               }
+       } else {
+               return make_int(geteuid());
+       }
 }
 
-DEFUN("user-gid", Fuser_gid, 0, 0, 0,  /*
-Return the effective gid of Emacs, as an integer.
+DEFUN("user-gid", Fuser_gid, 0, 1, 0,  /*
+Return the effective gid of the Emacs process, as an integer.
+If the optional argument `group_name' is specified it returns the gid of
+the group with that name. It will return `nil' if the system has no
+group with the specified name. 
 */
-      ())
+      (group_name))
 {
-       return make_int(getegid());
+       if (!NILP(group_name)) {
+               const char *group_name_ext = NULL;
+
+               CHECK_STRING(group_name);
+
+               TO_EXTERNAL_FORMAT(LISP_STRING, group_name,
+                                  C_STRING_ALLOCA, group_name_ext, Qnative);
+
+               struct group *grp = getgrnam(group_name_ext);
+               if (grp) {
+                       return make_int(grp->gr_gid);
+               } else {
+                       return Qnil;
+               }
+       } else {
+               return make_int(getegid());
+       }
 }
 
 DEFUN("user-real-uid", Fuser_real_uid, 0, 0, 0,        /*
-Return the real uid of Emacs, as an integer.
+Return the real uid of the Emacs process, as an integer.
 */
       ())
 {
@@ -788,7 +826,7 @@ Return the real uid of Emacs, as an integer.
 }
 
 DEFUN("user-real-gid", Fuser_real_gid, 0, 0, 0,        /*
-Return the real gid of Emacs, as an integer.
+Return the real gid of the Emacs process, as an integer.
 */
       ())
 {