Use https links in user-facing startup and error messages
[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 #if defined(mingw32_HOST_OS)
11 /* Using Secure APIs */
12 #define MINGW_HAS_SECURE_API 1
13 #include <wchar.h>
14 #endif
15
16 pathchar* pathdup(pathchar *path)
17 {
18 pathchar *ret;
19 #if defined(mingw32_HOST_OS)
20 ret = wcsdup(path);
21 #else
22 /* sigh, strdup() isn't a POSIX function, so do it the long way */
23 ret = stgMallocBytes( strlen(path)+1, "pathdup" );
24 strcpy(ret, path);
25 #endif
26 return ret;
27 }
28
29 pathchar* pathdir(pathchar *path)
30 {
31 pathchar *ret;
32 #if defined(mingw32_HOST_OS)
33 pathchar *drive, *dirName;
34 size_t memberLen = pathlen(path) + 1;
35 dirName = stgMallocBytes(pathsize * memberLen, "pathdir(path)");
36 ret = stgMallocBytes(pathsize * memberLen, "pathdir(path)");
37 drive = stgMallocBytes(pathsize * _MAX_DRIVE, "pathdir(path)");
38 _wsplitpath_s(path, drive, _MAX_DRIVE, dirName, pathsize * pathlen(path), NULL, 0, NULL, 0);
39 pathprintf(ret, memberLen, WSTR("%" PATH_FMT "%" PATH_FMT), drive, dirName);
40 stgFree(drive);
41 stgFree(dirName);
42 #else
43 pathchar* dirName = dirname(path);
44 size_t memberLen = pathlen(dirName);
45 ret = stgMallocBytes(pathsize * (memberLen + 2), "pathdir(path)");
46 strcpy(ret, dirName);
47 ret[memberLen ] = '/';
48 ret[memberLen+1] = '\0';
49 #endif
50 return ret;
51 }
52
53 pathchar* mkPath(char* path)
54 {
55 #if defined(mingw32_HOST_OS)
56 size_t required = mbstowcs(NULL, path, 0);
57 pathchar *ret = stgMallocBytes(sizeof(pathchar) * (required + 1), "mkPath");
58 if (mbstowcs(ret, path, required) == (size_t)-1)
59 {
60 barf("mkPath failed converting char* to wchar_t*");
61 }
62 ret[required] = '\0';
63 return ret;
64 #else
65 return pathdup(path);
66 #endif
67 }
68
69 HsBool endsWithPath(pathchar* base, pathchar* str) {
70 int blen = pathlen(base);
71 int slen = pathlen(str);
72 return (blen >= slen) && (0 == pathcmp(base + blen - slen, str));
73 }