diff --git a/common/stringhelp.c b/common/stringhelp.c
index c5e64a06c..dc5911ca6 100644
--- a/common/stringhelp.c
+++ b/common/stringhelp.c
@@ -1,1690 +1,1794 @@
 /* stringhelp.c -  standard string helper functions
  * Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007,
  *               2008, 2009, 2010  Free Software Foundation, Inc.
  * Copyright (C) 2014 Werner Koch
  * Copyright (C) 2015, 2021  g10 Code GmbH
  *
  * This file is part of GnuPG.
  *
  * GnuPG is free software; you can redistribute and/or modify this
  * part of GnuPG under the terms of either
  *
  *   - the GNU Lesser General Public License as published by the Free
  *     Software Foundation; either version 3 of the License, or (at
  *     your option) any later version.
  *
  * or
  *
  *   - the GNU General Public License as published by the Free
  *     Software Foundation; either version 2 of the License, or (at
  *     your option) any later version.
  *
  * or both in parallel, as here.
  *
  * GnuPG is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * General Public License for more details.
  *
  * You should have received a copies of the GNU General Public License
  * and the GNU Lesser General Public License along with this program;
  * if not, see <https://www.gnu.org/licenses/>.
  * SPDX-License-Identifier: (LGPL-3.0-or-later OR GPL-2.0-or-later)
  */
 
 #include <config.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
 #include <ctype.h>
 #include <errno.h>
 #ifdef HAVE_PWD_H
 # include <pwd.h>
 #endif
 #include <unistd.h>
 #include <sys/types.h>
 #ifdef HAVE_W32_SYSTEM
 # ifdef HAVE_WINSOCK2_H
 #  include <winsock2.h>
 # endif
 # include <windows.h>
 #endif
 #include <limits.h>
 
 #include "util.h"
 #include "common-defs.h"
 #include "utf8conv.h"
 #include "sysutils.h"
 #include "stringhelp.h"
 
 #define tohex_lower(n) ((n) < 10 ? ((n) + '0') : (((n) - 10) + 'a'))
 
 
 /* Sometimes we want to avoid mixing slashes and backslashes on W32
    and prefer backslashes.  There is usual no problem with mixing
    them, however a very few W32 API calls can't grok plain slashes.
    Printing filenames with mixed slashes also looks a bit strange.
    This function has no effext on POSIX. */
 static inline char *
 change_slashes (char *name)
 {
 #ifdef HAVE_DOSISH_SYSTEM
   char *p;
 
   if (strchr (name, '\\'))
     {
       for (p=name; *p; p++)
         if (*p == '/')
           *p = '\\';
     }
 #endif /*HAVE_DOSISH_SYSTEM*/
   return name;
 }
 
 
 /*
  * Check whether STRING starts with KEYWORD.  The keyword is
  * delimited by end of string, a space or a tab.  Returns NULL if not
  * found or a pointer into STRING to the next non-space character
  * after the KEYWORD (which may be end of string).
  */
 char *
 has_leading_keyword (const char *string, const char *keyword)
 {
   size_t n = strlen (keyword);
 
   if (!strncmp (string, keyword, n)
       && (!string[n] || string[n] == ' ' || string[n] == '\t'))
     {
       string += n;
       while (*string == ' ' || *string == '\t')
         string++;
       return (char*)string;
     }
   return NULL;
 }
 
 
 /*
  * Look for the substring SUB in buffer and return a pointer to that
  * substring in BUFFER or NULL if not found.
  * Comparison is case-insensitive.
  */
 const char *
 memistr (const void *buffer, size_t buflen, const char *sub)
 {
   const unsigned char *buf = buffer;
   const unsigned char *t = (const unsigned char *)buffer;
   const unsigned char *s = (const unsigned char *)sub;
   size_t n = buflen;
 
   for ( ; n ; t++, n-- )
     {
       if ( toupper (*t) == toupper (*s) )
         {
           for ( buf=t++, buflen = n--, s++;
                 n && toupper (*t) == toupper (*s); t++, s++, n-- )
             ;
           if (!*s)
             return (const char*)buf;
           t = buf;
           s = (const unsigned char *)sub ;
           n = buflen;
 	}
     }
   return NULL;
 }
 
 const char *
 ascii_memistr ( const void *buffer, size_t buflen, const char *sub )
 {
   const unsigned char *buf = buffer;
   const unsigned char *t = (const unsigned char *)buf;
   const unsigned char *s = (const unsigned char *)sub;
   size_t n = buflen;
 
   for ( ; n ; t++, n-- )
     {
       if (ascii_toupper (*t) == ascii_toupper (*s) )
         {
           for ( buf=t++, buflen = n--, s++;
                 n && ascii_toupper (*t) == ascii_toupper (*s); t++, s++, n-- )
             ;
           if (!*s)
             return (const char*)buf;
           t = (const unsigned char *)buf;
           s = (const unsigned char *)sub ;
           n = buflen;
 	}
     }
   return NULL;
 }
 
 /* This function is similar to strncpy().  However it won't copy more
    than N - 1 characters and makes sure that a '\0' is appended. With
    N given as 0, nothing will happen.  With DEST given as NULL, memory
    will be allocated using xmalloc (i.e. if it runs out of core
    the function terminates).  Returns DES or a pointer to the
    allocated memory.
  */
 char *
 mem2str( char *dest , const void *src , size_t n )
 {
     char *d;
     const char *s;
 
     if( n ) {
 	if( !dest )
 	    dest = xmalloc( n ) ;
 	d = dest;
 	s = src ;
 	for(n--; n && *s; n-- )
 	    *d++ = *s++;
 	*d = '\0' ;
     }
 
     return dest ;
 }
 
 
 /****************
  * remove leading and trailing white spaces
  */
 char *
 trim_spaces( char *str )
 {
     char *string, *p, *mark;
 
     string = str;
     /* find first non space character */
     for( p=string; *p && isspace( *(byte*)p ) ; p++ )
 	;
     /* move characters */
     for( (mark = NULL); (*string = *p); string++, p++ )
 	if( isspace( *(byte*)p ) ) {
 	    if( !mark )
 		mark = string ;
 	}
 	else
 	    mark = NULL ;
     if( mark )
 	*mark = '\0' ;  /* remove trailing spaces */
 
     return str ;
 }
 
 
 /* Same as trim_spaces but only consider, space, tab, cr and lf as space.  */
 char *
 ascii_trim_spaces (char *str)
 {
   char *string, *p, *mark;
 
   string = str;
 
   /* Find first non-ascii space character.  */
   for (p=string; *p && ascii_isspace (*p); p++)
     ;
   /* Move characters.  */
   for (mark=NULL; (*string = *p); string++, p++ )
     {
       if (ascii_isspace (*p))
         {
           if (!mark)
             mark = string;
         }
       else
         mark = NULL ;
     }
   if (mark)
     *mark = '\0' ;  /* Remove trailing spaces. */
 
   return str ;
 }
 
 
 /****************
  * remove trailing white spaces
  */
 char *
 trim_trailing_spaces( char *string )
 {
     char *p, *mark;
 
     for( mark = NULL, p = string; *p; p++ ) {
 	if( isspace( *(byte*)p ) ) {
 	    if( !mark )
 		mark = p;
 	}
 	else
 	    mark = NULL;
     }
     if( mark )
 	*mark = '\0' ;
 
     return string ;
 }
 
 
 unsigned
 trim_trailing_chars( byte *line, unsigned len, const char *trimchars )
 {
     byte *p, *mark;
     unsigned n;
 
     for(mark=NULL, p=line, n=0; n < len; n++, p++ ) {
 	if( strchr(trimchars, *p ) ) {
 	    if( !mark )
 		mark = p;
 	}
 	else
 	    mark = NULL;
     }
 
     if( mark ) {
 	*mark = 0;
 	return mark - line;
     }
     return len;
 }
 
 /****************
  * remove trailing white spaces and return the length of the buffer
  */
 unsigned
 trim_trailing_ws( byte *line, unsigned len )
 {
     return trim_trailing_chars( line, len, " \t\r\n" );
 }
 
 size_t
 length_sans_trailing_chars (const unsigned char *line, size_t len,
                             const char *trimchars )
 {
   const unsigned char *p, *mark;
   size_t n;
 
   for( mark=NULL, p=line, n=0; n < len; n++, p++ )
     {
       if (strchr (trimchars, *p ))
         {
           if( !mark )
             mark = p;
         }
       else
         mark = NULL;
     }
 
   if (mark)
     return mark - line;
   return len;
 }
 
 /*
  *  Return the length of line ignoring trailing white-space.
  */
 size_t
 length_sans_trailing_ws (const unsigned char *line, size_t len)
 {
   return length_sans_trailing_chars (line, len, " \t\r\n");
 }
 
 
 
 /*
  * Extract from a given path the filename component.  This function
  * terminates the process on memory shortage.
  */
 char *
 make_basename(const char *filepath, const char *inputpath)
 {
 #ifdef __riscos__
     return riscos_make_basename(filepath, inputpath);
 #else
     char *p;
 
     (void)inputpath; /* Only required for riscos.  */
 
     if ( !(p=strrchr(filepath, '/')) )
 #ifdef HAVE_DOSISH_SYSTEM
 	if ( !(p=strrchr(filepath, '\\')) )
 #endif
 #ifdef HAVE_DRIVE_LETTERS
 	    if ( !(p=strrchr(filepath, ':')) )
 #endif
 	      {
 		return xstrdup(filepath);
 	      }
 
     return xstrdup(p+1);
 #endif
 }
 
 
 
 /*
  * Extract from a given filename the path prepended to it.  If there
  * isn't a path prepended to the filename, a dot is returned ('.').
  * This function terminates the process on memory shortage.
  */
 char *
 make_dirname(const char *filepath)
 {
     char *dirname;
     int  dirname_length;
     char *p;
 
     if ( !(p=strrchr(filepath, '/')) )
 #ifdef HAVE_DOSISH_SYSTEM
 	if ( !(p=strrchr(filepath, '\\')) )
 #endif
 #ifdef HAVE_DRIVE_LETTERS
 	    if ( !(p=strrchr(filepath, ':')) )
 #endif
 	      {
 		return xstrdup(".");
 	      }
 
     dirname_length = p-filepath;
     dirname = xmalloc(dirname_length+1);
     strncpy(dirname, filepath, dirname_length);
     dirname[dirname_length] = 0;
 
     return dirname;
 }
 
 
 
 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 = xstrdup (pwd->pw_dir);
       else
         result = xtrystrdup (pwd->pw_dir);
     }
 #else /*!HAVE_PWD_H*/
   /* No support at all.  */
   (void)xmode;
   (void)name;
 #endif /*HAVE_PWD_H*/
   return result;
 }
 
 
 /* xmode 0 := Return NULL on error
          1 := Terminate on error
          2 := Make sure that name is absolute; return NULL on error
          3 := Make sure that name is absolute; terminate on error
  */
 static char *
 do_make_filename (int xmode, const char *first_part, va_list arg_ptr)
 {
   const char *argv[32];
   int argc;
   size_t n;
   int skip = 1;
   char *home_buffer = NULL;
   char *name, *home, *p;
   int want_abs;
 
   want_abs = !!(xmode & 2);
   xmode &= 1;
 
   n = strlen (first_part) + 1;
   argc = 0;
   while ( (argv[argc] = va_arg (arg_ptr, const char *)) )
     {
       n += strlen (argv[argc]) + 1;
       if (argc >= DIM (argv)-1)
         {
           if (xmode)
             BUG ();
           gpg_err_set_errno (EINVAL);
           return NULL;
         }
       argc++;
     }
   n++;
 
   home = NULL;
   if (*first_part == '~')
     {
       if (first_part[1] == '/' || !first_part[1])
         {
           /* This is the "~/" or "~" case.  */
           home = getenv("HOME");
           if (!home)
             home = home_buffer = get_pwdir (xmode, NULL);
           if (home && *home)
             n += strlen (home);
         }
       else
         {
           /* This is the "~username/" or "~username" case.  */
           char *user;
 
           if (xmode)
             user = xstrdup (first_part+1);
           else
             {
               user = xtrystrdup (first_part+1);
               if (!user)
                 return NULL;
             }
           p = strchr (user, '/');
           if (p)
             *p = 0;
           skip = 1 + strlen (user);
 
           home = home_buffer = get_pwdir (xmode, user);
           xfree (user);
           if (home)
             n += strlen (home);
           else
             skip = 1;
         }
     }
 
   if (xmode)
     name = xmalloc (n);
   else
     {
       name = xtrymalloc (n);
       if (!name)
         {
           xfree (home_buffer);
           return NULL;
         }
     }
 
   if (home)
     p = stpcpy (stpcpy (name, home), first_part + skip);
   else
     p = stpcpy (name, first_part);
 
   xfree (home_buffer);
   for (argc=0; argv[argc]; argc++)
     {
       /* Avoid a leading double slash if the first part was "/".  */
       if (!argc && name[0] == '/' && !name[1])
         p = stpcpy (p, argv[argc]);
       else
         p = stpcpy (stpcpy (p, "/"), argv[argc]);
     }
 
   if (want_abs)
     {
 #ifdef HAVE_DRIVE_LETTERS
       p = strchr (name, ':');
       if (p)
         p++;
       else
         p = name;
 #else
       p = name;
 #endif
       if (*p != '/'
 #ifdef HAVE_DRIVE_LETTERS
           && *p != '\\'
 #endif
           )
         {
           home = gnupg_getcwd ();
           if (!home)
             {
               if (xmode)
                 {
                   fprintf (stderr, "\nfatal: getcwd failed: %s\n",
                            strerror (errno));
                   exit(2);
                 }
               xfree (name);
               return NULL;
             }
           n = strlen (home) + 1 + strlen (name) + 1;
           if (xmode)
             home_buffer = xmalloc (n);
           else
             {
               home_buffer = xtrymalloc (n);
               if (!home_buffer)
                 {
                   xfree (home);
                   xfree (name);
                   return NULL;
                 }
             }
           if (p == name)
             p = home_buffer;
           else /* Windows case.  */
             {
               memcpy (home_buffer, p, p - name + 1);
               p = home_buffer + (p - name + 1);
             }
 
           /* Avoid a leading double slash if the cwd is "/".  */
           if (home[0] == '/' && !home[1])
             strcpy (stpcpy (p, "/"), name);
           else
             strcpy (stpcpy (stpcpy (p, home), "/"), name);
 
           xfree (home);
           xfree (name);
           name = home_buffer;
           /* Let's do a simple compression to catch the most common
              case of using "." for gpg's --homedir option.  */
           n = strlen (name);
           if (n > 2 && name[n-2] == '/' && name[n-1] == '.')
             name[n-2] = 0;
         }
     }
   return change_slashes (name);
 }
 
 /* Construct a filename from the NULL terminated list of parts.  Tilde
    expansion is done for the first argument.  This function terminates
    the process on memory shortage. */
 char *
 make_filename (const char *first_part, ... )
 {
   va_list arg_ptr;
   char *result;
 
   va_start (arg_ptr, first_part);
   result = do_make_filename (1, first_part, arg_ptr);
   va_end (arg_ptr);
   return result;
 }
 
 /* Construct a filename from the NULL terminated list of parts.  Tilde
    expansion is done for the first argument.  This function may return
    NULL on error. */
 char *
 make_filename_try (const char *first_part, ... )
 {
   va_list arg_ptr;
   char *result;
 
   va_start (arg_ptr, first_part);
   result = do_make_filename (0, first_part, arg_ptr);
   va_end (arg_ptr);
   return result;
 }
 
 /* Construct an absolute filename from the NULL terminated list of
    parts.  Tilde expansion is done for the first argument.  This
    function terminates the process on memory shortage. */
 char *
 make_absfilename (const char *first_part, ... )
 {
   va_list arg_ptr;
   char *result;
 
   va_start (arg_ptr, first_part);
   result = do_make_filename (3, first_part, arg_ptr);
   va_end (arg_ptr);
   return result;
 }
 
 /* Construct an absolute filename from the NULL terminated list of
    parts.  Tilde expansion is done for the first argument.  This
    function may return NULL on error. */
 char *
 make_absfilename_try (const char *first_part, ... )
 {
   va_list arg_ptr;
   char *result;
 
   va_start (arg_ptr, first_part);
   result = do_make_filename (2, first_part, arg_ptr);
   va_end (arg_ptr);
   return result;
 }
 
 
 
 /* Compare whether the filenames are identical.  This is a
    special version of strcmp() taking the semantics of filenames in
    account.  Note that this function works only on the supplied names
    without considering any context like the current directory.  See
    also same_file_p(). */
 int
 compare_filenames (const char *a, const char *b)
 {
 #ifdef HAVE_DOSISH_SYSTEM
   for ( ; *a && *b; a++, b++ )
     {
       if (*a != *b
           && (toupper (*(const unsigned char*)a)
               != toupper (*(const unsigned char*)b) )
           && !((*a == '/' && *b == '\\') || (*a == '\\' && *b == '/')))
         break;
     }
   if ((*a == '/' && *b == '\\') || (*a == '\\' && *b == '/'))
     return 0;
   else
     return (toupper (*(const unsigned char*)a)
             - toupper (*(const unsigned char*)b));
 #else
     return strcmp(a,b);
 #endif
 }
 
 
 /* Convert a base-10 number in STRING into a 64 bit unsigned int
  * value.  Leading white spaces are skipped but no error checking is
  * done.  Thus it is similar to atoi(). */
 uint64_t
 string_to_u64 (const char *string)
 {
   uint64_t val = 0;
 
   while (spacep (string))
     string++;
   for (; digitp (string); string++)
     {
       val *= 10;
       val += *string - '0';
     }
   return val;
 }
 
 
 /* Convert 2 hex characters at S to a byte value.  Return this value
    or -1 if there is an error. */
 int
 hextobyte (const char *s)
 {
   int c;
 
   if ( *s >= '0' && *s <= '9' )
     c = 16 * (*s - '0');
   else if ( *s >= 'A' && *s <= 'F' )
     c = 16 * (10 + *s - 'A');
   else if ( *s >= 'a' && *s <= 'f' )
     c = 16 * (10 + *s - 'a');
   else
     return -1;
   s++;
   if ( *s >= '0' && *s <= '9' )
     c += *s - '0';
   else if ( *s >= 'A' && *s <= 'F' )
     c += 10 + *s - 'A';
   else if ( *s >= 'a' && *s <= 'f' )
     c += 10 + *s - 'a';
   else
     return -1;
   return c;
 }
 
 /* Given a string containing an UTF-8 encoded text, return the number
    of characters in this string.  It differs from strlen in that it
    only counts complete UTF-8 characters.  SIZE is the maximum length
    of the string in bytes.  If SIZE is -1, then a NUL character is
    taken to be the end of the string.  Note, that this function does
    not take combined characters into account.  */
 size_t
 utf8_charcount (const char *s, int len)
 {
   size_t n;
 
   if (len == 0)
     return 0;
 
   for (n=0; *s; s++)
     {
       if ( (*s&0xc0) != 0x80 ) /* Exclude continuation bytes: 10xxxxxx */
         n++;
 
       if (len != -1)
         {
           len --;
           if (len == 0)
             break;
         }
     }
 
   return n;
 }
 
 
 /****************************************************
  **********  W32 specific functions  ****************
  ****************************************************/
 
 #ifdef HAVE_W32_SYSTEM
 const char *
 w32_strerror (int ec)
 {
   static char strerr[256];
 
   if (ec == -1)
     ec = (int)GetLastError ();
 #ifdef HAVE_W32CE_SYSTEM
   /* There is only a wchar_t FormatMessage.  It does not make much
      sense to play the conversion game; we print only the code.  */
   snprintf (strerr, sizeof strerr, "ec=%d", (int)GetLastError ());
 #else
   FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, ec,
                  MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
                  strerr, DIM (strerr)-1, NULL);
   {
     /* Strip the CR,LF - we want just the string.  */
     size_t n = strlen (strerr);
     if (n > 2 && strerr[n-2] == '\r' && strerr[n-1] == '\n' )
       strerr[n-2] = 0;
   }
 #endif
   return strerr;
 }
 #endif /*HAVE_W32_SYSTEM*/
 
 
 /****************************************************
  ******** Locale insensitive ctype functions ********
  ****************************************************/
 /* FIXME: replace them by a table lookup and macros */
 int
 ascii_isupper (int c)
 {
     return c >= 'A' && c <= 'Z';
 }
 
 int
 ascii_islower (int c)
 {
     return c >= 'a' && c <= 'z';
 }
 
 int
 ascii_toupper (int c)
 {
     if (c >= 'a' && c <= 'z')
         c &= ~0x20;
     return c;
 }
 
 int
 ascii_tolower (int c)
 {
     if (c >= 'A' && c <= 'Z')
         c |= 0x20;
     return c;
 }
 
 /* Lowercase all ASCII characters in S.  */
 char *
 ascii_strlwr (char *s)
 {
   char *p = s;
 
   for (p=s; *p; p++ )
     if (isascii (*p) && *p >= 'A' && *p <= 'Z')
       *p |= 0x20;
 
   return s;
 }
 
 /* Upcase all ASCII characters in S.  */
 char *
 ascii_strupr (char *s)
 {
   char *p = s;
 
   for (p=s; *p; p++ )
     if (isascii (*p) && *p >= 'a' && *p <= 'z')
       *p &= ~0x20;
 
   return s;
 }
 
 int
 ascii_strcasecmp( const char *a, const char *b )
 {
     if (a == b)
         return 0;
 
     for (; *a && *b; a++, b++) {
 	if (*a != *b && ascii_toupper(*a) != ascii_toupper(*b))
 	    break;
     }
     return *a == *b? 0 : (ascii_toupper (*a) - ascii_toupper (*b));
 }
 
 int
 ascii_strncasecmp (const char *a, const char *b, size_t n)
 {
   const unsigned char *p1 = (const unsigned char *)a;
   const unsigned char *p2 = (const unsigned char *)b;
   unsigned char c1, c2;
 
   if (p1 == p2 || !n )
     return 0;
 
   do
     {
       c1 = ascii_tolower (*p1);
       c2 = ascii_tolower (*p2);
 
       if ( !--n || c1 == '\0')
 	break;
 
       ++p1;
       ++p2;
     }
   while (c1 == c2);
 
   return c1 - c2;
 }
 
 
 int
 ascii_memcasecmp (const void *a_arg, const void *b_arg, size_t n )
 {
   const char *a = a_arg;
   const char *b = b_arg;
 
   if (a == b)
     return 0;
   for ( ; n; n--, a++, b++ )
     {
       if( *a != *b  && ascii_toupper (*a) != ascii_toupper (*b) )
         return *a == *b? 0 : (ascii_toupper (*a) - ascii_toupper (*b));
     }
   return 0;
 }
 
 int
 ascii_strcmp( const char *a, const char *b )
 {
     if (a == b)
         return 0;
 
     for (; *a && *b; a++, b++) {
 	if (*a != *b )
 	    break;
     }
     return *a == *b? 0 : (*(signed char *)a - *(signed char *)b);
 }
 
 
 void *
 ascii_memcasemem (const void *haystack, size_t nhaystack,
                   const void *needle, size_t nneedle)
 {
 
   if (!nneedle)
     return (void*)haystack; /* finding an empty needle is really easy */
   if (nneedle <= nhaystack)
     {
       const char *a = haystack;
       const char *b = a + nhaystack - nneedle;
 
       for (; a <= b; a++)
         {
           if ( !ascii_memcasecmp (a, needle, nneedle) )
             return (void *)a;
         }
     }
   return NULL;
 }
 
 /*********************************************
  ********** missing string functions *********
  *********************************************/
 
 #ifndef HAVE_STPCPY
 char *
 stpcpy(char *a,const char *b)
 {
     while( *b )
 	*a++ = *b++;
     *a = 0;
 
     return (char*)a;
 }
 #endif
 
 #ifndef HAVE_STRPBRK
 /* Find the first occurrence in S of any character in ACCEPT.
    Code taken from glibc-2.6/string/strpbrk.c (LGPLv2.1+) and modified. */
 char *
 strpbrk (const char *s, const char *accept)
 {
   while (*s != '\0')
     {
       const char *a = accept;
       while (*a != '\0')
 	if (*a++ == *s)
 	  return (char *) s;
       ++s;
     }
 
   return NULL;
 }
 #endif /*!HAVE_STRPBRK*/
 
 
 #ifndef HAVE_STRSEP
 /* Code taken from glibc-2.2.1/sysdeps/generic/strsep.c. */
 char *
 strsep (char **stringp, const char *delim)
 {
   char *begin, *end;
 
   begin = *stringp;
   if (begin == NULL)
     return NULL;
 
   /* A frequent case is when the delimiter string contains only one
      character.  Here we don't need to call the expensive 'strpbrk'
      function and instead work using 'strchr'.  */
   if (delim[0] == '\0' || delim[1] == '\0')
     {
       char ch = delim[0];
 
       if (ch == '\0')
         end = NULL;
       else
         {
           if (*begin == ch)
             end = begin;
           else if (*begin == '\0')
             end = NULL;
           else
             end = strchr (begin + 1, ch);
         }
     }
   else
     /* Find the end of the token.  */
     end = strpbrk (begin, delim);
 
   if (end)
     {
       /* Terminate the token and set *STRINGP past NUL character.  */
       *end++ = '\0';
       *stringp = end;
     }
   else
     /* No more delimiters; this is the last token.  */
     *stringp = NULL;
 
   return begin;
 }
 #endif /*HAVE_STRSEP*/
 
 
 #ifndef HAVE_STRLWR
 char *
 strlwr(char *s)
 {
     char *p;
     for(p=s; *p; p++ )
 	*p = tolower(*p);
     return s;
 }
 #endif
 
 
 #ifndef HAVE_STRCASECMP
 int
 strcasecmp( const char *a, const char *b )
 {
     for( ; *a && *b; a++, b++ ) {
 	if( *a != *b && toupper(*a) != toupper(*b) )
 	    break;
     }
     return *(const byte*)a - *(const byte*)b;
 }
 #endif
 
 
 /****************
  * mingw32/cpd has a memicmp()
  */
 #ifndef HAVE_MEMICMP
 int
 memicmp( const char *a, const char *b, size_t n )
 {
     for( ; n; n--, a++, b++ )
 	if( *a != *b  && toupper(*(const byte*)a) != toupper(*(const byte*)b) )
 	    return *(const byte *)a - *(const byte*)b;
     return 0;
 }
 #endif
 
 
 #ifndef HAVE_MEMRCHR
 void *
 memrchr (const void *buffer, int c, size_t n)
 {
   const unsigned char *p = buffer;
 
   for (p += n; n ; n--)
     if (*--p == c)
       return (void *)p;
   return NULL;
 }
 #endif /*HAVE_MEMRCHR*/
 
 
 /* Percent-escape the string STR by replacing colons with '%3a'.  If
    EXTRA is not NULL all characters in EXTRA are also escaped.  */
 static char *
 do_percent_escape (const char *str, const char *extra, int die)
 {
   int i, j;
   char *ptr;
 
   if (!str)
     return NULL;
 
   for (i=j=0; str[i]; i++)
     if (str[i] == ':' || str[i] == '%' || str[i] == '\n'
         || (extra && strchr (extra, str[i])))
       j++;
   if (die)
     ptr = xmalloc (i + 2 * j + 1);
   else
     {
       ptr = xtrymalloc (i + 2 * j + 1);
       if (!ptr)
         return NULL;
     }
   i = 0;
   while (*str)
     {
       if (*str == ':')
 	{
 	  ptr[i++] = '%';
 	  ptr[i++] = '3';
 	  ptr[i++] = 'a';
 	}
       else if (*str == '%')
 	{
 	  ptr[i++] = '%';
 	  ptr[i++] = '2';
 	  ptr[i++] = '5';
 	}
       else if (*str == '\n')
 	{
 	  /* The newline is problematic in a line-based format.  */
 	  ptr[i++] = '%';
 	  ptr[i++] = '0';
 	  ptr[i++] = 'a';
 	}
       else if (extra && strchr (extra, *str))
         {
 	  ptr[i++] = '%';
           ptr[i++] = tohex_lower ((*str>>4)&15);
           ptr[i++] = tohex_lower (*str&15);
         }
       else
 	ptr[i++] = *str;
       str++;
     }
   ptr[i] = '\0';
 
   return ptr;
 }
 
 /* Percent-escape the string STR by replacing colons with '%3a'.  If
    EXTRA is not NULL all characters in EXTRA are also escaped.  This
    function terminates the process on memory shortage.  */
 char *
 percent_escape (const char *str, const char *extra)
 {
   return do_percent_escape (str, extra, 1);
 }
 
 /* Same as percent_escape but return NULL instead of exiting on memory
    error. */
 char *
 try_percent_escape (const char *str, const char *extra)
 {
   return do_percent_escape (str, extra, 0);
 }
 
 
 
 static char *
 do_strconcat (const char *s1, va_list arg_ptr)
 {
   const char *argv[48];
   size_t argc;
   size_t needed;
   char *buffer, *p;
 
   argc = 0;
   argv[argc++] = s1;
   needed = strlen (s1);
   while (((argv[argc] = va_arg (arg_ptr, const char *))))
     {
       needed += strlen (argv[argc]);
       if (argc >= DIM (argv)-1)
         {
           gpg_err_set_errno (EINVAL);
           return NULL;
         }
       argc++;
     }
   needed++;
   buffer = xtrymalloc (needed);
   if (buffer)
     {
       for (p = buffer, argc=0; argv[argc]; argc++)
         p = stpcpy (p, argv[argc]);
     }
   return buffer;
 }
 
 
 /* Concatenate the string S1 with all the following strings up to a
    NULL.  Returns a malloced buffer with the new string or NULL on a
    malloc error or if too many arguments are given.  */
 char *
 strconcat (const char *s1, ...)
 {
   va_list arg_ptr;
   char *result;
 
   if (!s1)
     result = xtrystrdup ("");
   else
     {
       va_start (arg_ptr, s1);
       result = do_strconcat (s1, arg_ptr);
       va_end (arg_ptr);
     }
   return result;
 }
 
 /* Same as strconcat but terminate the process with an error message
    if something goes wrong.  */
 char *
 xstrconcat (const char *s1, ...)
 {
   va_list arg_ptr;
   char *result;
 
   if (!s1)
     result = xstrdup ("");
   else
     {
       va_start (arg_ptr, s1);
       result = do_strconcat (s1, arg_ptr);
       va_end (arg_ptr);
     }
   if (!result)
     {
       if (errno == EINVAL)
         fputs ("\nfatal: too many args for xstrconcat\n", stderr);
       else
         fputs ("\nfatal: out of memory\n", stderr);
       exit (2);
     }
   return result;
 }
 
 /* Split a string into fields at DELIM.  REPLACEMENT is the character
    to replace the delimiter with (normally: '\0' so that each field is
    NUL terminated).  The caller is responsible for freeing the result.
    Note: this function modifies STRING!  If you need the original
    value, then you should pass a copy to this function.
 
    If malloc fails, this function returns NULL.  */
 char **
 strsplit (char *string, char delim, char replacement, int *count)
 {
   int fields = 1;
   char *t;
   char **result;
 
   /* First, count the number of fields.  */
   for (t = strchr (string, delim); t; t = strchr (t + 1, delim))
     fields ++;
 
   result = xtrycalloc ((fields + 1), sizeof (*result));
   if (! result)
     return NULL;
 
   result[0] = string;
   fields = 1;
   for (t = strchr (string, delim); t; t = strchr (t + 1, delim))
     {
       result[fields ++] = t + 1;
       *t = replacement;
     }
 
   if (count)
     *count = fields;
 
   return result;
 }
 
 
 /* Tokenize STRING using the set of delimiters in DELIM.  Leading
  * spaces and tabs are removed from all tokens.  The caller must xfree
  * the result.
  *
  * Returns: A malloced and NULL delimited array with the tokens.  On
  *          memory error NULL is returned and ERRNO is set.
  */
 static char **
 do_strtokenize (const char *string, const char *delim, int trim)
 {
   const char *s;
   size_t fields;
   size_t bytes, n;
   char *buffer;
   char *p, *px, *pend;
   char **result;
 
   /* Count the number of fields.  */
   for (fields = 1, s = strpbrk (string, delim); s; s = strpbrk (s + 1, delim))
     fields++;
   fields++; /* Add one for the terminating NULL.  */
 
   /* Allocate an array for all fields, a terminating NULL, and space
      for a copy of the string.  */
   bytes = fields * sizeof *result;
   if (bytes / sizeof *result != fields)
     {
       gpg_err_set_errno (ENOMEM);
       return NULL;
     }
   n = strlen (string) + 1;
   bytes += n;
   if (bytes < n)
     {
       gpg_err_set_errno (ENOMEM);
       return NULL;
     }
   result = xtrymalloc (bytes);
   if (!result)
     return NULL;
   buffer = (char*)(result + fields);
 
   /* Copy and parse the string.  */
   strcpy (buffer, string);
   for (n = 0, p = buffer; (pend = strpbrk (p, delim)); p = pend + 1)
     {
       *pend = 0;
       if (trim)
         {
           while (spacep (p))
             p++;
           for (px = pend - 1; px >= p && spacep (px); px--)
             *px = 0;
         }
       result[n++] = p;
     }
   if (trim)
     {
       while (spacep (p))
         p++;
       for (px = p + strlen (p) - 1; px >= p && spacep (px); px--)
         *px = 0;
     }
   result[n++] = p;
   result[n] = NULL;
 
   log_assert ((char*)(result + n + 1) == buffer);
 
   return result;
 }
 
 /* Tokenize STRING using the set of delimiters in DELIM.  Leading
  * spaces and tabs are removed from all tokens.  The caller must xfree
  * the result.
  *
  * Returns: A malloced and NULL delimited array with the tokens.  On
  *          memory error NULL is returned and ERRNO is set.
  */
 char **
 strtokenize (const char *string, const char *delim)
 {
   return do_strtokenize (string, delim, 1);
 }
 
 /* Same as strtokenize but does not trim leading and trailing spaces
  * from the fields.  */
 char **
 strtokenize_nt (const char *string, const char *delim)
 {
   return do_strtokenize (string, delim, 0);
 }
 
 
 /* Split a string into space delimited fields and remove leading and
  * trailing spaces from each field.  A pointer to each field is stored
  * in ARRAY.  Stop splitting at ARRAYSIZE fields.  The function
  * modifies STRING.  The number of parsed fields is returned.
  * Example:
  *
  *   char *fields[2];
  *   if (split_fields (string, fields, DIM (fields)) < 2)
  *     return  // Not enough args.
  *   foo (fields[0]);
  *   foo (fields[1]);
  */
 int
 split_fields (char *string, char **array, int arraysize)
 {
   int n = 0;
   char *p, *pend;
 
   for (p = string; *p == ' '; p++)
     ;
   do
     {
       if (n == arraysize)
         break;
       array[n++] = p;
       pend = strchr (p, ' ');
       if (!pend)
         break;
       *pend++ = 0;
       for (p = pend; *p == ' '; p++)
         ;
     }
   while (*p);
 
   return n;
 }
 
 
 /* Split a string into colon delimited fields A pointer to each field
  * is stored in ARRAY.  Stop splitting at ARRAYSIZE fields.  The
  * function modifies STRING.  The number of parsed fields is returned.
  * Note that leading and trailing spaces are not removed from the fields.
  * Example:
  *
  *   char *fields[2];
  *   if (split_fields (string, fields, DIM (fields)) < 2)
  *     return  // Not enough args.
  *   foo (fields[0]);
  *   foo (fields[1]);
  */
 int
 split_fields_colon (char *string, char **array, int arraysize)
 {
   int n = 0;
   char *p, *pend;
 
   p = string;
   do
     {
       if (n == arraysize)
         break;
       array[n++] = p;
       pend = strchr (p, ':');
       if (!pend)
         break;
       *pend++ = 0;
       p = pend;
     }
   while (*p);
 
   return n;
 }
 
 
 
 /* Version number parsing.  */
 
 /* This function parses the first portion of the version number S and
    stores it in *NUMBER.  On success, this function returns a pointer
    into S starting with the first character, which is not part of the
    initial number portion; on failure, NULL is returned.  */
 static const char*
 parse_version_number (const char *s, int *number)
 {
   int val = 0;
 
   if (*s == '0' && digitp (s+1))
     return NULL;  /* Leading zeros are not allowed.  */
   for (; digitp (s); s++)
     {
       val *= 10;
       val += *s - '0';
     }
   *number = val;
   return val < 0 ? NULL : s;
 }
 
 
 /* This function breaks up the complete string-representation of the
    version number S, which is of the following struture: <major
    number>.<minor number>[.<micro number>]<patch level>.  The major,
    minor, and micro number components will be stored in *MAJOR, *MINOR
    and *MICRO.  If MICRO is not given 0 is used instead.
 
    On success, the last component, the patch level, will be returned;
    in failure, NULL will be returned.  */
 static const char *
 parse_version_string (const char *s, int *major, int *minor, int *micro)
 {
   s = parse_version_number (s, major);
   if (!s || *s != '.')
     return NULL;
   s++;
   s = parse_version_number (s, minor);
   if (!s)
     return NULL;
   if (*s == '.')
     {
       s++;
       s = parse_version_number (s, micro);
       if (!s)
         return NULL;
     }
   else
     *micro = 0;
   return s;  /* Patchlevel.  */
 }
 
 
 /* Compare the version string MY_VERSION to the version string
  * REQ_VERSION.  Returns -1, 0, or 1 if MY_VERSION is found,
  * respectively, to be less than, to match, or be greater than
  * REQ_VERSION.  This function works for three and two part version
  * strings; for a two part version string the micro part is assumed to
  * be 0.  Patch levels are compared as strings.  If a version number
  * is invalid INT_MIN is returned.  If REQ_VERSION is given as NULL
  * the function returns 0 if MY_VERSION is parsable version string. */
 int
 compare_version_strings (const char *my_version, const char *req_version)
 {
   int my_major, my_minor, my_micro;
   int rq_major, rq_minor, rq_micro;
   const char *my_patch, *rq_patch;
   int result;
 
   if (!my_version)
     return INT_MIN;
 
   my_patch = parse_version_string (my_version, &my_major, &my_minor, &my_micro);
   if (!my_patch)
     return INT_MIN;
   if (!req_version)
     return 0; /* MY_VERSION can be parsed.  */
   rq_patch = parse_version_string (req_version, &rq_major, &rq_minor,&rq_micro);
   if (!rq_patch)
     return INT_MIN;
 
   if (my_major == rq_major)
     {
       if (my_minor == rq_minor)
         {
           if (my_micro == rq_micro)
             result = strcmp (my_patch, rq_patch);
           else
             result = my_micro - rq_micro;
         }
       else
         result = my_minor - rq_minor;
     }
   else
     result = my_major - rq_major;
 
   return !result? 0 : result < 0 ? -1 : 1;
 }
 
 
 
 /* Format a string so that it fits within about TARGET_COLS columns.
  * TEXT_IN is copied to a new buffer, which is returned.  Normally,
  * target_cols will be 72 and max_cols is 80.  On error NULL is
  * returned and ERRNO is set. */
 char *
 format_text (const char *text_in, int target_cols, int max_cols)
 {
   /* const int do_debug = 0; */
 
   /* The character under consideration.  */
   char *p;
   /* The start of the current line.  */
   char *line;
   /* The last space that we saw.  */
   char *last_space = NULL;
   int last_space_cols = 0;
   int copied_last_space = 0;
   char *text;
 
   text = xtrystrdup (text_in);
   if (!text)
     return NULL;
 
   p = line = text;
   while (1)
     {
       /* The number of columns including any trailing space.  */
       int cols;
 
       p = p + strcspn (p, "\n ");
       if (! p)
         /* P now points to the NUL character.  */
         p = &text[strlen (text)];
 
       if (*p == '\n')
         /* Pass through any newlines.  */
         {
           p ++;
           line = p;
           last_space = NULL;
           last_space_cols = 0;
           copied_last_space = 1;
           continue;
         }
 
       /* Have a space or a NUL.  Note: we don't count the trailing
          space.  */
       cols = utf8_charcount (line, (uintptr_t) p - (uintptr_t) line);
       if (cols < target_cols)
         {
           if (! *p)
             /* Nothing left to break.  */
             break;
 
           last_space = p;
           last_space_cols = cols;
           p ++;
           /* Skip any immediately following spaces.  If we break:
              "... foo bar ..." between "foo" and "bar" then we want:
              "... foo\nbar ...", which means that the left space has
              to be the first space after foo, not the last space
              before bar.  */
           while (*p == ' ')
             p ++;
         }
       else
         {
           int cols_with_left_space;
           int cols_with_right_space;
           int left_penalty;
           int right_penalty;
 
           cols_with_left_space = last_space_cols;
           cols_with_right_space = cols;
 
           /* if (do_debug) */
           /*   log_debug ("Breaking: '%.*s'\n", */
           /*              (int) ((uintptr_t) p - (uintptr_t) line), line); */
 
           /* The number of columns away from TARGET_COLS.  We prefer
              to underflow than to overflow.  */
           left_penalty = target_cols - cols_with_left_space;
           right_penalty = 2 * (cols_with_right_space - target_cols);
 
           if (cols_with_right_space > max_cols)
             /* Add a large penalty for each column that exceeds
                max_cols.  */
             right_penalty += 4 * (cols_with_right_space - max_cols);
 
           /* if (do_debug) */
           /*   log_debug ("Left space => %d cols (penalty: %d); " */
           /*              "right space => %d cols (penalty: %d)\n", */
           /*              cols_with_left_space, left_penalty, */
           /*              cols_with_right_space, right_penalty); */
           if (last_space_cols && left_penalty <= right_penalty)
             {
               /* Prefer the left space.  */
               /* if (do_debug) */
               /*   log_debug ("Breaking at left space.\n"); */
               p = last_space;
             }
           else
             {
               /* if (do_debug) */
               /*   log_debug ("Breaking at right space.\n"); */
             }
 
           if (! *p)
             break;
 
           *p = '\n';
           p ++;
           if (*p == ' ')
             {
               int spaces;
               for (spaces = 1; p[spaces] == ' '; spaces ++)
                 ;
               memmove (p, &p[spaces], strlen (&p[spaces]) + 1);
             }
           line = p;
           last_space = NULL;
           last_space_cols = 0;
           copied_last_space = 0;
         }
     }
 
   /* Chop off any trailing space.  */
   trim_trailing_chars (text, strlen (text), " ");
   /* If we inserted the trailing newline, then remove it.  */
   if (! copied_last_space && *text && text[strlen (text) - 1] == '\n')
     text[strlen (text) - 1] = '\0';
 
   return text;
 }
+
+
+/* Substitute environment variables in STRING and return a new string.
+ * On error the function returns NULL.  */
+char *
+substitute_envvars (const char *string)
+{
+  char *line, *p, *pend;
+  const char *value;
+  size_t valuelen, n;
+  char *result = NULL;
+
+  result = line = xtrystrdup (string);
+  if (!result)
+    return NULL; /* Ooops */
+
+  while (*line)
+    {
+      p = strchr (line, '$');
+      if (!p)
+        goto leave; /* No or no more variables.  */
+
+      if (p[1] == '$') /* Escaped dollar sign. */
+        {
+          memmove (p, p+1, strlen (p+1)+1);
+          line = p + 1;
+          continue;
+        }
+
+      if (p[1] == '{')
+        {
+          int count = 0;
+
+          for (pend=p+2; *pend; pend++)
+            {
+              if (*pend == '{')
+                count++;
+              else if (*pend == '}')
+                {
+                  if (--count < 0)
+                    break;
+                }
+            }
+          if (!*pend)
+            goto leave; /* Unclosed - don't substitute.  */
+        }
+      else
+        {
+          for (pend = p+1; *pend && (alnump (pend) || *pend == '_'); pend++)
+            ;
+        }
+
+      if (p[1] == '{' && *pend == '}')
+        {
+          int save = *pend;
+          *pend = 0;
+          value = getenv (p+2);
+          *pend++ = save;
+        }
+      else
+        {
+          int save = *pend;
+          *pend = 0;
+          value = getenv (p+1);
+          *pend = save;
+        }
+
+      if (!value)
+        value = "";
+      valuelen = strlen (value);
+      if (valuelen <= pend - p)
+        {
+          memcpy (p, value, valuelen);
+          p += valuelen;
+          n = pend - p;
+          if (n)
+            memmove (p, p+n, strlen (p+n)+1);
+          line = p;
+        }
+      else
+        {
+          char *src = result;
+          char *dst;
+
+          dst = xtrymalloc (strlen (src) + valuelen + 1);
+          if (!dst)
+            {
+              xfree (result);
+              return NULL;
+            }
+          n = p - src;
+          memcpy (dst, src, n);
+          memcpy (dst + n, value, valuelen);
+          n += valuelen;
+          strcpy (dst + n, pend);
+          line = dst + n;
+          xfree (result);
+          result = dst;
+        }
+    }
+
+ leave:
+  return result;
+}
diff --git a/common/stringhelp.h b/common/stringhelp.h
index 70edb6e0d..16c1af397 100644
--- a/common/stringhelp.h
+++ b/common/stringhelp.h
@@ -1,173 +1,176 @@
 /* stringhelp.h
  * Copyright (C) 1998, 1999, 2000, 2001, 2003,
  *               2006, 2007, 2009  Free Software Foundation, Inc.
  *               2015  g10 Code GmbH
  *
  * This file is part of GnuPG.
  *
  * GnuPG is free software; you can redistribute and/or modify this
  * part of GnuPG under the terms of either
  *
  *   - the GNU Lesser General Public License as published by the Free
  *     Software Foundation; either version 3 of the License, or (at
  *     your option) any later version.
  *
  * or
  *
  *   - the GNU General Public License as published by the Free
  *     Software Foundation; either version 2 of the License, or (at
  *     your option) any later version.
  *
  * or both in parallel, as here.
  *
  * GnuPG is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * General Public License for more details.
  *
  * You should have received a copies of the GNU General Public License
  * and the GNU Lesser General Public License along with this program;
  * if not, see <https://www.gnu.org/licenses/>.
  */
 
 #ifndef GNUPG_COMMON_STRINGHELP_H
 #define GNUPG_COMMON_STRINGHELP_H
 
 #include <stdint.h>
 #include "types.h"
 
 /*-- stringhelp.c --*/
 char *has_leading_keyword (const char *string, const char *keyword);
 
 const char *memistr (const void *buf, size_t buflen, const char *sub);
 char *mem2str( char *, const void *, size_t);
 char *trim_spaces( char *string );
 char *ascii_trim_spaces (char *string);
 char *trim_trailing_spaces( char *string );
 unsigned int trim_trailing_chars( unsigned char *line, unsigned len,
 					      const char *trimchars);
 unsigned int trim_trailing_ws( unsigned char *line, unsigned len );
 size_t length_sans_trailing_chars (const unsigned char *line, size_t len,
                                    const char *trimchars );
 size_t length_sans_trailing_ws (const unsigned char *line, size_t len);
 
 
 char *make_basename(const char *filepath, const char *inputpath);
 char *make_dirname(const char *filepath);
 char *make_filename( const char *first_part, ... ) GPGRT_ATTR_SENTINEL(0);
 char *make_filename_try (const char *first_part, ... ) GPGRT_ATTR_SENTINEL(0);
 char *make_absfilename (const char *first_part, ...) GPGRT_ATTR_SENTINEL(0);
 char *make_absfilename_try (const char *first_part,
                             ...) GPGRT_ATTR_SENTINEL(0);
 int compare_filenames( const char *a, const char *b );
 
 uint64_t string_to_u64 (const char *string);
 int hextobyte (const char *s);
 
 size_t utf8_charcount (const char *s, int len);
 
 
 #ifdef HAVE_W32_SYSTEM
 const char *w32_strerror (int ec);
 #endif
 
 
 int ascii_isupper (int c);
 int ascii_islower (int c);
 int ascii_toupper (int c);
 int ascii_tolower (int c);
 char *ascii_strlwr (char *s);
 char *ascii_strupr (char *s);
 int ascii_strcasecmp( const char *a, const char *b );
 int ascii_strncasecmp (const char *a, const char *b, size_t n);
 int ascii_memcasecmp( const void *a, const void *b, size_t n );
 const char *ascii_memistr ( const void *buf, size_t buflen, const char *sub);
 void *ascii_memcasemem (const void *haystack, size_t nhaystack,
                         const void *needle, size_t nneedle);
 
 
 #ifndef HAVE_MEMICMP
 int memicmp( const char *a, const char *b, size_t n );
 #endif
 #ifndef HAVE_STPCPY
 char *stpcpy(char *a,const char *b);
 #endif
 #ifndef HAVE_STRPBRK
 char *strpbrk (const char *s, const char *accept);
 #endif
 #ifndef HAVE_STRSEP
 char *strsep (char **stringp, const char *delim);
 #endif
 #ifndef HAVE_STRLWR
 char *strlwr(char *a);
 #endif
 #ifndef HAVE_STRTOUL
 #  define strtoul(a,b,c)  ((unsigned long)strtol((a),(b),(c)))
 #endif
 #ifndef HAVE_MEMMOVE
 #  define memmove(d, s, n) bcopy((s), (d), (n))
 #endif
 #ifndef HAVE_STRICMP
 #  define stricmp(a,b)	 strcasecmp( (a), (b) )
 #endif
 #ifndef HAVE_MEMRCHR
 void *memrchr (const void *buffer, int c, size_t n);
 #endif
 
 
 #ifndef HAVE_ISASCII
 static inline int
 isascii (int c)
 {
   return (((c) & ~0x7f) == 0);
 }
 #endif /* !HAVE_ISASCII */
 
 
 #ifndef STR
 #  define STR(v) #v
 #endif
 #define STR2(v) STR(v)
 
 /* Percent-escape the string STR by replacing colons with '%3a'.  If
    EXTRA is not NULL, also replace all characters given in EXTRA.  The
    "try_" variant fails with NULL if not enough memory can be
    allocated.  */
 char *percent_escape (const char *str, const char *extra);
 char *try_percent_escape (const char *str, const char *extra);
 
 
 /* Concatenate the string S1 with all the following strings up to a
    NULL.  Returns a malloced buffer with the new string or NULL on a
    malloc error or if too many arguments are given.  */
 char *strconcat (const char *s1, ...) GPGRT_ATTR_SENTINEL(0);
 /* Ditto, but die on error.  */
 char *xstrconcat (const char *s1, ...) GPGRT_ATTR_SENTINEL(0);
 
 char **strsplit (char *string, char delim, char replacement, int *count);
 
 /* Tokenize STRING using the set of delimiters in DELIM.  */
 char **strtokenize (const char *string, const char *delim);
 /* Tokenize STRING using the set of delimiters in DELIM but do not
  * trim the tokens.  */
 char **strtokenize_nt (const char *string, const char *delim);
 
 /* Split STRING into space delimited fields and store them in the
  * provided ARRAY.  */
 int split_fields (char *string, char **array, int arraysize);
 
 /* Split STRING into colon delimited fields and store them in the
  * provided ARRAY.  */
 int split_fields_colon (char *string, char **array, int arraysize);
 
 /* Return True if MYVERSION is greater or equal than REQ_VERSION.  */
 int compare_version_strings (const char *my_version, const char *req_version);
 
 /* Format a string so that it fits within about TARGET_COLS columns.  */
 char *format_text (const char *text, int target_cols, int max_cols);
 
+/* Substitute environmen variabales in STRING.  */
+char *substitute_envvars (const char *string);
+
 
 /*-- mapstrings.c --*/
 const char *map_static_macro_string (const char *string);
 
 #endif /*GNUPG_COMMON_STRINGHELP_H*/
diff --git a/common/t-stringhelp.c b/common/t-stringhelp.c
index 332391689..d76991f91 100644
--- a/common/t-stringhelp.c
+++ b/common/t-stringhelp.c
@@ -1,1220 +1,1319 @@
 /* t-stringhelp.c - Regression tests for stringhelp.c
  * Copyright (C) 2007 Free Software Foundation, Inc.
  *               2015, 2021  g10 Code GmbH
  *
  * This file is part of GnuPG.
  *
  * GnuPG is free software; you can redistribute and/or modify this
  * part of GnuPG under the terms of either
  *
  *   - the GNU Lesser General Public License as published by the Free
  *     Software Foundation; either version 3 of the License, or (at
  *     your option) any later version.
  *
  * or
  *
  *   - the GNU General Public License as published by the Free
  *     Software Foundation; either version 2 of the License, or (at
  *     your option) any later version.
  *
  * or both in parallel, as here.
  *
  * GnuPG is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * General Public License for more details.
  *
  * You should have received a copies of the GNU General Public License
  * and the GNU Lesser General Public License along with this program;
  * if not, see <https://www.gnu.org/licenses/>.
  * SPDX-License-Identifier: (LGPL-3.0-or-later OR GPL-2.0-or-later)
  */
 
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #ifdef HAVE_PWD_H
 # include <pwd.h>
 #endif
 #include <unistd.h>
 #include <sys/types.h>
 #include <limits.h>
 #include <assert.h>
 
 #include "t-support.h"
+#include "sysutils.h"
 #include "stringhelp.h"
 
 
 static char *home_buffer;
 
 
 const char *
 gethome (void)
 {
   if (!home_buffer)
     {
       char *home = getenv("HOME");
 
       if(home)
         home_buffer = xstrdup (home);
 #if defined(HAVE_GETPWUID) && defined(HAVE_PWD_H)
       else
         {
           struct passwd *pwd;
 
           pwd = getpwuid (getuid());
           if (pwd)
             home_buffer = xstrdup (pwd->pw_dir);
         }
 #endif
     }
   return home_buffer;
 }
 
 
 static char *
 mygetcwd (void)
 {
   char *buffer;
   size_t size = 100;
 
   for (;;)
     {
       buffer = xmalloc (size+1);
 #ifdef HAVE_W32CE_SYSTEM
       strcpy (buffer, "/");  /* Always "/".  */
       return buffer;
 #else
       if (getcwd (buffer, size) == buffer)
         return buffer;
       xfree (buffer);
       if (errno != ERANGE)
         {
           fprintf (stderr,"error getting current cwd: %s\n",
                    strerror (errno));
           exit (2);
         }
       size *= 2;
 #endif
     }
 }
 
 
 static void
 test_percent_escape (void)
 {
   char *result;
   static struct {
     const char *extra;
     const char *value;
     const char *expected;
   } tests[] =
     {
       { NULL, "", "" },
       { NULL, "%", "%25" },
       { NULL, "%%", "%25%25" },
       { NULL, " %", " %25" },
       { NULL, ":", "%3a" },
       { NULL, " :", " %3a" },
       { NULL, ": ", "%3a " },
       { NULL, " : ", " %3a " },
       { NULL, "::", "%3a%3a" },
       { NULL, ": :", "%3a %3a" },
       { NULL, "%:", "%25%3a" },
       { NULL, ":%", "%3a%25" },
       { "\\\n:", ":%", "%3a%25" },
       { "\\\n:", "\\:%", "%5c%3a%25" },
       { "\\\n:", "\n:%", "%0a%3a%25" },
       { "\\\n:", "\xff:%", "\xff%3a%25" },
       { "\\\n:", "\xfe:%", "\xfe%3a%25" },
       { "\\\n:", "\x01:%", "\x01%3a%25" },
       { "\x01",  "\x01:%", "%01%3a%25" },
       { "\xfe",  "\xfe:%", "%fe%3a%25" },
       { "\xfe",  "\xff:%", "\xff%3a%25" },
 
       { NULL, NULL, NULL }
     };
   int testno;
 
   result = percent_escape (NULL, NULL);
   if (result)
     fail (0);
   for (testno=0; tests[testno].value; testno++)
     {
       result = percent_escape (tests[testno].value, tests[testno].extra);
       if (!result)
         fail (testno);
       else if (strcmp (result, tests[testno].expected))
         fail (testno);
       xfree (result);
     }
 
 }
 
 
 static void
 test_compare_filenames (void)
 {
   struct {
     const char *a;
     const char *b;
     int result;
   } tests[] = {
     { "", "", 0 },
     { "", "a", -1 },
     { "a", "", 1 },
     { "a", "a", 0 },
     { "a", "aa", -1 },
     { "aa", "a", 1 },
     { "a",  "b", -1  },
 
 #ifdef HAVE_W32_SYSTEM
     { "a", "A", 0 },
     { "A", "a", 0 },
     { "foo/bar", "foo\\bar", 0 },
     { "foo\\bar", "foo/bar", 0 },
     { "foo\\", "foo/", 0 },
     { "foo/", "foo\\", 0 },
 #endif /*HAVE_W32_SYSTEM*/
     { NULL, NULL, 0}
   };
   int testno, result;
 
   for (testno=0; tests[testno].a; testno++)
     {
       result = compare_filenames (tests[testno].a, tests[testno].b);
       result = result < 0? -1 : result > 0? 1 : 0;
       if (result != tests[testno].result)
         fail (testno);
     }
 }
 
 
 static void
 test_strconcat (void)
 {
   char *out;
 
   out = strconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                    "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                    "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                    "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                    "1", "2", "3", "4", "5", "6", "7", NULL);
   if (!out)
     fail (0);
   else
     xfree (out);
   out = strconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                    "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                    "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                    "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                    "1", "2", "3", "4", "5", "6", "7", "8", NULL);
   if (out)
     fail (0);
   else if (errno != EINVAL)
     fail (0);
 
   out = strconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                    "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                    "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                    "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                    "1", "2", "3", "4", "5", "6", "7", "8", "9", NULL);
   if (out)
     fail (0);
   else if (errno != EINVAL)
     fail (0);
   xfree (out);
 
 #if __GNUC__ < 4 /* gcc 4.0 has a sentinel attribute.  */
   out = strconcat (NULL);
   if (!out || *out)
     fail (1);
 #endif
   out = strconcat (NULL, NULL);
   if (!out || *out)
     fail (1);
   xfree (out);
 
   out = strconcat ("", NULL);
   if (!out || *out)
     fail (1);
   xfree (out);
 
   out = strconcat ("", "", NULL);
   if (!out || *out)
     fail (2);
   xfree (out);
 
   out = strconcat ("a", "b", NULL);
   if (!out || strcmp (out, "ab"))
     fail (3);
   xfree (out);
   out = strconcat ("a", "b", "c", NULL);
   if (!out || strcmp (out, "abc"))
     fail (3);
   xfree (out);
 
   out = strconcat ("a", "b", "cc", NULL);
   if (!out || strcmp (out, "abcc"))
     fail (4);
   xfree (out);
   out = strconcat ("a1", "b1", "c1", NULL);
   if (!out || strcmp (out, "a1b1c1"))
     fail (4);
   xfree (out);
 
   out = strconcat ("", " long b ", "", "--even-longer--", NULL);
   if (!out || strcmp (out, " long b --even-longer--"))
     fail (5);
   xfree (out);
 
   out = strconcat ("", " long b ", "", "--even-longer--", NULL);
   if (!out || strcmp (out, " long b --even-longer--"))
     fail (5);
   xfree (out);
 }
 
 static void
 test_xstrconcat (void)
 {
   char *out;
 
   out = xstrconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                    "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                    "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                    "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                    "1", "2", "3", "4", "5", "6", "7", NULL);
   if (!out)
     fail (0);
   xfree (out);
 
 #if __GNUC__ < 4 /* gcc 4.0 has a sentinel attribute.  */
   out = xstrconcat (NULL);
   if (!out)
     fail (1);
 #endif
   out = xstrconcat (NULL, NULL);
   if (!out)
     fail (1);
   xfree (out);
 
   out = xstrconcat ("", NULL);
   if (!out || *out)
     fail (1);
   xfree (out);
 
   out = xstrconcat ("", "", NULL);
   if (!out || *out)
     fail (2);
   xfree (out);
 
   out = xstrconcat ("a", "b", NULL);
   if (!out || strcmp (out, "ab"))
     fail (3);
   xfree (out);
   out = xstrconcat ("a", "b", "c", NULL);
   if (!out || strcmp (out, "abc"))
     fail (3);
   xfree (out);
 
   out = xstrconcat ("a", "b", "cc", NULL);
   if (!out || strcmp (out, "abcc"))
     fail (4);
   xfree (out);
   out = xstrconcat ("a1", "b1", "c1", NULL);
   if (!out || strcmp (out, "a1b1c1"))
     fail (4);
   xfree (out);
 
   out = xstrconcat ("", " long b ", "", "--even-longer--", NULL);
   if (!out || strcmp (out, " long b --even-longer--"))
     fail (5);
   xfree (out);
 
   out = xstrconcat ("", " long b ", "", "--even-longer--", NULL);
   if (!out || strcmp (out, " long b --even-longer--"))
     fail (5);
   xfree (out);
 }
 
 
 static void
 test_make_filename_try (void)
 {
   char *out;
   const char *home = gethome ();
   size_t homelen = home? strlen (home):0;
 
   out = make_filename_try ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                            "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                            "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                            "1", "2", "3", NULL);
   if (out)
     fail (0);
   else if (errno != EINVAL)
     fail (0);
   xfree (out);
   out = make_filename_try ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                            "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                            "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                            "1", "2", "3", "4", NULL);
   if (out)
     fail (0);
   else if (errno != EINVAL)
     fail (0);
   xfree (out);
 
   out = make_filename_try ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                            "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                            "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                            "1", "2", NULL);
   if (!out || strcmp (out,
                       "1/2/3/4/5/6/7/8/9/10/"
                       "1/2/3/4/5/6/7/8/9/10/"
                       "1/2/3/4/5/6/7/8/9/10/"
                       "1/2"))
     fail (0);
   xfree (out);
 
   out = make_filename_try ("foo", "~/bar", "baz/cde", NULL);
   if (!out || strcmp (out, "foo/~/bar/baz/cde"))
     fail (1);
   xfree (out);
 
   out = make_filename_try ("foo", "~/bar", "baz/cde/", NULL);
   if (!out || strcmp (out, "foo/~/bar/baz/cde/"))
     fail (1);
   xfree (out);
 
   out = make_filename_try ("/foo", "~/bar", "baz/cde/", NULL);
   if (!out || strcmp (out, "/foo/~/bar/baz/cde/"))
     fail (1);
   xfree (out);
 
   out = make_filename_try ("//foo", "~/bar", "baz/cde/", NULL);
   if (!out || strcmp (out, "//foo/~/bar/baz/cde/"))
     fail (1);
   xfree (out);
 
   out = make_filename_try ("", "~/bar", "baz/cde", NULL);
   if (!out || strcmp (out, "/~/bar/baz/cde"))
     fail (1);
   xfree (out);
 
 
   out = make_filename_try ("~/foo", "bar", NULL);
   if (!out)
     fail (2);
   else if (home)
     {
       if (strlen (out) < homelen + 7)
         fail (2);
       else if (strncmp (out, home, homelen))
         fail (2);
       else if (strcmp (out+homelen, "/foo/bar"))
         fail (2);
     }
   else
     {
       if (strcmp (out, "~/foo/bar"))
         fail (2);
     }
   xfree (out);
 
   out = make_filename_try ("~", "bar", NULL);
   if (!out)
     fail (2);
   else if (home)
     {
       if (strlen (out) < homelen + 3)
         fail (2);
       else if (strncmp (out, home, homelen))
         fail (2);
       else if (strcmp (out+homelen, "/bar"))
         fail (2);
     }
   else
     {
       if (strcmp (out, "~/bar"))
         fail (2);
     }
   xfree (out);
 }
 
 
 static void
 test_make_absfilename_try (void)
 {
   char *out;
   char *cwd = mygetcwd ();
   size_t cwdlen = strlen (cwd);
 
   out = make_absfilename_try ("foo", "bar", NULL);
   if (!out)
     fail (0);
   else if (strlen (out) < cwdlen + 7)
     fail (0);
   else if (strncmp (out, cwd, cwdlen))
     fail (0);
   else if (strcmp (out+cwdlen, "/foo/bar"))
     fail (0);
   xfree (out);
 
   out = make_absfilename_try ("./foo", NULL);
   if (!out)
     fail (1);
   else if (strlen (out) < cwdlen + 5)
     fail (1);
   else if (strncmp (out, cwd, cwdlen))
     fail (1);
   else if (strcmp (out+cwdlen, "/./foo"))
     fail (1);
   xfree (out);
 
   out = make_absfilename_try (".", NULL);
   if (!out)
     fail (2);
   else if (strlen (out) < cwdlen)
     fail (2);
   else if (strncmp (out, cwd, cwdlen))
     fail (2);
   else if (strcmp (out+cwdlen, ""))
     fail (2);
   xfree (out);
 
   xfree (cwd);
 }
 
 static void
 test_strsplit (void)
 {
   struct {
     const char *s;
     char delim;
     char replacement;
     const char *fields_expected[10];
   } tv[] = {
     {
       "a:bc:cde:fghi:jklmn::foo:", ':', '\0',
       { "a", "bc", "cde", "fghi", "jklmn", "", "foo", "", NULL }
     },
     {
       ",a,bc,,def,", ',', '!',
       { "!a!bc!!def!", "a!bc!!def!", "bc!!def!", "!def!", "def!", "", NULL }
     },
     {
       "", ':', ',',
       { "", NULL }
     }
   };
 
   int tidx;
 
   for (tidx = 0; tidx < DIM(tv); tidx++)
     {
       char *s2;
       int field_count;
       char **fields;
       int field_count_expected;
       int i;
 
       /* Count the fields.  */
       for (field_count_expected = 0;
            tv[tidx].fields_expected[field_count_expected];
            field_count_expected ++)
         ;
 
       /* We need to copy s since strsplit modifies it in place.  */
       s2 = xstrdup (tv[tidx].s);
       fields = strsplit (s2, tv[tidx].delim, tv[tidx].replacement,
                          &field_count);
 
       if (field_count != field_count_expected)
         fail (tidx * 1000);
 
       for (i = 0; i < field_count_expected; i ++)
         if (strcmp (tv[tidx].fields_expected[i], fields[i]) != 0)
           {
             printf ("For field %d, expected '%s', but got '%s'\n",
                     i, tv[tidx].fields_expected[i], fields[i]);
             fail (tidx * 1000 + i + 1);
           }
 
       xfree (fields);
       xfree (s2);
     }
 }
 
 
 
 static void
 test_strtokenize (void)
 {
   struct {
     const char *s;
     const char *delim;
     const char *fields_expected[10];
   } tv[] = {
     {
       "", ":",
       { "", NULL }
     },
     {
       "a", ":",
       { "a", NULL }
     },
     {
       ":", ":",
       { "", "", NULL }
     },
     {
       "::", ":",
       { "", "", "", NULL }
     },
     {
       "a:b:c", ":",
       { "a", "b", "c", NULL }
     },
     {
       "a:b:", ":",
       { "a", "b", "", NULL }
     },
     {
       "a:b", ":",
       { "a", "b", NULL }
     },
     {
       "aa:b:cd", ":",
       { "aa", "b", "cd", NULL }
     },
     {
       "aa::b:cd", ":",
       { "aa", "", "b", "cd", NULL }
     },
     {
       "::b:cd", ":",
       { "", "", "b", "cd", NULL }
     },
     {
       "aa:   : b:cd ", ":",
       { "aa", "", "b", "cd", NULL }
     },
     {
       "  aa:   : b:  cd ", ":",
       { "aa", "", "b", "cd", NULL }
     },
     {
       "  ", ":",
       { "", NULL }
     },
     {
       "  :", ":",
       { "", "", NULL }
     },
     {
       "  : ", ":",
       { "", "", NULL }
     },
     {
       ": ", ":",
       { "", "", NULL }
     },
     {
       ": x ", ":",
       { "", "x", NULL }
     },
     {
       "a:bc:cde:fghi:jklmn::foo:", ":",
       { "a", "bc", "cde", "fghi", "jklmn", "", "foo", "", NULL }
     },
     {
       ",a,bc,,def,", ",",
       { "", "a", "bc", "", "def", "", NULL }
     },
     {
       " a ", " ",
       { "", "a", "", NULL }
     },
     {
       " ", " ",
       { "", "", NULL }
     },
     {
       "", " ",
       { "", NULL }
     }
   };
 
   int tidx;
 
   for (tidx = 0; tidx < DIM(tv); tidx++)
     {
       char **fields;
       int field_count;
       int field_count_expected;
       int i;
 
       for (field_count_expected = 0;
            tv[tidx].fields_expected[field_count_expected];
            field_count_expected ++)
         ;
 
       fields = strtokenize (tv[tidx].s, tv[tidx].delim);
       if (!fields)
         fail (tidx * 1000);
       else
         {
           for (field_count = 0; fields[field_count]; field_count++)
             ;
           if (field_count != field_count_expected)
             fail (tidx * 1000);
           else
             {
               for (i = 0; i < field_count_expected; i++)
                 if (strcmp (tv[tidx].fields_expected[i], fields[i]))
                   {
                     printf ("For field %d, expected '%s', but got '%s'\n",
                             i, tv[tidx].fields_expected[i], fields[i]);
                     fail (tidx * 1000 + i + 1);
                   }
             }
           }
 
       xfree (fields);
     }
 }
 
 
 static void
 test_strtokenize_nt (void)
 {
   struct {
     const char *s;
     const char *delim;
     const char *fields_expected[10];
   } tv[] = {
     {
       "", ":",
       { "", NULL }
     },
     {
       "a", ":",
       { "a", NULL }
     },
     {
       ":", ":",
       { "", "", NULL }
     },
     {
       "::", ":",
       { "", "", "", NULL }
     },
     {
       "a:b:c", ":",
       { "a", "b", "c", NULL }
     },
     {
       "a:b:", ":",
       { "a", "b", "", NULL }
     },
     {
       "a:b", ":",
       { "a", "b", NULL }
     },
     {
       "aa:b:cd", ":",
       { "aa", "b", "cd", NULL }
     },
     {
       "aa::b:cd", ":",
       { "aa", "", "b", "cd", NULL }
     },
     {
       "::b:cd", ":",
       { "", "", "b", "cd", NULL }
     },
     {
       "aa:   : b:cd ", ":",
       { "aa", "   ", " b", "cd ", NULL }
     },
     {
       "  aa:   : b:  cd ", ":",
       { "  aa", "   ", " b", "  cd ", NULL }
     },
     {
       "  ", ":",
       { "  ", NULL }
     },
     {
       "  :", ":",
       { "  ", "", NULL }
     },
     {
       "  : ", ":",
       { "  ", " ", NULL }
     },
     {
       ": ", ":",
       { "", " ", NULL }
     },
     {
       ": x ", ":",
       { "", " x ", NULL }
     },
     {
       "a:bc:cde:fghi:jklmn::foo:", ":",
       { "a", "bc", "cde", "fghi", "jklmn", "", "foo", "", NULL }
     },
     {
       ",a,bc,,def,", ",",
       { "", "a", "bc", "", "def", "", NULL }
     },
     {
       " a ", " ",
       { "", "a", "", NULL }
     },
     {
       " ", " ",
       { "", "", NULL }
     },
     {
       "", " ",
       { "", NULL }
     }
   };
 
   int tidx;
 
   for (tidx = 0; tidx < DIM(tv); tidx++)
     {
       char **fields;
       int field_count;
       int field_count_expected;
       int i;
 
       for (field_count_expected = 0;
            tv[tidx].fields_expected[field_count_expected];
            field_count_expected ++)
         ;
 
       fields = strtokenize_nt (tv[tidx].s, tv[tidx].delim);
       if (!fields)
         fail (tidx * 1000);
       else
         {
           for (field_count = 0; fields[field_count]; field_count++)
             ;
           if (field_count != field_count_expected)
             fail (tidx * 1000);
           else
             {
               for (i = 0; i < field_count_expected; i++)
                 if (strcmp (tv[tidx].fields_expected[i], fields[i]))
                   {
                     printf ("For field %d, expected '%s', but got '%s'\n",
                             i, tv[tidx].fields_expected[i], fields[i]);
                     fail (tidx * 1000 + i + 1);
                   }
             }
           }
 
       xfree (fields);
     }
 }
 
 
 static void
 test_split_fields (void)
 {
   struct {
     const char *s;
     int nfields;
     const char *fields_expected[10];
   } tv[] = {
     {
       "a bc cde fghi jklmn   foo ", 6,
       { "a", "bc", "cde", "fghi", "jklmn", "foo", NULL }
     },
     {
       " a bc  def ", 2,
       { "a", "bc", "def", NULL }
     },
     {
       " a bc  def ", 3,
       { "a", "bc", "def", NULL }
     },
     {
       " a bc  def ", 4,
       { "a", "bc", "def", NULL }
     },
     {
       "", 0,
       { NULL }
     }
   };
 
   int tidx;
   char *fields[10];
   int field_count_expected, nfields, field_count, i;
   char *s2;
 
   for (tidx = 0; tidx < DIM(tv); tidx++)
     {
       nfields = tv[tidx].nfields;
       assert (nfields <= DIM (fields));
 
       /* Count the fields.  */
       for (field_count_expected = 0;
            tv[tidx].fields_expected[field_count_expected];
            field_count_expected ++)
         ;
       if (field_count_expected > nfields)
         field_count_expected = nfields;
 
       /* We need to copy s since split_fields modifies in place.  */
       s2 = xstrdup (tv[tidx].s);
       field_count = split_fields (s2, fields, nfields);
 
       if (field_count != field_count_expected)
         {
           printf ("%s: tidx %d: expected %d, got %d\n",
                   __func__, tidx, field_count_expected, field_count);
           fail (tidx * 1000);
         }
       else
         {
           for (i = 0; i < field_count_expected; i ++)
             if (strcmp (tv[tidx].fields_expected[i], fields[i]))
               {
                 printf ("%s: tidx %d, field %d: expected '%s', got '%s'\n",
                         __func__,
                         tidx, i, tv[tidx].fields_expected[i], fields[i]);
                 fail (tidx * 1000 + i + 1);
               }
         }
 
       xfree (s2);
     }
 }
 
 
 static void
 test_split_fields_colon (void)
 {
   struct {
     const char *s;
     int nfields;
     const char *fields_expected[10];
   } tv[] = {
     {
       "a:bc:cde:fghi:jklmn:  foo ", 6,
       { "a", "bc", "cde", "fghi", "jklmn", "  foo ", NULL }
     },
     {
       " a:bc: def ", 2,
       { " a", "bc", NULL }
     },
     {
       " a:bc :def ", 3,
       { " a", "bc ", "def ", NULL }
     },
     {
       " a:bc: def ", 4,
       { " a", "bc", " def ", NULL }
     },
     {
       "", 0,
       { NULL }
     }
   };
 
   int tidx;
   char *fields[10];
   int field_count_expected, nfields, field_count, i;
   char *s2;
 
   for (tidx = 0; tidx < DIM(tv); tidx++)
     {
       nfields = tv[tidx].nfields;
       assert (nfields <= DIM (fields));
 
       /* Count the fields.  */
       for (field_count_expected = 0;
            tv[tidx].fields_expected[field_count_expected];
            field_count_expected ++)
         ;
       if (field_count_expected > nfields)
         field_count_expected = nfields;
 
       /* We need to copy s since split_fields modifies in place.  */
       s2 = xstrdup (tv[tidx].s);
       field_count = split_fields_colon (s2, fields, nfields);
 
       if (field_count != field_count_expected)
         {
           printf ("%s: tidx %d: expected %d, got %d\n",
                   __func__, tidx, field_count_expected, field_count);
           fail (tidx * 1000);
         }
       else
         {
           for (i = 0; i < field_count_expected; i ++)
             if (strcmp (tv[tidx].fields_expected[i], fields[i]))
               {
                 printf ("%s: tidx %d, field %d: expected '%s', got '%s'\n",
                         __func__,
                         tidx, i, tv[tidx].fields_expected[i], fields[i]);
                 fail (tidx * 1000 + i + 1);
               }
         }
 
       xfree (s2);
     }
 }
 
 
 static char *
 stresc (char *s)
 {
   char *p;
   int l = strlen (s) + 1;
 
   for (p = s; *p; p ++)
     if (*p == '\n')
       l ++;
 
   p = xmalloc (l);
   for (l = 0; *s; s ++, l ++)
     {
       if (*s == ' ')
         p[l] = '_';
       else if (*p == '\n')
         {
           p[l ++] = '\\';
           p[l ++] = 'n';
           p[l] = '\n';
         }
       else
         p[l] = *s;
     }
   p[l] = *s;
 
   return p;
 }
 
 
 static void
 test_format_text (void)
 {
   struct test
   {
     int target_cols, max_cols;
     char *input;
     char *expected;
   };
 
   struct test tests[] = {
     {
       10, 12,
       "",
       "",
     },
     {
       10, 12,
       " ",
       "",
     },
     {
       10, 12,
       "  ",
       "",
     },
     {
       10, 12,
       " \n ",
       " \n",
     },
     {
       10, 12,
       " \n  \n ",
       " \n  \n",
     },
     {
       10, 12,
       "0123456789 0123456789 0",
       "0123456789\n0123456789\n0",
     },
     {
       10, 12,
       "   0123456789   0123456789   0  ",
       "   0123456789\n0123456789\n0",
     },
     {
       10, 12,
       "01 34 67 90 23 56  89 12 45 67 89 1",
       "01 34 67\n90 23 56\n89 12 45\n67 89 1"
     },
     {
       10, 12,
       "01 34 67 90 23 56  89 12 45 67 89 1",
       "01 34 67\n90 23 56\n89 12 45\n67 89 1"
     },
     {
       72, 80,
       "Warning: if you think you've seen more than 10 messages "
       "signed by this key, then this key might be a forgery!  "
       "Carefully examine the email address for small variations "
       "(e.g., additional white space).  If the key is suspect, "
       "then use 'gpg --tofu-policy bad \"FINGERPRINT\"' to mark it as being bad.\n",
       "Warning: if you think you've seen more than 10 messages signed by this\n"
       "key, then this key might be a forgery!  Carefully examine the email\n"
       "address for small variations (e.g., additional white space).  If the key\n"
       "is suspect, then use 'gpg --tofu-policy bad \"FINGERPRINT\"' to mark it as\n"
       "being bad.\n"
 
     },
     {
       72, 80,
       "Normally, there is only a single key associated with an email "
       "address.  However, people sometimes generate a new key if "
       "their key is too old or they think it might be compromised.  "
       "Alternatively, a new key may indicate a man-in-the-middle "
       "attack!  Before accepting this key, you should talk to or "
       "call the person to make sure this new key is legitimate.",
       "Normally, there is only a single key associated with an email "
       "address.\nHowever, people sometimes generate a new key if "
       "their key is too old or\nthey think it might be compromised.  "
       "Alternatively, a new key may indicate\na man-in-the-middle "
       "attack!  Before accepting this key, you should talk\nto or "
       "call the person to make sure this new key is legitimate.",
     }
   };
 
   int i;
   int failed = 0;
 
   for (i = 0; i < sizeof (tests) / sizeof (tests[0]); i ++)
     {
       struct test *test = &tests[i];
       char *result =
         format_text (test->input, test->target_cols, test->max_cols);
       if (!result)
         {
           fail (1);
           exit (2);
         }
       if (strcmp (result, test->expected) != 0)
         {
           printf ("%s: Test #%d failed.\nExpected: '%s'\nResult: '%s'\n",
                   __func__, i + 1, stresc (test->expected), stresc (result));
           failed ++;
         }
       xfree (result);
     }
 
   if (failed)
     fail(0);
 }
 
 
 static void
 test_compare_version_strings (void)
 {
   struct { const char *a; const char *b; int okay; } tests[] = {
     { "1.0.0",   "1.0.0", 0 },
     { "1.0.0-",  "1.0.0", 1 },
     { "1.0.0-1", "1.0.0", 1 },
     { "1.0.0.1", "1.0.0", 1 },
     { "1.0.0",   "1.0.1", -1 },
     { "1.0.0-",  "1.0.1", -1 },
     { "1.0.0-1", "1.0.1", -1 },
     { "1.0.0.1", "1.0.1", -1 },
     { "1.0.0",   "1.1.0", -1 },
     { "1.0.0-",  "1.1.0", -1 },
     { "1.0.0-1", "1.1.0", -1 },
     { "1.0.0.1", "1.1.0", -1 },
 
     { "1.0.0",   "1.0.0-", -1 },
     { "1.0.0",   "1.0.0-1", -1 },
     { "1.0.0",   "1.0.0.1", -1 },
     { "1.1.0",   "1.0.0", 1 },
     { "1.1.1",   "1.1.0", 1 },
     { "1.1.2",   "1.1.2", 0 },
     { "1.1.2",   "1.0.2", 1 },
     { "1.1.2",   "0.0.2", 1 },
     { "1.1.2",   "1.1.3", -1 },
 
     { "0.99.1",  "0.9.9", 1 },
     { "0.9.1",   "0.91.0", -1 },
 
     { "1.5.3",   "1.5",  1 },
     { "1.5.0",   "1.5",  0 },
     { "1.4.99",  "1.5",  -1 },
     { "1.5",     "1.4.99",  1 },
     { "1.5",     "1.5.0",  0 },
     { "1.5",     "1.5.1",  -1 },
 
     { "1.5.3-x17",   "1.5-23",  1 },
 
     { "1.5.3a",   "1.5.3",  1 },
     { "1.5.3a",   "1.5.3b",  -1 },
 
     { "3.1.4-ab", "3.1.4-ab", 0 },
     { "3.1.4-ab", "3.1.4-ac", -1 },
     { "3.1.4-ac", "3.1.4-ab", 1 },
     { "3.1.4-ab", "3.1.4-abb", -1 },
     { "3.1.4-abb", "3.1.4-ab", 1 },
 
     { "",       "",   INT_MIN },
     { NULL,     "",   INT_MIN },
     { "1.2.3",  "",   INT_MIN },
     { "1.2.3",  "2",  INT_MIN },
 
     /* Test cases for validity of A.  */
     { "",      NULL, INT_MIN },
     { "1",     NULL, INT_MIN },
     { "1.",    NULL, 0       },
     { "1.0",   NULL, 0       },
     { "1.0.",  NULL, 0       },
     { "a1.2",  NULL, INT_MIN },
     { NULL,    NULL, INT_MIN }
   };
   int idx;
   int res;
 
   for (idx=0; idx < DIM(tests); idx++)
     {
       res = compare_version_strings (tests[idx].a, tests[idx].b);
       /* printf ("test %d: '%s'  '%s'  %d  ->  %d\n", */
       /*         idx, tests[idx].a, tests[idx].b, tests[idx].okay, res); */
       if (res != tests[idx].okay)
         fail (idx);
     }
 }
 
 
+static void
+test_substitute_envvars (void)
+{
+  struct {
+    const char *name;
+    const char *value;
+  } envvars[] = {
+    { "HOME", "/home/joe" },
+    { "AVAR",  "avar" },
+    { "AVAR1", "avarx" },
+    { "AVAR2", "avarxy" },
+    { "AVAR3", "avarxyz" },
+    { "AVAR0", "ava" },
+    { "MY_VAR", "my_vars_value" },
+    { "STRANGE{X}VAR", "strange{x}vars-value" },
+    { "ZERO",  "" }
+  };
+  struct {
+    const char *string;
+    const char *result;
+  } tests[] = {
+    { "foo bar",
+      "foo bar"
+    },
+    { "foo $HOME",
+      "foo /home/joe"
+    },
+    { "foo $HOME ",
+      "foo /home/joe "
+    },
+    { "foo $HOME$$",
+      "foo /home/joe$"
+    },
+    { "foo ${HOME}/.ssh",
+      "foo /home/joe/.ssh"
+    },
+    { "foo $HOME/.ssh",
+      "foo /home/joe/.ssh"
+    },
+    { "foo $HOME_/.ssh",
+      "foo /.ssh"
+    },
+    { "foo $HOME/.ssh/$MY_VAR:1",
+      "foo /home/joe/.ssh/my_vars_value:1"
+    },
+    { "foo $HOME${MY_VAR}:1",
+      "foo /home/joemy_vars_value:1"
+    },
+    { "${STRANGE{X}VAR}-bla",
+      "strange{x}vars-value-bla"
+    },
+    { "${STRANGE{X}{VAR}-bla", /* missing "}" */
+      "${STRANGE{X}{VAR}-bla"
+    },
+    { "zero->$ZERO<-",
+      "zero-><-"
+    },
+    { "->$AVAR.$AVAR1.$AVAR2.$AVAR3.$AVAR0<-",
+      "->avar.avarx.avarxy.avarxyz.ava<-"
+    },
+    { "",
+      ""
+    }
+  };
+  int idx;
+  char *res;
+
+  for (idx=0; idx < DIM(envvars); idx++)
+    if (gnupg_setenv (envvars[idx].name, envvars[idx].value, 1))
+      {
+        fprintf (stderr,"error setting envvar '%s' to '%s': %s\n",
+                 envvars[idx].name, envvars[idx].value,
+                 strerror (errno));
+        exit (2);
+      }
+
+  for (idx=0; idx < DIM(tests); idx++)
+    {
+      res = substitute_envvars (tests[idx].string);
+      if (!res)
+        {
+          fprintf (stderr,"error substituting '%s' (test %d): %s\n",
+                   tests[idx].string, idx, strerror (errno));
+          exit (2);
+        }
+      if (strcmp (res, tests[idx].result))
+        {
+          fprintf (stderr, "substituted '%s'\n", tests[idx].string);
+          fprintf (stderr, "     wanted '%s'\n", tests[idx].result);
+          fprintf (stderr, "        got '%s'\n", res);
+          fail (idx);
+        }
+      xfree (res);
+    }
+}
+
+
 int
 main (int argc, char **argv)
 {
   (void)argc;
   (void)argv;
 
   test_percent_escape ();
   test_compare_filenames ();
   test_strconcat ();
   test_xstrconcat ();
   test_make_filename_try ();
   test_make_absfilename_try ();
   test_strsplit ();
   test_strtokenize ();
   test_strtokenize_nt ();
   test_split_fields ();
   test_split_fields_colon ();
   test_compare_version_strings ();
   test_format_text ();
+  test_substitute_envvars ();
 
   xfree (home_buffer);
   return !!errcount;
 }