From dda627412c5b2a51e2bfe7bc7a677ff6dc0b484f Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sat, 24 Mar 2012 00:26:04 -0400 Subject: [PATCH] More FIXME cleanup. --- src/physfs.c | 37 +++++-------------------------- src/physfs_internal.h | 17 +++++++++------ src/platform_beos.cpp | 6 +++-- src/platform_posix.c | 51 +++++++++++++++++++++++-------------------- src/platform_unix.c | 25 ++++++++++++--------- 5 files changed, 61 insertions(+), 75 deletions(-) diff --git a/src/physfs.c b/src/physfs.c index 6bbe451c..3d5013b8 100644 --- a/src/physfs.c +++ b/src/physfs.c @@ -1102,34 +1102,6 @@ static int freeDirHandle(DirHandle *dh, FileHandle *openList) } /* freeDirHandle */ -/* - * !!! FIXME: remove this and require userdir and basedir to have dirsep - * !!! FIXME: appended in the platform layer - */ -static int appendDirSep(char **dir) -{ - const char dirsep = __PHYSFS_platformDirSeparator; - char *ptr = *dir; - const size_t len = strlen(ptr); - - if (ptr[len - 1] == dirsep) - return 1; - - ptr = (char *) allocator.Realloc(ptr, len + 2); - if (!ptr) - { - allocator.Free(*dir); - return 0; - } /* if */ - - ptr[len] = dirsep; - ptr[len+1] = '\0'; - - *dir = ptr; - return 1; -} /* appendDirSep */ - - static char *calculateBaseDir(const char *argv0) { const char dirsep = __PHYSFS_platformDirSeparator; @@ -1203,17 +1175,18 @@ int PHYSFS_init(const char *argv0) baseDir = calculateBaseDir(argv0); BAIL_IF_MACRO(!baseDir, ERRPASS, 0); - /* Platform layer is required to append a dirsep. */ - assert(baseDir[strlen(baseDir) - 1] == __PHYSFS_platformDirSeparator); - userDir = __PHYSFS_platformCalcUserDir(); - if ((!userDir) || (!appendDirSep(&userDir))) + if (!userDir) { allocator.Free(baseDir); baseDir = NULL; return 0; } /* if */ + /* Platform layer is required to append a dirsep. */ + assert(baseDir[strlen(baseDir) - 1] == __PHYSFS_platformDirSeparator); + assert(userDir[strlen(userDir) - 1] == __PHYSFS_platformDirSeparator); + initialized = 1; /* This makes sure that the error subsystem is initialized. */ diff --git a/src/physfs_internal.h b/src/physfs_internal.h index ba75f0b1..f100408f 100644 --- a/src/physfs_internal.h +++ b/src/physfs_internal.h @@ -641,8 +641,9 @@ char *__PHYSFS_platformCalcBaseDir(const char *argv0); /* * Get the platform-specific user dir. - * Caller will allocator.Free() the retval if it's not NULL. If it's NULL, - * the userdir will default to basedir/username. + * As of PhysicsFS 2.1, returning NULL means fatal error. + * Your string must end with a dir separator if you don't return NULL. + * Caller will allocator.Free() the retval if it's not NULL. */ char *__PHYSFS_platformCalcUserDir(void); @@ -650,12 +651,14 @@ char *__PHYSFS_platformCalcUserDir(void); /* This is the cached version from PHYSFS_init(). This is a fast call. */ const char *__PHYSFS_getUserDir(void); /* not deprecated internal version. */ + /* - * Get the platform-specific pref dir. You must make sure the string ends - * with a dir separator. - * Caller will allocator.Free() the retval if it's not NULL. If it's NULL, - * it's a total failure. Caller will make missing directories if necessary; - * this just reports the final path. + * Get the platform-specific pref dir. + * Returning NULL means fatal error. + * Your string must end with a dir separator if you don't return NULL. + * Caller will allocator.Free() the retval if it's not NULL. + * Caller will make missing directories if necessary; this just reports + * the final path. */ char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app); diff --git a/src/platform_beos.cpp b/src/platform_beos.cpp index 437b5473..fd4bbd5a 100644 --- a/src/platform_beos.cpp +++ b/src/platform_beos.cpp @@ -176,10 +176,12 @@ char *__PHYSFS_platformCalcBaseDir(const char *argv0) assert(rc == B_OK); const char *str = path.Path(); assert(str != NULL); - char *retval = (char *) allocator.Malloc(strlen(str) + 2); + const size_t len = strlen(str); + char *retval = (char *) allocator.Malloc(len + 2); BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL); strcpy(retval, str); - strcat(retval, "/"); + retval[len] = '/'; + retval[len+1] = '\0'; return retval; } /* __PHYSFS_platformCalcBaseDir */ diff --git a/src/platform_posix.c b/src/platform_posix.c index 7b82f628..c72d320f 100644 --- a/src/platform_posix.c +++ b/src/platform_posix.c @@ -61,23 +61,6 @@ static inline PHYSFS_ErrorCode errcodeFromErrno(void) } /* errcodeFromErrno */ - -char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname) -{ - const char *envr = getenv(varname); - char *retval = NULL; - - if (envr != NULL) - { - retval = (char *) allocator.Malloc(strlen(envr) + 1); - BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL); - strcpy(retval, envr); - } /* if */ - - return retval; -} /* __PHYSFS_platformCopyEnvironmentVariable */ - - static char *getUserDirByUID(void) { uid_t uid = getuid(); @@ -85,11 +68,20 @@ static char *getUserDirByUID(void) char *retval = NULL; pw = getpwuid(uid); - if ((pw != NULL) && (pw->pw_dir != NULL)) + if ((pw != NULL) && (pw->pw_dir != NULL) && (*pw->pw_dir != '\0')) { - retval = (char *) allocator.Malloc(strlen(pw->pw_dir) + 1); + const size_t dlen = strlen(pw->pw_dir); + const size_t add_dirsep = (pw->pw_dir[dlen-1] != '/') ? 1 : 0; + retval = (char *) allocator.Malloc(dlen + 1 + add_dirsep); if (retval != NULL) + { strcpy(retval, pw->pw_dir); + if (add_dirsep) + { + retval[dlen] = '/'; + retval[dlen+1] = '\0'; + } /* if */ + } /* if */ } /* if */ return retval; @@ -98,16 +90,27 @@ static char *getUserDirByUID(void) char *__PHYSFS_platformCalcUserDir(void) { - char *retval = __PHYSFS_platformCopyEnvironmentVariable("HOME"); + char *retval = NULL; + char *envr = getenv("HOME"); /* if the environment variable was set, make sure it's really a dir. */ - if (retval != NULL) + if (envr != NULL) { struct stat statbuf; - if ((stat(retval, &statbuf) == -1) || (S_ISDIR(statbuf.st_mode) == 0)) + if ((stat(envr, &statbuf) != -1) && (S_ISDIR(statbuf.st_mode))) { - allocator.Free(retval); - retval = NULL; + const size_t envrlen = strlen(envr); + const size_t add_dirsep = (envr[envrlen-1] != '/') ? 1 : 0; + retval = allocator.Malloc(envrlen + 1 + add_dirsep); + if (retval) + { + strcpy(retval, envr); + if (add_dirsep) + { + retval[envrlen] = '/'; + retval[envrlen+1] = '\0'; + } /* if */ + } /* if */ } /* if */ } /* if */ diff --git a/src/platform_unix.c b/src/platform_unix.c index 30625581..51af43a1 100644 --- a/src/platform_unix.c +++ b/src/platform_unix.c @@ -141,10 +141,6 @@ void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data) } /* __PHYSFS_platformDetectAvailableCDs */ -/* this is in posix.c ... */ -extern char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname); - - /* * See where program (bin) resides in the $PATH specified by (envr). * returns a copy of the first element in envr that contains it, or NULL @@ -246,7 +242,7 @@ static char *readSymLink(const char *path) char *__PHYSFS_platformCalcBaseDir(const char *argv0) { char *retval = NULL; - char *envr = NULL; + const char *envr = NULL; /* * Try to avoid using argv0 unless forced to. If there's a Linux-like @@ -269,6 +265,11 @@ char *__PHYSFS_platformCalcBaseDir(const char *argv0) char *ptr = strrchr(retval, '/'); if (ptr != NULL) *(ptr+1) = '\0'; + else /* shouldn't happen, but just in case... */ + { + allocator.Free(retval); + retval = NULL; + } /* else */ } /* if */ /* No /proc/self/exe, but we have an argv[0] we can parse? */ @@ -279,11 +280,15 @@ char *__PHYSFS_platformCalcBaseDir(const char *argv0) return NULL; /* higher level parses out real path from argv0. */ /* If there's no dirsep on argv0, then look through $PATH for it. */ - /* !!! FIXME: smallAlloc? */ - envr = __PHYSFS_platformCopyEnvironmentVariable("PATH"); - BAIL_IF_MACRO(!envr, ERRPASS, NULL); - retval = findBinaryInPath(argv0, envr); - allocator.Free(envr); + envr = getenv("PATH"); + if (envr != NULL) + { + char *path = (char *) __PHYSFS_smallAlloc(strlen(envr) + 1); + BAIL_IF_MACRO(!path, PHYSFS_ERR_OUT_OF_MEMORY, NULL); + strcpy(path, envr); + retval = findBinaryInPath(argv0, path); + __PHYSFS_smallFree(path); + } /* if */ } /* if */ if (retval != NULL)