-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcygwrun.c
1439 lines (1324 loc) · 34.6 KB
/
cygwrun.c
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
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <errno.h>
#include <process.h>
#include <fcntl.h>
#include <io.h>
#include "cygwrun.h"
#define WNUL L'\0'
#define IS_PSW(_c) (((_c) == L'/') || ((_c) == L'\\'))
#define IS_EMPTY_WCS(_s) (((_s) == NULL) || (*(_s) == WNUL))
#define IS_VALID_WCS(_s) (((_s) != NULL) && (*(_s) != WNUL))
static int xrunexec = 1;
static int xdumparg = 0;
static int xdumpenv = 0;
static int xskipenv = 0;
static int xskiparg = 0;
static int xshowerr = 1;
static int xforcewp = 0;
static int xrmendps = 1;
static int xisbatch = 0;
static int xwoptind = 1; /* Index into parent argv vector */
static int xwoption = 0; /* Character checked for validity */
static const wchar_t *xwoptarg = NULL;
static wchar_t *posixroot = NULL;
static wchar_t **xrawenvs = NULL;
static const wchar_t *pathmatches[] = {
L"/cygdrive/?/+*",
L"/bin/*",
L"/dev/*",
L"/etc/*",
L"/home/*",
L"/lib/*",
L"/lib64/*",
L"/opt/*",
L"/root/*",
L"/run/*",
L"/sbin/*",
L"/tmp/*",
L"/usr/*",
L"/var/*",
NULL
};
static const wchar_t *pathfixed[] = {
L"/bin",
L"/dev",
L"/etc",
L"/home",
L"/lib",
L"/lib64",
L"/opt",
L"/root",
L"/run",
L"/sbin",
L"/tmp",
L"/usr",
L"/var",
NULL
};
static const wchar_t *removeext[] = {
L"OLDPWD",
L"ORIGINAL_PATH",
L"ORIGINAL_TEMP",
L"ORIGINAL_TMP",
NULL
};
static const wchar_t *specialenv = L"!::,!;,PS1,";
static const wchar_t *removeenv[] = {
L"_",
L"CYGWIN_ROOT",
L"PATH",
L"POSIX_ROOT",
L"PWD",
L"TEMP",
L"TMP",
NULL
};
static const wchar_t *posixrenv[] = {
L"POSIX_ROOT",
L"CYGWIN_ROOT",
NULL
};
const char xvalidvarname[128] =
{
/** Reject all ctrl codes... */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
/** ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0, 1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
/** @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ */
0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,1,
/** ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ */
0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0
};
static wchar_t zerostring[4] = { WNUL, WNUL, WNUL, WNUL };
static int usage(int rv)
{
if (xshowerr) {
FILE *os = rv == 0 ? stdout : stderr;
fputs("\nUsage " PROJECT_NAME " [OPTIONS]... PROGRAM [ARGUMENTS]...\n", os);
fputs("Execute PROGRAM [ARGUMENTS]...\n\nOptions are:\n", os);
fputs(" -r <DIR> use DIR as posix root\n", os);
fputs(" -w <DIR> change working directory to DIR before calling PROGRAM\n", os);
fputs(" -k keep extra posix environment variables.\n", os);
fputs(" -K keep trailing path separators for paths.\n", os);
fputs(" -f convert all unknown posix absolute paths\n", os);
fputs(" -n <ENV> do not translate ENV variable(s)\n", os);
fputs(" multiple variables are comma separated.\n", os);
fputs(" -s do not translate environment variables.\n", os);
fputs(" -S do not translate arguments.\n", os);
fputs(" -q do not print errors to stderr.\n", os);
fputs(" -v print version information and exit.\n", os);
fputs(" -V print detailed version information and exit.\n", os);
fputs(" -h | -? print this screen and exit.\n", os);
fputs(" -p print arguments instead executing PROGRAM.\n", os);
fputs(" -e print environment variables matching ARGUMENTS\n", os);
fputs(" if no ARGUMENTS are provided print all variables.\n", os);
fputs("To file bugs, visit " PROJECT_URL, os);
}
return rv;
}
static int version(int verbose)
{
if (verbose) {
fputs(PROJECT_NAME " version " PROJECT_VERSION_STR \
" (" __DATE__ " " __TIME__ ")\n", stdout);
fputs("\n" PROJECT_LICENSE "\n\n", stdout);
fputs("Visit " PROJECT_URL " for more details", stdout);
}
else {
fputs(PROJECT_NAME " version " PROJECT_VERSION_STR, stdout);
}
return 0;
}
static wchar_t *xwmalloc(size_t size)
{
wchar_t *p = (wchar_t *)malloc((size + 1) * sizeof(wchar_t));
if (p == NULL) {
_wperror(L"xwmalloc");
_exit(1);
}
p[size] = WNUL;
return p;
}
static wchar_t **waalloc(size_t size)
{
wchar_t **p = (wchar_t **)calloc(size + 2, sizeof(wchar_t *));
if (p == NULL) {
_wperror(L"waalloc");
_exit(1);
}
return p;
}
static void xfree(void *m)
{
if (m != NULL)
free(m);
}
static void waafree(wchar_t **array)
{
wchar_t **ptr = array;
if (array == NULL)
return;
while (*ptr != NULL)
xfree(*(ptr++));
free(array);
}
static wchar_t *xwcsdup(const wchar_t *s)
{
wchar_t *p;
size_t n;
if (IS_EMPTY_WCS(s))
return NULL;
n = wcslen(s);
p = xwmalloc(n);
return wmemcpy(p, s, n);
}
static wchar_t *xwcsndup(const wchar_t *s, size_t len)
{
wchar_t *p;
size_t n;
if (IS_EMPTY_WCS(s) || (len == 0))
return NULL;
n = wcsnlen(s, len);
p = xwmalloc(n);
return wmemcpy(p, s, n);
}
static wchar_t *xgetenv(const wchar_t *s)
{
wchar_t *d;
if (IS_EMPTY_WCS(s))
return NULL;
d = _wgetenv(s);
return xwcsdup(d);
}
static size_t xwcslen(const wchar_t *s)
{
if (IS_EMPTY_WCS(s))
return 0;
else
return wcslen(s);
}
static wchar_t *xwcsconcat(const wchar_t *s1, const wchar_t *s2)
{
wchar_t *rv;
size_t l1;
size_t l2;
l1 = xwcslen(s1);
l2 = xwcslen(s2);
if ((l1 + l2) == 0)
return NULL;
rv = xwmalloc(l1 + l2);
if(l1 > 0)
wmemcpy(rv, s1, l1);
if(l2 > 0)
wmemcpy(rv + l1, s2, l2);
return rv;
}
static wchar_t *xwcscpaths(const wchar_t *s1, const wchar_t *s2)
{
wchar_t *rv;
size_t l1, l2;
l1 = xwcslen(s1);
l2 = xwcslen(s2);
if ((l1 == 0) || (l2 == 0))
return NULL;
rv = xwmalloc(l1 + l2 + 1);
wmemcpy(rv, s1, l1);
rv[l1++] = L';';
wmemcpy(rv + l1 , s2, l2);
return rv;
}
static int xisvarchar(int c)
{
if ((c < 32) || (c > 127))
return 0;
else
return xvalidvarname[c];
}
/**
* Match = 0, NoMatch = 1, Abort = -1
* Based loosely on sections of wildmat.c by Rich Salz
*/
static int xwcsmatch(const wchar_t *wstr, const wchar_t *wexp)
{
int match;
int mflag;
for ( ; *wexp != WNUL; wstr++, wexp++) {
if (*wstr == WNUL && *wexp != L'*')
return -1;
switch (*wexp) {
case L'*':
wexp++;
while (*wexp == L'*') {
/**
* Skip multiple stars
*/
wexp++;
}
if (*wexp == WNUL)
return 0;
while (*wstr != WNUL) {
int rv = xwcsmatch(wstr++, wexp);
if (rv != 1)
return rv;
}
return -1;
break;
case L'?':
if (xisvarchar(*wstr) != 2)
return -1;
break;
case L'+':
/**
* Any character
*/
break;
case L'[':
match = 0;
mflag = 0;
if (*(++wexp) == L'!') {
/**
* Negate range
*/
wexp++;
if (*wexp != L']')
mflag = 1;
}
while (*wexp != L']') {
if ((*wexp == WNUL) || (*wexp == L'['))
return -1;
if (*wexp == *wstr)
match = 1;
wexp++;
}
if (match == mflag)
return -1;
break;
default:
if (*wstr != *wexp)
return 1;
break;
}
}
return (*wstr != WNUL);
}
static wchar_t *xwcsquote(wchar_t *s)
{
wchar_t *r;
size_t n;
if ((*s == L'"') || (wcspbrk(s, L" \t") == NULL))
return s;
n = xwcslen(s);
r = xwmalloc(n + 2);
r[0] = L'"';
wmemcpy(r + 1, s, n);
r[n + 1] = L'"';
xfree(s);
return r;
}
int xwgetopt(int nargc, const wchar_t **nargv, const wchar_t *opts)
{
const wchar_t *oli = NULL;
static int optcnt = 0;
static const wchar_t *place = zerostring;
xwoptarg = NULL;
if (*place == WNUL) {
optcnt = 0;
if (xwoptind >= nargc) {
/* No more arguments */
place = zerostring;
return EOF;
}
place = nargv[xwoptind];
if (*(place++) != L'-') {
/* Argument is not an option */
place = zerostring;
return EOF;
}
if (*place == WNUL) {
/* Single '-' skip and stop processing */
xwoptind++;
place = zerostring;
return EOF;
}
}
xwoption = *(place++);
if (xwoption != L':') {
oli = wcschr(opts, (wchar_t)xwoption);
}
if (oli == NULL) {
place = zerostring;
if (optcnt)
return EINVAL;
else
return EOF;
}
optcnt++;
/* Does this option need an argument? */
if (oli[1] == L':') {
/*
* Option-argument is either the rest of this argument
* or the entire next argument.
*/
if (*place != WNUL) {
xwoptarg = place;
}
else if (oli[2] != L':') {
if (nargc > ++xwoptind) {
xwoptarg = nargv[xwoptind];
}
}
++xwoptind;
place = zerostring;
if (IS_EMPTY_WCS(xwoptarg)) {
/* Option-argument is absent or empty */
return ENOENT;
}
}
else {
/* Don't need argument */
if (*place == WNUL) {
++xwoptind;
place = zerostring;
}
}
return oli[0];
}
static int xwcsisenvvar(const wchar_t *str, const wchar_t *var, int icase)
{
while (*str != WNUL) {
wchar_t cs = icase ? tolower(*str) : *str;
wchar_t cv = icase ? tolower(*var) : *var;
if (cs != cv)
break;
str++;
var++;
if (*var == WNUL)
return *str == L'=';
}
return 0;
}
static int xisbatchfile(const wchar_t *s)
{
size_t n = xwcslen(s);
/* a.bat */
if (n > 4) {
const wchar_t *e = s + n - 4;
if (*(e++) == L'.') {
if (_wcsicmp(e, L"bat") == 0)
return 1;
if (_wcsicmp(e, L"cmd") == 0)
return 1;
}
}
return 0;
}
static int iswinpath(const wchar_t *s)
{
int i = 0;
if (IS_PSW(s[0])) {
if (IS_PSW(s[1]) && (s[2] != WNUL)) {
i = 2;
s += 2;
if ((s[0] == L'?' ) &&
(IS_PSW(s[1]) ) &&
(s[2] != WNUL)) {
/**
* We have \\?\* path
*/
i += 2;
s += 2;
}
}
}
if (xisvarchar(s[0]) == 2) {
if (s[1] == L':') {
if (s[2] == WNUL)
i += 2;
else if (IS_PSW(s[2]))
i += 3;
}
}
return i;
}
static int isdotpath(const wchar_t *s)
{
if (s[0] == L'.') {
if (IS_PSW(s[1]))
return 300;
if ((s[1] == L'.') && IS_PSW(s[2]))
return 300;
}
return 0;
}
static int isposixpath(const wchar_t *str)
{
int i = 0;
if (str[0] != L'/')
return isdotpath(str);
if (str[1] == WNUL)
return 301;
if (str[1] == L'/')
return iswinpath(str);
if (wcschr(str + 1, L'/')) {
while (pathmatches[i] != NULL) {
if (xwcsmatch(str, pathmatches[i]) == 0)
return i + 100;
i++;
}
if (xforcewp)
return 150;
}
else {
while (pathfixed[i] != NULL) {
if (wcscmp(str, pathfixed[i]) == 0)
return i + 200;
i++;
}
if (xforcewp)
return 250;
}
return 0;
}
static int isanypath(const wchar_t *s)
{
int r;
if (IS_EMPTY_WCS(s))
return 0;
switch (s[0]) {
case L'"':
case L'\'':
r = 0;
break;
case L'/':
r = isposixpath(s);
break;
case L'.':
r = isdotpath(s);
break;
default:
r = iswinpath(s);
break;
}
return r;
}
/**
* If argument starts with '[--]name=' the
* function will return the string after '='.
*/
static wchar_t *cmdoptionval(wchar_t *s)
{
int n = 0;
if (IS_EMPTY_WCS(s))
return NULL;
while (*s != WNUL) {
int c = *(s++);
if (n > 0) {
if (c == L'=')
return s;
}
if (xisvarchar(c))
n++;
else
break;
}
return NULL;
}
static int envsort(const void *arg1, const void *arg2)
{
return _wcsicoll(*((wchar_t **)arg1), *((wchar_t **)arg2));
}
static void cleanpath(wchar_t *s)
{
int n;
int c;
int i;
i = (int)xwcslen(s);
for (n = 0, c = 0; n < i; n++) {
if (IS_PSW(s[n])) {
if ((n > 0) && IS_PSW(s[n + 1])) {
continue;
}
s[n] = L'\\';
}
s[c++] = s[n];
}
s[c--] = WNUL;
while (c > 0) {
if ((s[c] == L';') || (s[c] < L'!'))
s[c--] = WNUL;
else
break;
}
if (xrmendps) {
while (c > 1) {
if ((s[c] == L'\\') && (s[c - 1] != L'.'))
s[c--] = WNUL;
else
break;
}
}
}
static wchar_t **xsplitstr(const wchar_t *s, wchar_t sc)
{
int c = 0;
int x = 0;
wchar_t **sa;
const wchar_t *e;
e = s;
while (*e != WNUL) {
if (*(e++) == sc)
x++;
}
sa = waalloc(x + 1);
while ((e = wcschr(s, sc)) != NULL) {
wchar_t *p;
size_t n = (size_t)(e - s);
if (n == 0) {
/**
* Skip multiple separators
*/
s = e + 1;
continue;
}
p = xwcsndup(s, n);
sa[c++] = p;
s = e + 1;
}
if (*s != WNUL) {
sa[c++] = xwcsdup(s);
}
return sa;
}
static wchar_t **splitpath(const wchar_t *s, int *tokens, wchar_t ps)
{
int c = 0;
int x = 0;
wchar_t **sa;
const wchar_t *e;
*tokens = 0;
if (*s == ps)
return NULL;
e = s;
while (*e != WNUL) {
if (*(e++) == ps)
x++;
}
sa = waalloc(x + 1);
while ((e = wcschr(s, ps)) != NULL) {
wchar_t *p;
size_t n = (size_t)(e - s);
if (n == 0) {
/**
* Skip multiple separators
*/
s = e + 1;
continue;
}
p = xwcsndup(s, n);
sa[c++] = p;
if (isanypath(p) == 0) {
/**
* Token before separator was not a posix path.
* Return original str regardless if some
* previous tokens evaluated as posix path.
*/
waafree(sa);
return NULL;
}
else {
s = e + 1;
}
}
if (*s != WNUL) {
if (isanypath(s) == 0) {
waafree(sa);
return NULL;
}
else {
sa[c++] = xwcsdup(s);
}
}
*tokens = c;
return sa;
}
static wchar_t *mergepath(const wchar_t **pp)
{
int i, x;
size_t s[64];
size_t n, len = 0;
wchar_t *r, *p;
for (i = 0, x = 0; pp[i] != NULL; i++, x++) {
n = wcslen(pp[i]);
if (x < 64)
s[x] = n;
len += (n + 1);
}
r = p = xwmalloc(len + 2);
for (i = 0, x = 0; pp[i] != NULL; i++, x++) {
if (x < 64)
n = s[x];
else
n = wcslen(pp[i]);
if (i > 0)
*(p++) = L';';
wmemcpy(p, pp[i], n);
p += n;
}
*p = WNUL;
return r;
}
static wchar_t *posixtowin(wchar_t *pp, int m)
{
wchar_t *rv = NULL;
wchar_t windrive[] = { WNUL, L':', L'\\', WNUL};
if (m == 0)
m = isposixpath(pp);
if (m == 0) {
/**
* Not a posix path
*/
return pp;
}
else if (m == 100) {
/**
* /cygdrive/x/... absolute path
*/
windrive[0] = towupper(pp[10]);
rv = xwcsconcat(windrive, pp + 12);
cleanpath(rv + 3);
}
else if (m == 102) {
/**
* For anything from /dev/ return \\.\NUL
*/
rv = xwcsdup(L"\\\\.\\NUL");
}
else if (m == 300) {
cleanpath(pp);
return pp;
}
else if (m == 301) {
rv = xwcsdup(posixroot);
}
else {
cleanpath(pp);
rv = xwcsconcat(posixroot, pp);
}
xfree(pp);
return rv;
}
static wchar_t *pathtowin(wchar_t *pp)
{
int m;
wchar_t *rv = pp;
m = isanypath(pp);
if (m == 0)
return pp;
if (m < 100) {
cleanpath(pp);
}
else {
rv = posixtowin(pp, m);
}
return rv;
}
static wchar_t *towinpaths(const wchar_t *str, int m)
{
int i, n;
wchar_t *wp = xwcsdup(str);
if (m < 100) {
if (wcschr(wp, L';')) {
wchar_t **pa = splitpath(wp, &n, L';');
if (pa != NULL) {
for (i = 0; i < n; i++) {
cleanpath(pa[i]);
}
xfree(wp);
wp = mergepath(pa);
waafree(pa);
}
}
else if (iswinpath(wp)) {
cleanpath(wp);
}
}
else {
if (wcschr(wp, L':')) {
wchar_t **pa = splitpath(wp, &n, L':');
if (pa != NULL) {
for (i = 0; i < n; i++) {
pa[i] = posixtowin(pa[i], 0);
}
xfree(wp);
wp = mergepath(pa);
waafree(pa);
}
}
else {
wp = posixtowin(wp, 0);
}
}
return wp;
}
static wchar_t *getrealpathname(const wchar_t *path, int isdir)
{
wchar_t *buf = NULL;
DWORD siz = _MAX_FNAME;
DWORD len = 0;
HANDLE fh;
DWORD fa = isdir ? FILE_FLAG_BACKUP_SEMANTICS : FILE_ATTRIBUTE_NORMAL;
if (IS_EMPTY_WCS(path))
return NULL;
fh = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, fa, NULL);
if (IS_INVALID_HANDLE(fh))
return NULL;
while (buf == NULL) {
buf = xwmalloc(siz);
len = GetFinalPathNameByHandleW(fh, buf, siz, VOLUME_NAME_DOS);
if (len == 0) {
CloseHandle(fh);
xfree(buf);
return NULL;
}
if (len > siz) {
xfree(buf);
buf = NULL;
siz = len;
}
}
CloseHandle(fh);
if ((len > 5) && (len < _MAX_FNAME)) {
/**
* Strip leading \\?\ for short paths
* but not \\?\UNC\* paths
*/
if ((buf[0] == L'\\') &&
(buf[1] == L'\\') &&
(buf[2] == L'?') &&
(buf[3] == L'\\') &&
(buf[5] == L':')) {
wmemmove(buf, buf + 4, len - 3);
}
}
return buf;
}
static wchar_t *xsearchexe(const wchar_t *paths, const wchar_t *name)
{
wchar_t *sp = NULL;
wchar_t *rp = NULL;
DWORD ln = 0;
DWORD sz = _MAX_PATH;
while (sp == NULL) {
sp = xwmalloc(sz);
ln = SearchPathW(paths, name, L".exe", sz, sp, NULL);
if (ln == 0) {
xfree(sp);
return NULL;
}
else if (ln > sz) {
xfree(sp);
sp = NULL;
sz = ln;
}
}
rp = getrealpathname(sp, 0);
xfree(sp);
return rp;
}
static wchar_t *getposixroot(const wchar_t *rp, const wchar_t *sp)
{
wchar_t *d;
wchar_t *r = xwcsdup(rp);
if (r == NULL) {
const wchar_t **e = posixrenv;
while (*e != NULL) {
r = xgetenv(*e);
if (r != NULL) {
break;
}
e++;
}
if (r == NULL) {
r = xsearchexe(sp, L"bash.exe");
if (r != NULL) {
d = wcsstr(r, L"\\usr\\bin\\bash.exe");
if (d == NULL)
d = wcsstr(r, L"\\bin\\bash.exe");
if (d != NULL) {
*d = WNUL;
}
else {
xfree(r);
r = NULL;
}
}
}
}
if (r == NULL) {
/* Probably not usable */
r = xgetenv(L"APPDATA");
if (r == NULL)
r = xgetenv(L"SystemRoot");
}
else {
/* Ensure that path exists */
d = r;
cleanpath(d);
r = getrealpathname(d, 1);
xfree(d);
}
return r;
}
static wchar_t *realappname(const wchar_t *path)
{
int i;
const wchar_t *p = path;
i = (int)xwcslen(path);
while (--i > 0) {
if (IS_PSW(path[i])) {
/** Found path separator */
p = path + i + 1;
break;
}
}