diff options
Diffstat (limited to 'util/fileutil.c')
-rw-r--r-- | util/fileutil.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/util/fileutil.c b/util/fileutil.c index e2ea9b20e..28bd866c5 100644 --- a/util/fileutil.c +++ b/util/fileutil.c @@ -21,6 +21,7 @@ #include <config.h> #include <stdio.h> #include <stdlib.h> +#include <stdarg.h> #include <string.h> #include <assert.h> #include <unistd.h> @@ -29,3 +30,37 @@ #include "ttyio.h" +/**************** + * Construct a filename form the NULL terminated list of parts. + * Tilde expansion is done here. + */ +char * +make_filename( const char *first_part, ... ) +{ + va_list arg_ptr ; + size_t n; + const char *s; + char *name, *home, *p; + + va_start( arg_ptr, first_part ) ; + n = strlen(first_part)+1; + while( (s=va_arg(arg_ptr, const char *)) ) + n += strlen(s) + 1; + va_end(arg_ptr); + + home = NULL; + if( *first_part == '~' && first_part[1] == '/' + && (home = getenv("HOME")) && *home ) + n += strlen(home); + + name = m_alloc(n); + p = home ? stpcpy(stpcpy(name,home), first_part+1) + : stpcpy(name, first_part); + va_start( arg_ptr, first_part ) ; + while( (s=va_arg(arg_ptr, const char *)) ) + p = stpcpy(stpcpy(p,"/"), s); + va_end(arg_ptr); + + return name; +} + |