--- ./GNUnet.orig/src/util/storage.c Wed Jul 10 18:27:11 2002 +++ ./GNUnet/src/util/storage.c Wed Jul 10 18:29:49 2002 @@ -40,6 +40,7 @@ #include #include #include +#include static int atoo(char *s) { int n = 0; @@ -90,44 +91,33 @@ * @returns the full file name, expanded with wordexp, * NULL is returned on error **/ -FileName expandFileName(FileName fil) { - FileName fm; - FileName fn; - - if (fil == NULL) - return NULL; - - if (fil[0] == '~') { - fm = getenv("HOME"); - if (fm == NULL) - fm = "$HOME"; /* keep it symbolic to show error to user! */ - fn = xmalloc(strlen(fm) + strlen(fil) + 1, - "expandFileName: fn"); - fn[0] = 0; - strcat(fn, fm); - strcat(fn, "/"); - strcat(fn, &fil[1]); /* do not copy '~' */ - return fn; +FileName expandFileName(FileName filename) { + wordexp_t result; + FileName expanded_filename; + unsigned short i; + unsigned short expanded_filename_count; + + if (wordexp(filename, &result, 0)) { + /* An error occured, dont expand */ + wordfree(&result); + return(filename); } - if (fil[0] == '/') { - /* absolute path, just copy */ - fn = xmalloc(strlen(fil) + 1, - "expandFileName: fn (2)"); - strcpy(fn, fil); - return fn; + + expanded_filename_count = 1 + strlen(result.we_wordv[0]); + expanded_filename = malloc(expanded_filename_count); + strcpy(expanded_filename, result.we_wordv[0]); + + /* Handle filenames with spaces in them */ + for (i = 1; i < result.we_wordc; i++) { + expanded_filename_count += strlen(result.we_wordv[i]) + 1; + expanded_filename = realloc(expanded_filename, expanded_filename_count); + strcat(expanded_filename, " "); + strcat(expanded_filename, result.we_wordv[i]); } - fm = getenv("PWD"); - if (fm == NULL) - fm = "$PWD"; - fn = xmalloc(strlen(fm) + 1 + strlen(fil) + 1, - "expandFileName: fn (3)"); - fn[0] = 0; - strcat(fn, fm); - strcat(fn, "/"); - strcat(fn, fil); - - return fn; -} + + wordfree(&result); + return(expanded_filename); +} /** * implementation of "mkdir -p"