-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathdpathmgr.c
1392 lines (1238 loc) · 33.1 KB
/
dpathmgr.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
/*
* Copyright 2018, University Corporation for Atmospheric Research
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
#ifdef _WIN32
#include <windows.h>
#include <io.h>
#include <wchar.h>
#include <direct.h>
#endif
#include <locale.h>
#include "netcdf.h"
#include "ncpathmgr.h"
#include "nclog.h"
#include "nclist.h"
#include "ncbytes.h"
#include "ncuri.h"
#include "ncutf8.h"
#undef DEBUGPATH
static int pathdebug = -1;
#undef DEBUG
#ifdef DEBUG
#define REPORT(e,msg) report((e),(msg),__LINE__)
#else
#define REPORT(e,msg)
#endif
#ifdef _WIN32
#define access _access
#define mkdir _mkdir
#define rmdir _rmdir
#define getcwd _getcwd
#if WINVERMAJOR > 10 || (WINVERMAJOR == 10 && WINVERBUILD >= 17134)
/* Should be possible to use UTF8 directly */
#define WINUTF8
#else
#undef WINUTF8
#endif
#endif
/*
Code to provide some path conversion code so that
paths in one format can be used on a platform that uses
a different format.
See the documentation in ncpathmgr.h for details.
*/
/* Define legal windows drive letters */
static const char* windrive = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/";
static const char netdrive = '/';
static const size_t cdlen = 10; /* strlen("/cygdrive/") */
static int pathinitialized = 0;
static const char* cygwinspecial[] =
{"/bin/","/dev/","/etc/","/home/",
"/lib/","/proc/","/sbin/","/tmp/",
"/usr/","/var/",NULL};
static const struct Path {
int kind;
char drive;
char* path;
} empty = {NCPD_UNKNOWN,0,NULL};
/* Keep the working directory kind and drive */
static char wdprefix[8192];
/* The current codepage */
#ifdef _WIN32
static int acp = -1;
#endif
/* Keep CYGWIN/MSYS2 mount point */
static struct MountPoint {
int defined;
char prefix[8192]; /*minus leading drive */
char drive;
} mountpoint;
/* Pick the platform kind for testing */
static int platform = 0;
static int parsepath(const char* inpath, struct Path* path);
static int unparsepath(struct Path* p, char** pathp, int platform);
static int getwdpath(void);
static void clearPath(struct Path* path);
static void pathinit(void);
static int iscygwinspecial(const char* path);
static int testurl(const char* path);
#ifdef WINPATH
static int ansi2utf8(const char* local, char** u8p);
static int ansi2wide(const char* local, wchar_t** u16p);
static int utf82wide(const char* utf8, wchar_t** u16p);
static int wide2utf8(const wchar_t* u16, char** u8p);
#endif
/*Forward*/
#ifdef DEBUG
static void report(int stat, const char* msg, int line);
#endif
static char* printPATH(struct Path* p);
EXTERNL
char* /* caller frees */
NCpathcvt(const char* inpath)
{
int stat = NC_NOERR;
char* tmp1 = NULL;
char* result = NULL;
struct Path inparsed = empty;
int platform= NCgetlocalpathkind();
if(inpath == NULL) goto done; /* defensive driving */
if(!pathinitialized) pathinit();
if(testurl(inpath)) { /* Pass thru URLs */
if((result = strdup(inpath))==NULL) stat = NC_ENOMEM;
goto done;
}
if((stat = parsepath(inpath,&inparsed)))
{REPORT(stat,"NCpathcvt: parsepath"); goto done;}
if(pathdebug > 0)
fprintf(stderr,">>> NCpathcvt: inparsed=%s\n",printPATH(&inparsed));
if((stat = unparsepath(&inparsed,&result,platform)))
{REPORT(stat,"NCpathcvt: unparsepath"); goto done;}
done:
if(pathdebug > 0) {
fprintf(stderr,">>> inpath=|%s| result=|%s|\n",
inpath?inpath:"NULL",result?result:"NULL");
fflush(stderr);
}
if(stat) {
nullfree(result); result = NULL;
nclog(NCLOGERR,"NCpathcvt: stat=%d (%s)",
stat,nc_strerror(stat));
}
nullfree(tmp1);
clearPath(&inparsed);
//fprintf(stderr,">>> ncpathcvt: inpath=%s result=%s\n",inpath,result);
return result;
}
EXTERNL
int
NCpathcanonical(const char* srcpath, char** canonp)
{
int stat = NC_NOERR;
char* canon = NULL;
struct Path path = empty;
if(srcpath == NULL) goto done;
if(!pathinitialized) pathinit();
/* parse the src path */
if((stat = parsepath(srcpath,&path))) {goto done;}
/* Convert to cygwin form */
if((stat = unparsepath(&path,&canon, NCPD_CYGWIN)))
goto done;
if(canonp) {*canonp = canon; canon = NULL;}
done:
nullfree(canon);
clearPath(&path);
return stat;
}
EXTERNL
char* /* caller frees */
NCpathabsolute(const char* relpath)
{
int stat = NC_NOERR;
struct Path canon = empty;
char* tmp1 = NULL;
char* result = NULL;
size_t len;
if(relpath == NULL) goto done; /* defensive driving */
if(!pathinitialized) pathinit();
/* Decompose path */
if((stat = parsepath(relpath,&canon)))
{REPORT(stat,"pathabs: parsepath"); goto done;}
/* See if relative */
if(canon.kind == NCPD_REL) {
/* prepend the wd path to the inpath, including drive letter, if any */
len = strlen(wdprefix)+strlen(canon.path)+1+1;
if((tmp1 = (char*)malloc(len))==NULL)
{stat = NCTHROW(NC_ENOMEM); goto done;}
tmp1[0] = '\0';
strlcat(tmp1,wdprefix,len);
strlcat(tmp1,"/",len);
strlcat(tmp1,canon.path,len);
nullfree(canon.path);
canon.path = NULL;
/* Reparse */
result = NCpathabsolute(tmp1);
goto done;
}
/* rebuild */
if((stat=unparsepath(&canon,&result,NCgetlocalpathkind())))
{REPORT(stat,"pathabs: unparsepath"); goto done;}
done:
if(pathdebug > 0) {
fprintf(stderr,">>> relpath=|%s| result=|%s|\n",
relpath?relpath:"NULL",result?result:"NULL");
fflush(stderr);
}
if(stat) {
nullfree(tmp1); tmp1 = NULL;
nclog(NCLOGERR,"NCpathcvt: stat=%d (%s)",
stat,nc_strerror(stat));
}
clearPath(&canon);
nullfree(tmp1);
return result;
}
/* Testing support */
EXTERNL
char* /* caller frees */
NCpathcvt_test(const char* inpath, int ukind, int udrive)
{
char* result = NULL;
struct MountPoint old;
if(!pathinitialized) pathinit();
old = mountpoint;
memset(&mountpoint,0,sizeof(mountpoint));
mountpoint.drive = (char)udrive;
mountpoint.defined = (mountpoint.drive || nulllen(mountpoint.prefix) > 0);
platform = ukind;
result = NCpathcvt(inpath);
mountpoint = old;
return result;
}
static void
pathinit(void)
{
if(pathinitialized) return;
pathinitialized = 1; /* avoid recursion */
/* Check for path debug env vars */
if(pathdebug < 0) {
const char* s = getenv("NCPATHDEBUG");
pathdebug = (s == NULL ? 0 : 1);
}
(void)getwdpath();
#ifdef _WIN32
/* Get the current code page */
acp = GetACP();
#endif
memset(&mountpoint,0,sizeof(mountpoint));
#ifdef REGEDIT
{ /* See if we can get the MSYS2 prefix from the registry */
if(getmountpoint(mountpoint.prefix,sizeof(mountpoint.prefix)))
goto next;
mountpoint.defined = 1;
if(pathdebug > 0)
fprintf(stderr,">>>> registry: mountprefix=|%s|\n",mountpoint.prefix);
}
next:
#endif
if(!mountpoint.defined) {
mountpoint.prefix[0] = '\0';
/* See if MSYS2_PREFIX is defined */
if(getenv("MSYS2_PREFIX")) {
const char* m2 = getenv("MSYS2_PREFIX");
mountpoint.prefix[0] = '\0';
strlcat(mountpoint.prefix,m2,sizeof(mountpoint.prefix));
}
if(pathdebug > 0) {
fprintf(stderr,">>>> prefix: mountprefix=|%s|\n",mountpoint.prefix);
}
}
if(mountpoint.defined) {
char* p;
size_t size = strlen(mountpoint.prefix);
for(p=mountpoint.prefix;*p;p++) {if(*p == '\\') *p = '/';} /* forward slash*/
if(mountpoint.prefix[size-1] == '/') {
size--;
mountpoint.prefix[size] = '\0'; /* no trailing slash */
}
/* Finally extract the drive letter, if any */
/* assumes mount prefix is in windows form */
mountpoint.drive = 0;
if(strchr(windrive,mountpoint.prefix[0]) != NULL
&& mountpoint.prefix[1] == ':') {
char* q = mountpoint.prefix;
mountpoint.drive = mountpoint.prefix[0];
/* Shift prefix left 2 chars */
for(p=mountpoint.prefix+2;*p;p++) {*q++ = *p;}
*q = '\0';
}
}
pathinitialized = 1;
}
static void
clearPath(struct Path* path)
{
nullfree(path->path);
path->path = NULL;
}
/* Unfortunately, not all cygwin paths start with /cygdrive.
So see if the path starts with one of the special paths.
*/
static int
iscygwinspecial(const char* path)
{
const char** p;
if(path == NULL) return 0;
for(p=cygwinspecial;*p;p++) {
if(strncmp(*p,path,strlen(*p))==0) return 1;
}
return 0;
}
/* return 1 if path looks like a url; 0 otherwise */
static int
testurl(const char* path)
{
int isurl = 0;
NCURI* tmpurl = NULL;
if(path == NULL) return 0;
/* Ok, try to parse as a url */
ncuriparse(path,&tmpurl);
isurl = (tmpurl == NULL?0:1);
ncurifree(tmpurl);
return isurl;
}
#ifdef WINPATH
/*
Provide wrappers for Path-related functions
*/
EXTERNL
FILE*
NCfopen(const char* path, const char* flags)
{
int stat = NC_NOERR;
FILE* f = NULL;
char bflags[64];
char* path8 = NULL; /* ACP -> UTF=8 */
char* bflags8 = NULL;
char* cvtpath = NULL;
wchar_t* wpath = NULL;
wchar_t* wflags = NULL;
size_t flaglen = strlen(flags)+1+1;
bflags[0] = '\0';
strlcat(bflags, flags, sizeof(bflags));
#ifdef _WIN32
strlcat(bflags,"b",sizeof(bflags));
#endif
/* First, convert from current Code Page to utf8 */
if((stat = ansi2utf8(path,&path8))) goto done;
if((stat = ansi2utf8(bflags,&bflags8))) goto done;
/* Localize */
if((cvtpath = NCpathcvt(path8))==NULL) goto done;
#ifdef WINUTF8
if(acp == CP_UTF8) {
/* This should take utf8 directly */
f = fopen(cvtpath,bflags8);
} else
#endif
{
/* Convert from utf8 to wide */
if((stat = utf82wide(cvtpath,&wpath))) goto done;
if((stat = utf82wide(bflags8,&wflags))) goto done;
f = _wfopen(wpath,wflags);
}
done:
nullfree(cvtpath);
nullfree(path8);
nullfree(bflags8);
nullfree(wpath);
nullfree(wflags);
return f;
}
EXTERNL
int
NCopen3(const char* path, int flags, int mode)
{
int stat = NC_NOERR;
int fd = -1;
char* cvtpath = NULL;
char* path8 = NULL;
wchar_t* wpath = NULL;
/* First, convert from current Code Page to utf8 */
if((stat = ansi2utf8(path,&path8))) goto done;
if((cvtpath = NCpathcvt(path8))==NULL) goto done;
#ifdef _WIN32
flags |= O_BINARY;
#endif
#ifdef WINUTF8
if(acp == CP_UTF8) {
/* This should take utf8 directly */
fd = _open(cvtpath,flags,mode);
} else
#endif
{
/* Convert from utf8 to wide */
if((stat = utf82wide(cvtpath,&wpath))) goto done;
fd = _wopen(wpath,flags,mode);
}
done:
nullfree(cvtpath);
nullfree(path8);
nullfree(wpath);
return fd;
}
EXTERNL
int
NCopen2(const char *path, int flags)
{
return NCopen3(path,flags,0);
}
#ifdef HAVE_DIRENT_H
EXTERNL
DIR*
NCopendir(const char* path)
{
DIR* ent = NULL;
char* cvtname = NCpathcvt(path);
if(cvtname == NULL) return NULL;
ent = opendir(cvtname);
nullfree(cvtname);
return ent;
}
EXTERNL
int
NCclosedir(DIR* ent)
{
int stat = NC_NOERR;
if(closedir(ent) < 0) stat = errno;
return stat;
}
#endif
/*
Provide wrappers for other file system functions
*/
/* Return access applied to path+mode */
EXTERNL
int
NCaccess(const char* path, int mode)
{
int stat = 0;
char* cvtpath = NULL;
char* path8 = NULL;
wchar_t* wpath = NULL;
/* First, convert from current Code Page to utf8 */
if((stat = ansi2utf8(path,&path8))) goto done;
if((cvtpath = NCpathcvt(path8)) == NULL) {stat = EINVAL; goto done;}
#ifdef WINUTF8
if(acp == CP_UTF8) {
/* This should take utf8 directly */
if(_access(cvtpath,mode) < 0) {stat = errno; goto done;}
} else
#endif
{
/* Convert from utf8 to wide */
if((stat = utf82wide(cvtpath,&wpath))) goto done;
if(_waccess(wpath,mode) < 0) {stat = errno; goto done;}
}
done:
nullfree(cvtpath);
nullfree(path8);
nullfree(wpath);
errno = stat;
return (errno?-1:0);
}
EXTERNL
int
NCremove(const char* path)
{
int status = 0;
char* path8 = NULL;
char* cvtpath = NULL;
wchar_t* wpath = NULL;
/* First, convert from current Code Page to utf8 */
if((status = ansi2utf8(path,&path8))) goto done;
if((cvtpath = NCpathcvt(path8)) == NULL) {status=ENOMEM; goto done;}
#ifdef WINUTF8
if(acp == CP_UTF8) {
/* This should take utf8 directly */
if(remove(cvtpath) < 0) {status = errno; goto done;}
} else
#endif
{
if((status = utf82wide(cvtpath,&wpath))) {status = ENOENT; goto done;}
if(_wremove(wpath) < 0) {status = errno; goto done;}
}
done:
nullfree(cvtpath);
nullfree(path8);
nullfree(wpath);
errno = status;
return (errno?-1:0);
}
EXTERNL
int
NCmkdir(const char* path, int mode)
{
int status = 0;
char* cvtpath = NULL;
char* path8 = NULL;
wchar_t* wpath = NULL;
/* First, convert from current Code Page to utf8 */
if((status = ansi2utf8(path,&path8))) goto done;
if((cvtpath = NCpathcvt(path8)) == NULL) {status=ENOMEM; goto done;}
#ifdef WINUTF8
if(acp == CP_UTF8) {
/* This should take utf8 directly */
if(_mkdir(cvtpath) < 0) {status = errno; goto done;}
} else
#endif
{
if((status = utf82wide(cvtpath,&wpath))) {status = ENOENT; goto done;}
if(_wmkdir(wpath) < 0) {status = errno; goto done;}
}
done:
nullfree(cvtpath);
nullfree(path8);
nullfree(wpath);
errno = status;
return (errno?-1:0);
}
EXTERNL
int
NCrmdir(const char* path)
{
int status = 0;
char* cvtname = NULL;
char* path8 = NULL;
/* First, convert from current Code Page to utf8 */
if((status = ansi2utf8(path,&path8))) goto done;
cvtname = NCpathcvt(path8);
if(cvtname == NULL) {errno = ENOENT; status = -1;}
status = rmdir(cvtname);
done:
nullfree(cvtname);
nullfree(path8);
return status;
}
EXTERNL
char*
NCgetcwd(char* cwdbuf, size_t cwdlen)
{
int status = NC_NOERR;
char* path = NULL;
size_t len;
struct Path wd;
errno = 0;
if(cwdlen == 0) {status = ENAMETOOLONG; goto done;}
if(!pathinitialized) pathinit();
if((status = getwdpath())) {status = ENOENT; goto done;}
if((status = parsepath(wdprefix,&wd))) {status = EINVAL; goto done;}
if((status = unparsepath(&wd,&path,NCgetlocalpathkind()))) {status = EINVAL; goto done;}
len = strlen(path);
if(len >= cwdlen) {status = ENAMETOOLONG; goto done;}
if(cwdbuf == NULL) {
if((cwdbuf = malloc(cwdlen))==NULL)
{status = NCTHROW(ENOMEM); goto done;}
}
memcpy(cwdbuf,path,len+1);
done:
clearPath(&wd);
nullfree(path);
errno = status;
return cwdbuf;
}
EXTERNL
int
NCmkstemp(char* base)
{
int stat = 0;
int fd, rno;
char* tmp = NULL;
size_t len;
char* xp = NULL;
char* cvtpath = NULL;
int attempts;
cvtpath = NCpathcvt(base);
len = strlen(cvtpath);
xp = cvtpath+(len-6);
assert(memcmp(xp,"XXXXXX",6)==0);
for(attempts=10;attempts>0;attempts--) {
/* The Windows version of mkstemp does not work right;
it only allows for 26 possible XXXXXX values */
/* Need to simulate by using some kind of pseudo-random number */
rno = rand();
if(rno < 0) rno = -rno;
snprintf(xp,7,"%06d",rno);
fd=NCopen3(cvtpath,O_RDWR|O_BINARY|O_CREAT, _S_IREAD|_S_IWRITE);
if(fd >= 0) break;
}
if(fd < 0) {
nclog(NCLOGERR, "Could not create temp file: %s",tmp);
stat = EACCES;
goto done;
}
done:
nullfree(cvtpath);
if(stat && fd >= 0) {close(fd);}
return (stat?-1:fd);
}
#ifdef HAVE_SYS_STAT_H
EXTERNL
int
NCstat(const char* path, struct stat* buf)
{
int status = 0;
char* cvtpath = NULL;
char* path8 = NULL;
wchar_t* wpath = NULL;
if((status = ansi2utf8(path,&path8))) goto done;
if((cvtpath = NCpathcvt(path8)) == NULL) {status=ENOMEM; goto done;}
#ifdef WINUTF8
if(acp == CP_UTF8) {
if(_stat64(cvtpath,buf) < 0) {status = errno; goto done;}
} else
#endif
{
if((status = utf82wide(cvtpath,&wpath))) {status = ENOENT; goto done;}
if(_wstat64(wpath,buf) < 0) {status = errno; goto done;}
}
done:
nullfree(cvtpath);
nullfree(path8);
nullfree(wpath);
errno = status;
return (errno?-1:0);
}
#endif /*HAVE_SYS_STAT_H*/
int
NCpath2utf8(const char* s, char** u8p)
{
return ansi2utf8(s,u8p);
}
int
NCstdbinary(void)
{
int fd;
fd = _fileno(stdin);
if(_setmode(fd,_O_BINARY)<0) return NC_EINVAL;
fd = _fileno(stdout);
if(_setmode(fd,_O_BINARY)<0) return NC_EINVAL;
fd = _fileno(stderr);
if(_setmode(fd,_O_BINARY)<0) return NC_EINVAL;
return NC_NOERR;
}
#else /*!WINPATH*/
int
NCpath2utf8(const char* path, char** u8p)
{
int stat = NC_NOERR;
char* u8 = NULL;
if(path != NULL) {
u8 = strdup(path);
if(u8 == NULL) {stat = NC_ENOMEM; goto done;}
}
if(u8p) {*u8p = u8; u8 = NULL;}
done:
return stat;
}
int
NCstdbinary(void)
{
return NC_NOERR;
}
#endif /*!WINPATH*/
EXTERNL int
NChasdriveletter(const char* path)
{
int stat = NC_NOERR;
int hasdl = 0;
struct Path canon = empty;
if(!pathinitialized) pathinit();
if((stat = parsepath(path,&canon))) goto done;
hasdl = (canon.drive != 0);
done:
clearPath(&canon);
return hasdl;
}
EXTERNL int
NCisnetworkpath(const char* path)
{
int stat = NC_NOERR;
int isnp = 0;
struct Path canon = empty;
if(!pathinitialized) pathinit();
if((stat = parsepath(path,&canon))) goto done;
isnp = (canon.drive == netdrive);
done:
clearPath(&canon);
return isnp;
}
/**************************************************/
/* Utilities */
/* Parse a path */
static int
parsepath(const char* inpath, struct Path* path)
{
int stat = NC_NOERR;
char* tmp1 = NULL;
size_t len;
char* p;
int platform = NCgetlocalpathkind();
assert(path);
memset(path,0,sizeof(struct Path));
if(inpath == NULL) goto done; /* defensive driving */
if(!pathinitialized) pathinit();
#if 0
/* Convert to UTF8 */
if((stat = NCpath2utf8(inpath,&tmp1))) goto done;
#else
tmp1 = strdup(inpath);
#endif
/* Convert to forward slash to simplify later code */
for(p=tmp1;*p;p++) {if(*p == '\\') *p = '/';}
/* parse all paths to 2 parts:
1. drive letter (optional)
2. path after drive letter
*/
len = strlen(tmp1);
/* 1. look for Windows network path //...; drive letter is faked using
the character '/' */
if(len >= 2 && (tmp1[0] == '/') && (tmp1[1] == '/')) {
path->drive = netdrive;
/* Remainder */
if(tmp1[2] == '\0')
path->path = NULL;
else
path->path = strdup(tmp1+1); /*keep first '/' */
if(path == NULL)
{stat = NC_ENOMEM; goto done;}
path->kind = NCPD_WIN;
}
/* 2. Look for leading /cygdrive/D where D is a single-char drive letter */
else if(len >= (cdlen+1)
&& memcmp(tmp1,"/cygdrive/",cdlen)==0
&& strchr(windrive,tmp1[cdlen]) != NULL
&& (tmp1[cdlen+1] == '/'
|| tmp1[cdlen+1] == '\0')) {
/* Assume this is a cygwin path */
path->drive = tmp1[cdlen];
/* Remainder */
if(tmp1[cdlen+1] == '\0')
path->path = NULL;
else
path->path = strdup(tmp1+cdlen+1);
if(path == NULL)
{stat = NC_ENOMEM; goto done;}
path->kind = NCPD_CYGWIN;
}
/* 4. Look for windows path: D:/... where D is a single-char
drive letter */
else if(len >= 2
&& strchr(windrive,tmp1[0]) != NULL
&& tmp1[1] == ':'
&& (tmp1[2] == '\0' || tmp1[2] == '/')) {
/* Assume this is a windows path */
path->drive = tmp1[0];
/* Remainder */
if(tmp1[2] == '\0')
path->path = NULL;
else
path->path = strdup(tmp1+2);
if(path == NULL)
{stat = NC_ENOMEM; goto done;}
path->kind = NCPD_WIN; /* Might be MINGW */
}
/* The /D/x/y/z MSYS paths cause much parsing confusion.
So only use it if the current platform is windows of mingw.
Otherwise use windows paths
*/
/* X. look for MSYS path /D/... */
else if((platform== NCPD_WIN || platform == NCPD_MSYS)
&& len >= 2
&& (tmp1[0] == '/')
&& strchr(windrive,tmp1[1]) != NULL
&& (tmp1[2] == '/' || tmp1[2] == '\0')) {
/* Assume this is an MSYS path */
path->drive = tmp1[1];
/* Remainder */
if(tmp1[2] == '\0')
path->path = NULL;
else
path->path = strdup(tmp1+2);
if(path == NULL)
{stat = NC_ENOMEM; goto done;}
path->kind = NCPD_MSYS;
}
/* 5. look for *nix* path; note this includes MSYS2 paths as well */
else if(len >= 1 && tmp1[0] == '/') {
/* Assume this is a *nix path */
path->drive = 0; /* no drive letter */
/* Remainder */
path->path = tmp1; tmp1 = NULL;
path->kind = NCPD_NIX;
} else {/* 6. Relative path of unknown type */
path->kind = NCPD_REL;
path->path = tmp1; tmp1 = NULL;
}
done:
nullfree(tmp1);
if(stat) {clearPath(path);}
return stat;
}
static int
unparsepath(struct Path* xp, char** pathp, int platform)
{
int stat = NC_NOERR;
size_t len;
char* path = NULL;
char sdrive[4] = "\0\0\0\0";
char* p = NULL;
int cygspecial = 0;
char drive = 0;
/* Short circuit a relative path */
if(xp->kind == NCPD_REL) {
/* Pass thru relative paths, but with proper slashes */
if((path = strdup(xp->path))==NULL) stat = NC_ENOMEM;
if(platform == NCPD_WIN || platform == NCPD_MSYS) {
char* p;
for(p=path;*p;p++) {if(*p == '/') *p = '\\';} /* back slash*/
}
goto exit;
}
/* We need a two level switch with an arm
for every pair of (xp->kind,platform)
*/
#define CASE(k,t) case ((k)*10+(t))
switch (xp->kind*10 + platform) {
CASE(NCPD_NIX,NCPD_NIX):
assert(xp->drive == 0);
len = nulllen(xp->path)+1;
if((path = (char*)malloc(len))==NULL)
{stat = NCTHROW(NC_ENOMEM); goto done;}
path[0] = '\0';
if(xp->path != NULL)
strlcat(path,xp->path,len);
break;
CASE(NCPD_NIX,NCPD_MSYS):
CASE(NCPD_NIX,NCPD_WIN):
assert(xp->drive == 0);
len = nulllen(xp->path)+1;
if(!mountpoint.defined)
{stat = NC_EINVAL; goto done;} /* drive required */
len += (strlen(mountpoint.prefix) + 2);
if((path = (char*)malloc(len))==NULL)
{stat = NCTHROW(NC_ENOMEM); goto done;}
path[0] = '\0';
assert(mountpoint.drive != 0);
sdrive[0] = mountpoint.drive;
sdrive[1] = ':';
sdrive[2] = '\0';
strlcat(path,sdrive,len);
strlcat(path,mountpoint.prefix,len);
if(xp->path != NULL) strlcat(path,xp->path,len);
for(p=path;*p;p++) {if(*p == '/') *p = '\\';} /* restore back slash */
break;
CASE(NCPD_NIX,NCPD_CYGWIN):
assert(xp->drive == 0);
/* Is this one of the special cygwin paths? */
cygspecial = iscygwinspecial(xp->path);
len = 0;
if(!cygspecial && mountpoint.drive != 0) {
len = cdlen + 1+1; /* /cygdrive/D */
len += nulllen(mountpoint.prefix);
}
if(xp->path)
len += strlen(xp->path);
len++; /* nul term */
if((path = (char*)malloc(len))==NULL)
{stat = NCTHROW(NC_ENOMEM); goto done;}
path[0] = '\0';
if(!cygspecial && mountpoint.drive != 0) {
strlcat(path,"/cygdrive/",len);
sdrive[0] = mountpoint.drive;
sdrive[1] = '\0';
strlcat(path,sdrive,len);
strlcat(path,mountpoint.prefix,len);
}
if(xp->path)
strlcat(path,xp->path,len);
break;
CASE(NCPD_CYGWIN,NCPD_NIX):
len = nulllen(xp->path);
if(xp->drive != 0)