Refactor new code.
authorWerner Koch <wk@gnupg.org>
Wed, 26 Aug 2009 09:24:04 +0000 (09:24 +0000)
committerWerner Koch <wk@gnupg.org>
Wed, 26 Aug 2009 09:24:04 +0000 (09:24 +0000)
jnlib/ChangeLog
jnlib/stringhelp.c

index 2418be9..87c5bda 100644 (file)
@@ -1,5 +1,10 @@
 2009-08-26  Werner Koch  <wk@g10code.com>
 
+       * stringhelp.c (do_make_filename): Factor some code out to ..
+       (get_pwdir): .. new.
+
+2009-08-26  Werner Koch  <wk@g10code.com>
+
        * stringhelp.c [HAVE_PWD_H]: Include pwd.h.
        (do_make_filename): New.
        (make_filename, make_filename_try): Implement using the new
index ebdbfc8..3173ebc 100644 (file)
@@ -319,6 +319,38 @@ make_dirname(const char *filepath)
 
 \f
 static char *
+get_pwdir (int xmode, const char *name)
+{
+  char *result = NULL;
+#ifdef HAVE_PWD_H
+  struct passwd *pwd = NULL;
+
+  if (name)
+    {
+#ifdef HAVE_GETPWNAM
+      /* Fixme: We should use getpwnam_r if available.  */
+      pwd = getpwnam (name);
+#endif
+    }
+  else
+    {
+#ifdef HAVE_GETPWUID
+      /* Fixme: We should use getpwuid_r if available.  */
+      pwd = getpwuid (getuid());
+#endif
+    }
+  if (pwd)
+    {
+      if (xmode)
+        result = jnlib_xstrdup (pwd->pw_dir);
+      else
+        result = jnlib_strdup (pwd->pw_dir);
+    }
+#endif /*HAVE_PWD_H*/
+  return result;
+}
+
+static char *
 do_make_filename (int xmode, const char *first_part, va_list arg_ptr)
 {
   const char *argv[32];
@@ -351,37 +383,16 @@ do_make_filename (int xmode, const char *first_part, va_list arg_ptr)
         {
           /* This is the "~/" or "~" case.  */
           home = getenv("HOME");
-
-#if defined(HAVE_GETPWUID) && defined(HAVE_PWD_H)
           if (!home)
-            {
-              struct passwd *pwd;
-              
-              pwd = getpwuid (getuid());
-              if (pwd)
-                {
-                  if (xmode)
-                    home_buffer = home = jnlib_xstrdup (pwd->pw_dir);
-                  else
-                    {
-                      home_buffer = home = jnlib_strdup (pwd->pw_dir);
-                      if (!home)
-                        return NULL;
-                    }
-                }
-            }
-#endif /* HAVE_GETPWUID && HAVE_PWD_H */
-
+            home = home_buffer = get_pwdir (xmode, NULL);
           if (home && *home)
             n += strlen (home);                            
         }
-#if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
       else
         {
           /* This is the "~username/" or "~username" case.  */
           char *user;
-          struct passwd *pwd;
-
+    
           if (xmode)
             user = jnlib_xstrdup (first_part+1);
           else
@@ -394,27 +405,14 @@ do_make_filename (int xmode, const char *first_part, va_list arg_ptr)
           if (p)
             *p = 0;
           skip = 1 + strlen (user);
-
-          /* Fixme: Use getwpnam_r if available.  */
-          pwd = getpwnam (user);
+          
+          home = home_buffer = get_pwdir (xmode, user);
           jnlib_free (user);
-          if (pwd)
-            {
-              if (xmode)
-                home_buffer = home = jnlib_xstrdup (pwd->pw_dir);
-              else
-                {
-                  home_buffer = home = jnlib_strdup (pwd->pw_dir);
-                  if (!home)
-                    return NULL;
-                }
-            }
           if (home)
             n += strlen (home);
           else
             skip = 1;
         }
-#endif /*HAVE_GETPWNAM && HAVE_PWD_H*/
     }
 
   if (xmode)