Skip to content

Commit

Permalink
add path_utils file
Browse files Browse the repository at this point in the history
  • Loading branch information
James Edwards committed Jul 17, 2016
1 parent e9e2a7c commit 6a004ef
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 76 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ objs = \
src/parser.o \
src/execute.o \
src/config.o \
src/path_utils.o \
src/huo.o

all: huo
Expand Down
77 changes: 1 addition & 76 deletions src/huo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <libgen.h>
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 <Windows.h>

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;
Expand Down
81 changes: 81 additions & 0 deletions src/path_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <string.h>
#include <errno.h>
#include <sys/param.h>
#include "base_util.h"

#if defined(_POSIX_VERSION) || defined(__linux__) || defined(__APPLE__)

#include <libgen.h>
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 <Windows.h>

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
8 changes: 8 additions & 0 deletions src/path_utils.h
Original file line number Diff line number Diff line change
@@ -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

1 comment on commit 6a004ef

@TheLoneWolfling
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good, that needed to be done.

One other thing that we could do (later) is have posix_util.c / windows_util.c, both sharing the same header file, and have the makefile select which one to use.

Please sign in to comment.