From 5742b8eaf3fa9cda3dfb6b3ad0fea7485fff1a12 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Tue, 18 Feb 2020 08:53:30 +0100 Subject: core: Add gpgrt_fnameconcat and gpgrt_absfnameconcat. * src/gpg-error.h.in (gpgrt_fnameconcat): New. (gpgrt_absfnameconcat): New. * src/visibility.c (gpgrt_fnameconcat, gpgrt_absfnameconcat): New. * src/stringutils.c: New file. (_gpgrt_vfnameconcat): New. (_gpgrt_fnameconcat, _gpgrt_absfnameconcat): New. * src/gpg-error.def.in: Add new functions. * src/gpg-error.vers: Ditto. * src/sysutils.c: Include pwd.h. (_gpgrt_getpwdir): New. * configure.ac: Test for pwd.h, getpwnam, getpwuid, and their _r variants. * src/Makefile.am (libgpg_error_la_SOURCES): Add new file. * tests/t-stringutils.c: New. * tests/t-common.h (xmalloc, xstrdup, xfree): New. (die): Kludge to avoid compiler warnings. -- The new functions are based on the code of make_filename from GnuPG. They have been written by me ages ago with only minor modifications by David Shaw. I re-license them from LGPL-3.0+ OR GPL-2.0+ to LGPL-2.1-or-later. Signed-off-by: Werner Koch --- src/sysutils.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'src/sysutils.c') 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 #include +#ifdef HAVE_PWD_H +# include +#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; +} -- cgit v1.2.3