xiaoxiang781216 commented on code in PR #20987:
URL: https://github.com/apache/nuttx/pull/20987#discussion_r4371324290


##########
libs/libc/misc/lib_glob.c:
##########
@@ -403,6 +404,77 @@ static int sort(FAR const void *a, FAR const void *b)
   return strcmp(*(FAR const char**)a, *(FAR const char**)b);
 }
 
+/****************************************************************************
+ * Name: glob_home
+ *
+ * Description:
+ *   Copy the home directory of a user to home (PATH_MAX bytes).  An empty
+ *   user name is the calling user: then HOME is used when it is set, else
+ *   the password database.
+ *
+ * Returned Value:
+ *   Zero on success, GLOB_NOMATCH when the home directory is not known,
+ *   GLOB_NOSPACE when there is no memory.
+ *
+ ****************************************************************************/
+
+static int glob_home(FAR const char *user, FAR char *home)
+{
+  FAR const char *dir = NULL;
+  FAR struct passwd *entry = NULL;
+  FAR char *pwbuf = NULL;
+  struct passwd pwd;
+  int ret = 0;
+
+  if (user[0] == '\0')
+    {
+      dir = getenv("HOME");
+    }
+
+  if (dir == NULL)
+    {
+      pwbuf = lib_get_pathbuffer();

Review Comment:
   lib_get_tempbuffer



##########
libs/libc/misc/lib_glob.c:
##########
@@ -403,6 +404,77 @@ static int sort(FAR const void *a, FAR const void *b)
   return strcmp(*(FAR const char**)a, *(FAR const char**)b);
 }
 
+/****************************************************************************
+ * Name: glob_home
+ *
+ * Description:
+ *   Copy the home directory of a user to home (PATH_MAX bytes).  An empty
+ *   user name is the calling user: then HOME is used when it is set, else
+ *   the password database.
+ *
+ * Returned Value:
+ *   Zero on success, GLOB_NOMATCH when the home directory is not known,
+ *   GLOB_NOSPACE when there is no memory.
+ *
+ ****************************************************************************/
+
+static int glob_home(FAR const char *user, FAR char *home)
+{
+  FAR const char *dir = NULL;
+  FAR struct passwd *entry = NULL;
+  FAR char *pwbuf = NULL;
+  struct passwd pwd;
+  int ret = 0;
+
+  if (user[0] == '\0')
+    {
+      dir = getenv("HOME");
+    }
+
+  if (dir == NULL)
+    {
+      pwbuf = lib_get_pathbuffer();
+      if (pwbuf == NULL)
+        {
+          return GLOB_NOSPACE;
+        }
+
+      if (user[0] == '\0')
+        {
+          ret = getpwuid_r(getuid(), &pwd, pwbuf, PATH_MAX, &entry);
+        }
+      else
+        {
+          ret = getpwnam_r(user, &pwd, pwbuf, PATH_MAX, &entry);
+        }
+
+      if (ret == 0 && entry != NULL)
+        {
+          dir = entry->pw_dir;
+        }
+    }
+
+  if (dir == NULL)
+    {
+      ret = ret == ENOMEM ? GLOB_NOSPACE : GLOB_NOMATCH;
+    }
+  else if (strlcpy(home, dir, PATH_MAX) >= PATH_MAX - 1)
+    {
+      ret = GLOB_NOMATCH;
+    }
+  else
+    {
+      ret = 0;

Review Comment:
   why need



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to