-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
James Edwards
committed
Jul 17, 2016
1 parent
e9e2a7c
commit 6a004ef
Showing
4 changed files
with
91 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ objs = \ | |
src/parser.o \ | ||
src/execute.o \ | ||
src/config.o \ | ||
src/path_utils.o \ | ||
src/huo.o | ||
|
||
all: huo | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
6a004ef
There was a problem hiding this comment.
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.