[svn] GnuPG - r3877 - in trunk: . util

svn author dshaw cvs at cvs.gnupg.org
Wed Aug 31 17:36:53 CEST 2005


Author: dshaw
Date: 2005-08-31 17:36:50 +0200 (Wed, 31 Aug 2005)
New Revision: 3877

Modified:
   trunk/ChangeLog
   trunk/configure.ac
   trunk/util/ChangeLog
   trunk/util/fileutil.c
Log:
* 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.


Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2005-08-27 03:09:40 UTC (rev 3876)
+++ trunk/ChangeLog	2005-08-31 15:36:50 UTC (rev 3877)
@@ -1,3 +1,7 @@
+2005-08-31  David Shaw  <dshaw at jabberwocky.com>
+
+	* configure.ac: Check for getpwnam, getpwuid, and pwd.h.
+
 2005-08-09  David Shaw  <dshaw at jabberwocky.com>
 
 	* configure.ac: Remove hardcoded -I and -L for /usr/local on

Modified: trunk/configure.ac
===================================================================
--- trunk/configure.ac	2005-08-27 03:09:40 UTC (rev 3876)
+++ trunk/configure.ac	2005-08-31 15:36:50 UTC (rev 3877)
@@ -765,7 +765,7 @@
 
 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_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)

Modified: trunk/util/ChangeLog
===================================================================
--- trunk/util/ChangeLog	2005-08-27 03:09:40 UTC (rev 3876)
+++ trunk/util/ChangeLog	2005-08-31 15:36:50 UTC (rev 3877)
@@ -1,3 +1,10 @@
+2005-08-31  David Shaw  <dshaw at jabberwocky.com>
+
+	* 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  <wk at g10code.com>
 
 	* pka.c: New.

Modified: trunk/util/fileutil.c
===================================================================
--- trunk/util/fileutil.c	2005-08-27 03:09:40 UTC (rev 3876)
+++ trunk/util/fileutil.c	2005-08-31 15:36:50 UTC (rev 3877)
@@ -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 <string.h>
 #include <assert.h>
 #include <unistd.h>
+#include <sys/types.h>
+#ifdef HAVE_PWD_H
+#include <pwd.h>
+#endif
 #include "util.h"
 #include "memory.h"
 #include "ttyio.h"
@@ -88,8 +92,63 @@
     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
   expansion is done here.  Note that FIRST_PART must never be NULL and
@@ -100,7 +159,7 @@
     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 @@
 	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;




More information about the Gnupg-commits mailing list