diff --git a/include/QF/quakefs.h b/include/QF/quakefs.h index 14f009d03..f80e32bfb 100644 --- a/include/QF/quakefs.h +++ b/include/QF/quakefs.h @@ -384,21 +384,6 @@ void QFS_FilelistFill (filelist_t *list, const char *path, const char *ext, */ void QFS_FilelistFree (filelist_t *list); - -/** Expand leading "~/" in \a path to the user's home directory. - On Linux-like systems, the user's home directory is obtained from the - system, or failing that, the \c HOME environment variable. - - On Windows systems, first the \c HOME environment variable is checked. - If \c HOME is not set, \c WINDIR is used. - - \param path the path to check for "~/" - \return the expanded path - \note It is the caller's responsibility to free the returned string. - FIXME: rename to QFS_* -*/ -char *expand_squiggle (const char *path); - //@} #endif // __quakefs_h diff --git a/include/QF/sys.h b/include/QF/sys.h index d53c30942..5274da04d 100644 --- a/include/QF/sys.h +++ b/include/QF/sys.h @@ -125,4 +125,18 @@ int Sys_CreatePath (const char *path); //@} +/** Expand leading "~/" in \a path to the user's home directory. + On Linux-like systems, the user's home directory is obtained from the + system, or failing that, the \c HOME environment variable. + + On Windows systems, first the \c HOME environment variable is checked. + If \c HOME is not set, \c WINDIR is used. + + \param path the path to check for "~/" + \return the expanded path + \note It is the caller's responsibility to free the returned string. +*/ +char *Sys_ExpandSquiggle (const char *path); + +//@} #endif // __sys_h diff --git a/libs/util/cmd.c b/libs/util/cmd.c index fb2284250..af961c544 100644 --- a/libs/util/cmd.c +++ b/libs/util/cmd.c @@ -649,7 +649,7 @@ Cmd_Exec_File (cbuf_t *cbuf, const char *path, int qfs) if (qfs) { QFS_FOpenFile (path, &file); } else { - char *newpath = expand_squiggle (path); + char *newpath = Sys_ExpandSquiggle (path); file = Qopen (newpath, "r"); free (newpath); } diff --git a/libs/util/quakefs.c b/libs/util/quakefs.c index be4c61017..20808d751 100644 --- a/libs/util/quakefs.c +++ b/libs/util/quakefs.c @@ -43,9 +43,6 @@ static __attribute__ ((used)) const char rcsid[] = #ifdef HAVE_IO_H # include #endif -#ifdef HAVE_PWD_H -# include -#endif #ifdef HAVE_MALLOC_H #include @@ -576,7 +573,7 @@ qfs_load_config (void) char *dirconf; if (*fs_dirconf->string) { - dirconf = expand_squiggle (fs_dirconf->string); + dirconf = Sys_ExpandSquiggle (fs_dirconf->string); if (!(f = Qopen (dirconf, "rt"))) Sys_DPrintf ("Could not load `%s', using builtin defaults\n", dirconf); @@ -1246,38 +1243,6 @@ QFS_GamedirCallback (gamedir_callback_t *func) num_gamedir_callbacks++; } -char * -expand_squiggle (const char *path) -{ - char *home; - -#ifndef _WIN32 - struct passwd *pwd_ent; -#endif - - if (strncmp (path, "~/", 2) != 0) { - return strdup (path); - } - -#ifdef _WIN32 - // LordHavoc: first check HOME to duplicate previous version behavior - // (also handy if someone wants it elsewhere than their windows directory) - home = getenv ("HOME"); - if (!home || !home[0]) - home = getenv ("WINDIR"); -#else - if ((pwd_ent = getpwuid (getuid ()))) { - home = pwd_ent->pw_dir; - } else - home = getenv ("HOME"); -#endif - - if (home) - return nva ("%s%s", home, path + 1); // skip leading ~ - - return strdup (path); -} - static void qfs_path_cvar (cvar_t *var) { @@ -1304,7 +1269,7 @@ QFS_Init (const char *game) Cmd_AddCommand ("path", qfs_path_f, "Show what paths Quake is using"); - qfs_userpath = expand_squiggle (fs_userpath->string); + qfs_userpath = Sys_ExpandSquiggle (fs_userpath->string); qfs_load_config (); diff --git a/libs/util/sys.c b/libs/util/sys.c index 97afd3e58..6be131f44 100644 --- a/libs/util/sys.c +++ b/libs/util/sys.c @@ -55,6 +55,9 @@ static __attribute__ ((used)) const char rcsid[] = #ifdef HAVE_SYS_MMAN_H # include #endif +#ifdef HAVE_PWD_H +# include +#endif #include #include @@ -76,6 +79,7 @@ static __attribute__ ((used)) const char rcsid[] = #include "QF/dstring.h" #include "QF/sys.h" #include "QF/quakefs.h" +#include "QF/va.h" #include "compat.h" @@ -775,3 +779,35 @@ Sys_CreatePath (const char *path) } return 0; } + +char * +Sys_ExpandSquiggle (const char *path) +{ + char *home; + +#ifndef _WIN32 + struct passwd *pwd_ent; +#endif + + if (strncmp (path, "~/", 2) != 0) { + return strdup (path); + } + +#ifdef _WIN32 + // LordHavoc: first check HOME to duplicate previous version behavior + // (also handy if someone wants it elsewhere than their windows directory) + home = getenv ("HOME"); + if (!home || !home[0]) + home = getenv ("WINDIR"); +#else + if ((pwd_ent = getpwuid (getuid ()))) { + home = pwd_ent->pw_dir; + } else + home = getenv ("HOME"); +#endif + + if (home) + return nva ("%s%s", home, path + 1); // skip leading ~ + + return strdup (path); +}