Refactor new code.

This commit is contained in:
Werner Koch 2009-08-26 09:24:04 +00:00
parent 5134fee5b0
commit 53476e5413
2 changed files with 41 additions and 38 deletions

View File

@ -1,3 +1,8 @@
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.

View File

@ -318,6 +318,38 @@ make_dirname(const char *filepath)
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)
{
@ -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)