Add compatibility to MS Visual C
[git/wgit.git] / compat / msvc.c
blob9ece1cdf2a43dda4c5ee11562780a52a747659f7
1 #include "../git-compat-util.h"
2 #include <windows.h>
4 inline void utf16_to_utf8(char *dst, const wchar_t *src, size_t len)
6 if (!WideCharToMultiByte(CP_UTF8, 0, src, len, dst, len, NULL, NULL))
7 die("failed to convert from UTF-16 to UTF-8: %d", GetLastError());
10 const char *msvc_get_exec_path()
12 static char *exe_path;
14 if (!exe_path) {
15 wchar_t *wpath = NULL;
16 size_t wlen = MAX_PATH / 4;
17 char *path;
18 size_t len;
20 do {
21 wlen *= 4;
22 wpath = xrealloc(wpath, wlen * sizeof(wchar_t));
23 len = GetModuleFileNameW(NULL, wpath, wlen);
25 } while (len == wlen);
27 path = xmalloc((len + 1) * sizeof(wchar_t));
28 utf16_to_utf8(path, wpath, len + 1);
29 free(wpath);
31 len = (strrchr(path, '\\') - path);
32 assert(len != ((char*) NULL - path));
33 path[len] = '\0';
34 exe_path = xrealloc(path, len + 1);
37 return exe_path;
40 const char *msvc_get_etc_gitconfig()
42 static char *etc_path;
44 if (!etc_path) {
45 static const char etc[] = "/etc/gitconfig";
46 const char *epath = msvc_get_exec_path();
47 char *path;
48 size_t len = strlen(epath);
50 path = xmalloc(len + sizeof(etc));
51 memcpy(path, epath, len);
52 memcpy(path + len, etc, sizeof(etc));
53 etc_path = path;
56 return etc_path;
59 const char *msvc_get_man_path()
61 static char *man_path;
63 if (!man_path) {
64 static const char man[] = "/man";
65 const char *epath = msvc_get_exec_path();
66 char *path;
67 size_t len = strlen(epath);
69 path = xmalloc(len + sizeof(man));
70 memcpy(path, epath, len);
71 memcpy(path + len, man, sizeof(man));
72 man_path = path;
75 return man_path;
78 const char *msvc_get_info_path()
80 static char *info_path;
82 if (!info_path) {
83 static const char info[] = "/info";
84 const char *epath = msvc_get_exec_path();
85 char *path;
86 size_t len = strlen(epath);
88 path = xmalloc(len + sizeof(info));
89 memcpy(path, epath, len);
90 memcpy(path + len, info, sizeof(info));
91 info_path = path;
94 return info_path;
97 const char *msvc_get_html_path()
99 static char *html_path;
101 if (!html_path) {
102 static const char html[] = "/html";
103 const char *epath = msvc_get_exec_path();
104 char *path;
105 size_t len = strlen(epath);
107 path = xmalloc(len + sizeof(html));
108 memcpy(path, epath, len);
109 memcpy(path + len, html, sizeof(html));
110 html_path = path;
113 return html_path;
116 const char *msvc_get_template_path()
118 static char *template_path;
120 if (!template_path) {
121 static const char template[] = "/template";
122 const char *epath = msvc_get_exec_path();
123 char *path;
124 size_t len = strlen(epath);
126 path = xmalloc(len + sizeof(template));
127 memcpy(path, epath, len);
128 memcpy(path + len, template, sizeof(template));
129 template_path = path;
132 return template_path;