From 6a004ef0ac59d7abd3c622457fa7de0deb45d1a2 Mon Sep 17 00:00:00 2001 From: James Edwards Date: Sun, 17 Jul 2016 10:15:25 -0700 Subject: [PATCH] add path_utils file --- Makefile | 1 + src/huo.c | 77 +-------------------------------------------- src/path_utils.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ src/path_utils.h | 8 +++++ 4 files changed, 91 insertions(+), 76 deletions(-) create mode 100644 src/path_utils.c create mode 100644 src/path_utils.h diff --git a/Makefile b/Makefile index 1de92d1..42d8ebb 100644 --- a/Makefile +++ b/Makefile @@ -50,6 +50,7 @@ objs = \ src/parser.o \ src/execute.o \ src/config.o \ + src/path_utils.o \ src/huo.o all: huo diff --git a/src/huo.c b/src/huo.c index bd60fbe..268e644 100644 --- a/src/huo.c +++ b/src/huo.c @@ -15,86 +15,11 @@ #include "execute.h" #include "store_defs.h" #include "base_util.h" +#include "path_utils.h" #include "huo.h" #include "config.h" #include "core_functions.h" -#if defined(_POSIX_VERSION) || defined(__linux__) || defined(__APPLE__) -#include -char *get_exe_path(const char *called_name) { - char *path_to_exe = realpath(called_name, NULL); - - if (path_to_exe == NULL) { - ERROR("Error getting real path: %d (%s)", errno, strerror(errno)); - } - - return path_to_exe; -} - -char *get_path_dir(char *path) { - char *temp = dirname(path); - if (temp == NULL) { - ERROR("Error splitting directory: %s", path); - } - char *dup = o_strdup(temp); // dirname strings should not be freed - if (dup == NULL) { - ERROR("Malloc failure"); - } - return dup; -} - -char *path_merge(const char *dir, const char *rest) { - // Bleh - char *path = ARR_MALLOC(strlen(dir) + 1 + strlen(rest) + 1, char); // sizeof(char) is defined as 1, I know. - if (path == NULL) { - ERROR("Malloc failure"); - } - path[0] = 0; - strcat(path, dir); - strcat(path, "/"); - strcat(path, rest); - return path; -} -#elif defined(_WIN16) || defined(_WIN32) || defined(_WIN64) -// UNTESTED!! -#include - -char *get_exe_path(const char *called_name) { - char *buffer = malloc_or_die(MAX_PATH); - GetModuleFileName(NULL, buffer, MAX_PATH) ; - - return buffer; -} - -char *get_path_dir(char *path) { - // Bleh - char *temp = o_strdup(path); - size_t len = strlen(temp); - do { - temp[len] = 0; - if (len <= 1) { - ERROR("Could not find directory of %s", path); - } - } while (temp[--len] != '/'); - temp[len] = 0; - return temp; -} - -char *path_merge(const char *dir, const char *rest) { - char *path = ARR_MALLOC(strlen(dir) + 1 + strlen(rest) + 1, char); - if (path == NULL) { - ERROR("Malloc failure"); - } - path[0] = 0; - strcat(path, dir); - strcat(path, "\\"); - strcat(path, rest); - return path; -} -#else -#error "Unknown system!" -#endif - int main(int argc, char const *argv[]) { bool help_flag = false; bool command_flag = false; diff --git a/src/path_utils.c b/src/path_utils.c new file mode 100644 index 0000000..1e7e2a6 --- /dev/null +++ b/src/path_utils.c @@ -0,0 +1,81 @@ +#include +#include +#include +#include "base_util.h" + +#if defined(_POSIX_VERSION) || defined(__linux__) || defined(__APPLE__) + +#include +char *get_exe_path(const char *called_name) { + char *path_to_exe = realpath(called_name, NULL); + + if (path_to_exe == NULL) { + ERROR("Error getting real path: %d (%s)", errno, strerror(errno)); + } + + return path_to_exe; +} + +char *get_path_dir(char *path) { + char *temp = dirname(path); + if (temp == NULL) { + ERROR("Error splitting directory: %s", path); + } + char *dup = o_strdup(temp); // dirname strings should not be freed + if (dup == NULL) { + ERROR("Malloc failure"); + } + return dup; +} + +char *path_merge(const char *dir, const char *rest) { + // Bleh + char *path = ARR_MALLOC(strlen(dir) + 1 + strlen(rest) + 1, char); // sizeof(char) is defined as 1, I know. + if (path == NULL) { + ERROR("Malloc failure"); + } + path[0] = 0; + strcat(path, dir); + strcat(path, "/"); + strcat(path, rest); + return path; +} +#elif defined(_WIN16) || defined(_WIN32) || defined(_WIN64) +// UNTESTED!! +#include + +char *get_exe_path(const char *called_name) { + char *buffer = malloc_or_die(MAX_PATH); + GetModuleFileName(NULL, buffer, MAX_PATH) ; + + return buffer; +} + +char *get_path_dir(char *path) { + // Bleh + char *temp = o_strdup(path); + size_t len = strlen(temp); + do { + temp[len] = 0; + if (len <= 1) { + ERROR("Could not find directory of %s", path); + } + } while (temp[--len] != '/'); + temp[len] = 0; + return temp; +} + +char *path_merge(const char *dir, const char *rest) { + char *path = ARR_MALLOC(strlen(dir) + 1 + strlen(rest) + 1, char); + if (path == NULL) { + ERROR("Malloc failure"); + } + path[0] = 0; + strcat(path, dir); + strcat(path, "\\"); + strcat(path, rest); + return path; +} +#else +#error "Unknown system!" +#endif diff --git a/src/path_utils.h b/src/path_utils.h new file mode 100644 index 0000000..34f91f2 --- /dev/null +++ b/src/path_utils.h @@ -0,0 +1,8 @@ +#ifndef _PATH_UTILS_H +#define _PATH_UTILS_H + +char *get_exe_path(const char *called_name); +char *get_path_dir(char *path); +char *path_merge(const char *dir, const char *rest); + +#endif