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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
|
/*
* Copyright (C) 2009-2016 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
int header_done = 0;
/* 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);
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);
_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) {
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) {
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;
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);
D2U_ANSI_FPRINTF(stdout,_(" -ascii convert only line breaks (default)\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,_(" -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"));
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
#ifdef ENABLE_NLS
D2U_ANSI_FPRINTF(stdout,"LOCALEDIR: %s\n", localedir);
#endif
D2U_ANSI_FPRINTF(stdout,"http://waterlan.home.xs4all.nl/dos2unix.html\n");
}
/* opens file of name ipFN in read only mode
* RetVal: 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
* RetVal: 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
* RetVal: 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);
#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;
if ((fp=OpenOutFiled(fd)) == NULL)
goto make_failed;
#endif
return (fp);
make_failed:
free(*fname_ret);
*fname_ret = NULL;
return NULL;
}
/* Test if *lFN is the name of a symbolic link. If not, set *rFN equal
* to lFN, and return 0. If so, then use canonicalize_file_name or
* realpath to determine the pointed-to file; the resulting name is
* stored in newly allocated memory, *rFN is set to point to that value,
* and 1 is returned. On error, -1 is returned and errno is set as
* appropriate.
*
* Note that if symbolic links are not supported, then 0 is always returned
* and *rFN = lFN.
*
* RetVal: 0 if success, and *lFN is not a symlink
* 1 if success, and *lFN is a symlink
* -1 otherwise
*/
int ResolveSymbolicLink(char *lFN, char **rFN, CFlag *ipFlag, const char *progname)
{
int RetVal = 0;
#ifdef S_ISLNK
struct stat StatBuf;
char *errstr;
char *targetFN = NULL;
if (STAT(lFN, &StatBuf)) {
if (ipFlag->verbose) {
ipFlag->error = errno;
errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: %s:", progname, lFN);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
RetVal = -1;
}
else if (S_ISLNK(StatBuf.st_mode)) {
#if USE_CANONICALIZE_FILE_NAME
targetFN = canonicalize_file_name(lFN);
if (!targetFN) {
if (ipFlag->verbose) {
ipFlag->error = errno;
errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: %s:", progname, lFN);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
RetVal = -1;
}
else {
*rFN = targetFN;
RetVal = 1;
}
#else
/* Sigh. Use realpath, but realize that it has a fatal
* flaw: PATH_MAX isn't necessarily the maximum path
* length -- so realpath() might fail. */
targetFN = (char *) malloc(PATH_MAX * sizeof(char));
if (!targetFN) {
if (ipFlag->verbose) {
ipFlag->error = errno;
errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: %s:", progname, lFN);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
RetVal = -1;
}
else {
/* is there any platform with S_ISLNK that does not have realpath? */
char *rVal = realpath(lFN, targetFN);
if (!rVal) {
if (ipFlag->verbose) {
ipFlag->error = errno;
errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: %s:", progname, lFN);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
free(targetFN);
RetVal = -1;
}
else {
*rFN = rVal;
RetVal = 1;
}
}
#endif /* !USE_CANONICALIZE_FILE_NAME */
}
else
*rFN = lFN;
#else /* !S_ISLNK */
*rFN = lFN;
#endif /* !S_ISLNK */
return RetVal;
}
/* Read the Byte Order Mark.
Returns file pointer or NULL in case of a read error */
FILE *read_bom (FILE *f, int *bomtype)
{
/* BOMs
* UTF16-LE ff fe
* UTF16-BE fe ff
* UTF-8 ef bb bf
* GB18030 84 31 95 33
*/
*bomtype = FILE_MBS;
/* Check for BOM */
if (f != NULL) {
int bom[4];
if ((bom[0] = fgetc(f)) == EOF) {
if (ferror(f)) {
return NULL;
}
*bomtype = FILE_MBS;
return(f);
}
if ((bom[0] != 0xff) && (bom[0] != 0xfe) && (bom[0] != 0xef) && (bom[0] != 0x84)) {
if (ungetc(bom[0], f) == EOF) return NULL;
*bomtype = FILE_MBS;
return(f);
}
if ((bom[1] = fgetc(f)) == EOF) {
if (ferror(f)) {
return NULL;
}
if (ungetc(bom[1], f) == EOF) return NULL;
if (ungetc(bom[0], f) == EOF) return NULL;
*bomtype = FILE_MBS;
return(f);
}
if ((bom[0] == 0xff) && (bom[1] == 0xfe)) { /* UTF16-LE */
*bomtype = FILE_UTF16LE;
return(f);
}
if ((bom[0] == 0xfe) && (bom[1] == 0xff)) { /* UTF16-BE */
*bomtype = FILE_UTF16BE;
return(f);
}
if ((bom[2] = fgetc(f)) == EOF) {
if (ferror(f)) {
return NULL;
}
if (ungetc(bom[2], f) == EOF) return NULL;
if (ungetc(bom[1], f) == EOF) return NULL;
if (ungetc(bom[0], f) == EOF) return NULL;
*bomtype = FILE_MBS;
return(f);
}
if ((bom[0] == 0xef) && (bom[1] == 0xbb) && (bom[2]== 0xbf)) { /* UTF-8 */
*bomtype = FILE_UTF8;
return(f);
}
if ((bom[0] == 0x84) && (bom[1] == 0x31) && (bom[2]== 0x95)) {
bom[3] = fgetc(f);
if (ferror(f)) {
return NULL;
}
if (bom[3]== 0x33) { /* GB18030 */
*bomtype = FILE_GB18030;
return(f);
}
if (ungetc(bom[3], f) == EOF) return NULL;
}
if (ungetc(bom[2], f) == EOF) return NULL;
if (ungetc(bom[1], f) == EOF) return NULL;
if (ungetc(bom[0], f) == EOF) return NULL;
*bomtype = FILE_MBS;
return(f);
}
return(f);
}
FILE *write_bom (FILE *f, CFlag *ipFlag, const char *progname)
{
int bomtype = ipFlag->bomtype;
if ((bomtype == FILE_MBS)&&(ipFlag->locale_target == TARGET_GB18030))
bomtype = FILE_GB18030;
if (ipFlag->keep_utf16)
{
switch (bomtype) {
case FILE_UTF16LE: /* UTF-16 Little Endian */
if (fprintf(f, "%s", "\xFF\xFE") < 0) return NULL;
if (ipFlag->verbose > 1) {
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_ANSI_FPRINTF(stderr, _("Writing %s BOM.\n"), _("UTF-16LE"));
}
break;
case FILE_UTF16BE: /* UTF-16 Big Endian */
if (fprintf(f, "%s", "\xFE\xFF") < 0) return NULL;
if (ipFlag->verbose > 1) {
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_ANSI_FPRINTF(stderr, _("Writing %s BOM.\n"), _("UTF-16BE"));
}
break;
case FILE_GB18030: /* GB18030 */
if (fprintf(f, "%s", "\x84\x31\x95\x33") < 0) return NULL;
if (ipFlag->verbose > 1) {
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_ANSI_FPRINTF(stderr, _("Writing %s BOM.\n"), _("GB18030"));
}
break;
default: /* UTF-8 */
if (fprintf(f, "%s", "\xEF\xBB\xBF") < 0) return NULL;
if (ipFlag->verbose > 1) {
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_ANSI_FPRINTF(stderr, _("Writing %s BOM.\n"), _("UTF-8"));
}
;
}
} else {
if ((bomtype == FILE_GB18030) ||
(((bomtype == FILE_UTF16LE)||(bomtype == FILE_UTF16BE))&&(ipFlag->locale_target == TARGET_GB18030))
) {
if (fprintf(f, "%s", "\x84\x31\x95\x33") < 0) return NULL; /* GB18030 */
if (ipFlag->verbose > 1)
{
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_ANSI_FPRINTF(stderr, _("Writing %s BOM.\n"), _("GB18030"));
}
} else {
if (fprintf(f, "%s", "\xEF\xBB\xBF") < 0) return NULL; /* UTF-8 */
if (ipFlag->verbose > 1)
{
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_ANSI_FPRINTF(stderr, _("Writing %s BOM.\n"), _("UTF-8"));
}
}
}
return(f);
}
void print_bom (const int bomtype, const char *filename, const char *progname)
{
char informat[64];
switch (bomtype) {
case FILE_UTF16LE: /* UTF-16 Little Endian */
d2u_strncpy(informat,_("UTF-16LE"),sizeof(informat));
break;
case FILE_UTF16BE: /* UTF-16 Big Endian */
d2u_strncpy(informat,_("UTF-16BE"),sizeof(informat));
break;
case FILE_UTF8: /* UTF-8 */
d2u_strncpy(informat,_("UTF-8"),sizeof(informat));
break;
case FILE_GB18030: /* GB18030 */
d2u_strncpy(informat,_("GB18030"),sizeof(informat));
break;
default:
;
}
if (bomtype > 0) {
#ifdef D2U_UNIFILE
wchar_t informatw[64];
#endif
informat[sizeof(informat)-1] = '\0';
/* Change informat to UTF-8 for d2u_utf8_fprintf. */
#ifdef D2U_UNIFILE
/* 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, informat, -1, informatw, sizeof(informat));
/* then convert the format string to UTF-8 */
d2u_WideCharToMultiByte(CP_UTF8, 0, informatw, -1, informat, sizeof(informat), NULL, NULL);
#endif
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_UTF8_FPRINTF(stderr, _("Input file %s has %s BOM.\n"), filename, informat);
}
}
void print_bom_info (const int bomtype)
{
/* The BOM info must not be translated to other languages, otherwise scripts
that process the output may not work in other than English locales. */
switch (bomtype) {
case FILE_UTF16LE: /* UTF-16 Little Endian */
D2U_UTF8_FPRINTF(stdout, " UTF-16LE");
break;
case FILE_UTF16BE: /* UTF-16 Big Endian */
D2U_UTF8_FPRINTF(stdout, " UTF-16BE");
break;
case FILE_UTF8: /* UTF-8 */
D2U_UTF8_FPRINTF(stdout, " UTF-8 ");
break;
case FILE_GB18030: /* GB18030 */
D2U_UTF8_FPRINTF(stdout, " GB18030 ");
break;
default:
D2U_UTF8_FPRINTF(stdout, " no_bom ");
;
}
}
int check_unicode_info(FILE *InF, CFlag *ipFlag, const char *progname, int *bomtype_orig)
{
int RetVal = 0;
#ifdef D2U_UNICODE
if (ipFlag->verbose > 1) {
if (ipFlag->ConvMode == CONVMODE_UTF16LE) {
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_UTF8_FPRINTF(stderr, _("Assuming UTF-16LE encoding.\n") );
}
if (ipFlag->ConvMode == CONVMODE_UTF16BE) {
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_UTF8_FPRINTF(stderr, _("Assuming UTF-16BE encoding.\n") );
}
}
#endif
if ((InF = read_bom(InF, &ipFlag->bomtype)) == NULL) {
d2u_getc_error(ipFlag,progname);
return -1;
}
*bomtype_orig = ipFlag->bomtype;
#ifdef D2U_UNICODE
if ((ipFlag->bomtype == FILE_MBS) && (ipFlag->ConvMode == CONVMODE_UTF16LE))
ipFlag->bomtype = FILE_UTF16LE;
if ((ipFlag->bomtype == FILE_MBS) && (ipFlag->ConvMode == CONVMODE_UTF16BE))
ipFlag->bomtype = FILE_UTF16BE;
#if !defined(_WIN32) && !defined(__CYGWIN__) /* Not Windows or Cygwin */
if (!ipFlag->keep_utf16 && ((ipFlag->bomtype == FILE_UTF16LE) || (ipFlag->bomtype == FILE_UTF16BE))) {
if (sizeof(wchar_t) < 4) {
/* A decoded UTF-16 surrogate pair must fit in a wchar_t */
ipFlag->status |= WCHAR_T_TOO_SMALL ;
if (!ipFlag->error) ipFlag->error = 1;
RetVal = -1;
}
}
#endif
#endif
return RetVal;
}
int check_unicode(FILE *InF, FILE *TempF, CFlag *ipFlag, const char *ipInFN, const char *progname)
{
#ifdef D2U_UNICODE
if (ipFlag->verbose > 1) {
if (ipFlag->ConvMode == CONVMODE_UTF16LE) {
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_UTF8_FPRINTF(stderr, _("Assuming UTF-16LE encoding.\n") );
}
if (ipFlag->ConvMode == CONVMODE_UTF16BE) {
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_UTF8_FPRINTF(stderr, _("Assuming UTF-16BE encoding.\n") );
}
}
#endif
if ((InF = read_bom(InF, &ipFlag->bomtype)) == NULL) {
d2u_getc_error(ipFlag,progname);
return -1;
}
if (ipFlag->verbose > 1)
print_bom(ipFlag->bomtype, ipInFN, progname);
#ifndef D2U_UNICODE
/* It is possible that an UTF-16 has no 8-bit binary symbols. We must stop
* processing an UTF-16 file when UTF-16 is not supported. Don't trust on
* finding a binary symbol.
*/
if ((ipFlag->bomtype == FILE_UTF16LE) || (ipFlag->bomtype == FILE_UTF16BE)) {
ipFlag->status |= UNICODE_NOT_SUPPORTED ;
return -1;
}
#endif
#ifdef D2U_UNICODE
if ((ipFlag->bomtype == FILE_MBS) && (ipFlag->ConvMode == CONVMODE_UTF16LE))
ipFlag->bomtype = FILE_UTF16LE;
if ((ipFlag->bomtype == FILE_MBS) && (ipFlag->ConvMode == CONVMODE_UTF16BE))
ipFlag->bomtype = FILE_UTF16BE;
#if !defined(_WIN32) && !defined(__CYGWIN__) /* Not Windows or Cygwin */
if (!ipFlag->keep_utf16 && ((ipFlag->bomtype == FILE_UTF16LE) || (ipFlag->bomtype == FILE_UTF16BE))) {
if (sizeof(wchar_t) < 4) {
/* A decoded UTF-16 surrogate pair must fit in a wchar_t */
ipFlag->status |= WCHAR_T_TOO_SMALL ;
if (!ipFlag->error) ipFlag->error = 1;
return -1;
}
}
#endif
#if !defined(__MSDOS__) && !defined(_WIN32) && !defined(__OS2__) /* Unix, Cygwin */
if (strcmp(nl_langinfo(CODESET), "GB18030") == 0)
ipFlag->locale_target = TARGET_GB18030;
#endif
#endif
if ((ipFlag->add_bom) || ((ipFlag->keep_bom) && (ipFlag->bomtype > 0)))
if (write_bom(TempF, ipFlag, progname) == NULL) return -1;
return 0;
}
/* convert file ipInFN and write to file ipOutFN
* RetVal: 0 if success
* -1 otherwise
*/
int ConvertNewFile(char *ipInFN, char *ipOutFN, CFlag *ipFlag, const char *progname,
int (*Convert)(FILE*, FILE*, CFlag *, const char *)
#ifdef D2U_UNICODE
, int (*ConvertW)(FILE*, FILE*, CFlag *, const char *)
#endif
)
{
int RetVal = 0;
FILE *InF = NULL;
FILE *TempF = NULL;
char *TempPath;
char *errstr;
#ifdef D2U_UNIFILE
struct _stat StatBuf;
wchar_t pathw[D2U_MAX_PATH];
#else
struct stat StatBuf;
#endif
struct utimbuf UTimeBuf;
#ifndef NO_CHMOD
mode_t mask;
#endif
char *TargetFN = NULL;
int ResolveSymlinkResult = 0;
ipFlag->status = 0 ;
/* Test if output file is a symbolic link */
if (symbolic_link(ipOutFN) && !ipFlag->Follow) {
ipFlag->status |= OUTPUTFILE_SYMLINK ;
/* Not a failure, skipping input file according spec. (keep symbolic link unchanged) */
return -1;
}
/* Test if input file is a regular file or symbolic link */
if (regfile(ipInFN, 1, ipFlag, progname)) {
ipFlag->status |= NO_REGFILE ;
/* Not a failure, skipping non-regular input file according spec. */
return -1;
}
/* Test if input file target is a regular file */
if (symbolic_link(ipInFN) && regfile_target(ipInFN, ipFlag,progname)) {
ipFlag->status |= INPUT_TARGET_NO_REGFILE ;
/* Not a failure, skipping non-regular input file according spec. */
return -1;
}
/* Test if output file target is a regular file */
if (symbolic_link(ipOutFN) && (ipFlag->Follow == SYMLINK_FOLLOW) && regfile_target(ipOutFN, ipFlag,progname)) {
ipFlag->status |= OUTPUT_TARGET_NO_REGFILE ;
/* Failure, input is regular, cannot produce output. */
if (!ipFlag->error) ipFlag->error = 1;
return -1;
}
/* retrieve ipInFN file date stamp */
#ifdef D2U_UNIFILE
d2u_MultiByteToWideChar(CP_UTF8, 0, ipInFN, -1, pathw, D2U_MAX_PATH);
if (_wstat(pathw, &StatBuf)) {
#else
if (stat(ipInFN, &StatBuf)) {
#endif
if (ipFlag->verbose) {
ipFlag->error = errno;
errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: %s:", progname, ipInFN);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
return -1;
}
/* can open in file? */
InF=OpenInFile(ipInFN);
if (InF == NULL) {
if (ipFlag->verbose) {
ipFlag->error = errno;
errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: %s:", progname, ipInFN);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
return -1;
}
/* can open temp output file? */
if((TempF = MakeTempFileFrom(ipOutFN, &TempPath))==NULL) {
if (ipFlag->verbose) {
if (errno) {
ipFlag->error = errno;
errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_ANSI_FPRINTF(stderr, _("Failed to open temporary output file: %s\n"), errstr);
} else {
/* In case temp path was too long on Windows, errno is 0. */
if (!ipFlag->error) ipFlag->error = 1;
}
}
RetVal = -1;
}
#if DEBUG
if (TempPath != NULL) {
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_UTF8_FPRINTF(stderr, _("using %s as temporary file\n"), TempPath);
}
#endif
if (!RetVal)
if (check_unicode(InF, TempF, ipFlag, ipInFN, progname))
RetVal = -1;
/* conversion sucessful? */
#ifdef D2U_UNICODE
if ((ipFlag->bomtype == FILE_UTF16LE) || (ipFlag->bomtype == FILE_UTF16BE)) {
if ((!RetVal) && (ConvertW(InF, TempF, ipFlag, progname)))
RetVal = -1;
if (ipFlag->status & UNICODE_CONVERSION_ERROR) {
if (!ipFlag->error) ipFlag->error = 1;
RetVal = -1;
}
} else {
if ((!RetVal) && (Convert(InF, TempF, ipFlag, progname)))
RetVal = -1;
}
#else
if ((!RetVal) && (Convert(InF, TempF, ipFlag, progname)))
RetVal = -1;
#endif
/* can close in file? */
if (d2u_fclose(InF, ipInFN, ipFlag, "r", progname) == EOF)
RetVal = -1;
/* can close output file? */
if (TempF) {
if (d2u_fclose(TempF, TempPath, ipFlag, "w", progname) == EOF)
RetVal = -1;
}
#ifndef NO_CHMOD
if (!RetVal)
{
if (ipFlag->NewFile == 0) { /* old-file mode */
RetVal = chmod (TempPath, StatBuf.st_mode); /* set original permissions */
} else {
mask = umask(0); /* get process's umask */
umask(mask); /* set umask back to original */
RetVal = chmod(TempPath, StatBuf.st_mode & ~mask); /* set original permissions, minus umask */
}
if (RetVal) {
if (ipFlag->verbose) {
ipFlag->error = errno;
errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_UTF8_FPRINTF(stderr, _("Failed to change the permissions of temporary output file %s:"), TempPath);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
}
}
#endif
#ifndef NO_CHOWN
if (!RetVal && (ipFlag->NewFile == 0)) { /* old-file mode */
/* Change owner and group of the temporary output file to the original file's uid and gid. */
/* Required when a different user (e.g. root) has write permission on the original file. */
/* Make sure that the original owner can still access the file. */
if (chown(TempPath, StatBuf.st_uid, StatBuf.st_gid)) {
if (ipFlag->verbose) {
ipFlag->error = errno;
errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_UTF8_FPRINTF(stderr, _("Failed to change the owner and group of temporary output file %s:"), TempPath);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
RetVal = -1;
}
}
#endif
if ((!RetVal) && (ipFlag->KeepDate))
{
UTimeBuf.actime = StatBuf.st_atime;
UTimeBuf.modtime = StatBuf.st_mtime;
/* can change output file time to in file time? */
if (utime(TempPath, &UTimeBuf) == -1) {
if (ipFlag->verbose) {
ipFlag->error = errno;
errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: %s:", progname, TempPath);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
RetVal = -1;
}
}
/* any error? cleanup the temp file */
if (RetVal && (TempPath != NULL)) {
if (d2u_unlink(TempPath) && (errno != ENOENT)) {
if (ipFlag->verbose) {
ipFlag->error = errno;
errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: %s:", progname, TempPath);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
RetVal = -1;
}
}
/* If output file is a symbolic link, optional resolve the link and modify */
/* the target, instead of removing the link and creating a new regular file */
TargetFN = ipOutFN;
if (symbolic_link(ipOutFN) && !RetVal) {
ResolveSymlinkResult = 0; /* indicates that TargetFN need not be freed */
if (ipFlag->Follow == SYMLINK_FOLLOW) {
ResolveSymlinkResult = ResolveSymbolicLink(ipOutFN, &TargetFN, ipFlag, progname);
if (ResolveSymlinkResult < 0) {
if (ipFlag->verbose) {
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_UTF8_FPRINTF(stderr, _("problems resolving symbolic link '%s'\n"), ipOutFN);
D2U_UTF8_FPRINTF(stderr, _(" output file remains in '%s'\n"), TempPath);
}
RetVal = -1;
}
}
}
/* can rename temporary file to output file? */
if (!RetVal) {
#ifdef NEED_REMOVE
if (d2u_unlink(TargetFN) && (errno != ENOENT)) {
if (ipFlag->verbose) {
ipFlag->error = errno;
errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: %s:", progname, TargetFN);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
RetVal = -1;
}
#endif
if (d2u_rename(TempPath, TargetFN) != 0) {
if (ipFlag->verbose) {
ipFlag->error = errno;
errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_UTF8_FPRINTF(stderr, _("problems renaming '%s' to '%s':"), TempPath, TargetFN);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
#ifdef S_ISLNK
if (ResolveSymlinkResult > 0)
D2U_UTF8_FPRINTF(stderr, _(" which is the target of symbolic link '%s'\n"), ipOutFN);
#endif
D2U_UTF8_FPRINTF(stderr, _(" output file remains in '%s'\n"), TempPath);
}
RetVal = -1;
}
if (ResolveSymlinkResult > 0)
free(TargetFN);
}
free(TempPath);
return RetVal;
}
/* convert stdin and write to stdout
* RetVal: 0 if success
* -1 otherwise
*/
int ConvertStdio(CFlag *ipFlag, const char *progname,
int (*Convert)(FILE*, FILE*, CFlag *, const char *)
#ifdef D2U_UNICODE
, int (*ConvertW)(FILE*, FILE*, CFlag *, const char *)
#endif
)
{
ipFlag->NewFile = 1;
ipFlag->KeepDate = 0;
#if defined(_WIN32) && !defined(__CYGWIN__)
/* stdin and stdout are by default text streams. We need
* to set them to binary mode. Otherwise an LF will
* automatically be converted to CR-LF on DOS/Windows.
* Erwin */
/* POSIX 'setmode' was deprecated by MicroSoft since
* Visual C++ 2005. Use ISO C++ conformant '_setmode' instead. */
_setmode(_fileno(stdout), _O_BINARY);
_setmode(_fileno(stdin), _O_BINARY);
#elif defined(__MSDOS__) || defined(__CYGWIN__) || defined(__OS2__)
setmode(fileno(stdout), O_BINARY);
setmode(fileno(stdin), O_BINARY);
#endif
if (check_unicode(stdin, stdout, ipFlag, "stdin", progname))
return -1;
#ifdef D2U_UNICODE
if ((ipFlag->bomtype == FILE_UTF16LE) || (ipFlag->bomtype == FILE_UTF16BE)) {
return ConvertW(stdin, stdout, ipFlag, progname);
} else {
return Convert(stdin, stdout, ipFlag, progname);
}
#else
return Convert(stdin, stdout, ipFlag, progname);
#endif
}
void print_messages_stdio(const CFlag *pFlag, const char *progname)
{
if (pFlag->status & BINARY_FILE) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping binary file %s\n"), "stdin");
} else if (pFlag->status & WRONG_CODEPAGE) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("code page %d is not supported.\n"), pFlag->ConvMode);
#ifdef D2U_UNICODE
} else if (pFlag->status & WCHAR_T_TOO_SMALL) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping UTF-16 file %s, the size of wchar_t is %d bytes.\n"), "stdin", (int)sizeof(wchar_t));
} else if (pFlag->status & UNICODE_CONVERSION_ERROR) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping UTF-16 file %s, an UTF-16 conversion error occurred on line %u.\n"), "stdin", pFlag->line_nr);
#else
} else if (pFlag->status & UNICODE_NOT_SUPPORTED) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping UTF-16 file %s, UTF-16 conversion is not supported in this version of %s.\n"), "stdin", progname);
#endif
}
}
void print_format(const CFlag *pFlag, char *informat, char *outformat, size_t lin, size_t lout)
{
informat[0]='\0';
outformat[0]='\0';
if (pFlag->bomtype == FILE_UTF16LE)
d2u_strncpy(informat,_("UTF-16LE"),lin);
if (pFlag->bomtype == FILE_UTF16BE)
d2u_strncpy(informat,_("UTF-16BE"),lin);
informat[lin-1]='\0';
#ifdef D2U_UNICODE
if ((pFlag->bomtype == FILE_UTF16LE)||(pFlag->bomtype == FILE_UTF16BE)) {
#if !defined(__MSDOS__) && !defined(_WIN32) && !defined(__OS2__) /* Unix, Cygwin */
d2u_strncpy(outformat,nl_langinfo(CODESET),lout);
#endif
#if defined(_WIN32) && !defined(__CYGWIN__) /* Windows, not Cygwin */
if (pFlag->locale_target == TARGET_GB18030)
d2u_strncpy(outformat, _("GB18030"),lout);
else
d2u_strncpy(outformat, _("UTF-8"),lout);
#endif
if (pFlag->keep_utf16)
{
if (pFlag->bomtype == FILE_UTF16LE)
d2u_strncpy(outformat,_("UTF-16LE"),lout);
if (pFlag->bomtype == FILE_UTF16BE)
d2u_strncpy(outformat,_("UTF-16BE"),lout);
}
outformat[lout-1]='\0';
}
#endif
}
void print_messages_newfile(const CFlag *pFlag, const char *infile, const char *outfile, const char *progname, const int RetVal)
{
char informat[32];
char outformat[64];
# ifdef D2U_UNIFILE
wchar_t informatw[32];
wchar_t outformatw[64];
#endif
print_format(pFlag, informat, outformat, sizeof(informat), sizeof(outformat));
/* Change informat and outformat to UTF-8 for d2u_utf8_fprintf. */
# ifdef D2U_UNIFILE
/* 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, informat, -1, informatw, sizeof(informat));
d2u_MultiByteToWideChar(CP_ACP,0, outformat, -1, outformatw, sizeof(outformat));
/* then convert the format string to UTF-8 */
d2u_WideCharToMultiByte(CP_UTF8, 0, informatw, -1, informat, sizeof(informat), NULL, NULL);
d2u_WideCharToMultiByte(CP_UTF8, 0, outformatw, -1, outformat, sizeof(outformat), NULL, NULL);
#endif
if (pFlag->status & NO_REGFILE) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping %s, not a regular file.\n"), infile);
} else if (pFlag->status & OUTPUTFILE_SYMLINK) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping %s, output file %s is a symbolic link.\n"), infile, outfile);
} else if (pFlag->status & INPUT_TARGET_NO_REGFILE) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping symbolic link %s, target is not a regular file.\n"), infile);
} else if (pFlag->status & OUTPUT_TARGET_NO_REGFILE) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping %s, target of symbolic link %s is not a regular file.\n"), infile, outfile);
} else if (pFlag->status & BINARY_FILE) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping binary file %s\n"), infile);
} else if (pFlag->status & WRONG_CODEPAGE) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("code page %d is not supported.\n"), pFlag->ConvMode);
#ifdef D2U_UNICODE
} else if (pFlag->status & WCHAR_T_TOO_SMALL) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping UTF-16 file %s, the size of wchar_t is %d bytes.\n"), infile, (int)sizeof(wchar_t));
} else if (pFlag->status & UNICODE_CONVERSION_ERROR) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping UTF-16 file %s, an UTF-16 conversion error occurred on line %u.\n"), infile, pFlag->line_nr);
#else
} else if (pFlag->status & UNICODE_NOT_SUPPORTED) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping UTF-16 file %s, UTF-16 conversion is not supported in this version of %s.\n"), infile, progname);
#endif
} else {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
if (informat[0] == '\0') {
if (is_dos2unix(progname)) {
if (!RetVal) D2U_UTF8_FPRINTF(stderr, _("converting file %s to file %s in Unix format...\n"), infile, outfile);
} else {
if (pFlag->FromToMode == FROMTO_UNIX2MAC) {
if (!RetVal) D2U_UTF8_FPRINTF(stderr, _("converting file %s to file %s in Mac format...\n"), infile, outfile);
} else {
if (!RetVal) D2U_UTF8_FPRINTF(stderr, _("converting file %s to file %s in DOS format...\n"), infile, outfile);
}
}
} else {
if (is_dos2unix(progname)) {
/* TRANSLATORS:
1st %s is encoding of input file.
2nd %s is name of input file.
3rd %s is encoding of output file.
4th %s is name of output file.
E.g.: converting UTF-16LE file in.txt to UTF-8 file out.txt in Unix format... */
if (!RetVal) D2U_UTF8_FPRINTF(stderr, _("converting %s file %s to %s file %s in Unix format...\n"), informat, infile, outformat, outfile);
} else {
if (pFlag->FromToMode == FROMTO_UNIX2MAC) {
if (!RetVal) D2U_UTF8_FPRINTF(stderr, _("converting %s file %s to %s file %s in Mac format...\n"), informat, infile, outformat, outfile);
} else {
if (!RetVal) D2U_UTF8_FPRINTF(stderr, _("converting %s file %s to %s file %s in DOS format...\n"), informat, infile, outformat, outfile);
}
}
}
if (RetVal) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("problems converting file %s to file %s\n"), infile, outfile);
}
}
}
void print_messages_oldfile(const CFlag *pFlag, const char *infile, const char *progname, const int RetVal)
{
char informat[10];
char outformat[32];
# ifdef D2U_UNIFILE
wchar_t informatw[32];
wchar_t outformatw[64];
#endif
print_format(pFlag, informat, outformat, sizeof(informat), sizeof(outformat));
/* Change informat and outformat to UTF-8 for d2u_utf8_fprintf. */
# ifdef D2U_UNIFILE
/* 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, informat, -1, informatw, sizeof(informat));
d2u_MultiByteToWideChar(CP_ACP,0, outformat, -1, outformatw, sizeof(outformat));
/* then convert the format string to UTF-8 */
d2u_WideCharToMultiByte(CP_UTF8, 0, informatw, -1, informat, sizeof(informat), NULL, NULL);
d2u_WideCharToMultiByte(CP_UTF8, 0, outformatw, -1, outformat, sizeof(outformat), NULL, NULL);
#endif
if (pFlag->status & NO_REGFILE) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping %s, not a regular file.\n"), infile);
} else if (pFlag->status & OUTPUTFILE_SYMLINK) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping symbolic link %s.\n"), infile);
} else if (pFlag->status & INPUT_TARGET_NO_REGFILE) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping symbolic link %s, target is not a regular file.\n"), infile);
} else if (pFlag->status & BINARY_FILE) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping binary file %s\n"), infile);
} else if (pFlag->status & WRONG_CODEPAGE) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("code page %d is not supported.\n"), pFlag->ConvMode);
#ifdef D2U_UNICODE
} else if (pFlag->status & WCHAR_T_TOO_SMALL) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping UTF-16 file %s, the size of wchar_t is %d bytes.\n"), infile, (int)sizeof(wchar_t));
} else if (pFlag->status & UNICODE_CONVERSION_ERROR) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping UTF-16 file %s, an UTF-16 conversion error occurred on line %u.\n"), infile, pFlag->line_nr);
#else
} else if (pFlag->status & UNICODE_NOT_SUPPORTED) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping UTF-16 file %s, UTF-16 conversion is not supported in this version of %s.\n"), infile, progname);
#endif
} else {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
if (informat[0] == '\0') {
if (is_dos2unix(progname)) {
if (!RetVal) D2U_UTF8_FPRINTF(stderr, _("converting file %s to Unix format...\n"), infile);
} else {
if (pFlag->FromToMode == FROMTO_UNIX2MAC) {
if (!RetVal) D2U_UTF8_FPRINTF(stderr, _("converting file %s to Mac format...\n"), infile);
} else {
if (!RetVal) D2U_UTF8_FPRINTF(stderr, _("converting file %s to DOS format...\n"), infile);
}
}
} else {
if (is_dos2unix(progname)) {
/* TRANSLATORS:
1st %s is encoding of input file.
2nd %s is name of input file.
3rd %s is encoding of output (input file is overwritten).
E.g.: converting UTF-16LE file foo.txt to UTF-8 Unix format... */
if (!RetVal) D2U_UTF8_FPRINTF(stderr, _("converting %s file %s to %s Unix format...\n"), informat, infile, outformat);
} else {
if (pFlag->FromToMode == FROMTO_UNIX2MAC) {
if (!RetVal) D2U_UTF8_FPRINTF(stderr, _("converting %s file %s to %s Mac format...\n"), informat, infile, outformat);
} else {
if (!RetVal) D2U_UTF8_FPRINTF(stderr, _("converting %s file %s to %s DOS format...\n"), informat, infile, outformat);
}
}
}
if (RetVal) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("problems converting file %s\n"), infile);
}
}
}
void print_messages_info(const CFlag *pFlag, const char *infile, const char *progname)
{
if (pFlag->status & NO_REGFILE) {
if (pFlag->verbose) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping %s, not a regular file.\n"), infile);
}
} else if (pFlag->status & INPUT_TARGET_NO_REGFILE) {
if (pFlag->verbose) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping symbolic link %s, target is not a regular file.\n"), infile);
}
#ifdef D2U_UNICODE
} else if (pFlag->status & WCHAR_T_TOO_SMALL) {
if (pFlag->verbose) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("Skipping UTF-16 file %s, the size of wchar_t is %d bytes.\n"), infile, (int)sizeof(wchar_t));
}
#endif
}
}
void printInfo(CFlag *ipFlag, const char *filename, int bomtype, unsigned int lb_dos, unsigned int lb_unix, unsigned int lb_mac)
{
if (ipFlag->file_info & INFO_CONVERT) {
if ((ipFlag->FromToMode == FROMTO_DOS2UNIX) && (lb_dos == 0))
return;
if (((ipFlag->FromToMode == FROMTO_UNIX2DOS)||(ipFlag->FromToMode == FROMTO_UNIX2MAC)) && (lb_unix == 0))
return;
if ((ipFlag->FromToMode == FROMTO_MAC2UNIX) && (lb_mac == 0))
return;
if ((ipFlag->Force == 0) && (ipFlag->status & BINARY_FILE))
return;
}
if ((ipFlag->file_info & INFO_HEADER) && (! header_done)) {
if (ipFlag->file_info & INFO_DOS)
D2U_UTF8_FPRINTF(stdout, " DOS");
if (ipFlag->file_info & INFO_UNIX)
D2U_UTF8_FPRINTF(stdout, " UNIX");
if (ipFlag->file_info & INFO_MAC)
D2U_UTF8_FPRINTF(stdout, " MAC");
if (ipFlag->file_info & INFO_BOM)
D2U_UTF8_FPRINTF(stdout, " BOM ");
if (ipFlag->file_info & INFO_TEXT)
D2U_UTF8_FPRINTF(stdout, " TXTBIN");
if (*filename != '\0') {
if (ipFlag->file_info & INFO_DEFAULT)
D2U_UTF8_FPRINTF(stdout, " ");
D2U_UTF8_FPRINTF(stdout, "FILE");
}
if (ipFlag->file_info & INFO_PRINT0)
fputc(0, stdout);
else
D2U_UTF8_FPRINTF(stdout, "\n");
header_done = 1;
}
if (ipFlag->file_info & INFO_DOS)
D2U_UTF8_FPRINTF(stdout, " %6u", lb_dos);
if (ipFlag->file_info & INFO_UNIX)
D2U_UTF8_FPRINTF(stdout, " %6u", lb_unix);
if (ipFlag->file_info & INFO_MAC)
D2U_UTF8_FPRINTF(stdout, " %6u", lb_mac);
if (ipFlag->file_info & INFO_BOM)
print_bom_info(bomtype);
if (ipFlag->file_info & INFO_TEXT) {
if (ipFlag->status & BINARY_FILE)
D2U_UTF8_FPRINTF(stdout, " binary");
else
D2U_UTF8_FPRINTF(stdout, " text ");
}
if (*filename != '\0') {
const char *ptr;
if ((ipFlag->file_info & INFO_NOPATH) && (((ptr=strrchr(filename,'/')) != NULL) || ((ptr=strrchr(filename,'\\')) != NULL)) )
ptr++;
else
ptr = filename;
if (ipFlag->file_info & INFO_DEFAULT)
D2U_UTF8_FPRINTF(stdout, " ");
D2U_UTF8_FPRINTF(stdout, "%s",ptr);
}
if (ipFlag->file_info & INFO_PRINT0)
fputc(0, stdout);
else
D2U_UTF8_FPRINTF(stdout, "\n");
}
#ifdef D2U_UNICODE
void FileInfoW(FILE* ipInF, CFlag *ipFlag, const char *filename, int bomtype, const char *progname)
{
wint_t TempChar;
wint_t PreviousChar = 0;
unsigned int lb_dos = 0;
unsigned int lb_unix = 0;
unsigned int lb_mac = 0;
ipFlag->status = 0;
while ((TempChar = d2u_getwc(ipInF, ipFlag->bomtype)) != WEOF) {
if ( (TempChar < 32) &&
(TempChar != 0x0a) && /* Not an LF */
(TempChar != 0x0d) && /* Not a CR */
(TempChar != 0x09) && /* Not a TAB */
(TempChar != 0x0c)) { /* Not a form feed */
ipFlag->status |= BINARY_FILE ;
}
if (TempChar != 0x0a) { /* Not an LF */
PreviousChar = TempChar;
if (TempChar == 0x0d) /* CR */
lb_mac++;
} else{
/* TempChar is an LF */
if ( PreviousChar == 0x0d ) { /* CR,LF pair. */
lb_dos++;
lb_mac--;
PreviousChar = TempChar;
continue;
}
PreviousChar = TempChar;
lb_unix++; /* Unix line end (LF). */
}
}
if ((TempChar == WEOF) && ferror(ipInF)) {
ipFlag->error = errno;
if (ipFlag->verbose) {
char *errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_UTF8_FPRINTF(stderr, _("can not read from input file %s:"), filename);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
return;
}
printInfo(ipFlag, filename, bomtype, lb_dos, lb_unix, lb_mac);
}
#endif
void FileInfo(FILE* ipInF, CFlag *ipFlag, const char *filename, int bomtype, const char *progname)
{
int TempChar;
int PreviousChar = 0;
unsigned int lb_dos = 0;
unsigned int lb_unix = 0;
unsigned int lb_mac = 0;
ipFlag->status = 0;
while ((TempChar = fgetc(ipInF)) != EOF) {
if ( (TempChar < 32) &&
(TempChar != '\x0a') && /* Not an LF */
(TempChar != '\x0d') && /* Not a CR */
(TempChar != '\x09') && /* Not a TAB */
(TempChar != '\x0c')) { /* Not a form feed */
ipFlag->status |= BINARY_FILE ;
}
if (TempChar != '\x0a') { /* Not an LF */
PreviousChar = TempChar;
if (TempChar == '\x0d') /* CR */
lb_mac++;
} else {
/* TempChar is an LF */
if ( PreviousChar == '\x0d' ) { /* CR,LF pair. */
lb_dos++;
lb_mac--;
PreviousChar = TempChar;
continue;
}
PreviousChar = TempChar;
lb_unix++; /* Unix line end (LF). */
}
}
if ((TempChar == EOF) && ferror(ipInF)) {
ipFlag->error = errno;
if (ipFlag->verbose) {
char *errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_UTF8_FPRINTF(stderr, _("can not read from input file %s:"), filename);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
return;
}
printInfo(ipFlag, filename, bomtype, lb_dos, lb_unix, lb_mac);
}
int GetFileInfo(char *ipInFN, CFlag *ipFlag, const char *progname)
{
FILE *InF = NULL;
int bomtype_orig = FILE_MBS; /* messages must print the real bomtype, not the assumed bomtype */
ipFlag->status = 0 ;
/* Test if input file is a regular file or symbolic link */
if (regfile(ipInFN, 1, ipFlag, progname)) {
ipFlag->status |= NO_REGFILE ;
/* Not a failure, skipping non-regular input file according spec. */
return -1;
}
/* Test if input file target is a regular file */
if (symbolic_link(ipInFN) && regfile_target(ipInFN, ipFlag,progname)) {
ipFlag->status |= INPUT_TARGET_NO_REGFILE ;
/* Not a failure, skipping non-regular input file according spec. */
return -1;
}
/* can open in file? */
InF=OpenInFile(ipInFN);
if (InF == NULL) {
if (ipFlag->verbose) {
char *errstr = strerror(errno);
ipFlag->error = errno;
D2U_UTF8_FPRINTF(stderr, "%s: %s: ", progname, ipInFN);
D2U_ANSI_FPRINTF(stderr, "%s\n", errstr);
}
return -1;
}
if (check_unicode_info(InF, ipFlag, progname, &bomtype_orig)) {
d2u_fclose(InF, ipInFN, ipFlag, "r", progname);
return -1;
}
/* info sucessful? */
#ifdef D2U_UNICODE
if ((ipFlag->bomtype == FILE_UTF16LE) || (ipFlag->bomtype == FILE_UTF16BE)) {
FileInfoW(InF, ipFlag, ipInFN, bomtype_orig, progname);
} else {
FileInfo(InF, ipFlag, ipInFN, bomtype_orig, progname);
}
#else
FileInfo(InF, ipFlag, ipInFN, bomtype_orig, progname);
#endif
/* can close in file? */
if (d2u_fclose(InF, ipInFN, ipFlag, "r", progname) == EOF)
return -1;
return 0;
}
int GetFileInfoStdio(CFlag *ipFlag, const char *progname)
{
int RetVal = 0;
int bomtype_orig = FILE_MBS; /* messages must print the real bomtype, not the assumed bomtype */
ipFlag->status = 0 ;
#if defined(_WIN32) && !defined(__CYGWIN__)
/* stdin and stdout are by default text streams. We need
* to set them to binary mode. Otherwise an LF will
* automatically be converted to CR-LF on DOS/Windows.
* Erwin */
/* POSIX 'setmode' was deprecated by MicroSoft since
* Visual C++ 2005. Use ISO C++ conformant '_setmode' instead. */
_setmode(_fileno(stdin), _O_BINARY);
#elif defined(__MSDOS__) || defined(__CYGWIN__) || defined(__OS2__)
setmode(fileno(stdin), O_BINARY);
#endif
if (check_unicode_info(stdin, ipFlag, progname, &bomtype_orig))
RetVal = -1;
/* info sucessful? */
#ifdef D2U_UNICODE
if (!RetVal) {
if ((ipFlag->bomtype == FILE_UTF16LE) || (ipFlag->bomtype == FILE_UTF16BE)) {
FileInfoW(stdin, ipFlag, "", bomtype_orig, progname);
} else {
FileInfo(stdin, ipFlag, "", bomtype_orig, progname);
}
}
#else
if (!RetVal)
FileInfo(stdin, ipFlag, "", bomtype_orig, progname);
#endif
return RetVal;
}
void get_info_options(char *option, CFlag *pFlag, const char *progname)
{
char *ptr;
int default_info = 1;
ptr = option;
if (*ptr == '\0') { /* no flags */
pFlag->file_info |= INFO_DEFAULT;
return;
}
while (*ptr != '\0') {
switch (*ptr) {
case '0': /* Print null characters instead of newline characters. */
pFlag->file_info |= INFO_PRINT0;
break;
case 'd': /* Print nr of DOS line breaks. */
pFlag->file_info |= INFO_DOS;
default_info = 0;
break;
case 'u': /* Print nr of Unix line breaks. */
pFlag->file_info |= INFO_UNIX;
default_info = 0;
break;
case 'm': /* Print nr of Mac line breaks. */
pFlag->file_info |= INFO_MAC;
default_info = 0;
break;
case 'b': /* Print BOM. */
pFlag->file_info |= INFO_BOM;
default_info = 0;
break;
case 't': /* Text or binary. */
pFlag->file_info |= INFO_TEXT;
default_info = 0;
break;
case 'c': /* Print only files that would be converted. */
pFlag->file_info |= INFO_CONVERT;
default_info = 0;
break;
case 'h': /* Print a header. */
pFlag->file_info |= INFO_HEADER;
break;
case 'p': /* Remove path from file names. */
pFlag->file_info |= INFO_NOPATH;
break;
default:
/* Terminate the program on a wrong option. If pFlag->file_info is
zero and the program goes on, it may do unwanted conversions. */
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr,_("wrong flag '%c' for option -i or --info\n"), *ptr);
exit(1);
;
}
ptr++;
}
if (default_info)
pFlag->file_info |= INFO_DEFAULT;
}
int parse_options(int argc, char *argv[],
CFlag *pFlag, const char *localedir, const char *progname,
void (*PrintLicense)(void),
int (*Convert)(FILE*, FILE*, CFlag *, const char *)
#ifdef D2U_UNICODE
, int (*ConvertW)(FILE*, FILE*, CFlag *, const char *)
#endif
)
{
int ArgIdx = 0;
int ShouldExit = 0;
int CanSwitchFileMode = 1;
int process_options = 1;
int RetVal = 0;
#ifdef D2U_UNIFILE
char *ptr;
#endif
/* variable initialisations */
pFlag->NewFile = 0;
pFlag->verbose = 1;
pFlag->KeepDate = 0;
pFlag->ConvMode = CONVMODE_ASCII; /* default ascii */
pFlag->NewLine = 0;
pFlag->Force = 0;
pFlag->Follow = SYMLINK_SKIP;
pFlag->status = 0;
pFlag->stdio_mode = 1;
pFlag->error = 0;
pFlag->bomtype = FILE_MBS;
pFlag->add_bom = 0;
pFlag->keep_utf16 = 0;
pFlag->file_info = 0;
pFlag->locale_target = TARGET_UTF8;
#ifdef D2U_UNIFILE
ptr = getenv("DOS2UNIX_DISPLAY_ENC");
if (ptr != NULL) {
if (strncmp(ptr, "ansi", sizeof("ansi")) == 0)
d2u_display_encoding = D2U_DISPLAY_ANSI;
else if (strncmp(ptr, "unicode", sizeof("unicode")) == 0)
d2u_display_encoding = D2U_DISPLAY_UNICODE;
else if (strncmp(ptr, "unicodebom", sizeof("unicodebom")) == 0)
d2u_display_encoding = D2U_DISPLAY_UNICODEBOM;
else if (strncmp(ptr, "utf8", sizeof("utf8")) == 0)
d2u_display_encoding = D2U_DISPLAY_UTF8;
else if (strncmp(ptr, "utf8bom", sizeof("utf8bom")) == 0)
d2u_display_encoding = D2U_DISPLAY_UTF8BOM;
}
#endif
while ((++ArgIdx < argc) && (!ShouldExit))
{
/* is it an option? */
if ((argv[ArgIdx][0] == '-') && process_options)
{
/* an option */
if (strcmp(argv[ArgIdx],"--") == 0)
process_options = 0;
else if ((strcmp(argv[ArgIdx],"-h") == 0) || (strcmp(argv[ArgIdx],"--help") == 0))
{
PrintUsage(progname);
return(pFlag->error);
}
else if ((strcmp(argv[ArgIdx],"-b") == 0) || (strcmp(argv[ArgIdx],"--keep-bom") == 0))
pFlag->keep_bom = 1;
else if ((strcmp(argv[ArgIdx],"-k") == 0) || (strcmp(argv[ArgIdx],"--keepdate") == 0))
pFlag->KeepDate = 1;
else if ((strcmp(argv[ArgIdx],"-f") == 0) || (strcmp(argv[ArgIdx],"--force") == 0))
pFlag->Force = 1;
#ifdef D2U_UNICODE
#if (defined(_WIN32) && !defined(__CYGWIN__))
else if ((strcmp(argv[ArgIdx],"-gb") == 0) || (strcmp(argv[ArgIdx],"--gb18030") == 0))
pFlag->locale_target = TARGET_GB18030;
#endif
#endif
else if ((strcmp(argv[ArgIdx],"-s") == 0) || (strcmp(argv[ArgIdx],"--safe") == 0))
pFlag->Force = 0;
else if ((strcmp(argv[ArgIdx],"-q") == 0) || (strcmp(argv[ArgIdx],"--quiet") == 0))
pFlag->verbose = 0;
else if ((strcmp(argv[ArgIdx],"-v") == 0) || (strcmp(argv[ArgIdx],"--verbose") == 0))
pFlag->verbose = 2;
else if ((strcmp(argv[ArgIdx],"-l") == 0) || (strcmp(argv[ArgIdx],"--newline") == 0))
pFlag->NewLine = 1;
else if ((strcmp(argv[ArgIdx],"-m") == 0) || (strcmp(argv[ArgIdx],"--add-bom") == 0))
pFlag->add_bom = 1;
else if ((strcmp(argv[ArgIdx],"-r") == 0) || (strcmp(argv[ArgIdx],"--remove-bom") == 0)) {
pFlag->keep_bom = 0;
pFlag->add_bom = 0;
}
else if ((strcmp(argv[ArgIdx],"-S") == 0) || (strcmp(argv[ArgIdx],"--skip-symlink") == 0))
pFlag->Follow = SYMLINK_SKIP;
else if ((strcmp(argv[ArgIdx],"-F") == 0) || (strcmp(argv[ArgIdx],"--follow-symlink") == 0))
pFlag->Follow = SYMLINK_FOLLOW;
else if ((strcmp(argv[ArgIdx],"-R") == 0) || (strcmp(argv[ArgIdx],"--replace-symlink") == 0))
pFlag->Follow = SYMLINK_REPLACE;
else if ((strcmp(argv[ArgIdx],"-V") == 0) || (strcmp(argv[ArgIdx],"--version") == 0)) {
PrintVersion(progname, localedir);
return(pFlag->error);
}
else if ((strcmp(argv[ArgIdx],"-L") == 0) || (strcmp(argv[ArgIdx],"--license") == 0)) {
PrintLicense();
return(pFlag->error);
}
else if (strcmp(argv[ArgIdx],"-ascii") == 0) { /* SunOS compatible options */
pFlag->ConvMode = CONVMODE_ASCII;
pFlag->keep_utf16 = 0;
pFlag->locale_target = TARGET_UTF8;
}
else if (strcmp(argv[ArgIdx],"-7") == 0)
pFlag->ConvMode = CONVMODE_7BIT;
else if (strcmp(argv[ArgIdx],"-iso") == 0) {
pFlag->ConvMode = (int)query_con_codepage();
if (pFlag->verbose) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr,_("active code page: %d\n"), pFlag->ConvMode);
}
if (pFlag->ConvMode < 2)
pFlag->ConvMode = CONVMODE_437;
}
else if (strcmp(argv[ArgIdx],"-437") == 0)
pFlag->ConvMode = CONVMODE_437;
else if (strcmp(argv[ArgIdx],"-850") == 0)
pFlag->ConvMode = CONVMODE_850;
else if (strcmp(argv[ArgIdx],"-860") == 0)
pFlag->ConvMode = CONVMODE_860;
else if (strcmp(argv[ArgIdx],"-863") == 0)
pFlag->ConvMode = CONVMODE_863;
else if (strcmp(argv[ArgIdx],"-865") == 0)
pFlag->ConvMode = CONVMODE_865;
else if (strcmp(argv[ArgIdx],"-1252") == 0)
pFlag->ConvMode = CONVMODE_1252;
#ifdef D2U_UNICODE
else if ((strcmp(argv[ArgIdx],"-u") == 0) || (strcmp(argv[ArgIdx],"--keep-utf16") == 0))
pFlag->keep_utf16 = 1;
else if ((strcmp(argv[ArgIdx],"-ul") == 0) || (strcmp(argv[ArgIdx],"--assume-utf16le") == 0))
pFlag->ConvMode = CONVMODE_UTF16LE;
else if ((strcmp(argv[ArgIdx],"-ub") == 0) || (strcmp(argv[ArgIdx],"--assume-utf16be") == 0))
pFlag->ConvMode = CONVMODE_UTF16BE;
#endif
else if (strcmp(argv[ArgIdx],"--info") == 0)
pFlag->file_info |= INFO_DEFAULT;
else if (strncmp(argv[ArgIdx],"--info=", (size_t)7) == 0) {
get_info_options(argv[ArgIdx]+7, pFlag, progname);
} else if (strncmp(argv[ArgIdx],"-i", (size_t)2) == 0) {
get_info_options(argv[ArgIdx]+2, pFlag, progname);
} else if ((strcmp(argv[ArgIdx],"-c") == 0) || (strcmp(argv[ArgIdx],"--convmode") == 0)) {
if (++ArgIdx < argc) {
if (strcmpi(argv[ArgIdx],"ascii") == 0) { /* Benjamin Lin's legacy options */
pFlag->ConvMode = CONVMODE_ASCII;
pFlag->keep_utf16 = 0;
}
else if (strcmpi(argv[ArgIdx], "7bit") == 0)
pFlag->ConvMode = CONVMODE_7BIT;
else if (strcmpi(argv[ArgIdx], "iso") == 0) {
pFlag->ConvMode = (int)query_con_codepage();
if (pFlag->verbose) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr,_("active code page: %d\n"), pFlag->ConvMode);
}
if (pFlag->ConvMode < 2)
pFlag->ConvMode = CONVMODE_437;
}
else if (strcmpi(argv[ArgIdx], "mac") == 0) {
if (is_dos2unix(progname))
pFlag->FromToMode = FROMTO_MAC2UNIX;
else
pFlag->FromToMode = FROMTO_UNIX2MAC;
} else {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("invalid %s conversion mode specified\n"),argv[ArgIdx]);
pFlag->error = 1;
ShouldExit = 1;
pFlag->stdio_mode = 0;
}
} else {
ArgIdx--;
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr,_("option '%s' requires an argument\n"),argv[ArgIdx]);
pFlag->error = 1;
ShouldExit = 1;
pFlag->stdio_mode = 0;
}
}
#ifdef D2U_UNIFILE
else if ((strcmp(argv[ArgIdx],"-D") == 0) || (strcmp(argv[ArgIdx],"--display-enc") == 0)) {
if (++ArgIdx < argc) {
if (strcmpi(argv[ArgIdx],"ansi") == 0)
d2u_display_encoding = D2U_DISPLAY_ANSI;
else if (strcmpi(argv[ArgIdx], "unicode") == 0)
d2u_display_encoding = D2U_DISPLAY_UNICODE;
else if (strcmpi(argv[ArgIdx], "unicodebom") == 0)
d2u_display_encoding = D2U_DISPLAY_UNICODEBOM;
else if (strcmpi(argv[ArgIdx], "utf8") == 0)
d2u_display_encoding = D2U_DISPLAY_UTF8;
else if (strcmpi(argv[ArgIdx], "utf8bom") == 0) {
d2u_display_encoding = D2U_DISPLAY_UTF8BOM;
} else {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("invalid %s display encoding specified\n"),argv[ArgIdx]);
pFlag->error = 1;
ShouldExit = 1;
pFlag->stdio_mode = 0;
}
} else {
ArgIdx--;
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr,_("option '%s' requires an argument\n"),argv[ArgIdx]);
pFlag->error = 1;
ShouldExit = 1;
pFlag->stdio_mode = 0;
}
}
#endif
else if ((strcmp(argv[ArgIdx],"-o") == 0) || (strcmp(argv[ArgIdx],"--oldfile") == 0)) {
/* last convert not paired */
if (!CanSwitchFileMode) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("target of file %s not specified in new-file mode\n"), argv[ArgIdx-1]);
pFlag->error = 1;
ShouldExit = 1;
pFlag->stdio_mode = 0;
}
pFlag->NewFile = 0;
pFlag->file_info = 0;
}
else if ((strcmp(argv[ArgIdx],"-n") == 0) || (strcmp(argv[ArgIdx],"--newfile") == 0)) {
/* last convert not paired */
if (!CanSwitchFileMode) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("target of file %s not specified in new-file mode\n"), argv[ArgIdx-1]);
pFlag->error = 1;
ShouldExit = 1;
pFlag->stdio_mode = 0;
}
pFlag->NewFile = 1;
pFlag->file_info = 0;
}
else { /* wrong option */
PrintUsage(progname);
ShouldExit = 1;
pFlag->error = 1;
pFlag->stdio_mode = 0;
}
} else {
pFlag->stdio_mode = 0;
/* not an option */
if (pFlag->NewFile) {
if (CanSwitchFileMode)
CanSwitchFileMode = 0;
else {
#ifdef D2U_UNICODE
RetVal = ConvertNewFile(argv[ArgIdx-1], argv[ArgIdx], pFlag, progname, Convert, ConvertW);
#else
RetVal = ConvertNewFile(argv[ArgIdx-1], argv[ArgIdx], pFlag, progname, Convert);
#endif
if (pFlag->verbose)
print_messages_newfile(pFlag, argv[ArgIdx-1], argv[ArgIdx], progname, RetVal);
CanSwitchFileMode = 1;
}
} else {
if (pFlag->file_info) {
RetVal = GetFileInfo(argv[ArgIdx], pFlag, progname);
print_messages_info(pFlag, argv[ArgIdx], progname);
} else {
#ifdef D2U_UNICODE
RetVal = ConvertNewFile(argv[ArgIdx], argv[ArgIdx], pFlag, progname, Convert, ConvertW);
#else
RetVal = ConvertNewFile(argv[ArgIdx], argv[ArgIdx], pFlag, progname, Convert);
#endif
if (pFlag->verbose)
print_messages_oldfile(pFlag, argv[ArgIdx], progname, RetVal);
}
}
}
}
/* no file argument, use stdin and stdout */
if ( (argc > 0) && pFlag->stdio_mode) {
if (pFlag->file_info) {
GetFileInfoStdio(pFlag, progname);
print_messages_info(pFlag, "stdin", progname);
} else {
#ifdef D2U_UNICODE
ConvertStdio(pFlag, progname, Convert, ConvertW);
#else
ConvertStdio(pFlag, progname, Convert);
#endif
if (pFlag->verbose)
print_messages_stdio(pFlag, progname);
}
return pFlag->error;
}
if (!CanSwitchFileMode) {
D2U_UTF8_FPRINTF(stderr,"%s: ",progname);
D2U_UTF8_FPRINTF(stderr, _("target of file %s not specified in new-file mode\n"), argv[ArgIdx-1]);
pFlag->error = 1;
}
return pFlag->error;
}
void d2u_getc_error(CFlag *ipFlag, const char *progname)
{
ipFlag->error = errno;
if (ipFlag->verbose) {
char *errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_ANSI_FPRINTF(stderr, _("can not read from input file: %s\n"), errstr);
}
}
void d2u_putc_error(CFlag *ipFlag, const char *progname)
{
ipFlag->error = errno;
if (ipFlag->verbose) {
char *errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_ANSI_FPRINTF(stderr, _("can not write to output file: %s\n"), errstr);
}
}
#ifdef D2U_UNICODE
void d2u_putwc_error(CFlag *ipFlag, const char *progname)
{
if (!(ipFlag->status & UNICODE_CONVERSION_ERROR)) {
ipFlag->error = errno;
if (ipFlag->verbose) {
char *errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_ANSI_FPRINTF(stderr, _("can not write to output file: %s\n"), errstr);
}
}
}
wint_t d2u_getwc(FILE *f, int bomtype)
{
int c_trail, c_lead;
wint_t wc;
if (((c_lead=fgetc(f)) == EOF) || ((c_trail=fgetc(f)) == EOF))
return(WEOF);
if (bomtype == FILE_UTF16LE) { /* UTF16 little endian */
c_trail <<=8;
wc = (wint_t)(c_trail + c_lead) ;
} else { /* UTF16 big endian */
c_lead <<=8;
wc = (wint_t)(c_trail + c_lead) ;
}
return(wc);
}
wint_t d2u_ungetwc(wint_t wc, FILE *f, int bomtype)
{
int c_trail, c_lead;
if (bomtype == FILE_UTF16LE) { /* UTF16 little endian */
c_trail = (int)(wc & 0xff00);
c_trail >>=8;
c_lead = (int)(wc & 0xff);
} else { /* UTF16 big endian */
c_lead = (int)(wc & 0xff00);
c_lead >>=8;
c_trail = (int)(wc & 0xff);
}
/* push back in reverse order */
if ((ungetc(c_trail,f) == EOF) || (ungetc(c_lead,f) == EOF))
return(WEOF);
return(wc);
}
/* Put wide character */
wint_t d2u_putwc(wint_t wc, FILE *f, CFlag *ipFlag, const char *progname)
{
static char mbs[8];
static wchar_t lead=0x01; /* lead get's invalid value */
static wchar_t wstr[3];
size_t len;
#if (defined(_WIN32) && !defined(__CYGWIN__))
DWORD dwFlags;
#endif
if (ipFlag->keep_utf16) {
int c_trail, c_lead;
if (ipFlag->bomtype == FILE_UTF16LE) { /* UTF16 little endian */
c_trail = (int)(wc & 0xff00);
c_trail >>=8;
c_lead = (int)(wc & 0xff);
} else { /* UTF16 big endian */
c_lead = (int)(wc & 0xff00);
c_lead >>=8;
c_trail = (int)(wc & 0xff);
}
if ((fputc(c_lead,f) == EOF) || (fputc(c_trail,f) == EOF))
return(WEOF);
return wc;
}
/* Note: In the new Unicode standard lead is named "high", and trail is name "low". */
/* check for lead without a trail */
if ((lead >= 0xd800) && (lead < 0xdc00) && ((wc < 0xdc00) || (wc >= 0xe000))) {
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_UTF8_FPRINTF(stderr, _("error: Invalid surrogate pair. Missing low surrogate.\n"));
ipFlag->status |= UNICODE_CONVERSION_ERROR ;
return(WEOF);
}
if ((wc >= 0xd800) && (wc < 0xdc00)) { /* Surrogate lead */
/* fprintf(stderr, "UTF-16 lead %x\n",wc); */
lead = (wchar_t)wc; /* lead (high) surrogate */
return(wc);
}
if ((wc >= 0xdc00) && (wc < 0xe000)) { /* Surrogate trail */
static wchar_t trail;
/* check for trail without a lead */
if ((lead < 0xd800) || (lead >= 0xdc00)) {
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_UTF8_FPRINTF(stderr, _("error: Invalid surrogate pair. Missing high surrogate.\n"));
ipFlag->status |= UNICODE_CONVERSION_ERROR ;
return(WEOF);
}
/* fprintf(stderr, "UTF-16 trail %x\n",wc); */
trail = (wchar_t)wc; /* trail (low) surrogate */
#if defined(_WIN32) || defined(__CYGWIN__)
/* On Windows (including Cygwin) wchar_t is 16 bit */
/* We cannot decode an UTF-16 surrogate pair, because it will
not fit in a 16 bit wchar_t. */
wstr[0] = lead;
wstr[1] = trail;
wstr[2] = L'\0';
lead = 0x01; /* make lead invalid */
#else
/* On Unix wchar_t is 32 bit */
/* When we don't decode the UTF-16 surrogate pair, wcstombs() does not
* produce the same UTF-8 as WideCharToMultiByte(). The UTF-8 output
* produced by wcstombs() is bigger, because it just translates the wide
* characters in the range 0xD800..0xDBFF individually to UTF-8 sequences
* (although these code points are reserved for use only as surrogate
* pairs in UTF-16).
*
* Some smart viewers can still display this UTF-8 correctly (like Total
* Commander lister), however the UTF-8 is not readable by Windows
* Notepad (on Windows 7). When we decode the UTF-16 surrogate pairs
* ourselves the wcstombs() UTF-8 output is identical to what
* WideCharToMultiByte() produces, and is readable by Notepad.
*
* Surrogate halves in UTF-8 are invalid. See also
* http://en.wikipedia.org/wiki/UTF-8#Invalid_code_points
* http://tools.ietf.org/html/rfc3629#page-5
* It is a bug in (some implemenatations of) wcstombs().
* On Cygwin 1.7 wcstombs() produces correct UTF-8 from UTF-16 surrogate pairs.
*/
/* Decode UTF-16 surrogate pair */
wstr[0] = 0x10000;
wstr[0] += (lead & 0x03FF) << 10;
wstr[0] += (trail & 0x03FF);
wstr[1] = L'\0';
lead = 0x01; /* make lead invalid */
/* fprintf(stderr, "UTF-32 %x\n",wstr[0]); */
#endif
} else {
wstr[0] = (wchar_t)wc;
wstr[1] = L'\0';
}
if (wc == 0x0000) {
if (fputc(0, f) == EOF)
return(WEOF);
return(wc);
}
#if (defined(_WIN32) && !defined(__CYGWIN__))
/* The WC_ERR_INVALID_CHARS flag is available since Windows Vista (0x0600). It enables checking for
invalid input characters. */
#if WINVER >= 0x0600
dwFlags = WC_ERR_INVALID_CHARS;
#else
dwFlags = 0;
#endif
/* On Windows we convert UTF-16 always to UTF-8 or GB18030 */
if (ipFlag->locale_target == TARGET_GB18030) {
len = (size_t)(WideCharToMultiByte(54936, dwFlags, wstr, -1, mbs, sizeof(mbs), NULL, NULL) -1);
} else {
len = (size_t)(WideCharToMultiByte(CP_UTF8, dwFlags, wstr, -1, mbs, sizeof(mbs), NULL, NULL) -1);
}
#else
/* On Unix we convert UTF-16 to the locale encoding */
len = wcstombs(mbs, wstr, sizeof(mbs));
/* fprintf(stderr, "len %d\n",len); */
#endif
if ( len == (size_t)(-1) ) {
/* Stop when there is a conversion error */
/* On Windows we convert UTF-16 always to UTF-8 or GB18030 */
if (ipFlag->verbose) {
#if (defined(_WIN32) && !defined(__CYGWIN__))
d2u_PrintLastError(progname);
#else
char *errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s:", progname);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
#endif
}
ipFlag->status |= UNICODE_CONVERSION_ERROR ;
return(WEOF);
} else {
size_t i;
for (i=0; i<len; i++) {
if (fputc(mbs[i], f) == EOF)
return(WEOF);
}
}
return(wc);
}
#endif
|