/
platform_windows.c
1630 lines (1322 loc) · 44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* MojoSetup; a portable, flexible installation application.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#if PLATFORM_WINDOWS
#include "platform.h"
#include "gui.h"
// Much of this file was lifted from PhysicsFS, http://icculus.org/physfs/ ...
// I wrote all this code under the same open source license, and own the
// copyright on it anyhow, so transferring it here is "safe".
18
// Forcibly disable UNICODE, since we manage this ourselves.
19
20
21
22
#ifdef UNICODE
#undef UNICODE
#endif
23
#define WIN32_LEAN_AND_MEAN 1
24
#include <windows.h>
25
#include <shellapi.h>
26
27
28
29
30
// !!! FIXME: this is for stdio redirection crap.
#include <io.h>
#include <fcntl.h>
31
32
33
34
35
36
37
38
39
40
// is Win95/Win98/WinME? (no Unicode, etc)
static boolean osIsWin9x = false;
static uint32 osMajorVer = 0;
static uint32 osMinorVer = 0;
static uint32 osBuildVer = 0;
static uint32 startupTime = 0;
// These allocation macros are much more complicated in PhysicsFS.
#define smallAlloc(x) xmalloc(x)
41
#define smallFree(x) free(x)
42
43
44
45
46
// ...so is this.
#define BAIL_IF_MACRO(cond, err, ret) if (cond) return ret;
#define BAIL_MACRO(err, ret) return ret;
47
48
#define LOWORDER_UINT64(pos) ((uint32) (pos & 0xFFFFFFFF))
#define HIGHORDER_UINT64(pos) ((uint32) ((pos >> 32) & 0xFFFFFFFF))
49
50
51
52
// Users without the platform SDK don't have this defined. The original docs
// for SetFilePointer() just said to compare with 0xFFFFFFFF, so this should
// work as desired.
53
54
55
56
#ifndef INVALID_SET_FILE_POINTER
#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
#endif
57
// just in case...
58
59
60
61
#ifndef INVALID_FILE_ATTRIBUTES
#define INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
#endif
62
// Not defined before the Vista SDK.
63
64
65
66
#ifndef IO_REPARSE_TAG_SYMLINK
#define IO_REPARSE_TAG_SYMLINK 0xA000000C
#endif
67
68
69
70
71
// This is in shlobj.h, but we can't include it due to universal.h conflicts.
#ifndef CSIDL_PERSONAL
#define CSIDL_PERSONAL 0x0005
#endif
72
// This is in shlobj.h, but we can't include it due to universal.h conflicts.
73
74
75
76
#ifndef SHGFP_TYPE_CURRENT
#define SHGFP_TYPE_CURRENT 0x0000
#endif
77
78
// these utf-8 functions may move to mojosetup.c some day...
79
80
81
void utf8ToUcs2(const char *src, uint16 *dst, uint64 len)
{
82
len -= sizeof (uint16); // save room for null char.
83
84
85
86
87
88
89
90
while (len >= sizeof (uint16))
{
uint32 cp = utf8codepoint(&src);
if (cp == 0)
break;
else if (cp == UNICODE_BOGUS_CHAR_VALUE)
cp = UNICODE_BOGUS_CHAR_CODEPOINT;
91
// !!! FIXME: UTF-16 surrogates?
92
93
94
95
96
if (cp > 0xFFFF)
cp = UNICODE_BOGUS_CHAR_CODEPOINT;
*(dst++) = cp;
len -= sizeof (uint16);
97
} // while
98
99
*dst = 0;
100
} // utf8ToUcs2
101
102
103
104
105
106
107
108
109
110
111
static void utf8fromcodepoint(uint32 cp, char **_dst, uint64 *_len)
{
char *dst = *_dst;
uint64 len = *_len;
if (len == 0)
return;
if (cp > 0x10FFFF)
cp = UNICODE_BOGUS_CHAR_CODEPOINT;
112
else if ((cp == 0xFFFE) || (cp == 0xFFFF)) // illegal values.
113
114
115
cp = UNICODE_BOGUS_CHAR_CODEPOINT;
else
{
116
// There are seven "UTF-16 surrogates" that are illegal in UTF-8.
117
118
119
120
121
122
123
124
125
126
switch (cp)
{
case 0xD800:
case 0xDB7F:
case 0xDB80:
case 0xDBFF:
case 0xDC00:
case 0xDF80:
case 0xDFFF:
cp = UNICODE_BOGUS_CHAR_CODEPOINT;
127
128
} // switch
} // else
129
130
// Do the encoding...
131
132
133
134
if (cp < 0x80)
{
*(dst++) = (char) cp;
len--;
135
} // if
136
137
138
139
140
141
142
143
144
145
else if (cp < 0x800)
{
if (len < 2)
len = 0;
else
{
*(dst++) = (char) ((cp >> 6) | 128 | 64);
*(dst++) = (char) (cp & 0x3F) | 128;
len -= 2;
146
147
} // else
} // else if
148
149
150
151
152
153
154
155
156
157
158
else if (cp < 0x10000)
{
if (len < 3)
len = 0;
else
{
*(dst++) = (char) ((cp >> 12) | 128 | 64 | 32);
*(dst++) = (char) ((cp >> 6) & 0x3F) | 128;
*(dst++) = (char) (cp & 0x3F) | 128;
len -= 3;
159
160
} // else
} // else if
161
162
163
164
165
166
167
168
169
170
171
172
else
{
if (len < 4)
len = 0;
else
{
*(dst++) = (char) ((cp >> 18) | 128 | 64 | 32 | 16);
*(dst++) = (char) ((cp >> 12) & 0x3F) | 128;
*(dst++) = (char) ((cp >> 6) & 0x3F) | 128;
*(dst++) = (char) (cp & 0x3F) | 128;
len -= 4;
173
174
} // else if
} // else
175
176
177
*_dst = dst;
*_len = len;
178
} // utf8fromcodepoint
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#define UTF8FROMTYPE(typ, src, dst, len) \
len--; \
while (len) \
{ \
const uint32 cp = (uint32) *(src++); \
if (cp == 0) break; \
utf8fromcodepoint(cp, &dst, &len); \
} \
*dst = '\0'; \
void utf8FromUcs2(const uint16 *src, char *dst, uint64 len)
{
UTF8FROMTYPE(uint64, src, dst, len);
} // utf8FromUcs4
195
196
197
198
#define UTF8_TO_UNICODE_STACK_MACRO(w_assignto, str) { \
if (str == NULL) \
w_assignto = NULL; \
else { \
199
const uint32 len = (uint32) ((strlen(str) * 4) + 1); \
200
201
202
203
204
205
w_assignto = (WCHAR *) smallAlloc(len); \
if (w_assignto != NULL) \
utf8ToUcs2(str, (uint16 *) w_assignto, len); \
} \
} \
206
static uint32 wStrLen(const WCHAR *wstr)
207
{
208
uint32 len = 0;
209
210
while (*(wstr++))
len++;
211
return len;
212
213
214
215
216
217
218
219
220
} // wStrLen
static char *unicodeToUtf8Heap(const WCHAR *w_str)
{
char *retval = NULL;
if (w_str != NULL)
{
void *ptr = NULL;
221
const uint32 len = (wStrLen(w_str) * 4) + 1;
222
223
224
225
retval = (char *) xmalloc(len);
utf8FromUcs2((const uint16 *) w_str, retval, len);
retval = xrealloc(retval, strlen(retval) + 1); // shrink.
} // if
226
return retval;
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
} // unicodeToUtf8Heap
static char *codepageToUtf8Heap(const char *cpstr)
{
char *retval = NULL;
if (cpstr != NULL)
{
const int len = (int) (strlen(cpstr) + 1);
WCHAR *wbuf = (WCHAR *) smallAlloc(len * sizeof (WCHAR));
BAIL_IF_MACRO(wbuf == NULL, ERR_OUT_OF_MEMORY, NULL);
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, cpstr, len, wbuf, len);
retval = (char *) xmalloc(len * 4);
utf8FromUcs2(wbuf, retval, len * 4);
smallFree(wbuf);
242
} // if
243
return retval;
244
} // codepageToUtf8Heap
245
246
247
// pointers for APIs that may not exist on some Windows versions...
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
static HANDLE libKernel32 = NULL;
static HANDLE libUserEnv = NULL;
static HANDLE libAdvApi32 = NULL;
static HANDLE libShell32 = NULL;
static DWORD (WINAPI *pGetModuleFileNameW)(HMODULE, LPWCH, DWORD);
static BOOL (WINAPI *pGetUserProfileDirectoryW)(HANDLE, LPWSTR, LPDWORD);
static BOOL (WINAPI *pGetUserNameW)(LPWSTR, LPDWORD);
static DWORD (WINAPI *pGetFileAttributesW)(LPCWSTR);
static HANDLE (WINAPI *pFindFirstFileW)(LPCWSTR, LPWIN32_FIND_DATAW);
static BOOL (WINAPI *pFindNextFileW)(HANDLE, LPWIN32_FIND_DATAW);
static DWORD (WINAPI *pGetCurrentDirectoryW)(DWORD, LPWSTR);
static BOOL (WINAPI *pDeleteFileW)(LPCWSTR);
static BOOL (WINAPI *pRemoveDirectoryW)(LPCWSTR);
static BOOL (WINAPI *pCreateDirectoryW)(LPCWSTR, LPSECURITY_ATTRIBUTES);
static BOOL (WINAPI *pGetFileAttributesExA)
(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
static BOOL (WINAPI *pGetFileAttributesExW)
(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
static DWORD (WINAPI *pFormatMessageW)
(DWORD, LPCVOID, DWORD, DWORD, LPWSTR, DWORD, va_list *);
static HANDLE (WINAPI *pCreateFileW)
(LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE);
static HRESULT (WINAPI *pSHGetFolderPathA)(HWND, int, HANDLE, DWORD, LPTSTR);
static BOOL (WINAPI *pMoveFileW)(LPCWSTR, LPCWSTR);
static void (WINAPI *pOutputDebugStringW)(LPCWSTR);
274
275
276
277
278
279
// Fallbacks for missing Unicode functions on Win95/98/ME. These are filled
// into the function pointers if looking up the real Unicode entry points
// in the system DLLs fails, so they're never used on WinNT/XP/Vista/etc.
// They make an earnest effort to convert to/from UTF-8 and UCS-2 to
// the user's current codepage.
280
281
282
283
284
285
286
287
288
static BOOL WINAPI fallbackGetUserNameW(LPWSTR buf, LPDWORD len)
{
const DWORD cplen = *len;
char *cpstr = smallAlloc(cplen);
BOOL retval = GetUserNameA(cpstr, len);
if (buf != NULL)
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, cpstr, cplen, buf, *len);
smallFree(cpstr);
289
return retval;
290
} // fallbackGetUserNameW
291
292
293
294
295
296
297
298
299
300
301
302
static DWORD WINAPI fallbackFormatMessageW(DWORD dwFlags, LPCVOID lpSource,
DWORD dwMessageId, DWORD dwLangId,
LPWSTR lpBuf, DWORD nSize,
va_list *Arguments)
{
char *cpbuf = (char *) smallAlloc(nSize);
DWORD retval = FormatMessageA(dwFlags, lpSource, dwMessageId, dwLangId,
cpbuf, nSize, Arguments);
if (retval > 0)
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,cpbuf,retval,lpBuf,nSize);
smallFree(cpbuf);
303
return retval;
304
} // fallbackFormatMessageW
305
306
307
308
309
310
311
312
313
static DWORD WINAPI fallbackGetModuleFileNameW(HMODULE hMod, LPWCH lpBuf,
DWORD nSize)
{
char *cpbuf = (char *) smallAlloc(nSize);
DWORD retval = GetModuleFileNameA(hMod, cpbuf, nSize);
if (retval > 0)
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,cpbuf,retval,lpBuf,nSize);
smallFree(cpbuf);
314
return retval;
315
} // fallbackGetModuleFileNameW
316
317
318
319
320
321
322
323
324
static DWORD WINAPI fallbackGetFileAttributesW(LPCWSTR fname)
{
DWORD retval = 0;
const int buflen = (int) (wStrLen(fname) + 1);
char *cpstr = (char *) smallAlloc(buflen);
WideCharToMultiByte(CP_ACP, 0, fname, buflen, cpstr, buflen, NULL, NULL);
retval = GetFileAttributesA(cpstr);
smallFree(cpstr);
325
return retval;
326
} // fallbackGetFileAttributesW
327
328
329
330
331
332
333
334
335
336
337
338
static DWORD WINAPI fallbackGetCurrentDirectoryW(DWORD buflen, LPWSTR buf)
{
DWORD retval = 0;
char *cpbuf = NULL;
if (buf != NULL)
cpbuf = (char *) smallAlloc(buflen);
retval = GetCurrentDirectoryA(buflen, cpbuf);
if (cpbuf != NULL)
{
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,cpbuf,retval,buf,buflen);
smallFree(cpbuf);
339
340
341
} // if
return retval;
} // fallbackGetCurrentDirectoryW
342
343
344
345
346
347
348
349
350
static BOOL WINAPI fallbackRemoveDirectoryW(LPCWSTR dname)
{
BOOL retval = 0;
const int buflen = (int) (wStrLen(dname) + 1);
char *cpstr = (char *) smallAlloc(buflen);
WideCharToMultiByte(CP_ACP, 0, dname, buflen, cpstr, buflen, NULL, NULL);
retval = RemoveDirectoryA(cpstr);
smallFree(cpstr);
351
return retval;
352
} // fallbackRemoveDirectoryW
353
354
355
356
357
358
359
360
361
362
static BOOL WINAPI fallbackCreateDirectoryW(LPCWSTR dname,
LPSECURITY_ATTRIBUTES attr)
{
BOOL retval = 0;
const int buflen = (int) (wStrLen(dname) + 1);
char *cpstr = (char *) smallAlloc(buflen);
WideCharToMultiByte(CP_ACP, 0, dname, buflen, cpstr, buflen, NULL, NULL);
retval = CreateDirectoryA(cpstr, attr);
smallFree(cpstr);
363
return retval;
364
} // fallbackCreateDirectoryW
365
366
367
368
369
370
371
372
373
static BOOL WINAPI fallbackDeleteFileW(LPCWSTR fname)
{
BOOL retval = 0;
const int buflen = (int) (wStrLen(fname) + 1);
char *cpstr = (char *) smallAlloc(buflen);
WideCharToMultiByte(CP_ACP, 0, fname, buflen, cpstr, buflen, NULL, NULL);
retval = DeleteFileA(cpstr);
smallFree(cpstr);
374
return retval;
375
} // fallbackDeleteFileW
376
377
378
379
380
381
382
383
384
385
386
387
388
389
static HANDLE WINAPI fallbackCreateFileW(LPCWSTR fname,
DWORD dwDesiredAccess, DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttrs,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttrs, HANDLE hTemplFile)
{
HANDLE retval;
const int buflen = (int) (wStrLen(fname) + 1);
char *cpstr = (char *) smallAlloc(buflen);
WideCharToMultiByte(CP_ACP, 0, fname, buflen, cpstr, buflen, NULL, NULL);
retval = CreateFileA(cpstr, dwDesiredAccess, dwShareMode, lpSecurityAttrs,
dwCreationDisposition, dwFlagsAndAttrs, hTemplFile);
smallFree(cpstr);
390
return retval;
391
} // fallbackCreateFileW
392
393
394
395
396
397
398
399
400
401
402
403
404
static BOOL WINAPI fallbackMoveFileW(LPCWSTR src, LPCWSTR dst)
{
BOOL retval;
const int srcbuflen = (int) (wStrLen(src) + 1);
char *srccpstr = (char *) smallAlloc(srcbuflen);
const int dstbuflen = (int) (wStrLen(dst) + 1);
char *dstcpstr = (char *) smallAlloc(dstbuflen);
WideCharToMultiByte(CP_ACP,0,src,srcbuflen,srccpstr,srcbuflen,NULL,NULL);
WideCharToMultiByte(CP_ACP,0,dst,dstbuflen,dstcpstr,dstbuflen,NULL,NULL);
retval = MoveFileA(srccpstr, dstcpstr);
smallFree(srccpstr);
smallFree(dstcpstr);
405
return retval;
406
} // fallbackMoveFileW
407
408
409
410
411
412
static void WINAPI fallbackOutputDebugStringW(LPCWSTR str)
{
const int buflen = (int) (wStrLen(str) + 1);
char *cpstr = (char *) smallAlloc(buflen);
WideCharToMultiByte(CP_ACP, 0, str, buflen, cpstr, buflen, NULL, NULL);
413
OutputDebugStringA(cpstr);
414
415
416
417
smallFree(cpstr);
} // fallbackOutputDebugStringW
418
// A blatant abuse of pointer casting...
419
420
static int symLookup(HMODULE dll, void **addr, const char *sym)
{
421
return ((*addr = GetProcAddress(dll, sym)) != NULL);
422
} // symLookup
423
424
425
static boolean findApiSymbols(void)
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
{
const boolean osHasUnicode = !osIsWin9x;
HMODULE dll = NULL;
#define LOOKUP_NOFALLBACK(x, reallyLook) { \
if (reallyLook) \
symLookup(dll, (void **) &p##x, #x); \
else \
p##x = NULL; \
}
#define LOOKUP(x, reallyLook) { \
if ((!reallyLook) || (!symLookup(dll, (void **) &p##x, #x))) \
p##x = fallback##x; \
}
442
443
// Apparently Win9x HAS the Unicode entry points, they just don't WORK.
// ...so don't look them up unless we're on NT+. (see osHasUnicode.)
444
445
446
447
448
dll = libUserEnv = LoadLibraryA("userenv.dll");
if (dll != NULL)
LOOKUP_NOFALLBACK(GetUserProfileDirectoryW, osHasUnicode);
449
// !!! FIXME: what do they call advapi32.dll on Win64?
450
451
452
453
dll = libAdvApi32 = LoadLibraryA("advapi32.dll");
if (dll != NULL)
LOOKUP(GetUserNameW, osHasUnicode);
454
// !!! FIXME: what do they call kernel32.dll on Win64?
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
dll = libKernel32 = LoadLibraryA("kernel32.dll");
if (dll != NULL)
{
LOOKUP_NOFALLBACK(GetFileAttributesExA, 1);
LOOKUP_NOFALLBACK(GetFileAttributesExW, osHasUnicode);
LOOKUP_NOFALLBACK(FindFirstFileW, osHasUnicode);
LOOKUP_NOFALLBACK(FindNextFileW, osHasUnicode);
LOOKUP(GetModuleFileNameW, osHasUnicode);
LOOKUP(FormatMessageW, osHasUnicode);
LOOKUP(GetFileAttributesW, osHasUnicode);
LOOKUP(GetCurrentDirectoryW, osHasUnicode);
LOOKUP(CreateDirectoryW, osHasUnicode);
LOOKUP(RemoveDirectoryW, osHasUnicode);
LOOKUP(CreateFileW, osHasUnicode);
LOOKUP(DeleteFileW, osHasUnicode);
LOOKUP(MoveFileW, osHasUnicode);
LOOKUP(OutputDebugStringW, osHasUnicode);
472
} // if
473
474
// !!! FIXME: what do they call shell32.dll on Win64?
475
476
477
478
479
480
481
dll = libShell32 = LoadLibraryA("shell32.dll");
if (dll != NULL)
LOOKUP_NOFALLBACK(SHGetFolderPathA, 1);
#undef LOOKUP_NOFALLBACK
#undef LOOKUP
482
return true;
483
} // findApiSymbols
484
485
486
487
488
// ok, now the actual platform layer implementation...
489
490
boolean MojoPlatform_istty(void)
{
491
return (GetConsoleWindow() != 0);
492
493
494
} // MojoPlatform_istty
495
496
497
498
499
boolean MojoPlatform_launchBrowser(const char *url)
{
// msdn says:
// "Returns a value greater than 32 if successful, or an error value that
// is less than or equal to 32 otherwise."
500
return (((int) ShellExecuteA(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL)) > 32);
501
502
503
} // MojoPlatform_launchBrowser
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
boolean MojoPlatform_installDesktopMenuItem(const char *data)
{
// !!! FIXME: write me.
STUBBED("desktop menu support");
return false;
} // MojoPlatform_installDesktopMenuItem
boolean MojoPlatform_uninstallDesktopMenuItem(const char *data)
{
// !!! FIXME: write me.
STUBBED("desktop menu support");
return false;
} // MojoPlatform_uninstallDesktopMenuItem
520
521
522
523
524
525
526
int MojoPlatform_exec(const char *cmd)
{
STUBBED("exec");
return 127;
} // MojoPlatform_exec
527
528
529
530
531
int MojoPlatform_runScript(const char *script, boolean devnull, const char **argv)
{
STUBBED("runScript");
return 0;
}
532
533
boolean MojoPlatform_spawnTerminal(void)
534
{
535
assert(!MojoPlatform_istty());
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
if (AllocConsole())
{
int hCrt;
FILE *hf;
hCrt = _open_osfhandle((long) GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
hf = _fdopen( hCrt, "w" );
*stdout = *hf;
setvbuf( stdout, NULL, _IONBF, 0 );
hCrt = _open_osfhandle((long) GetStdHandle(STD_ERROR_HANDLE), _O_TEXT);
hf = _fdopen( hCrt, "w" );
*stderr = *hf;
setvbuf( stderr, NULL, _IONBF, 0 );
hCrt = _open_osfhandle((long) GetStdHandle(STD_INPUT_HANDLE), _O_TEXT);
hf = _fdopen( hCrt, "r" );
*stdin = *hf;
setvbuf( stdin, NULL, _IONBF, 0 );
return true;
} // if
return false;
560
561
562
} // MojoPlatform_spawnTerminal
563
564
565
566
567
568
569
uint8 *MojoPlatform_decodeImage(const uint8 *data, uint32 size,
uint32 *w, uint32 *h)
{
return NULL; // !!! FIXME: try IPicture?
} // MojoPlatform_decodeImage
570
571
572
573
574
575
576
577
578
579
580
581
char *MojoPlatform_currentWorkingDir(void)
{
char *retval = NULL;
WCHAR *wbuf = NULL;
DWORD buflen = 0;
buflen = pGetCurrentDirectoryW(buflen, NULL);
wbuf = (WCHAR *) smallAlloc((buflen + 2) * sizeof (WCHAR));
BAIL_IF_MACRO(wbuf == NULL, ERR_OUT_OF_MEMORY, NULL);
pGetCurrentDirectoryW(buflen, wbuf);
if (wbuf[buflen - 2] == '\\')
582
wbuf[buflen - 1] = '\0'; // just in case...
583
584
585
586
else
{
wbuf[buflen - 1] = '\\';
wbuf[buflen] = '\0';
587
} // else
588
589
590
retval = unicodeToUtf8Heap(wbuf);
smallFree(wbuf);
591
return retval;
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
} // MojoPlatform_currentWorkingDir
char *MojoPlatform_readlink(const char *linkname)
{
STUBBED("should use symlinks (reparse points) on Vista");
return NULL;
} // MojoPlatform_readlink
// !!! FIXME: this code sort of sucks.
char *MojoPlatform_realpath(const char *path)
{
// !!! FIXME: this should return NULL if (path) doesn't exist?
// !!! FIXME: Need to handle symlinks in Vista...
// !!! FIXME: try GetFullPathName() instead?
// this function should be UTF-8 clean.
char *retval = NULL;
char *p = NULL;
if ((path == NULL) || (*path == '\0'))
return NULL;
retval = (char *) xmalloc(MAX_PATH);
618
619
// If in \\server\path format, it's already an absolute path.
// We'll need to check for "." and ".." dirs, though, just in case.
620
621
622
623
624
625
626
627
628
629
630
631
if ((path[0] == '\\') && (path[1] == '\\'))
strcpy(retval, path);
else
{
char *currentDir = MojoPlatform_currentWorkingDir();
if (currentDir == NULL)
{
free(retval);
return NULL;
} // if
632
if (path[1] == ':') // drive letter specified?
633
634
635
636
637
638
639
640
641
642
643
644
645
{
// Apparently, "D:mypath" is the same as "D:\\mypath" if
// D: is not the current drive. However, if D: is the
// current drive, then "D:mypath" is a relative path. Ugh.
if (path[2] == '\\') // maybe an absolute path?
strcpy(retval, path);
else // definitely an absolute path.
{
if (path[0] == currentDir[0]) // current drive; relative.
{
strcpy(retval, currentDir);
strcat(retval, path + 2);
646
} // if
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
else // not current drive; absolute.
{
retval[0] = path[0];
retval[1] = ':';
retval[2] = '\\';
strcpy(retval + 3, path + 2);
} // else
} // else
} // if
else // no drive letter specified.
{
if (path[0] == '\\') // absolute path.
{
retval[0] = currentDir[0];
retval[1] = ':';
strcpy(retval + 2, path);
665
} // if
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
else
{
strcpy(retval, currentDir);
strcat(retval, path);
} // else
} // else
free(currentDir);
} // else
// (whew.) Ok, now take out "." and ".." path entries...
p = retval;
while ( (p = strstr(p, "\\.")) != NULL)
{
// it's a "." entry that doesn't end the string.
if (p[2] == '\\')
memmove(p + 1, p + 3, strlen(p + 3) + 1);
// it's a "." entry that ends the string.
else if (p[2] == '\0')
p[0] = '\0';
// it's a ".." entry.
else if (p[2] == '.')
{
char *prevEntry = p - 1;
while ((prevEntry != retval) && (*prevEntry != '\\'))
prevEntry--;
if (prevEntry == retval) // make it look like a "." entry.
memmove(p + 1, p + 2, strlen(p + 2) + 1);
else
{
if (p[3] != '\0') // doesn't end string.
*prevEntry = '\0';
else // ends string.
memmove(prevEntry + 1, p + 4, strlen(p + 4) + 1);
p = prevEntry;
} // else
} // else if
else
{
p++; // look past current char.
} // else
} // while
// shrink the retval's memory block if possible...
716
return (char *) xrealloc(retval, strlen(retval) + 1);
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
} // MojoPlatform_realpath
char *MojoPlatform_appBinaryPath(void)
{
DWORD buflen = 64;
LPWSTR modpath = NULL;
char *retval = NULL;
DWORD rc = 0;
while (true)
{
void *ptr = xrealloc(modpath, buflen * sizeof(WCHAR));
modpath = (LPWSTR) ptr;
rc = pGetModuleFileNameW(NULL, modpath, buflen);
if (rc == 0)
{
free(modpath);
return NULL;
} // if
if (rc < buflen)
{
buflen = rc;
break;
} // if
buflen *= 2;
} // while
if (buflen > 0) // just in case...
{
WCHAR *ptr = (modpath + buflen) - 1;
while (ptr != modpath)
{
if (*ptr == '\\')
break;
ptr--;
} // while
if ((ptr != modpath) || (*ptr == '\\')) // should always be true...
{
*(ptr + 1) = '\0'; // chop off filename.
retval = unicodeToUtf8Heap(modpath);
} // else
} // else
free(modpath);
767
return retval;
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
} // MojoPlatform_appBinaryPath
// Try to make use of GetUserProfileDirectoryW(), which isn't available on
// some common variants of Win32. If we can't use this, we just punt and
// use the binary path for the user dir, too.
//
// On success, module-scope variable (userDir) will have a pointer to
// a malloc()'d string of the user's profile dir, and a non-zero value is
// returned. If we can't determine the profile dir, (userDir) will
// be NULL, and zero is returned.
char *MojoPlatform_homedir(void)
{
char *userDir = NULL;
// GetUserProfileDirectoryW() is only available on NT 4.0 and later.
// This means Win95/98/ME (and CE?) users have to do without, so for
// them, we'll default to the base directory when we can't get the
// function pointer. Since this is originally an NT API, we don't
// offer a non-Unicode fallback.
if (pGetUserProfileDirectoryW != NULL)
{
HANDLE accessToken = NULL; // Security handle to process
HANDLE processHandle = GetCurrentProcess();
if (OpenProcessToken(processHandle, TOKEN_QUERY, &accessToken))
{
DWORD psize = 0;
WCHAR dummy = 0;
LPWSTR wstr = NULL;
BOOL rc = 0;
// Should fail. Will write the size of the profile path in
// psize. Also note that the second parameter can't be
// NULL or the function fails.
rc = pGetUserProfileDirectoryW(accessToken, &dummy, &psize);
if (rc == 0) // this should always be true!
{
// Allocate memory for the profile directory
wstr = (LPWSTR) smallAlloc(psize * sizeof (WCHAR));
if (wstr != NULL)
{
if (pGetUserProfileDirectoryW(accessToken, wstr, &psize))
userDir = unicodeToUtf8Heap(wstr);
smallFree(wstr);
} // else
} // if
CloseHandle(accessToken);
} // if
} // if
if (userDir == NULL) // couldn't get profile for some reason.
{
// Might just be a non-NT system; try SHGetFolderPathA()...
824
if (pSHGetFolderPathA != NULL) // can be NULL if IE5+ isn't installed!
825
826
{
char shellPath[MAX_PATH];
827
828
HRESULT status = pSHGetFolderPathA(NULL, CSIDL_PERSONAL, NULL,
SHGFP_TYPE_CURRENT, shellPath);
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
if (SUCCEEDED(status))
userDir = codepageToUtf8Heap(shellPath);
} // if
} // if
if (userDir == NULL) // Still nothing?!
{
// this either means we had a catastrophic failure, or we're on a
// Win95 system without at least Internet Explorer 5.0. Bleh!
userDir = xstrdup("C:\\My Documents"); // good luck with that.
} // if
return userDir;
} // MojoPlatform_homedir
// This implementation is a bit naive.
846
char *MojoPlatform_locale(void)
847
848
849
850
851
852
853
854
855
856
857
858
859
{
char lang[16];
char country[16];
const int langrc = GetLocaleInfoA(LOCALE_USER_DEFAULT,
LOCALE_SISO639LANGNAME,
lang, sizeof (lang));
const int ctryrc = GetLocaleInfoA(LOCALE_USER_DEFAULT,
LOCALE_SISO3166CTRYNAME,
country, sizeof (country));
// Win95 systems will fail, because they don't have LOCALE_SISO*NAME ...
860
if ((langrc != 0) && (ctryrc != 0))
861
return format("%0_%1", lang, country);
862
863
return NULL;
864
865
866
} // MojoPlatform_locale
867
char *MojoPlatform_osType(void)
868
869
{
if (osIsWin9x)
870
return xstrdup("win9x");
871
else
872
return xstrdup("winnt");
873
874
return NULL;
875
876
877
} // MojoPlatform_ostype
878
char *MojoPlatform_osVersion(void)
879
{
880
return format("%0.%1.%2",
881
882
883
numstr(osMajorVer),
numstr(osMinorVer),
numstr(osBuildVer));
884
885
886
} // MojoPlatform_osversion
887
888
889
890
891
892
893
char *MojoPlatform_osMachine(void)
{
// !!! FIXME: return "x86" for win32 and "x64" (bleh!) for win64.
return NULL;
} // MojoPlatform_osMachine
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
void MojoPlatform_sleep(uint32 ticks)
{
Sleep(ticks);
} // MojoPlatform_sleep
uint32 MojoPlatform_ticks(void)
{
return GetTickCount() - startupTime;
} // MojoPlatform_ticks
void MojoPlatform_die(void)
{
STUBBED("Win32 equivalent of _exit()?");
_exit(86);
} // MojoPlatform_die
boolean MojoPlatform_unlink(const char *fname)
{
int retval = 0;
LPWSTR wpath;
UTF8_TO_UNICODE_STACK_MACRO(wpath, fname);
if (pGetFileAttributesW(wpath) == FILE_ATTRIBUTE_DIRECTORY)
retval = (pRemoveDirectoryW(wpath) != 0);
else
retval = (pDeleteFileW(wpath) != 0);
smallFree(wpath);
return retval;
} // MojoPlatform_unlink
boolean MojoPlatform_symlink(const char *src, const char *dst)
{
STUBBED("Vista has symlink support");
return false;
} // MojoPlatform_symlink
boolean MojoPlatform_mkdir(const char *path, uint16 perms)
{
// !!! FIXME: error if already exists?
// !!! FIXME: perms?
WCHAR *wpath;
DWORD rc;
UTF8_TO_UNICODE_STACK_MACRO(wpath, path);
rc = pCreateDirectoryW(wpath, NULL);
smallFree(wpath);
return (rc != 0);
} // MojoPlatform_mkdir
boolean MojoPlatform_rename(const char *src, const char *dst)
{
WCHAR *srcwpath;
WCHAR *dstwpath;
BOOL rc;
MojoPlatform_unlink(dst); // to match Unix rename()...
UTF8_TO_UNICODE_STACK_MACRO(srcwpath, src);
UTF8_TO_UNICODE_STACK_MACRO(dstwpath, dst);
rc = pMoveFileW(srcwpath, dstwpath);
smallFree(srcwpath);
smallFree(dstwpath);
return (rc != 0);
} // MojoPlatform_rename
boolean MojoPlatform_exists(const char *dir, const char *fname)
{
WCHAR *wpath;
char *fullpath = NULL;
boolean retval = false;
if (fname == NULL)
fullpath = xstrdup(dir);
else
{
const size_t len = strlen(dir) + strlen(fname) + 1;
fullpath = (char *) xmalloc(len);
snprintf(fullpath, len, "%s\\%s", dir, fname);
} // else
UTF8_TO_UNICODE_STACK_MACRO(wpath, fullpath);
978
retval = (pGetFileAttributesW(wpath) != INVALID_FILE_ATTRIBUTES);
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
smallFree(wpath);
free(fullpath);
return retval;
} // MojoPlatform_exists
boolean MojoPlatform_writable(const char *fname)
{
boolean retval = false;
DWORD attr = 0;
WCHAR *wpath;
UTF8_TO_UNICODE_STACK_MACRO(wpath, fname);
attr = pGetFileAttributesW(wpath);
smallFree(wpath);
if (attr != INVALID_FILE_ATTRIBUTES)
retval = ((attr & FILE_ATTRIBUTE_READONLY) == 0);
return retval;
} // MojoPlatform_writable
boolean MojoPlatform_isdir(const char *dir)