diff options
Diffstat (limited to 'src/sysutils.c')
-rw-r--r-- | src/sysutils.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/sysutils.c b/src/sysutils.c index bc31d92..6bdd76f 100644 --- a/src/sysutils.c +++ b/src/sysutils.c @@ -32,6 +32,9 @@ #endif #include <sys/types.h> #include <fcntl.h> +#ifdef HAVE_PWD_H +# include <pwd.h> +#endif #include "gpgrt-int.h" @@ -337,3 +340,39 @@ _gpgrt_getcwd (void) #endif } } + + +/* Get the standard home directory for user NAME. If NAME is NULL the + * directory for the current user is retruned. Caller must release + * the returned string. */ +char * +_gpgrt_getpwdir (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) + { + result = _gpgrt_strdup (pwd->pw_dir); + } +#else /*!HAVE_PWD_H*/ + /* No support at all. */ + (void)name; +#endif /*HAVE_PWD_H*/ + return result; +} |