This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 5826e8eeb1 libc/passwd: add pw_gecos field(userinfo)
5826e8eeb1 is described below

commit 5826e8eeb15ee4e0d9aa29d1e9f922c8fc5bc43e
Author: dongjiuzhu1 <[email protected]>
AuthorDate: Wed Nov 16 12:32:32 2022 +0800

    libc/passwd: add pw_gecos field(userinfo)
    
    The pw_gecos field is not specified in POSIX, but is
    present on most implementations.
    passwd file format:
    name:uid:gid:gecos:dir:shell
    ex: gdm:x:127:133:Gnome Display Manager:/var/lib/gdm3:/bin/false
    
    Signed-off-by: dongjiuzhu1 <[email protected]>
---
 include/pwd.h                    |  1 +
 libs/libc/pwd/lib_find_pwdfile.c | 22 +++++++++++++++++++++-
 libs/libc/pwd/lib_getpwbuf.c     |  9 ++++++---
 libs/libc/pwd/lib_getpwbufr.c    | 16 ++++++++++------
 libs/libc/pwd/lib_getpwnam.c     |  3 ++-
 libs/libc/pwd/lib_getpwnamr.c    |  4 ++--
 libs/libc/pwd/lib_getpwuid.c     |  3 ++-
 libs/libc/pwd/lib_getpwuidr.c    |  4 ++--
 libs/libc/pwd/lib_pwd.h          |  9 +++++----
 9 files changed, 51 insertions(+), 20 deletions(-)

diff --git a/include/pwd.h b/include/pwd.h
index d390f30c4a..216ddbd04c 100644
--- a/include/pwd.h
+++ b/include/pwd.h
@@ -49,6 +49,7 @@ struct passwd
   FAR char *pw_name;
   uid_t     pw_uid;
   gid_t     pw_gid;
+  FAR char *pw_gecos;
   FAR char *pw_dir;
   FAR char *pw_shell;
 };
diff --git a/libs/libc/pwd/lib_find_pwdfile.c b/libs/libc/pwd/lib_find_pwdfile.c
index ad445ce53d..d2020b7748 100644
--- a/libs/libc/pwd/lib_find_pwdfile.c
+++ b/libs/libc/pwd/lib_find_pwdfile.c
@@ -219,7 +219,27 @@ static int pwd_foreach(pwd_foreach_match_t match, 
uintptr_t arg,
 
       *ptr++        = '\0';
       entry->pw_gid = (gid_t)atoi(save);
-      entry->pw_dir = ptr;
+      save          = ptr;
+
+      /* Skip to the end of the user information and properly terminate it.
+       * The user information must be terminated with the field delimiter
+       * ':'.
+       */
+
+      for (; *ptr != '\n' && *ptr != '\0' && *ptr != ':'; ptr++)
+        {
+        }
+
+      if (*ptr == '\n' || *ptr == '\0')
+        {
+          /* Bad line format? */
+
+          continue;
+        }
+
+      *ptr++          = '\0';
+      entry->pw_gecos = save;
+      entry->pw_dir   = ptr;
 
       /* Skip to the end of the home directory and properly terminate it.
        * The home directory must be the last thing on the line.
diff --git a/libs/libc/pwd/lib_getpwbuf.c b/libs/libc/pwd/lib_getpwbuf.c
index 51411a4808..5a3fe8f292 100644
--- a/libs/libc/pwd/lib_getpwbuf.c
+++ b/libs/libc/pwd/lib_getpwbuf.c
@@ -54,6 +54,7 @@ static FAR struct passwd *g_pwd;
  *   uid   - Value to set the passwd structure's pw_uid field to.
  *   gid   - Value to set the passwd structure's pw_gid field to.
  *   name  - Value to set the passwd structure's pw_name field to.
+ *   geocs - Value to set the passwd structure's pw_gecos field to.
  *   dir   - Value to set the passwd structure's pw_dir field to.
  *   shell - Value to set the passwd structure's pw_shell field to.
  *
@@ -64,14 +65,16 @@ static FAR struct passwd *g_pwd;
  ****************************************************************************/
 
 FAR struct passwd *getpwbuf(uid_t uid, gid_t gid, FAR const char *name,
-                            FAR const char *dir, FAR const char *shell)
+                            FAR const char *gecos, FAR const char *dir,
+                            FAR const char *shell)
 {
   FAR struct passwd *result;
   FAR char *newbuf;
   size_t buflen;
   int err;
 
-  buflen = strlen(name) + 1 + strlen(dir) + 1 + strlen(shell) + 1;
+  buflen = strlen(name) + 1 + strlen(gecos) + 1 + strlen(dir) + 1 +
+           strlen(shell) + 1;
 
   newbuf = (FAR char *)lib_realloc(g_buf, buflen);
 
@@ -94,7 +97,7 @@ FAR struct passwd *getpwbuf(uid_t uid, gid_t gid, FAR const 
char *name,
       goto error;
     }
 
-  err = getpwbuf_r(uid, gid, name, dir, shell,
+  err = getpwbuf_r(uid, gid, name, gecos, dir, shell,
                    g_pwd, g_buf, buflen, &result);
 
   if (err)
diff --git a/libs/libc/pwd/lib_getpwbufr.c b/libs/libc/pwd/lib_getpwbufr.c
index 388627185e..f4d1effb77 100644
--- a/libs/libc/pwd/lib_getpwbufr.c
+++ b/libs/libc/pwd/lib_getpwbufr.c
@@ -47,6 +47,7 @@
  *   uid    - Value to set the passwd structure's pw_uid field to.
  *   gid    - Value to set the passwd structure's pw_gid field to.
  *   name   - Value to set the passwd structure's pw_name field to.
+ *   gecos  - Value to set the passwd structure's pw_gecos field to.
  *   dir    - Value to set the passwd structure's pw_dir field to.
  *   shell  - Value to set the passwd structure's pw_shell field to.
  *   pwd    - Pointer to the space to store the retrieved passwd structure
@@ -63,13 +64,14 @@
  ****************************************************************************/
 
 int getpwbuf_r(uid_t uid, gid_t gid, FAR const char *name,
-               FAR const char *dir, FAR const char *shell,
-               FAR struct passwd *pwd, FAR char *buf, size_t buflen,
-               FAR struct passwd **result)
+               FAR const char *gecos, FAR const char *dir,
+               FAR const char *shell, FAR struct passwd *pwd,
+               FAR char *buf, size_t buflen, FAR struct passwd **result)
 {
   size_t reqdlen;
 
-  reqdlen = strlen(name) + 1 + strlen(dir) + 1 + strlen(shell) + 1;
+  reqdlen = strlen(name) + 1 + strlen(gecos) + 1 + strlen(dir) + 1 +
+            strlen(shell) + 1;
 
   if (buflen < reqdlen)
     {
@@ -80,12 +82,14 @@ int getpwbuf_r(uid_t uid, gid_t gid, FAR const char *name,
     }
 
   pwd->pw_name  = buf;
-  pwd->pw_dir   = &buf[strlen(name) + 1];
-  pwd->pw_shell = &buf[strlen(name) + 1 + strlen(dir) + 1];
+  pwd->pw_gecos = &buf[strlen(name) + 1];
+  pwd->pw_dir   = &buf[strlen(name) + strlen(gecos) + 2];
+  pwd->pw_shell = &buf[strlen(name) + strlen(gecos) + strlen(dir) + 3];
 
   pwd->pw_uid = uid;
   pwd->pw_gid = gid;
   strcpy(pwd->pw_name, name);
+  strcpy(pwd->pw_gecos, gecos);
   strcpy(pwd->pw_dir, dir);
   strcpy(pwd->pw_shell, shell);
 
diff --git a/libs/libc/pwd/lib_getpwnam.c b/libs/libc/pwd/lib_getpwnam.c
index 16cd3505a0..61c1b14fd5 100644
--- a/libs/libc/pwd/lib_getpwnam.c
+++ b/libs/libc/pwd/lib_getpwnam.c
@@ -71,6 +71,7 @@ FAR struct passwd *getpwnam(FAR const char *name)
       return NULL;
     }
 
-  return getpwbuf(ROOT_UID, ROOT_GID, ROOT_NAME, ROOT_DIR, ROOT_SHELL);
+  return getpwbuf(ROOT_UID, ROOT_GID, ROOT_NAME, ROOT_NAME, ROOT_DIR,
+                  ROOT_SHELL);
 #endif
 }
diff --git a/libs/libc/pwd/lib_getpwnamr.c b/libs/libc/pwd/lib_getpwnamr.c
index 61340bf4f9..557cf45d3f 100644
--- a/libs/libc/pwd/lib_getpwnamr.c
+++ b/libs/libc/pwd/lib_getpwnamr.c
@@ -82,7 +82,7 @@ int getpwnam_r(FAR const char *name, FAR struct passwd *pwd, 
FAR char *buf,
       return 0;
     }
 
-  return getpwbuf_r(ROOT_UID, ROOT_GID, ROOT_NAME, ROOT_DIR, ROOT_SHELL, pwd,
-                    buf, buflen, result);
+  return getpwbuf_r(ROOT_UID, ROOT_GID, ROOT_NAME, ROOT_NAME, ROOT_DIR,
+                    ROOT_SHELL, pwd, buf, buflen, result);
 #endif
 }
diff --git a/libs/libc/pwd/lib_getpwuid.c b/libs/libc/pwd/lib_getpwuid.c
index 3fac2a0996..f9606d9fa6 100644
--- a/libs/libc/pwd/lib_getpwuid.c
+++ b/libs/libc/pwd/lib_getpwuid.c
@@ -70,6 +70,7 @@ FAR struct passwd *getpwuid(uid_t uid)
       return NULL;
     }
 
-  return getpwbuf(ROOT_UID, ROOT_GID, ROOT_NAME, ROOT_DIR, ROOT_SHELL);
+  return getpwbuf(ROOT_UID, ROOT_GID, ROOT_NAME, ROOT_NAME, ROOT_DIR,
+                  ROOT_SHELL);
 #endif
 }
diff --git a/libs/libc/pwd/lib_getpwuidr.c b/libs/libc/pwd/lib_getpwuidr.c
index 279edd173e..593c50b552 100644
--- a/libs/libc/pwd/lib_getpwuidr.c
+++ b/libs/libc/pwd/lib_getpwuidr.c
@@ -81,7 +81,7 @@ int getpwuid_r(uid_t uid, FAR struct passwd *pwd, FAR char 
*buf,
       return 0;
     }
 
-  return getpwbuf_r(ROOT_UID, ROOT_GID, ROOT_NAME, ROOT_DIR, ROOT_SHELL, pwd,
-                    buf, buflen, result);
+  return getpwbuf_r(ROOT_UID, ROOT_GID, ROOT_NAME, ROOT_NAME, ROOT_DIR,
+                    ROOT_SHELL, pwd, buf, buflen, result);
 #endif
 }
diff --git a/libs/libc/pwd/lib_pwd.h b/libs/libc/pwd/lib_pwd.h
index a1cf42a09b..6df60732a6 100644
--- a/libs/libc/pwd/lib_pwd.h
+++ b/libs/libc/pwd/lib_pwd.h
@@ -63,11 +63,12 @@ EXTERN char g_passwd_buffer[CONFIG_LIBC_PASSWD_LINESIZE];
  ****************************************************************************/
 
 FAR struct passwd *getpwbuf(uid_t uid, gid_t gid, FAR const char *name,
-                            FAR const char *dir, FAR const char *shell);
+                            FAR const char *gecos, FAR const char *dir,
+                            FAR const char *shell);
 int getpwbuf_r(uid_t uid, gid_t gid, FAR const char *name,
-               FAR const char *dir, FAR const char *shell,
-               FAR struct passwd *pwd, FAR char *buf, size_t buflen,
-               FAR struct passwd **result);
+               FAR const char *gecos, FAR const char *dir,
+               FAR const char *shell, FAR struct passwd *pwd,
+               FAR char *buf, size_t buflen, FAR struct passwd **result);
 
 #ifdef CONFIG_LIBC_PASSWD_FILE
 int pwd_findby_name(FAR const char *uname, FAR struct passwd *entry,

Reply via email to