-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathcommon.c
2961 lines (2711 loc) · 98 KB
/
common.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 (C) 2009-2024 Erwin Waterlander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice in the documentation and/or other materials provided with
* the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "common.h"
#include "dos2unix.h"
#include "querycp.h"
#include <stdarg.h>
#if defined(D2U_UNIFILE) || (defined(D2U_UNICODE) && defined(_WIN32))
#include <windows.h>
#endif
#if defined(D2U_UNICODE) && !defined(__MSDOS__) && !defined(_WIN32) && !defined(__OS2__) /* Unix, Cygwin */
# include <langinfo.h>
#endif
#if defined(__GLIBC__)
/* on glibc, canonicalize_file_name() broken prior to 2.4 (06-Mar-2006) */
# if __GNUC_PREREQ (2,4)
# define USE_CANONICALIZE_FILE_NAME 1
# endif
#elif defined(__CYGWIN__)
/* on cygwin, canonicalize_file_name() available since api 0/213 */
/* (1.7.0beta61, 25-Sep-09) */
# include <cygwin/version.h>
# if (CYGWIN_VERSION_DLL_COMBINED >= 213) && (CYGWIN_VERSION_DLL_MAJOR >= 1007)
# define USE_CANONICALIZE_FILE_NAME 1
# endif
#endif
/* global variables */
#ifdef D2U_UNIFILE
int d2u_display_encoding = D2U_DISPLAY_ANSI ;
#endif
/* Copy string src to dest, and null terminate dest.
dest_size must be the buffer size of dest. */
char *d2u_strncpy(char *dest, const char *src, size_t dest_size)
{
strncpy(dest,src,dest_size);
dest[dest_size-1] = '\0';
#ifdef DEBUG
if(strlen(src) > (dest_size-1)) {
D2U_UTF8_FPRINTF(stderr, "Text %s has been truncated from %d to %d characters in %s to prevent a buffer overflow.\n", src, (int)strlen(src), (int)dest_size, "d2u_strncpy()");
}
#endif
return dest;
}
int d2u_fclose (FILE *fp, const char *filename, CFlag *ipFlag, const char *m, const char *progname)
{
if (fclose(fp) != 0) {
if (ipFlag->verbose) {
ipFlag->error = errno;
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
if (m[0] == 'w')
D2U_UTF8_FPRINTF(stderr, _("Failed to write to temporary output file %s:"), filename);
else
D2U_UTF8_FPRINTF(stderr, _("Failed to close input file %s:"), filename);
D2U_ANSI_FPRINTF(stderr, " %s\n", strerror(errno));
}
return EOF;
}
#if DEBUG
else
fprintf(stderr, "%s: Closing file \"%s\" OK.\n", progname, filename);
#endif
return 0;
}
/*
* Print last system error on Windows.
*
*/
#if (defined(_WIN32) && !defined(__CYGWIN__))
void d2u_PrintLastError(const char *progname)
{
/* Retrieve the system error message for the last-error code */
LPVOID lpMsgBuf;
DWORD dw;
dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
/* Display the error message */
/* MessageBox(NULL, (LPCTSTR)lpMsgBuf, TEXT("Error"), MB_OK); */
D2U_UTF8_FPRINTF(stderr, "%s: ",progname);
#ifdef _UNICODE
fwprintf(stderr, L"%ls\n",(LPCTSTR)lpMsgBuf);
#else
fprintf(stderr, "%s\n",(LPCTSTR)lpMsgBuf);
#endif
LocalFree(lpMsgBuf);
}
int d2u_WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, int cchWideChar, LPSTR lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar)
{
int i;
if ( (i = WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, lpMultiByteStr, cbMultiByte, lpDefaultChar, lpUsedDefaultChar)) == 0)
d2u_PrintLastError("dos2unix");
return i;
}
int d2u_MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cbMultiByte, LPWSTR lpWideCharStr, int cchWideChar)
{
int i;
if ( (i = MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, lpWideCharStr, cchWideChar)) == 0)
d2u_PrintLastError("dos2unix");
return i;
}
#endif
#ifdef D2U_UNIFILE
/*
* d2u_utf8_fprintf() : printf wrapper, print in Windows Command Prompt in Unicode
* mode, to have consistent output. Regardless of active code page.
*
* On Windows the file system uses always Unicode UTF-16 encoding, regardless
* of the system default code page. This means that files and directories can
* have names that can't be encoded in the default system Windows ANSI code
* page.
*
* Dos2unix for Windows with Unicode file name support translates all directory
* names to UTF-8, to be able to work with char type strings. This is also
* done to keep the code portable.
*
* Dos2unix's messages are encoded in the default Windows ANSI code page, which
* can be translated with gettext. Gettext/libintl recodes messages (format) to
* the system default ANSI code page.
*
* d2u_utf8_fprintf() on Windows assumes that:
* - The format string is encoded in the system default ANSI code page.
* - The arguments are encoded in UTF-8.
*
* There are several methods for printing Unicode in the Windows Console, but
* none of them is perfect. There are so many issues that I decided to go back
* to ANSI by default.
*
* The use of setlocale() has influence on this function when ANSI or UTF-8 is
* printed. See also dos2unix.c and unix2dos.c and test/setlocale.c and
* test/setlocale.png.
*/
void d2u_utf8_fprintf( FILE *stream, const char* format, ... ) {
va_list args;
char buf[D2U_MAX_PATH];
char formatmbs[D2U_MAX_PATH];
wchar_t formatwcs[D2U_MAX_PATH];
UINT outputCP;
wchar_t wstr[D2U_MAX_PATH];
int prevmode;
static int BOM_printed = 0;
va_start(args, format);
/* The format string is encoded in the system default
* Windows ANSI code page. May have been translated
* by gettext. Convert it to wide characters. */
d2u_MultiByteToWideChar(CP_ACP,0, format, -1, formatwcs, D2U_MAX_PATH);
/* then convert the format string to UTF-8 */
d2u_WideCharToMultiByte(CP_UTF8, 0, formatwcs, -1, formatmbs, D2U_MAX_PATH, NULL, NULL);
/* The arguments (file names) are in UTF-8 encoding, because
* in dos2unix for Windows all file names are in UTF-8 format.
* Print to buffer (UTF-8) */
vsnprintf(buf, sizeof(buf), formatmbs, args);
if ((d2u_display_encoding == D2U_DISPLAY_UTF8) || (d2u_display_encoding == D2U_DISPLAY_UTF8BOM)) {
/* A disadvantage of this method is that all non-ASCII characters are printed
wrongly when the console uses raster font (which is the default).
When I switch the system ANSI code page to 936 (Simplified Chinese) or 932 (Japanese)
I see lot of flickering in the console when I print UTF-8.
The cause could be that I have a Dutch Windows installation, and when the console is
switched to UTF-8 mode (CP65001) the font is switched back to Western font (Lucida Console,
Consolas). These are the only fonts which I can select when I set the code page in the
console to 65001 with chcp, while the system ANSI code is 936 or 932.
*/
/* print UTF-8 buffer to console in UTF-8 mode */
outputCP = GetConsoleOutputCP();
SetConsoleOutputCP(CP_UTF8);
if (! BOM_printed) {
if (d2u_display_encoding == D2U_DISPLAY_UTF8BOM)
fwprintf(stream, L"%S","\xEF\xBB\xBF");
BOM_printed = 1;
}
fwprintf(stream,L"%S",buf);
fflush(stream);
SetConsoleOutputCP(outputCP);
/* The following UTF-8 method does not give correct output. I don't know why. */
/*prevmode = _setmode(_fileno(stream), _O_U8TEXT);
fwprintf(stream,L"%S",buf);
fflush(stream);
_setmode(_fileno(stream), prevmode); */
} else if ((d2u_display_encoding == D2U_DISPLAY_UNICODE) || (d2u_display_encoding == D2U_DISPLAY_UNICODEBOM)) {
/* Printing UTF-16 works correctly. Works also good with raster fonts.
No need to change the OEM code page to the system ANSI code page.
*/
d2u_MultiByteToWideChar(CP_UTF8,0, buf, -1, wstr, D2U_MAX_PATH);
prevmode = _setmode(_fileno(stream), _O_U16TEXT);
if (! BOM_printed) {
/* For correct redirection in PowerShell we need to print a BOM */
if (d2u_display_encoding == D2U_DISPLAY_UNICODEBOM)
fwprintf(stream, L"\xfeff");
BOM_printed = 1;
}
fwprintf(stream,L"%ls",wstr);
fflush(stream); /* Flushing is required to get correct UTF-16 when stdout is redirected. */
_setmode(_fileno(stream), prevmode);
} else { /* ANSI */
d2u_MultiByteToWideChar(CP_UTF8,0, buf, -1, wstr, D2U_MAX_PATH);
/* Convert the whole message to ANSI, some Unicode characters may fail to translate to ANSI.
They will be displayed as a question mark. */
d2u_WideCharToMultiByte(CP_ACP, 0, wstr, -1, buf, D2U_MAX_PATH, NULL, NULL);
fprintf(stream,"%s",buf);
}
va_end( args );
}
/* d2u_ansi_fprintf()
fprintf wrapper for Windows console.
Format and arguments are in ANSI format.
Redirect the printing to d2u_utf8_fprintf such that the output
format is consistent. To prevent a mix of ANSI/UTF-8/UTF-16
encodings in the print output. Mixed format printing may get the whole
console mixed up.
*/
void d2u_ansi_fprintf( FILE *stream, const char* format, ... ) {
va_list args;
char buf[D2U_MAX_PATH]; /* ANSI encoded string */
char bufmbs[D2U_MAX_PATH]; /* UTF-8 encoded string */
wchar_t bufwcs[D2U_MAX_PATH]; /* Wide encoded string */
va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args);
/* The format string and arguments are encoded in the system default
* Windows ANSI code page. May have been translated
* by gettext. Convert it to wide characters. */
d2u_MultiByteToWideChar(CP_ACP,0, buf, -1, bufwcs, D2U_MAX_PATH);
/* then convert the format string to UTF-8 */
d2u_WideCharToMultiByte(CP_UTF8, 0, bufwcs, -1, bufmbs, D2U_MAX_PATH, NULL, NULL);
d2u_utf8_fprintf(stream, "%s",bufmbs);
va_end( args );
}
#endif
/* d2u_rename
* wrapper for rename().
* On Windows file names are encoded in UTF-8.
*/
int d2u_rename(const char *oldname, const char *newname)
{
#ifdef D2U_UNIFILE
wchar_t oldnamew[D2U_MAX_PATH];
wchar_t newnamew[D2U_MAX_PATH];
d2u_MultiByteToWideChar(CP_UTF8, 0, oldname, -1, oldnamew, D2U_MAX_PATH);
d2u_MultiByteToWideChar(CP_UTF8, 0, newname, -1, newnamew, D2U_MAX_PATH);
return _wrename(oldnamew, newnamew);
#else
return rename(oldname, newname);
#endif
}
/* d2u_unlink
* wrapper for unlink().
* On Windows file names are encoded in UTF-8.
*/
int d2u_unlink(const char *filename)
{
#ifdef D2U_UNIFILE
wchar_t filenamew[D2U_MAX_PATH];
d2u_MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenamew, D2U_MAX_PATH);
return _wunlink(filenamew);
#else
return unlink(filename);
#endif
}
/******************************************************************
*
* int symbolic_link(char *path)
*
* test if *path points to a file that exists and is a symbolic link
*
* returns 1 on success, 0 when it fails.
*
******************************************************************/
#ifdef D2U_UNIFILE
int symbolic_link(const char *path)
{
DWORD attrs;
wchar_t pathw[D2U_MAX_PATH];
d2u_MultiByteToWideChar(CP_UTF8, 0, path, -1, pathw, D2U_MAX_PATH);
attrs = GetFileAttributesW(pathw);
if (attrs == INVALID_FILE_ATTRIBUTES)
return(0);
return ((attrs & FILE_ATTRIBUTE_REPARSE_POINT) != 0);
}
#elif(defined(_WIN32) && !defined(__CYGWIN__))
int symbolic_link(const char *path)
{
DWORD attrs;
attrs = GetFileAttributes(path);
if (attrs == INVALID_FILE_ATTRIBUTES)
return(0);
return ((attrs & FILE_ATTRIBUTE_REPARSE_POINT) != 0);
}
#else
int symbolic_link(const char *path)
{
#ifdef S_ISLNK
struct stat buf;
if (STAT(path, &buf) == 0) {
if (S_ISLNK(buf.st_mode))
return(1);
}
#endif
return(0);
}
#endif
/******************************************************************
*
* int regfile(char *path, int allowSymlinks)
*
* test if *path points to a regular file (or is a symbolic link,
* if allowSymlinks != 0).
*
* returns 0 on success, -1 when it fails.
*
******************************************************************/
int regfile(char *path, int allowSymlinks, CFlag *ipFlag, const char *progname)
{
#ifdef D2U_UNIFILE
struct _stat buf;
wchar_t pathw[D2U_MAX_PATH];
#else
struct stat buf;
#endif
#ifdef D2U_UNIFILE
d2u_MultiByteToWideChar(CP_UTF8, 0, path, -1, pathw, D2U_MAX_PATH);
if (_wstat(pathw, &buf) == 0) {
#else
if (STAT(path, &buf) == 0) {
#endif
#if DEBUG
D2U_UTF8_FPRINTF(stderr, "%s: %s", progname, path);
D2U_UTF8_FPRINTF(stderr, " MODE 0%o ", buf.st_mode);
#ifdef S_ISSOCK
if (S_ISSOCK(buf.st_mode))
D2U_UTF8_FPRINTF(stderr, " (socket)");
#endif
#ifdef S_ISLNK
if (S_ISLNK(buf.st_mode))
D2U_UTF8_FPRINTF(stderr, " (symbolic link)");
#endif
if (S_ISREG(buf.st_mode))
D2U_UTF8_FPRINTF(stderr, " (regular file)");
#ifdef S_ISBLK
if (S_ISBLK(buf.st_mode))
D2U_UTF8_FPRINTF(stderr, " (block device)");
#endif
if (S_ISDIR(buf.st_mode))
D2U_UTF8_FPRINTF(stderr, " (directory)");
if (S_ISCHR(buf.st_mode))
D2U_UTF8_FPRINTF(stderr, " (character device)");
if (S_ISFIFO(buf.st_mode))
D2U_UTF8_FPRINTF(stderr, " (FIFO)");
D2U_UTF8_FPRINTF(stderr, "\n");
#endif
if ((S_ISREG(buf.st_mode))
#ifdef S_ISLNK
|| (S_ISLNK(buf.st_mode) && allowSymlinks)
#endif
)
return(0);
else
return(-1);
}
else {
if (ipFlag->verbose) {
const char *errstr = strerror(errno);
ipFlag->error = errno;
D2U_UTF8_FPRINTF(stderr, "%s: %s:", progname, path);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
return(-1);
}
}
/******************************************************************
*
* int regfile_target(char *path)
*
* test if *path points to a regular file (follow symbolic link)
*
* returns 0 on success, -1 when it fails.
*
******************************************************************/
int regfile_target(char *path, CFlag *ipFlag, const char *progname)
{
#ifdef D2U_UNIFILE
struct _stat buf;
wchar_t pathw[D2U_MAX_PATH];
#else
struct stat buf;
#endif
#ifdef D2U_UNIFILE
d2u_MultiByteToWideChar(CP_UTF8, 0, path, -1, pathw, D2U_MAX_PATH);
if (_wstat(pathw, &buf) == 0) {
#else
if (stat(path, &buf) == 0) {
#endif
if (S_ISREG(buf.st_mode))
return(0);
else
return(-1);
}
else {
if (ipFlag->verbose) {
const char *errstr = strerror(errno);
ipFlag->error = errno;
D2U_UTF8_FPRINTF(stderr, "%s: %s:", progname, path);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
return(-1);
}
}
/*
* glob_warg() expands the wide command line arguments.
* Input : wide Unicode arguments.
* Output : argv : expanded arguments in UTF-8 format.
* Returns: new argc value.
* -1 when an error occurred.
*
*/
#ifdef D2U_UNIFILE
int glob_warg(int argc, wchar_t *wargv[], char ***argv, CFlag *ipFlag, const char *progname)
{
int i;
int argc_glob = 0;
wchar_t *warg;
wchar_t *path;
wchar_t *path_and_filename;
wchar_t *ptr;
char *arg;
char **argv_new;
const char *errstr;
size_t len;
int found, add_path;
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
argv_new = (char **)malloc(sizeof(char**));
if (argv_new == NULL) goto glob_failed;
len = (size_t)d2u_WideCharToMultiByte(CP_UTF8, 0, wargv[0], -1, NULL, 0, NULL, NULL);
arg = (char *)malloc(len);
if (arg == NULL) goto glob_failed;
d2u_WideCharToMultiByte(CP_UTF8, 0, wargv[argc_glob], -1, arg, (int)len, NULL, NULL);
argv_new[argc_glob] = arg;
for (i=1; i<argc; ++i)
{
warg = wargv[i];
found = 0;
add_path = 0;
/* FindFileData.cFileName has the path stripped off. We need to add it again. */
path = _wcsdup(warg);
/* replace all back slashes with slashes */
while ( (ptr = wcschr(path,L'\\')) != NULL) {
*ptr = L'/';
}
if ( (ptr = wcsrchr(path,L'/')) != NULL) {
ptr++;
*ptr = L'\0';
add_path = 1;
}
hFind = FindFirstFileW(warg, &FindFileData);
while (hFind != INVALID_HANDLE_VALUE)
{
char **new_argv_new;
len = wcslen(path) + wcslen(FindFileData.cFileName) + 2;
path_and_filename = (wchar_t *)malloc(len*sizeof(wchar_t));
if (path_and_filename == NULL) goto glob_failed;
if (add_path) {
wcsncpy(path_and_filename, path, wcslen(path)+1);
wcsncat(path_and_filename, FindFileData.cFileName, wcslen(FindFileData.cFileName)+1);
} else {
wcsncpy(path_and_filename, FindFileData.cFileName, wcslen(FindFileData.cFileName)+1);
}
found = 1;
++argc_glob;
len =(size_t) d2u_WideCharToMultiByte(CP_UTF8, 0, path_and_filename, -1, NULL, 0, NULL, NULL);
arg = (char *)malloc((size_t)len);
if (arg == NULL) goto glob_failed;
d2u_WideCharToMultiByte(CP_UTF8, 0, path_and_filename, -1, arg, (int)len, NULL, NULL);
free(path_and_filename);
new_argv_new = (char **)realloc(argv_new, (size_t)(argc_glob+1)*sizeof(char**));
if (new_argv_new == NULL) goto glob_failed;
else
argv_new = new_argv_new;
argv_new[argc_glob] = arg;
if (!FindNextFileW(hFind, &FindFileData)) {
FindClose(hFind);
hFind = INVALID_HANDLE_VALUE;
}
}
free(path);
if (found == 0) {
/* Not a file. Just copy the argument */
char **new_argv_new;
++argc_glob;
len =(size_t) d2u_WideCharToMultiByte(CP_UTF8, 0, warg, -1, NULL, 0, NULL, NULL);
arg = (char *)malloc((size_t)len);
if (arg == NULL) goto glob_failed;
d2u_WideCharToMultiByte(CP_UTF8, 0, warg, -1, arg, (int)len, NULL, NULL);
new_argv_new = (char **)realloc(argv_new, (size_t)(argc_glob+1)*sizeof(char**));
if (new_argv_new == NULL) goto glob_failed;
else
argv_new = new_argv_new;
argv_new[argc_glob] = arg;
}
}
*argv = argv_new;
return ++argc_glob;
glob_failed:
if (ipFlag->verbose) {
ipFlag->error = errno;
errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s:", progname);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
return -1;
}
#endif
void PrintBSDLicense(void)
{
D2U_ANSI_FPRINTF(stdout,"%s", _("\
Redistribution and use in source and binary forms, with or without\n\
modification, are permitted provided that the following conditions\n\
are met:\n\
1. Redistributions of source code must retain the above copyright\n\
notice, this list of conditions and the following disclaimer.\n\
2. Redistributions in binary form must reproduce the above copyright\n\
notice in the documentation and/or other materials provided with\n\
the distribution.\n\n\
"));
D2U_ANSI_FPRINTF(stdout,"%s", _("\
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY\n\
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n\
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\n\
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE\n\
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n\
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT\n\
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR\n\
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\n\
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n\
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN\n\
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\
"));
}
int is_dos2unix(const char *progname)
{
if ((strncmp(progname, "dos2unix", sizeof("dos2unix")) == 0) || (strncmp(progname, "mac2unix", sizeof("mac2unix")) == 0))
return 1;
else
return 0;
}
void PrintUsage(const char *progname)
{
D2U_ANSI_FPRINTF(stdout,_("Usage: %s [options] [file ...] [-n infile outfile ...]\n"), progname);
#ifndef NO_CHOWN
D2U_ANSI_FPRINTF(stdout,_(" --allow-chown allow file ownership change\n"));
#endif
D2U_ANSI_FPRINTF(stdout,_(" -ascii default conversion mode\n"));
D2U_ANSI_FPRINTF(stdout,_(" -iso conversion between DOS and ISO-8859-1 character set\n"));
D2U_ANSI_FPRINTF(stdout,_(" -1252 use Windows code page 1252 (Western European)\n"));
D2U_ANSI_FPRINTF(stdout,_(" -437 use DOS code page 437 (US) (default)\n"));
D2U_ANSI_FPRINTF(stdout,_(" -850 use DOS code page 850 (Western European)\n"));
D2U_ANSI_FPRINTF(stdout,_(" -860 use DOS code page 860 (Portuguese)\n"));
D2U_ANSI_FPRINTF(stdout,_(" -863 use DOS code page 863 (French Canadian)\n"));
D2U_ANSI_FPRINTF(stdout,_(" -865 use DOS code page 865 (Nordic)\n"));
D2U_ANSI_FPRINTF(stdout,_(" -7 convert 8 bit characters to 7 bit space\n"));
if (is_dos2unix(progname))
D2U_ANSI_FPRINTF(stdout,_(" -b, --keep-bom keep Byte Order Mark\n"));
else
D2U_ANSI_FPRINTF(stdout,_(" -b, --keep-bom keep Byte Order Mark (default)\n"));
D2U_ANSI_FPRINTF(stdout,_(" -c, --convmode conversion mode\n\
convmode ascii, 7bit, iso, mac, default to ascii\n"));
#ifdef D2U_UNIFILE
D2U_ANSI_FPRINTF(stdout,_(" -D, --display-enc set encoding of displayed text messages\n\
encoding ansi, unicode, utf8, default to ansi\n"));
#endif
D2U_ANSI_FPRINTF(stdout,_(" -e, --add-eol add a line break to the last line if there isn't one\n"));
D2U_ANSI_FPRINTF(stdout,_(" -f, --force force conversion of binary files\n"));
#ifdef D2U_UNICODE
#if (defined(_WIN32) && !defined(__CYGWIN__))
D2U_ANSI_FPRINTF(stdout,_(" -gb, --gb18030 convert UTF-16 to GB18030\n"));
#endif
#endif
D2U_ANSI_FPRINTF(stdout,_(" -h, --help display this help text\n"));
D2U_ANSI_FPRINTF(stdout,_(" -i, --info[=FLAGS] display file information\n\
file ... files to analyze\n"));
D2U_ANSI_FPRINTF(stdout,_(" -k, --keepdate keep output file date\n"));
D2U_ANSI_FPRINTF(stdout,_(" -L, --license display software license\n"));
D2U_ANSI_FPRINTF(stdout,_(" -l, --newline add additional newline\n"));
D2U_ANSI_FPRINTF(stdout,_(" -m, --add-bom add Byte Order Mark (default UTF-8)\n"));
D2U_ANSI_FPRINTF(stdout,_(" -n, --newfile write to new file\n\
infile original file in new-file mode\n\
outfile output file in new-file mode\n"));
#ifndef NO_CHOWN
D2U_ANSI_FPRINTF(stdout,_(" --no-allow-chown don't allow file ownership change (default)\n"));
#endif
D2U_ANSI_FPRINTF(stdout,_(" --no-add-eol don't add a line break to the last line if there isn't one (default)\n"));
D2U_ANSI_FPRINTF(stdout,_(" -O, --to-stdout write to standard output\n"));
D2U_ANSI_FPRINTF(stdout,_(" -o, --oldfile write to old file (default)\n\
file ... files to convert in old-file mode\n"));
D2U_ANSI_FPRINTF(stdout,_(" -q, --quiet quiet mode, suppress all warnings\n"));
if (is_dos2unix(progname))
D2U_ANSI_FPRINTF(stdout,_(" -r, --remove-bom remove Byte Order Mark (default)\n"));
else
D2U_ANSI_FPRINTF(stdout,_(" -r, --remove-bom remove Byte Order Mark\n"));
D2U_ANSI_FPRINTF(stdout,_(" -s, --safe skip binary files (default)\n"));
#ifdef D2U_UNICODE
D2U_ANSI_FPRINTF(stdout,_(" -u, --keep-utf16 keep UTF-16 encoding\n"));
D2U_ANSI_FPRINTF(stdout,_(" -ul, --assume-utf16le assume that the input format is UTF-16LE\n"));
D2U_ANSI_FPRINTF(stdout,_(" -ub, --assume-utf16be assume that the input format is UTF-16BE\n"));
#endif
D2U_ANSI_FPRINTF(stdout,_(" -v, --verbose verbose operation\n"));
#ifdef S_ISLNK
D2U_ANSI_FPRINTF(stdout,_(" -F, --follow-symlink follow symbolic links and convert the targets\n"));
#endif
#if defined(S_ISLNK) || (defined(_WIN32) && !defined(__CYGWIN__))
D2U_ANSI_FPRINTF(stdout,_(" -R, --replace-symlink replace symbolic links with converted files\n\
(original target files remain unchanged)\n"));
D2U_ANSI_FPRINTF(stdout,_(" -S, --skip-symlink keep symbolic links and targets unchanged (default)\n"));
#endif
D2U_ANSI_FPRINTF(stdout,_(" -V, --version display version number\n"));
}
#define MINGW32_W64 1
void PrintVersion(const char *progname, const char *localedir)
{
D2U_ANSI_FPRINTF(stdout,"%s %s (%s)\n", progname, VER_REVISION, VER_DATE);
#if DEBUG
D2U_ANSI_FPRINTF(stdout,"VER_AUTHOR: %s\n", VER_AUTHOR);
#endif
#if defined(__WATCOMC__) && defined(__I86__)
D2U_ANSI_FPRINTF(stdout,"%s", _("DOS 16 bit version (WATCOMC).\n"));
#elif defined(__TURBOC__) && defined(__MSDOS__)
D2U_ANSI_FPRINTF(stdout,"%s", _("DOS 16 bit version (TURBOC).\n"));
#elif defined(__WATCOMC__) && defined(__DOS__)
D2U_ANSI_FPRINTF(stdout,"%s", _("DOS 32 bit version (WATCOMC).\n"));
#elif defined(__DJGPP__)
D2U_ANSI_FPRINTF(stdout,"%s", _("DOS 32 bit version (DJGPP).\n"));
#elif defined(__MSYS__)
D2U_ANSI_FPRINTF(stdout,"%s", _("MSYS version.\n"));
#elif defined(__CYGWIN__)
D2U_ANSI_FPRINTF(stdout,"%s", _("Cygwin version.\n"));
#elif defined(__WIN64__) && defined(__MINGW64__)
D2U_ANSI_FPRINTF(stdout,"%s", _("Windows 64 bit version (MinGW-w64).\n"));
#elif defined(__WATCOMC__) && defined(__NT__)
D2U_ANSI_FPRINTF(stdout,"%s", _("Windows 32 bit version (WATCOMC).\n"));
#elif defined(_WIN32) && defined(__MINGW32__) && (D2U_COMPILER == MINGW32_W64)
D2U_ANSI_FPRINTF(stdout,"%s", _("Windows 32 bit version (MinGW-w64).\n"));
#elif defined(_WIN32) && defined(__MINGW32__)
D2U_ANSI_FPRINTF(stdout,"%s", _("Windows 32 bit version (MinGW).\n"));
#elif defined(_WIN64) && defined(_MSC_VER)
D2U_ANSI_FPRINTF(stdout,_("Windows 64 bit version (MSVC %d).\n"),_MSC_VER);
#elif defined(_WIN32) && defined(_MSC_VER)
D2U_ANSI_FPRINTF(stdout,_("Windows 32 bit version (MSVC %d).\n"),_MSC_VER);
#elif defined (__OS2__) && defined(__WATCOMC__) /* OS/2 Warp */
D2U_ANSI_FPRINTF(stdout,"%s", _("OS/2 version (WATCOMC).\n"));
#elif defined (__OS2__) && defined(__EMX__) /* OS/2 Warp */
D2U_ANSI_FPRINTF(stdout,"%s", _("OS/2 version (EMX).\n"));
#elif defined(__OS)
D2U_ANSI_FPRINTF(stdout,_("%s version.\n"), __OS);
#endif
#if defined(_WIN32) && defined(WINVER)
D2U_ANSI_FPRINTF(stdout,"WINVER 0x%X\n",WINVER);
#endif
#ifdef D2U_UNICODE
D2U_ANSI_FPRINTF(stdout,"%s", _("With Unicode UTF-16 support.\n"));
#else
D2U_ANSI_FPRINTF(stdout,"%s", _("Without Unicode UTF-16 support.\n"));
#endif
#ifdef _WIN32
#ifdef D2U_UNIFILE
D2U_ANSI_FPRINTF(stdout,"%s", _("With Unicode file name support.\n"));
#else
D2U_ANSI_FPRINTF(stdout,"%s", _("Without Unicode file name support.\n"));
#endif
#endif
#ifdef ENABLE_NLS
D2U_ANSI_FPRINTF(stdout,"%s", _("With native language support.\n"));
#else
D2U_ANSI_FPRINTF(stdout,"%s", "Without native language support.\n");
#endif
#ifndef NO_CHOWN
D2U_ANSI_FPRINTF(stdout,"%s", _("With support to preserve the user and group ownership of files.\n"));
#else
D2U_ANSI_FPRINTF(stdout,"%s", _("Without support to preserve the user and group ownership of files.\n"));
#endif
#ifdef ENABLE_NLS
D2U_ANSI_FPRINTF(stdout,"LOCALEDIR: %s\n", localedir);
#endif
D2U_ANSI_FPRINTF(stdout,"https://waterlan.home.xs4all.nl/dos2unix.html\n");
D2U_ANSI_FPRINTF(stdout,"https://dos2unix.sourceforge.io/\n");
}
/* opens file of name ipFN in read only mode
* returns: NULL if failure
* file stream otherwise
*/
FILE* OpenInFile(char *ipFN)
{
#ifdef D2U_UNIFILE
wchar_t pathw[D2U_MAX_PATH];
d2u_MultiByteToWideChar(CP_UTF8, 0, ipFN, -1, pathw, D2U_MAX_PATH);
return _wfopen(pathw, R_CNTRLW);
#else
return (fopen(ipFN, R_CNTRL));
#endif
}
/* opens file of name opFN in write only mode
* returns: NULL if failure
* file stream otherwise
*/
FILE* OpenOutFile(char *opFN)
{
#ifdef D2U_UNIFILE
wchar_t pathw[D2U_MAX_PATH];
d2u_MultiByteToWideChar(CP_UTF8, 0, opFN, -1, pathw, D2U_MAX_PATH);
return _wfopen(pathw, W_CNTRLW);
#else
return (fopen(opFN, W_CNTRL));
#endif
}
/* opens file descriptor in write only mode
* returns: NULL if failure
* file stream otherwise
*/
FILE* OpenOutFiled(int fd)
{
return (fdopen(fd, W_CNTRL));
}
#if defined(__TURBOC__) || defined(__MSYS__) || defined(_MSC_VER)
/* Both dirname() and basename() may modify the contents of path.
* It may be desirable to pass a copy. */
char *dirname(char *path)
{
char *ptr;
/* replace all back slashes with slashes */
while ( (ptr = strchr(path,'\\')) != NULL)
*ptr = '/';
/* Code checkers may report that the condition (path == NULL) is redundant.
E.g. Cppcheck 1.72. The condition (path == NULL) is needed, because
the behaviour of strrchr is not specified when it get's a NULL string.
The behaviour may be undefined, dependent on the implementation. */
if ((path == NULL) || ((ptr=strrchr(path,'/')) == NULL))
return ".";
if (strcmp(path,"/") == 0)
return "/";
*ptr = '\0';
return path;
}
#ifdef NO_MKSTEMP
char *basename(char *path)
{
char *ptr;
/* replace all back slashes with slashes */
while ( (ptr = strchr(path,'\\')) != NULL)
*ptr = '/';
/* Code checkers may report that the condition (path == NULL) is redundant.
E.g. Cppcheck 1.72. The condition (path == NULL) is needed, because
the behaviour of strrchr is not specified when it get's a NULL string.
The behaviour may be undefined, dependent on the implementation. */
if ((path == NULL) || ((ptr=strrchr(path,'/')) == NULL))
return path ;
if (strcmp(path,"/") == 0)
return "/";
ptr++;
return ptr ;
}
#endif
#endif
/* Standard mktemp() is not safe to use (See mktemp(3)).
* On Windows it is recommended to use GetTempFileName() (See MSDN).
* This mktemp() wrapper redirects to GetTempFileName() on Windows.
* On Windows template is not modified, the returned pointer has to
* be used.
*/
#ifdef NO_MKSTEMP
char *d2u_mktemp(char *template)
{
#if defined(_WIN32) && !defined(__CYGWIN__)
unsigned int uRetVal;
char *cpy1, *cpy2, *dn, *bn;
char *ptr;
size_t len;
#ifdef D2U_UNIFILE /* template is UTF-8 formatted. */
wchar_t dnw[MAX_PATH];
wchar_t bnw[MAX_PATH];
wchar_t szTempFileNamew[MAX_PATH];
char *fname_str;
int error = 0;
#else
char szTempFileName[MAX_PATH];
char *fname_str;
#endif
if ((cpy1 = strdup(template)) == NULL)
return NULL;
if ((cpy2 = strdup(template)) == NULL) {
free(cpy1);
return NULL;
}
dn = dirname(cpy1);
bn = basename(cpy2);
#ifdef D2U_UNIFILE /* template is UTF-8 formatted. */
if (d2u_MultiByteToWideChar(CP_UTF8, 0, dn, -1, NULL, 0) > (MAX_PATH - 15)) {
D2U_UTF8_FPRINTF(stderr, "%s: ", "dos2unix");
D2U_ANSI_FPRINTF(stderr, _("Path for temporary output file is too long:"));
D2U_UTF8_FPRINTF(stderr, " %s\n", dn);
error=1;
}
if ((!error) && (d2u_MultiByteToWideChar(CP_UTF8, 0, dn, -1, dnw, MAX_PATH) == 0))
error=1;
if ((!error) && (d2u_MultiByteToWideChar(CP_UTF8, 0, bn, -1, bnw, MAX_PATH) == 0))
error=1;
free(cpy1);
free(cpy2);
if (error)
return NULL;
uRetVal = GetTempFileNameW(dnw, bnw, 0, szTempFileNamew);
if (! uRetVal) {
d2u_PrintLastError("dos2unix");
return NULL;
}
len =(size_t) d2u_WideCharToMultiByte(CP_UTF8, 0, szTempFileNamew, -1, NULL, 0, NULL, NULL);
fname_str = (char *)malloc(len);
if (! fname_str)
return NULL;
if (d2u_WideCharToMultiByte(CP_UTF8, 0, szTempFileNamew, -1, fname_str, MAX_PATH, NULL, NULL) == 0)
return NULL;
#else
uRetVal = GetTempFileNameA(dn, bn, 0, szTempFileName);
free(cpy1);
free(cpy2);
if (! uRetVal) {
d2u_PrintLastError("dos2unix");
return NULL;
}
len = strlen(szTempFileName) +1;
fname_str = (char *)malloc(len);
if (! fname_str)
return NULL;
d2u_strncpy(fname_str, szTempFileName,len);
#endif
/* replace all back slashes with slashes */
while ( (ptr = strchr(fname_str,'\\')) != NULL)
*ptr = '/';
return fname_str;
#else
return mktemp(template);
#endif
}
#endif
FILE* MakeTempFileFrom(const char *OutFN, char **fname_ret)
{
char *cpy = strdup(OutFN);
char *dir = NULL;
size_t fname_len = 0;
char *fname_str = NULL;
FILE *fp = NULL; /* file pointer */
#ifdef NO_MKSTEMP
char *name;
#else
int fd = -1; /* file descriptor */
#endif
*fname_ret = NULL;
if (!cpy)
goto make_failed;
dir = dirname(cpy);
fname_len = strlen(dir) + strlen("/d2utmpXXXXXX") + sizeof (char);
if (!(fname_str = (char *)malloc(fname_len)))
goto make_failed;
sprintf(fname_str, "%s%s", dir, "/d2utmpXXXXXX");
*fname_ret = fname_str;
free(cpy);
cpy = NULL;
#ifdef NO_MKSTEMP
if ((name = d2u_mktemp(fname_str)) == NULL)
goto make_failed;
*fname_ret = name;
if ((fp = OpenOutFile(name)) == NULL)
goto make_failed;
#else
if ((fd = mkstemp(fname_str)) == -1)
goto make_failed;