From 903b990a10d86f0f848d6227d60ebc4859782792 Mon Sep 17 00:00:00 2001 From: Holger Vogt Date: Sun, 11 Aug 2019 09:45:46 +0200 Subject: [PATCH] separate handling of tilde expansion: MS Windows MINGW and VC++ versus all the others --- src/misc/tilde.c | 89 +++++++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/src/misc/tilde.c b/src/misc/tilde.c index db0ef8bd4..3da407501 100644 --- a/src/misc/tilde.c +++ b/src/misc/tilde.c @@ -14,45 +14,61 @@ Modified: 2002 R. Oktas, #if defined(__MINGW32__) || defined(_MSC_VER) #undef BOOLEAN #include /* win32 functions */ -#include "shlobj.h" /* SHGetFolderPath */ +#include /* SHGetFolderPath */ #endif -/* XXX To prevent a name collision with `readline's `tilde_expand', - the original name: `tilde_expand' has changed to `tildexpand'. This - situation naturally brings to mind that `tilde_expand' could be used - directly from `readline' (since it will already be included if we - wish to activate the `readline' support). Following implementation of - 'tilde expanding' has some problems which constitutes another good - reason why it should be replaced: eg. it returns NULL which should - not behave this way, IMHO. Anyway... Don't care for the moment, may - be in the future. -- ro */ - - +/* For Windows MINGW and Visual Studio: Expand the tilde '~' to the + environmental variable HOME, if set by the user. If not set, it + expands to the env variable USERPROFILE, that typically returns + C:\Users\. If this is not available, a folder path led + to by CSIDL_PERSONAL, i.e. C:\Users\\Documents. + If not MINGW or Visual Studio, and if HAVE_PWD_H is defined, + then '~' is expanded to HOME, else string is returned. +*/ char * tildexpand(char *string) { -#ifdef HAVE_PWD_H - char buf[BSIZE_SP]; - char *k, c; -#endif -#if defined(__MINGW32__) || defined(_MSC_VER) - char buf2[BSIZE_SP]; -#endif char *result = NULL; if (!string) - return NULL; + return NULL; string = skip_ws(string); if (*string != '~') return copy(string); +#if defined(__MINGW32__) || defined(_MSC_VER) + char buf2[BSIZE_SP]; + string += 1; + result = getenv("HOME"); + if (!result) + result = getenv("USERPROFILE"); + if (!result) + if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, buf2))) { + if (*string) + strcat(buf2, string); + return copy(buf2); + } + else + return NULL; + else { + strcpy(buf2, result); + if (*string) + strcat(buf2, string); + return copy(buf2); + } +#else + +#ifdef HAVE_PWD_H + char buf[BSIZE_SP]; + char *k, c; +#endif + string += 1; if (!*string || *string == '/') { - /* First try the environment setting. May also make life easier - for non-unix platforms, eg. MS-DOS. -- ro */ + /* First try the environment setting. */ result = getenv("HOME"); #ifdef HAVE_PWD_H /* Can't find a result from the environment, let's try @@ -78,31 +94,20 @@ tildexpand(char *string) } if (result) { #ifdef HAVE_PWD_H - strcpy(buf, result); - if (*string) - strcat(buf, string); - return copy(buf); - + strcpy(buf, result); + if (*string) + strcat(buf, string); + return copy(buf); } else - return NULL; + return NULL; #else - /* Emulate the old behavior to prevent side effects. -- ro */ - return copy(string); + /* Emulate the old behavior to prevent side effects. -- ro */ + return copy(string); } -#if defined(__MINGW32__) || defined(_MSC_VER) - else if(SUCCEEDED(SHGetFolderPath(NULL, - CSIDL_PERSONAL, - NULL, - 0, - buf2))) - { - if (*string) - strcat(buf2, string); - return copy(buf2); - } -#endif + return NULL; #endif +#endif }