Release console for ghci wrapper
[ghc.git] / rts / PathUtils.c
1 #include <Rts.h>
2 #include "RtsUtils.h"
3 #include "PathUtils.h"
4
5 #include <string.h>
6 #include <stddef.h>
7
8 #include <libgen.h>
9 #include <ctype.h>
10
11 pathchar* pathdup(pathchar *path)
12 {
13 pathchar *ret;
14 #if defined(mingw32_HOST_OS)
15 ret = wcsdup(path);
16 #else
17 /* sigh, strdup() isn't a POSIX function, so do it the long way */
18 ret = stgMallocBytes( strlen(path)+1, "pathdup" );
19 strcpy(ret, path);
20 #endif
21 return ret;
22 }
23
24 pathchar* pathdir(pathchar *path)
25 {
26 pathchar *ret;
27 #if defined(mingw32_HOST_OS)
28 pathchar *drive, *dirName;
29 size_t memberLen = pathlen(path) + 1;
30 dirName = stgMallocBytes(pathsize * memberLen, "pathdir(path)");
31 ret = stgMallocBytes(pathsize * memberLen, "pathdir(path)");
32 drive = stgMallocBytes(pathsize * _MAX_DRIVE, "pathdir(path)");
33 _wsplitpath_s(path, drive, _MAX_DRIVE, dirName, pathsize * pathlen(path), NULL, 0, NULL, 0);
34 pathprintf(ret, memberLen, WSTR("%" PATH_FMT "%" PATH_FMT), drive, dirName);
35 stgFree(drive);
36 stgFree(dirName);
37 #else
38 pathchar* dirName = dirname(path);
39 size_t memberLen = pathlen(dirName);
40 ret = stgMallocBytes(pathsize * (memberLen + 2), "pathdir(path)");
41 strcpy(ret, dirName);
42 ret[memberLen ] = '/';
43 ret[memberLen+1] = '\0';
44 #endif
45 return ret;
46 }
47
48 pathchar* mkPath(char* path)
49 {
50 #if defined(mingw32_HOST_OS)
51 size_t required = mbstowcs(NULL, path, 0);
52 pathchar *ret = stgMallocBytes(sizeof(pathchar) * (required + 1), "mkPath");
53 if (mbstowcs(ret, path, required) == (size_t)-1)
54 {
55 barf("mkPath failed converting char* to wchar_t*");
56 }
57 ret[required] = '\0';
58 return ret;
59 #else
60 return pathdup(path);
61 #endif
62 }
63
64 HsBool endsWithPath(pathchar* base, pathchar* str) {
65 int blen = pathlen(base);
66 int slen = pathlen(str);
67 return (blen >= slen) && (0 == pathcmp(base + blen - slen, str));
68 }