summaryrefslogtreecommitdiff
path: root/rts/PathUtils.c
diff options
context:
space:
mode:
authorBen Gamari <bgamari.foss@gmail.com>2016-10-23 14:03:48 -0400
committerBen Gamari <ben@smart-cactus.org>2016-10-23 14:05:10 -0400
commitf084e6845515fbfb774a09ae5d2af1eea8fdc3f0 (patch)
tree780b9a2e21d79055b0abe05f6c93fefee8e1c36b /rts/PathUtils.c
parenta6bcf8783ff758a82d003ea8f669d7216695fa59 (diff)
downloadhaskell-f084e6845515fbfb774a09ae5d2af1eea8fdc3f0.tar.gz
rts: Move path utilities to separate source file
Test Plan: Validate Reviewers: simonmar, austin, erikd Reviewed By: simonmar Subscribers: thomie Differential Revision: https://phabricator.haskell.org/D2614
Diffstat (limited to 'rts/PathUtils.c')
-rw-r--r--rts/PathUtils.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/rts/PathUtils.c b/rts/PathUtils.c
new file mode 100644
index 0000000000..f27e03ff79
--- /dev/null
+++ b/rts/PathUtils.c
@@ -0,0 +1,68 @@
+#include <string.h>
+#include <stddef.h>
+
+#include <Rts.h>
+#include "RtsUtils.h"
+#include "PathUtils.h"
+
+#include <libgen.h>
+#include <ctype.h>
+
+pathchar* pathdup(pathchar *path)
+{
+ pathchar *ret;
+#if defined(mingw32_HOST_OS)
+ ret = wcsdup(path);
+#else
+ /* sigh, strdup() isn't a POSIX function, so do it the long way */
+ ret = stgMallocBytes( strlen(path)+1, "pathdup" );
+ strcpy(ret, path);
+#endif
+ return ret;
+}
+
+pathchar* pathdir(pathchar *path)
+{
+ pathchar *ret;
+#if defined(mingw32_HOST_OS)
+ pathchar *drive, *dirName;
+ size_t memberLen = pathlen(path) + 1;
+ dirName = stgMallocBytes(pathsize * memberLen, "pathdir(path)");
+ ret = stgMallocBytes(pathsize * memberLen, "pathdir(path)");
+ drive = stgMallocBytes(pathsize * _MAX_DRIVE, "pathdir(path)");
+ _wsplitpath_s(path, drive, _MAX_DRIVE, dirName, pathsize * pathlen(path), NULL, 0, NULL, 0);
+ pathprintf(ret, memberLen, WSTR("%" PATH_FMT "%" PATH_FMT), drive, dirName);
+ stgFree(drive);
+ stgFree(dirName);
+#else
+ pathchar* dirName = dirname(path);
+ size_t memberLen = pathlen(dirName);
+ ret = stgMallocBytes(pathsize * (memberLen + 2), "pathdir(path)");
+ strcpy(ret, dirName);
+ ret[memberLen ] = '/';
+ ret[memberLen+1] = '\0';
+#endif
+ return ret;
+}
+
+pathchar* mkPath(char* path)
+{
+#if defined(mingw32_HOST_OS)
+ size_t required = mbstowcs(NULL, path, 0);
+ pathchar *ret = stgMallocBytes(sizeof(pathchar) * (required + 1), "mkPath");
+ if (mbstowcs(ret, path, required) == (size_t)-1)
+ {
+ barf("mkPath failed converting char* to wchar_t*");
+ }
+ ret[required] = '\0';
+ return ret;
+#else
+ return pathdup(path);
+#endif
+}
+
+HsBool endsWithPath(pathchar* base, pathchar* str) {
+ int blen = pathlen(base);
+ int slen = pathlen(str);
+ return (blen >= slen) && (0 == pathcmp(base + blen - slen, str));
+}