From cb00951803af46f35d9f1bffe00328da9d976558 Mon Sep 17 00:00:00 2001 From: David Shaw Date: Wed, 31 Aug 2005 15:36:50 +0000 Subject: * fileutil.c (untilde): New. Expand ~/foo and ~username/foo filenames into full paths using $HOME if possible, or getpwuid/getpwnam if necessary. (make_filename): Use it here. --- ChangeLog | 4 +++ configure.ac | 4 +-- util/ChangeLog | 7 ++++++ util/fileutil.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 82 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index df0c72613..8504ed3a2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2005-08-31 David Shaw + + * configure.ac: Check for getpwnam, getpwuid, and pwd.h. + 2005-08-09 David Shaw * configure.ac: Remove hardcoded -I and -L for /usr/local on diff --git a/configure.ac b/configure.ac index 517e05d8d..6ab6fe0ba 100644 --- a/configure.ac +++ b/configure.ac @@ -765,7 +765,7 @@ AM_CONDITIONAL(ENABLE_AGENT_SUPPORT, test "$agent_support" = yes) dnl Checks for header files. AC_HEADER_STDC -AC_CHECK_HEADERS([unistd.h langinfo.h termio.h locale.h getopt.h]) +AC_CHECK_HEADERS([unistd.h langinfo.h termio.h locale.h getopt.h pwd.h]) # Note that we do not check for iconv here because this is done anyway # by the gettext checks and thus it allows us to disable the use of @@ -827,7 +827,7 @@ AC_FUNC_FSEEKO AC_FUNC_VPRINTF AC_FUNC_FORK AC_CHECK_FUNCS(strerror stpcpy strsep strlwr tcgetattr strtoul mmap) -AC_CHECK_FUNCS(strcasecmp strncasecmp ctermid times unsetenv) +AC_CHECK_FUNCS(strcasecmp strncasecmp ctermid times unsetenv getpwnam getpwuid) AC_CHECK_FUNCS(memmove gettimeofday getrusage setrlimit clock_gettime) AC_CHECK_FUNCS(atexit raise getpagesize strftime nl_langinfo setlocale) AC_CHECK_FUNCS(waitpid wait4 sigaction sigprocmask rand pipe stat getaddrinfo) diff --git a/util/ChangeLog b/util/ChangeLog index ec3b31161..be2e764fa 100644 --- a/util/ChangeLog +++ b/util/ChangeLog @@ -1,3 +1,10 @@ +2005-08-31 David Shaw + + * fileutil.c (untilde): New. Expand ~/foo and ~username/foo + filenames into full paths using $HOME if possible, or + getpwuid/getpwnam if necessary. + (make_filename): Use it here. + 2005-07-28 Werner Koch * pka.c: New. diff --git a/util/fileutil.c b/util/fileutil.c index 8b23d619b..c956b4fec 100644 --- a/util/fileutil.c +++ b/util/fileutil.c @@ -1,5 +1,5 @@ /* fileutil.c - file utilities - * Copyright (C) 1998, 2003 Free Software Foundation, Inc. + * Copyright (C) 1998, 2003, 2005 Free Software Foundation, Inc. * * This file is part of GnuPG. * @@ -26,6 +26,10 @@ #include #include #include +#include +#ifdef HAVE_PWD_H +#include +#endif #include "util.h" #include "memory.h" #include "ttyio.h" @@ -88,7 +92,62 @@ make_dirname(const char *filepath) return dirname; } +/* Expand tildes. Handles both the ~/foo and ~username/foo cases. + Returns what the tilde expands to. *name is advanced to be past + the tilde expansion. */ +static char * +untilde(const char **name) +{ + char *home=NULL; + + assert((*name)[0]=='~'); + + if((*name)[1]==DIRSEP_C || (*name)[1]=='\0') + { + /* This is the "~/foo" or "~" case. */ + char *tmp=getenv("HOME"); + if(tmp) + home=xstrdup(tmp); + +#ifdef HAVE_GETPWUID + if(!home) + { + struct passwd *pwd; + + pwd=getpwuid(getuid()); + if(pwd) + home=xstrdup(pwd->pw_dir); + } +#endif + if(home) + (*name)++; + } +#ifdef HAVE_GETPWNAM + else + { + /* This is the "~username" case. */ + char *user,*sep; + struct passwd *pwd; + + user=xstrdup((*name)+1); + + sep=strchr(user,DIRSEP_C); + if(sep) + *sep='\0'; + + pwd=getpwnam(user); + if(pwd) + { + home=xstrdup(pwd->pw_dir); + (*name)+=1+strlen(user); + } + + xfree(user); + } +#endif + return home; +} /* Construct a filename from the NULL terminated list of parts. Tilde @@ -100,7 +159,7 @@ make_filename( const char *first_part, ... ) va_list arg_ptr ; size_t n; const char *s; - char *name, *home, *p; + char *name, *p, *home=NULL; va_start( arg_ptr, first_part ) ; n = strlen(first_part)+1; @@ -108,19 +167,22 @@ make_filename( const char *first_part, ... ) n += strlen(s) + 1; va_end(arg_ptr); - home = NULL; #ifndef __riscos__ - if( *first_part == '~' && first_part[1] == DIRSEP_C - && (home = getenv("HOME")) && *home ) - n += strlen(home); + if(*first_part=='~') + { + home=untilde(&first_part); + if(home) + n+=strlen(home); + } #endif name = xmalloc(n); - p = home ? stpcpy(stpcpy(name,home), first_part+1) + p = home ? stpcpy(stpcpy(name,home), first_part) : stpcpy(name, first_part); va_start( arg_ptr, first_part ) ; while( (s=va_arg(arg_ptr, const char *)) ) p = stpcpy(stpcpy(p, DIRSEP_S), s); va_end(arg_ptr); + xfree(home); #ifndef __riscos__ return name; -- cgit v1.2.3