This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathdskw32.cpp
More file actions
3888 lines (3326 loc) · 116 KB
/
dskw32.cpp
File metadata and controls
3888 lines (3326 loc) · 116 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
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
716
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
767
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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
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
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "prefix.h"
#include <AclAPI.h>
#ifdef DeleteFile
#undef DeleteFile
#endif // DeleteFile
#ifdef GetCurrentTime
#undef GetCurrentTime
#endif // GetCurrentTime
#include "globdefs.h"
#include "parsedef.h"
#include "filedefs.h"
#include "objdefs.h"
#include "mcio.h"
#include "system.h"
#include "exec.h"
#include "globals.h"
#include "system.h"
#include "osspec.h"
#include "mcerror.h"
#include "util.h"
#include "mcio.h"
#include "stack.h"
#include "handler.h"
#include "dispatch.h"
#include "card.h"
#include "group.h"
#include "button.h"
#include "param.h"
#include "mode.h"
#include "securemode.h"
#include "scriptenvironment.h"
#include "text.h"
#include "notify.h"
#include "socket.h"
#include "w32dc.h"
#include "w32dsk-legacy.h"
#include <locale.h>
#include <sys/stat.h>
#include <time.h>
#include <Winsock2.h>
#include <Mstcpip.h>
#include <Mswsock.h>
#include <Shlobj.h>
#include <sys\Timeb.h>
#include <Iphlpapi.h>
#include <process.h>
#include <signal.h>
#include <io.h>
#include <strsafe.h>
#include <Shlwapi.h>
//////////////////////////////////////////////////////////////////////////////////
int *g_mainthread_errno;
//////////////////////////////////////////////////////////////////////////////////
#define PATH_DELIMITER '\\'
//////////////////////////////////////////////////////////////////////////////////
extern bool MCFiltersUrlEncode(MCStringRef p_source, bool p_use_utf8, MCStringRef& r_result);
extern bool MCStringsSplit(MCStringRef p_string, codepoint_t p_separator, MCStringRef*&r_strings, uindex_t& r_count);
//////////////////////////////////////////////////////////////////////////////////
// This function is used to modify paths so that lengths > MAX_PATH characters
// are supported. It does, however, disable parsing of . and .. as well as
// converting / to \ so these must be done before calling this
static void legacy_path_to_nt_path(MCStringRef p_legacy, MCStringRef &r_nt)
{
// Don't do anything if it isn't necessary
if (MCStringGetLength(p_legacy) < (MAX_PATH - 1))
{
r_nt = MCValueRetain(p_legacy);
return;
}
// UNC and local paths are treated differently
if (MCStringGetCharAtIndex(p_legacy, 0) == '\\' && MCStringGetCharAtIndex(p_legacy, 1) == '\\')
{
// Check that an explicit NT path isn't already being specified
if (MCStringGetCharAtIndex(p_legacy, 2) == '?' // Legacy paths namespace
|| MCStringGetCharAtIndex(p_legacy, 2) == '.') // Device paths namespace
{
r_nt = MCValueRetain(p_legacy);
return;
}
// UNC path
MCStringRef t_temp;
/* UNCHECKED */ MCStringCreateMutable(0, t_temp);
/* UNCHECKED */ MCStringAppend(t_temp, MCSTR("\\\\?\\UNC\\"));
/* UNCHECKED */ MCStringAppendSubstring(t_temp, p_legacy, MCRangeMakeMinMax(2, MCStringGetLength(p_legacy)));
/* UNCHECKED */ MCStringCopyAndRelease(t_temp, r_nt);
}
else
{
// Local path
/* UNCHECKED */ MCStringFormat(r_nt, "\\\\?\\%@", p_legacy);
}
}
// This function strips the leading \\?\ or \\?\UNC\ from an NT-style path
static void nt_path_to_legacy_path(MCStringRef p_nt, MCStringRef &r_legacy)
{
// Check for the leading NT path characters
if (MCStringBeginsWithCString(p_nt, (const char_t*)"\\\\?\\UNC\\", kMCStringOptionCompareCaseless))
{
MCStringRef t_temp;
/* UNCHECKED */ MCStringMutableCopySubstring(p_nt, MCRangeMakeMinMax(8, MCStringGetLength(p_nt)), t_temp);
/* UNCHECKED */ MCStringPrepend(t_temp, MCSTR("\\\\"));
/* UNCHECKED */ MCStringCopyAndRelease(t_temp, r_legacy);
}
else if (MCStringBeginsWithCString(p_nt, (const char_t*)"\\\\?\\", kMCStringOptionCompareCaseless))
{
/* UNCHECKED */ MCStringCopySubstring(p_nt, MCRangeMakeMinMax(4, MCStringGetLength(p_nt)), r_legacy);
}
else
{
// Not an NT-style path, no changes required.
r_legacy = MCValueRetain(p_nt);
}
}
static bool get_device_path(MCStringRef p_path, MCStringRef &r_device_path)
{
// Device paths are opened as "COM<n>:" where <n> is an integer
if (MCStringBeginsWithCString(p_path, (const char_t*)"COM", kMCStringOptionCompareCaseless)
&& MCStringGetCharAtIndex(p_path, MCStringGetLength(p_path) - 1) == ':')
{
// Serial ports live in the NT device namespace (but don't have a ':' suffix)
MCStringRef t_temp;
return MCStringCreateMutable(0, t_temp)
&& MCStringAppend(t_temp, MCSTR("\\\\.\\"))
&& MCStringAppendSubstring(t_temp, p_path, MCRangeMake(0, MCStringGetLength(p_path) - 1))
&& MCStringCopyAndRelease(t_temp, r_device_path);
}
else
return MCStringCopy(p_path, r_device_path);
}
//////////////////////////////////////////////////////////////////////////////////
// MW-2005-02-22: Make these global for opensslsocket.cpp
static Boolean wsainited = False;
HWND sockethwnd;
HANDLE g_socket_wakeup;
Boolean wsainit()
{
if (!wsainited)
{
WORD request = MAKEWORD(1,1);
WSADATA wsaData;
if (WSAStartup(request, (LPWSADATA)&wsaData))
MCresult->sets("can't find a usable winsock.dll");
else
{
wsainited = True;
// OK-2009-02-24: [[Bug 7628]]
MCresult -> sets("");
#ifdef _WINDOWS_DESKTOP
if (!MCnoui)
sockethwnd = CreateWindowA(MC_WIN_CLASS_NAME, "MCsocket", WS_POPUP, 0, 0,
8, 8, NULL, NULL, MChInst, NULL);
#endif
}
}
MCS_seterrno(0);
return wsainited;
}
static bool read_blob_from_pipe(HANDLE p_pipe, void*& r_data, uint32_t& r_data_length)
{
uint32_t t_amount;
DWORD t_read;
if (!ReadFile(p_pipe, &t_amount, sizeof(uint32_t), &t_read, NULL) ||
t_read != sizeof(uint32_t))
return false;
void *t_buffer;
t_buffer = malloc(t_amount);
if (t_buffer == nil)
return false;
if (!ReadFile(p_pipe, t_buffer, t_amount, &t_read, NULL) ||
t_read != t_amount)
return false;
r_data = t_buffer;
r_data_length = t_amount;
return true;
}
//////////////////////////////////////////////////////////////////////////////////
static bool write_blob_to_pipe(HANDLE p_pipe, uint32_t p_count, const void *p_data)
{
DWORD t_written;
if (!WriteFile(p_pipe, &p_count, sizeof(p_count), &t_written, NULL) ||
t_written != 4)
return false;
if (!WriteFile(p_pipe, p_data, p_count, &t_written, NULL) ||
t_written != p_count)
return false;
return true;
}
// MW-2004-11-28: A null FPE signal handler.
static void handle_fp_exception(int p_signal)
{
p_signal = p_signal;
}
static bool handle_is_pipe(MCWinSysHandle p_handle)
{
DWORD t_flags;
int t_result;
t_result = GetNamedPipeInfo((HANDLE)p_handle, &t_flags, NULL, NULL, NULL);
return t_result != 0;
}
// MW-2010-05-11: This call is only present on Vista and above, which is the place we
// need it - so weakly bind.
typedef DWORD (APIENTRY *GetProcessIdOfThreadPtr)(HANDLE thread);
static DWORD DoGetProcessIdOfThread(HANDLE p_thread)
{
// We can safely assume that the kernel dll is loaded!
HMODULE t_module;
t_module = GetModuleHandleA("Kernel32.dll");
if (t_module == NULL)
return -1;
// Resolve the symbol
void *t_ptr;
t_ptr = GetProcAddress(t_module, "GetProcessIdOfThread");
if (t_ptr == NULL)
return -1;
// Call it
return ((GetProcessIdOfThreadPtr)t_ptr)(p_thread);
}
//////////////////////////////////////////////////////////////////////////////////
typedef struct
{ //struct for WIN registry
const char *token;
HKEY key;
uint32_t mode;
}
reg_keytype;
static reg_keytype Regkeys[] = { //WIN registry root keys struct
{"HKEY_CLASSES_ROOT", HKEY_CLASSES_ROOT, 0},
{"HKEY_CURRENT_USER", HKEY_CURRENT_USER, 0},
{"HKEY_LOCAL_MACHINE", HKEY_LOCAL_MACHINE, 0},
{"HKEY_LOCAL_MACHINE_32", HKEY_LOCAL_MACHINE, KEY_WOW64_32KEY},
{"HKEY_LOCAL_MACHINE_64", HKEY_LOCAL_MACHINE, KEY_WOW64_64KEY},
{"HKEY_USERS", HKEY_USERS, 0},
{"HKEY_PERFORMANCE_DATA", HKEY_PERFORMANCE_DATA, 0},
{"HKEY_CURRENT_CONFIG", HKEY_CURRENT_CONFIG, 0},
{"HKEY_DYN_DATA", HKEY_DYN_DATA, 0}
};
typedef struct
{
const char *token;
DWORD type;
}
reg_datatype;
static reg_datatype RegDatatypes[] = { //WIN registry value types struct
{"binary", REG_BINARY},
{"dword", REG_DWORD},
{"dwordlittleendian", REG_DWORD_LITTLE_ENDIAN},
{"dwordbigendian", REG_DWORD_BIG_ENDIAN},
{"expandsz", REG_EXPAND_SZ},
{"link", REG_LINK},
{"multisz", REG_MULTI_SZ},
{"none", REG_NONE},
{"resourcelist", REG_RESOURCE_LIST},
{"string", REG_SZ},
{"sz", REG_SZ},
{"qword", REG_QWORD},
{"qwordlittleendian", REG_QWORD_LITTLE_ENDIAN}
};
bool MCS_registry_split_key(MCStringRef p_path, MCStringRef& r_root, MCStringRef& r_key)
{
uindex_t t_length = MCStringGetLength(p_path);
uindex_t t_offset = t_length;
if (!MCStringFirstIndexOfChar(p_path, '\\', 0, kMCStringOptionCompareExact, t_offset))
{
t_offset = t_length;
}
return MCStringCopySubstring(p_path, MCRangeMake(0, t_offset), r_root) &&
MCStringCopySubstring(p_path, MCRangeMake(t_offset + 1, t_length), r_key);
}
bool MCS_registry_split_key(MCStringRef p_path, MCStringRef& r_root, MCStringRef& r_key, MCStringRef& r_value)
{
// only copy components out if they are present - <ROOT>\<KEY>\<VALUE>, <ROOT>\<VALUE>, <ROOT>
// (components may be empty)
bool t_success = true;
uindex_t t_length = MCStringGetLength(p_path);
uindex_t t_path_offset = t_length;
uindex_t t_value_offset = t_length;
if (MCStringLastIndexOfChar(p_path, '\\', t_length, kMCStringOptionCompareExact, t_value_offset))
{
if (MCStringFirstIndexOfChar(p_path, '\\', 0, kMCStringOptionCompareExact, t_path_offset))
{
if (t_value_offset > t_path_offset)
t_success = t_success && MCStringCopySubstring(p_path, MCRangeMakeMinMax(t_path_offset + 1, t_value_offset), r_key);
}
t_success = t_success && MCStringCopySubstring(p_path, MCRangeMakeMinMax(t_value_offset + 1, t_length), r_value);
}
return t_success && MCStringCopySubstring(p_path, MCRangeMake(0, t_path_offset), r_root);
}
bool MCS_registry_root_to_hkey(MCStringRef p_root, HKEY& r_hkey)
{
for (uindex_t i = 0 ; i < ELEMENTS(Regkeys) ; i++)
{
if (MCStringIsEqualToCString(p_root, Regkeys[i].token, kMCCompareCaseless))
{
r_hkey = Regkeys[i].key;
return true;
}
}
return false;
}
bool MCS_registry_root_to_hkey(MCStringRef p_root, HKEY& r_hkey, uint32_t& x_access_mode)
{
for (uindex_t i = 0 ; i < ELEMENTS(Regkeys) ; i++)
{
if (MCStringIsEqualToCString(p_root, Regkeys[i].token, kMCCompareCaseless))
{
r_hkey = Regkeys[i].key;
if (MCmajorosversion >= MCOSVersionMake(5,1,0))
x_access_mode |= Regkeys[i].mode;
return true;
}
}
return false;
}
bool MCS_registry_type_to_string(uint32_t p_type, MCStringRef& r_string)
{
for (uindex_t i = 0 ; i < ELEMENTS(RegDatatypes) ; i++)
{
if (p_type == RegDatatypes[i].type)
{
r_string = MCSTR(RegDatatypes[i].token);
return true;
}
}
// SN-2014-11-18: [[ Bug 14052 ]] Avoid to return a nil string (the result is not checked anyway).
r_string = MCValueRetain(kMCEmptyString);
return false;
}
MCSRegistryValueType MCS_registry_type_from_string(MCStringRef p_string)
{
if (p_string != nil)
{
for (uindex_t i = 0; i < ELEMENTS(RegDatatypes); i++)
{
if (MCStringIsEqualToCString(p_string, RegDatatypes[i].token, kMCCompareCaseless))
return (MCSRegistryValueType)RegDatatypes[i].type;
}
}
return kMCSRegistryValueTypeSz;
}
class MCAutoRegistryKey
{
public:
MCAutoRegistryKey(void)
{
m_key = NULL;
}
~MCAutoRegistryKey(void)
{
if (m_key != NULL)
RegCloseKey(m_key);
}
HKEY operator = (HKEY value)
{
MCAssert(m_key == NULL);
m_key = value;
return m_key;
}
HKEY* operator & (void)
{
MCAssert(m_key == NULL);
return &m_key;
}
HKEY operator * (void) const
{
return m_key;
}
private:
HKEY m_key;
};
///////////////////////////////////////////////////////////////////////////////
static MMRESULT tid;
void CALLBACK MCS_tp(UINT id, UINT msg, DWORD_PTR user, DWORD_PTR dw1, DWORD_PTR dw2)
{
MCalarm = True;
}
///////////////////////////////////////////////////////////////////////////////
#define DEFINE_GUID_(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
EXTERN_C const GUID DECLSPEC_SELECTANY name \
= { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
// {F0B7A1A2-9847-11cf-8F20-00805F2CD064}
DEFINE_GUID_(CATID_ActiveScriptParse, 0xf0b7a1a2, 0x9847, 0x11cf, 0x8f, 0x20, 0x00, 0x80, 0x5f, 0x2c, 0xd0, 0x64);
///////////////////////////////////////////////////////////////////////////////
typedef struct
{
MCNameRef *token;
uint4 winfolder;
}
sysfolders;
// need to add at least a temp folder and the system folder here
static sysfolders sysfolderlist[] = {
{&MCN_desktop, CSIDL_DESKTOP},
{&MCN_fonts, CSIDL_FONTS},
{&MCN_documents, CSIDL_PERSONAL},
{&MCN_start, CSIDL_STARTMENU},
{&MCN_system, CSIDL_WINDOWS},
{&MCN_home, CSIDL_PROFILE}, //TS-2007-08-20 Added so that "home" gives something useful across all platforms
{&MCN_support, CSIDL_APPDATA},
};
bool MCS_specialfolder_to_csidl(MCNameRef p_folder, MCNumberRef& r_csidl)
{
for (uindex_t i = 0 ; i < ELEMENTS(sysfolderlist) ; i++)
{
if (MCNameIsEqualToCaseless(p_folder, *(sysfolderlist[i].token)))
{
return MCNumberCreateWithUnsignedInteger(sysfolderlist[i].winfolder, r_csidl);
}
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
/* thread created to read data from child process's pipe */
static bool s_finished_reading = false;
static void readThreadDone(void *param)
{
s_finished_reading = true;
}
static DWORD readThread(Streamnode *process)
{
DWORD nread;
uint32_t t_bufsize = READ_PIPE_SIZE;
char* t_buffer = new (nothrow) char[t_bufsize];
while (process -> ihandle != NULL)
{
if (!PeekNamedPipe(process -> ihandle -> GetFilePointer(), NULL, 0, NULL, &nread, NULL))
break;
if (nread == 0)
{
if (WaitForSingleObject(process -> phandle, 0) != WAIT_TIMEOUT)
break;
Sleep(100);
continue;
}
if (!ReadFile(process -> ihandle -> GetFilePointer(), (LPVOID)t_buffer,
t_bufsize, &nread, NULL)
|| nread == 0)
break;
/* UNCHECKED */ process -> ohandle -> Write(t_buffer, nread);
}
delete[] t_buffer;
MCNotifyPush(readThreadDone, nil, false, false);
return 0;
}
//////////////////////////////////////////////////////////////////////////////////
bool MCS_getcurdir_native(MCStringRef& r_path)
{
// NOTE:
// Get/SetCurrentDirectory are not supported by Windows in multithreaded environments
MCAutoArray<unichar_t> t_buffer;
// Retrieve the length of the current directory
DWORD t_path_len = GetCurrentDirectoryW(0, NULL);
/* UNCHECKED */ t_buffer.New(t_path_len);
DWORD t_result = GetCurrentDirectoryW(t_path_len, t_buffer.Ptr());
if (t_result == 0 || t_result >= t_path_len)
{
// Something went wrong
return false;
}
return MCStringCreateWithChars(t_buffer.Ptr(), t_result, r_path);
}
bool MCS_native_path_exists(MCStringRef p_path, bool p_is_file)
{
MCAutoStringRefAsWString t_path_wstr;
/* UNCHECKED */ t_path_wstr.Lock(p_path);
// MW-2010-10-22: [[ Bug 8259 ]] Use a proper Win32 API function - otherwise network shares don't work.
DWORD t_attrs;
t_attrs = GetFileAttributesW(*t_path_wstr);
if (t_attrs == INVALID_FILE_ATTRIBUTES)
return false;
return p_is_file == ((t_attrs & FILE_ATTRIBUTE_DIRECTORY) == 0);
}
bool MCS_path_exists(MCStringRef p_path, bool p_is_file)
{
// MW-2004-04-20: [[ Purify ]] If *newpath == 0 then we should return False
if (MCStringGetLength(p_path) == 0)
return False;
// MW-2008-01-15: [[ Bug 4981 ]] - It seems that stat will fail for checking
// a folder 'C:' and requires that it be 'C:\'
// TODO: still necessary with GetFileAttributes instead of stat?
/*if (MCStringGetLength(p_path) == 2 && MCStringGetCharAtIndex(p_path, 1) == ':')
{
MCAutoStringRef t_drive_string;
return MCStringMutableCopy(p_path, &t_drive_string) &&
MCStringAppendChar(*t_drive_string, '\\') &&
MCS_native_path_exists(*t_drive_string, p_is_file);
}*/
// OK-2007-12-05 : Bug 5555, modified to allow paths with trailing backslashes on Windows.
// TODO: still necessary with GetFileAttributes instead of stat?
uindex_t t_path_len = MCStringGetLength(p_path);
unichar_t t_last_char = MCStringGetCharAtIndex(p_path, t_path_len - 1);
if ((t_last_char == '\\' || t_last_char == '/') &&
!(t_path_len == 3 && MCStringGetCharAtIndex(p_path, 1) == ':'))
{
MCAutoStringRef t_trimmed_string;
return MCStringCopySubstring(p_path, MCRangeMake(0, t_path_len - 1), &t_trimmed_string) &&
MCS_native_path_exists(*t_trimmed_string, p_is_file);
}
return MCS_native_path_exists(p_path, p_is_file);
}
bool MCS_path_append(MCStringRef p_base, unichar_t p_separator, MCStringRef p_component, MCStringRef& r_path)
{
MCAutoStringRef t_path;
if (!MCStringMutableCopy(p_base, &t_path))
return false;
if (MCStringGetCharAtIndex(p_base, MCStringGetLength(p_base) - 1) != p_separator &&
!MCStringAppendChars(*t_path, &p_separator, 1))
return false;
if (!MCStringAppend(*t_path, p_component))
return false;
r_path = MCValueRetain(*t_path);
return true;
}
//////////////////////////////////////////////////////////////////////////////////
static inline bool is_legal_drive(char p_char)
{
return (p_char >= 'a' && p_char <= 'z') || (p_char >= 'A' && p_char <= 'Z');
}
//////////////////////////////////////////////////////////////////////////////////
bool dns_servers_from_network_params(MCListRef& r_list)
{
MCAutoListRef t_list;
ULONG t_buffer_size = 0;
MCAutoBlock<byte_t> t_buffer;
FIXED_INFO *t_fixed_info = nil;
errno = GetNetworkParams(t_fixed_info, &t_buffer_size);
if (errno == ERROR_NO_DATA)
{
r_list = MCValueRetain(kMCEmptyList);
return true;
}
else if (errno != ERROR_BUFFER_OVERFLOW)
return false;
if (!t_buffer.Allocate(t_buffer_size))
return false;
if (!MCListCreateMutable('\n', &t_list))
return false;
t_fixed_info = (FIXED_INFO*)*t_buffer;
MCMemoryClear(t_fixed_info, t_buffer_size);
errno = GetNetworkParams(t_fixed_info, &t_buffer_size);
if (errno == ERROR_SUCCESS)
{
IP_ADDR_STRING *t_addr_string = &t_fixed_info->DnsServerList;
while (t_addr_string != nil && t_addr_string->IpAddress.String[0] != '\0')
{
if (!MCListAppendCString(*t_list, t_addr_string->IpAddress.String))
return false;
t_addr_string = t_addr_string->Next;
}
}
return MCListCopy(*t_list, r_list);
}
#define NAMESERVER_REG_KEY "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\NameServer"
bool dns_servers_from_registry(MCListRef& r_list)
{
MCAutoStringRef t_key, t_type, t_error;
MCAutoValueRef t_value;
t_key = MCSTR(NAMESERVER_REG_KEY);
if (!MCS_query_registry(*t_key, &t_value, &t_type, &t_error))
return false;
if (!MCStringIsEmpty(*t_error))
{
r_list = MCValueRetain(kMCEmptyList);
return true;
}
// If the type is wrong, abort
if (MCValueGetTypeCode(*t_value) != kMCValueTypeCodeString)
return false;
MCStringRef t_string = (MCStringRef)*t_value;
MCAutoListRef t_list;
if (!MCListCreateMutable('\n', &t_list))
return false;
const unichar_t *t_chars;
uindex_t t_char_count;
t_chars = MCStringGetCharPtr(t_string);
t_char_count = MCStringGetLength(t_string);
uindex_t t_start = 0;
for (uindex_t i = 0; i < t_char_count; i++)
{
if (t_chars[i] == ' ' || t_chars[i] == ',' || t_chars[i] == '\n')
{
MCAutoStringRef t_substring;
if (!MCStringCopySubstring(t_string, MCRangeMakeMinMax(t_start, i), &t_substring) ||
!MCListAppend(*t_list, *t_substring))
return false;
t_start = i + 1;
}
}
if (t_start < t_char_count)
{
MCAutoStringRef t_final_string;
if (!MCStringCopySubstring(t_string, MCRangeMakeMinMax(t_start, MCStringGetLength(t_string)), &t_final_string) ||
!MCListAppend(*t_list, *t_final_string))
return false;
}
return MCListCopy(*t_list, r_list);
}
//////////////////////////////////////////////////////////////////////////////////
static void MCS_do_launch(MCStringRef p_document)
{
MCAutoStringRefAsWString t_document_wstr;
/* UNCHECKED */ t_document_wstr.Lock(p_document);
// MW-2011-02-01: [[ Bug 9332 ]] Adjust the 'show' hint to normal to see if that makes
// things always appear on top...
int t_result;
t_result = (int)ShellExecuteW(NULL, L"open", *t_document_wstr, NULL, NULL, SW_SHOWNORMAL);
if (t_result < 32)
{
switch(t_result)
{
case ERROR_BAD_FORMAT:
case SE_ERR_ACCESSDENIED:
case SE_ERR_FNF:
case SE_ERR_PNF:
case SE_ERR_SHARE:
case SE_ERR_DLLNOTFOUND:
MCresult -> sets("can't open file");
break;
case SE_ERR_ASSOCINCOMPLETE:
case SE_ERR_NOASSOC:
MCresult -> sets("no association");
break;
case SE_ERR_DDEBUSY:
case SE_ERR_DDEFAIL:
case SE_ERR_DDETIMEOUT:
MCresult -> sets("request failed");
break;
case 0:
case SE_ERR_OOM:
MCresult -> sets("no memory");
break;
}
}
else
MCresult -> clear();
}
//////////////////////////////////////////////////////////////////////////////////
struct MCWindowsSystemService: public MCWindowsSystemServiceInterface
{
virtual bool MCISendString(MCStringRef p_command, MCStringRef& r_result, bool& r_error)
{
MCAutoStringRefAsWString t_command_wstr;
/* UNCHECKED */ t_command_wstr.Lock(p_command);
DWORD t_error_code;
WCHAR t_buffer[256];
t_buffer[0] = '\0';
t_error_code = mciSendStringW(*t_command_wstr, t_buffer, 255, NULL);
r_error = t_error_code != 0;
if (!r_error)
{
size_t t_length;
if (SUCCEEDED(StringCchLengthW(t_buffer, 256, &t_length)))
return MCStringCreateWithChars(t_buffer, t_length, r_result);
}
size_t t_length;
return mciGetErrorStringW(t_error_code, t_buffer, 255) &&
SUCCEEDED(StringCchLengthW(t_buffer, 256, &t_length)) &&
MCStringCreateWithChars(t_buffer, t_length, r_result);
}
virtual bool QueryRegistry(MCStringRef p_key, MCValueRef& r_value, MCStringRef& r_type, MCStringRef& r_error)
{
MCAutoStringRef t_root, t_key, t_value;
r_value = r_error = nil;
//key = full path info such as: HKEY_LOCAL_MACHINE\\Software\\..\\valuename
if (!MCS_registry_split_key(p_key, &t_root, &t_key, &t_value))
return false;
if (*t_value == nil)
{
/* RESULT */ //MCresult->sets("no key");
r_error = MCSTR("no key");
r_value = MCValueRetain(kMCEmptyString);
return true;
}
if (*t_key == nil)
t_key = kMCEmptyString;
HKEY t_hkey;
MCAutoRegistryKey t_regkey;
uint32_t t_access_mode = KEY_READ;
MCAutoStringRefAsWString t_key_wstr;
/* UNCHECKED */ t_key_wstr.Lock(*t_key);
if (!MCS_registry_root_to_hkey(*t_root, t_hkey, t_access_mode) ||
(RegOpenKeyExW(t_hkey, *t_key_wstr, 0, t_access_mode, &t_regkey) != ERROR_SUCCESS))
{
/* RESULT */ //MCresult->sets("bad key");
r_error = MCSTR("bad key");
return true;
}
LONG err = 0;
MCAutoArray<BYTE> t_buffer;
DWORD t_buffer_len = 0;
DWORD t_type;
MCAutoStringRefAsWString t_value_wstr;
/* UNCHECKED */ t_value_wstr.Lock(*t_value);
//determine the size of value buffer needed
err = RegQueryValueExW(*t_regkey, *t_value_wstr, 0, NULL, NULL, &t_buffer_len);
if (err == ERROR_SUCCESS)
{
if (!t_buffer.New(t_buffer_len))
return false;
if ((err = RegQueryValueExW(*t_regkey, *t_value_wstr, 0, &t_type, t_buffer.Ptr(), &t_buffer_len))
== ERROR_SUCCESS && t_buffer_len > 0)
{
// Sets the type
MCS_registry_type_to_string(t_type, r_type);
// Convert the value from the registry to the appropriate value type
switch (t_type)
{
case REG_LINK:
if (!MCStringCreateWithChars((const unichar_t*)t_buffer.Ptr(), t_buffer_len/2, (MCStringRef&)r_value))
return false;
break;
case REG_EXPAND_SZ:
case REG_MULTI_SZ:
case REG_SZ:
{
DWORD t_unicode_len;
t_unicode_len = t_buffer_len / 2;
unichar_t *t_chars = (unichar_t*)t_buffer.Ptr();
// Get rid of any trailing NULL character
while(t_unicode_len > 0 && t_chars[t_unicode_len - 1] == '\0')
t_unicode_len -= 1;
if (t_type == REG_MULTI_SZ && t_unicode_len < t_buffer_len / 2)
t_unicode_len += 1;
return MCStringCreateWithChars((unichar_t*)t_buffer.Ptr(), t_unicode_len, (MCStringRef&)r_value);
}
case REG_NONE:
{
// No data. Return an empty string ref as the nearest equivalent
r_value = MCValueRetain(kMCEmptyString);
break;
}
case REG_DWORD:
case REG_QWORD:
case REG_BINARY:
default:
{
// Binary or unsupported type. Return as a binary blob
// (For compatibility with existing scripts, DWORD and QWORD are binary)
MCAutoDataRef t_data;
if (!MCDataCreateWithBytes((const byte_t*)t_buffer.Ptr(), t_buffer_len, (MCDataRef&)t_data))
return false;
r_value = MCValueRetain(*t_data);
break;
}
}
}
}
else if (err == ERROR_FILE_NOT_FOUND)
{
// The query may have been for a value that is really a key, either
// with or without a default/un-named value attached to it. Try
// opening the path to the value as a key.
MCAutoStringRef t_alt_key;
MCAutoStringRefAsWString t_alt_key_wstr;
if (MCStringIsEmpty(*t_key))
t_alt_key = *t_value;
else
/* UNCHECKED */ MCStringFormat(&t_alt_key, "%@\\%@", *t_key, *t_value);
/* UNCHECKED */ t_alt_key_wstr.Lock(*t_alt_key);
MCAutoRegistryKey t_alt_regkey;
err = RegOpenKeyExW(t_hkey, *t_alt_key_wstr, 0, t_access_mode, &t_alt_regkey);
if (err == ERROR_SUCCESS)
{
// Key exists
r_value = MCValueRetain(kMCEmptyString);
MCS_registry_type_to_string(REG_NONE, r_type);
}
}
if (err != ERROR_SUCCESS)
{
errno = err;
r_error = MCSTR("can't find key");
return true;
}
return true;
}
virtual bool SetRegistry(MCStringRef p_key, MCValueRef p_value, MCSRegistryValueType p_type, MCStringRef& r_error)
{
MCAutoStringRef t_root, t_key, t_value;
HKEY t_root_hkey;
if (!MCS_registry_split_key(p_key, &t_root, &t_key, &t_value))
return false;
if (!MCS_registry_root_to_hkey(*t_root, t_root_hkey))
{
/* RESULT */ //MCresult->sets("bad key");
r_error = MCSTR("bad key");
return true;
}
if (*t_key == nil)
{
/* RESULT */ //MCresult->sets("bad key specified");
r_error = MCSTR("bad key specified");
return true;
}
MCAutoRegistryKey t_regkey;
DWORD t_keystate;
MCAutoStringRefAsWString t_key_wstr;
/* UNCHECKED */ t_key_wstr.Lock(*t_key);
if (RegCreateKeyExW(t_root_hkey, *t_key_wstr, 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, &t_regkey, &t_keystate) != ERROR_SUCCESS)
{
MCS_seterrno(GetLastError());
/* RESULT */ //MCresult->sets("can't create key");
r_error = MCSTR("can't create key");
return true;
}
MCAutoStringRefAsWString t_value_wstr;
/* UNCHECKED */ t_value_wstr.Lock(*t_value);
if (MCValueIsEmpty(p_value))
{//delete this value
if ((errno = RegDeleteValueW(*t_regkey, *t_value_wstr)) != ERROR_SUCCESS)
{
MCS_seterrno(GetLastError());
/* RESULT */ //MCresult->sets("can't delete value");
r_error = MCSTR("can't delete value");
}
return true;
}
else
{
// The type enumeration is designed to have values equivalent to the REG_* values
DWORD t_type;
t_type = (DWORD)p_type;
switch (t_type)
{
case REG_SZ:
case REG_EXPAND_SZ:
case REG_MULTI_SZ:
case REG_LINK:
{
// Expecting a StringRef type
if (MCValueGetTypeCode(p_value) != kMCValueTypeCodeString)
return false;
MCAutoStringRefAsWString t_wstring;