命令简介:
dos2unix是将Windows格式文件转换为Unix、Linux格式的实用命令。Windows格式文件的换行符为 ,而Unix&Linux文件的换行符为 . dos2unix命令其实就是将文件中的 转换为 。
而unix2dos则是和dos2unix互为孪生的一个命令,它是将Linux&Unix格式文件转换为Windows格式文件的命令。
命令语法:
dos2unix [options] [-c convmode] [-o file ...] [-n infile outfile ...]
unix2dos [options] [-c convmode] [-o file ...] [-n infile outfile ...]
命令参数:
此命令参数是Red Hat Enterprise Linux Server release 5.7下dos2unix命令参数,不同版本Linux的dos2nnix命令参数有可能不同。
参数 | 长参数 | 描叙 |
-h |
| 显示命令dos2unix联机帮助信息。 |
-k |
| 保持文件时间戳不变 |
-q |
| 静默模式,不输出转换结果信息等 |
-V |
| 显示命令版本信息 |
-c |
| 转换模式 |
-o |
| 在源文件转换,默认参数 |
-n |
| 保留原本的旧档,将转换后的内容输出到新档案.默认都会直接在原来的文件上修改, |
使用示例:
1: 查看dos2unix命令的帮助信息
[root@DB-Server myscript]# man dos2unix
[root@DB-Server myscript]# dos2unix -h
dos2unix Copyright (c) 1994-1995 Benjamin Lin
Copyright (c) 1998 Bernd Johannes Wuebben (Version 3.0)
Copyright (c) 1998 Christian Wurll (Version 3.1)
Usage: dos2unix [-hkqV] [-c convmode] [-o file ...] [-n infile outfile ...]
-h --help give this help
-k --keepdate keep output file date
-q --quiet quiet mode, suppress all warnings
always on in stdin->stdout mode
-V --version display version number
-c --convmode conversion mode
convmode ASCII, 7bit, ISO, Mac, default to ASCII
-l --newline add additional newline in all but Mac convmode
-o --oldfile write to old file
file ... files to convert in old file mode
-n --newfile write to new file
infile original file in new file mode
outfile output file in new file mode
2: dos2unix filename 将Windows格式文本转换为Unix&Linux格式文件
1: [root@DB-Server myscript]# cat -v test.sh
2: . /home/oracle/.bash_profile^M
3: echo ' '^M
4: date^M
5: echo ' '^M
6: ^M
7: sqlplus test/test @/home/oracle/scripts/test.sql^M
8: ^M
9: echo ' '^M
10: date^M
11: echo ' '^M
12: [root@DB-Server myscript]# dos2unix test.sh
13: dos2unix: converting file test.sh to UNIX format ...
14: [root@DB-Server myscript]# cat -v test.sh
15: . /home/oracle/.bash_profile
16: echo ' '
17: date
18: echo ' '
19:
20: sqlplus test/test @/home/oracle/scripts/test.sql
21:
22: echo ' '
23: date
24: echo ' '
3: dos2unix 可以一次转换多个文件
1: dos2unix filename1 filename2 filename3
4: 默认情况下会在源文件上进行转换,如果需要保留源文件,那么可以使用参数-n dos2unix -n oldfilename newfilename
1: [root@DB-Server myscript]# dos2unix -n dosfile linuxfile
2: dos2unix: converting file dosfile to file linuxfile in UNIX format ...
3: [root@DB-Server myscript]# cat -v dosfile
4: it is a windows dos file^M
5: you should convert to unix&linux format^M
6: [root@DB-Server myscript]# cat -v linuxfile
7: it is a windows dos file
8: you should convert to unix&linux format
9: [root@DB-Server myscript]#
5:保持文件时间戳不变
1: [root@DB-Server myscript]# ls -lrt dosfile
2: -rw-r--r-- 1 root root 67 Dec 26 11:46 dosfile
3: [root@DB-Server myscript]# dos2unix dosfile
4: dos2unix: converting file dosfile to UNIX format ...
5: [root@DB-Server myscript]# ls -lrt dosfile
6: -rw-r--r-- 1 root root 65 Dec 26 11:58 dosfile
7: [root@DB-Server myscript]# dos2unix -k dosfile
8: dos2unix: converting file dosfile to UNIX format ...
9: [root@DB-Server myscript]# ls -lrt dosfile
10: -rw-r--r-- 1 root root 65 Dec 26 11:58 dosfile
6:静默模式格式化文件
1: [root@DB-Server myscript]# unix2dos -q dosfile
2:
3: [root@DB-Server myscript]#
dos2unix的下载地址为http://sourceforge.net/projects/dos2unix/ ,可以从上面下载最新版本的dos2unix、unix2dos等命令工具以及相关文档,dos2unix的源码如下所示
1: /*
2:
3: * Name: dos2unix
4:
5: * Documentation:
6:
7: * Remove cr ('x0d') characters from a file.
8:
9: *
10:
11: * The dos2unix package is distributed under FreeBSD style license.
12:
13: * See also http://www.freebsd.org/copyright/freebsd-license.html
14:
15: * --------
16:
17: *
18:
19: * Copyright (C) 2009-2015 Erwin Waterlander
20:
21: * Copyright (C) 1998 Christian Wurll
22:
23: * Copyright (C) 1998 Bernd Johannes Wuebben
24:
25: * Copyright (C) 1994-1995 Benjamin Lin.
26:
27: * All rights reserved.
28:
29: *
30:
31: * Redistribution and use in source and binary forms, with or without
32:
33: * modification, are permitted provided that the following conditions
34:
35: * are met:
36:
37: * 1. Redistributions of source code must retain the above copyright
38:
39: * notice, this list of conditions and the following disclaimer.
40:
41: * 2. Redistributions in binary form must reproduce the above copyright
42:
43: * notice in the documentation and/or other materials provided with
44:
45: * the distribution.
46:
47: *
48:
49: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
50:
51: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52:
53: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
54:
55: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
56:
57: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58:
59: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
60:
61: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
62:
63: * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
64:
65: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
66:
67: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
68:
69: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
70:
71: *
72:
73: * == 1.0 == 1989.10.04 == John Birchfield (jb@koko.csustan.edu)
74:
75: * == 1.1 == 1994.12.20 == Benjamin Lin (blin@socs.uts.edu.au)
76:
77: * Cleaned up for Borland C/C++ 4.02
78:
79: * == 1.2 == 1995.03.16 == Benjamin Lin (blin@socs.uts.edu.au)
80:
81: * Modified to more conform to UNIX style.
82:
83: * == 2.0 == 1995.03.19 == Benjamin Lin (blin@socs.uts.edu.au)
84:
85: * Rewritten from scratch.
86:
87: * == 2.1 == 1995.03.29 == Benjamin Lin (blin@socs.uts.edu.au)
88:
89: * Conversion to SunOS charset implemented.
90:
91: * == 2.2 == 1995.03.30 == Benjamin Lin (blin@socs.uts.edu.au)
92:
93: * Fixed a bug in 2.1 where in new-file mode, if outfile already exists
94:
95: * conversion can not be completed properly.
96:
97: *
98:
99: * Added Mac text file translation, i.e. to conversion
100:
101: * Bernd Johannes Wuebben, wuebben@kde.org
102:
103: * Wed Feb 4 19:12:58 EST 1998
104:
105: *
106:
107: * Added extra newline if ^M occurs
108:
109: * Christian Wurll, wurll@ira.uka.de
110:
111: * Thu Nov 19 1998
112:
113: *
114:
115: * See ChangeLog.txt for complete version history.
116:
117: *
118:
119: */
120:
121:
122:
123: /* #define DEBUG 1 */
124:
125: #define __DOS2UNIX_C
126:
127:
128: #include "common.h"
129:
130: #include "dos2unix.h"
131:
132: # if (defined(_WIN32) && !defined(__CYGWIN__))
133:
134: #include <windows.h>
135:
136: #endif
137:
138: #ifdef D2U_UNICODE
139:
140: #if !defined(__MSDOS__) && !defined(_WIN32) && !defined(__OS2__) /* Unix, Cygwin */
141:
142: # include <langinfo.h>
143:
144: #endif
145:
146: #endif
147:
148:
149: void PrintLicense(void)
150:
151: {
152:
153: printf("%s", _("
154:
155: Copyright (C) 2009-2015 Erwin Waterlander
156:
157: Copyright (C) 1998 Christian Wurll (Version 3.1)
158:
159: Copyright (C) 1998 Bernd Johannes Wuebben (Version 3.0)
160:
161: Copyright (C) 1994-1995 Benjamin Lin
162:
163: All rights reserved.
"));
164:
165: PrintBSDLicense();
166:
167: }
168:
169:
170: #ifdef D2U_UNICODE
171:
172: wint_t StripDelimiterW(FILE* ipInF, FILE* ipOutF, CFlag *ipFlag, wint_t CurChar, unsigned int *converted, const char *progname)
173:
174: {
175:
176: wint_t TempNextChar;
177:
178: /* CurChar is always CR (x0d) */
179:
180: /* In normal dos2unix mode put nothing (skip CR). */
181:
182: /* Don't modify Mac files when in dos2unix mode. */
183:
184: if ( (TempNextChar = d2u_getwc(ipInF, ipFlag->bomtype)) != WEOF) {
185:
186: if (d2u_ungetwc( TempNextChar, ipInF, ipFlag->bomtype) == WEOF) { /* put back peek char */
187:
188: d2u_getc_error(ipFlag,progname);
189:
190: return WEOF;
191:
192: }
193:
194: if ( TempNextChar != 0x0a ) {
195:
196: if (d2u_putwc(CurChar, ipOutF, ipFlag, progname) == WEOF) { /* Mac line, put CR */
197:
198: d2u_putwc_error(ipFlag,progname);
199:
200: return WEOF;
201:
202: }
203:
204: } else {
205:
206: (*converted)++;
207:
208: if (ipFlag->NewLine) { /* add additional LF? */
209:
210: if (d2u_putwc(0x0a, ipOutF, ipFlag, progname) == WEOF) {
211:
212: d2u_putwc_error(ipFlag,progname);
213:
214: return WEOF;
215:
216: }
217:
218: }
219:
220: }
221:
222: } else {
223:
224: if (ferror(ipInF)) {
225:
226: d2u_getc_error(ipFlag,progname);
227:
228: return WEOF;
229:
230: }
231:
232: if ( CurChar == 0x0d ) { /* EOF: last Mac line delimiter (CR)? */
233:
234: if (d2u_putwc(CurChar, ipOutF, ipFlag, progname) == WEOF) {
235:
236: d2u_putwc_error(ipFlag,progname);
237:
238: return WEOF;
239:
240: }
241:
242: }
243:
244: }
245:
246: return CurChar;
247:
248: }
249:
250: #endif
251:
252:
253: /* CUR NEXT
254:
255: 0xd(CR) 0xa(LF) => put LF if option -l was used
256:
257: 0xd(CR) ! 0xa(LF) => put CR
258:
259: 0xd(CR) EOF => put CR
260:
261: */
262:
263: int StripDelimiter(FILE* ipInF, FILE* ipOutF, CFlag *ipFlag, int CurChar, unsigned int *converted, const char *progname)
264:
265: {
266:
267: int TempNextChar;
268:
269: /* CurChar is always CR (x0d) */
270:
271: /* In normal dos2unix mode put nothing (skip CR). */
272:
273: /* Don't modify Mac files when in dos2unix mode. */
274:
275: if ( (TempNextChar = fgetc(ipInF)) != EOF) {
276:
277: if (ungetc( TempNextChar, ipInF ) == EOF) { /* put back peek char */
278:
279: d2u_getc_error(ipFlag,progname);
280:
281: return EOF;
282:
283: }
284:
285: if ( TempNextChar != 'x0a' ) {
286:
287: if (fputc( CurChar, ipOutF ) == EOF) { /* Mac line, put CR */
288:
289: d2u_putc_error(ipFlag,progname);
290:
291: return EOF;
292:
293: }
294:
295: } else {
296:
297: (*converted)++;
298:
299: if (ipFlag->NewLine) { /* add additional LF? */
300:
301: if (fputc('x0a', ipOutF) == EOF) {
302:
303: d2u_putc_error(ipFlag,progname);
304:
305: return EOF;
306:
307: }
308:
309: }
310:
311: }
312:
313: } else {
314:
315: if (ferror(ipInF)) {
316:
317: d2u_getc_error(ipFlag,progname);
318:
319: return EOF;
320:
321: }
322:
323: if ( CurChar == 'x0d' ) { /* EOF: last Mac line delimiter (CR)? */
324:
325: if (fputc( CurChar, ipOutF ) == EOF) {
326:
327: d2u_putc_error(ipFlag,progname);
328:
329: return EOF;
330:
331: }
332:
333: }
334:
335: }
336:
337: return CurChar;
338:
339: }
340:
341:
342: /* converts stream ipInF to UNIX format text and write to stream ipOutF
343:
344: * RetVal: 0 if success
345:
346: * -1 otherwise
347:
348: */
349:
350: #ifdef D2U_UNICODE
351:
352: int ConvertDosToUnixW(FILE* ipInF, FILE* ipOutF, CFlag *ipFlag, const char *progname)
353:
354: {
355:
356: int RetVal = 0;
357:
358: wint_t TempChar;
359:
360: wint_t TempNextChar;
361:
362: unsigned int line_nr = 1;
363:
364: unsigned int converted = 0;
365:
366:
367: ipFlag->status = 0;
368:
369:
370: /* CR-LF -> LF */
371:
372: /* LF -> LF, in case the input file is a Unix text file */
373:
374: /* CR -> CR, in dos2unix mode (don't modify Mac file) */
375:
376: /* CR -> LF, in Mac mode */
377:
378: /* x0a = Newline/Line Feed (LF) */
379:
380: /* x0d = Carriage Return (CR) */
381:
382:
383: switch (ipFlag->FromToMode)
384:
385: {
386:
387: case FROMTO_DOS2UNIX: /* dos2unix */
388:
389: while ((TempChar = d2u_getwc(ipInF, ipFlag->bomtype)) != WEOF) { /* get character */
390:
391: if ((ipFlag->Force == 0) &&
392:
393: (TempChar < 32) &&
394:
395: (TempChar != 0x0a) && /* Not an LF */
396:
397: (TempChar != 0x0d) && /* Not a CR */
398:
399: (TempChar != 0x09) && /* Not a TAB */
400:
401: (TempChar != 0x0c)) { /* Not a form feed */
402:
403: RetVal = -1;
404:
405: ipFlag->status |= BINARY_FILE ;
406:
407: if (ipFlag->verbose) {
408:
409: if ((ipFlag->stdio_mode) && (!ipFlag->error)) ipFlag->error = 1;
410:
411: d2u_fprintf(stderr, "%s: ", progname);
412:
413: d2u_fprintf(stderr, _("Binary symbol 0x00%02X found at line %u "),TempChar, line_nr);
414:
415: }
416:
417: break;
418:
419: }
420:
421: if (TempChar != 0x0d) {
422:
423: if (TempChar == 0x0a) /* Count all DOS and Unix line breaks */
424:
425: ++line_nr;
426:
427: if (d2u_putwc(TempChar, ipOutF, ipFlag, progname) == WEOF) {
428:
429: RetVal = -1;
430:
431: d2u_putwc_error(ipFlag,progname);
432:
433: break;
434:
435: }
436:
437: } else {
438:
439: if (StripDelimiterW( ipInF, ipOutF, ipFlag, TempChar, &converted, progname) == WEOF) {
440:
441: RetVal = -1;
442:
443: break;
444:
445: }
446:
447: }
448:
449: }
450:
451: if ((TempChar == WEOF) && ferror(ipInF)) {
452:
453: RetVal = -1;
454:
455: d2u_getc_error(ipFlag,progname);
456:
457: }
458:
459: break;
460:
461: case FROMTO_MAC2UNIX: /* mac2unix */
462:
463: while ((TempChar = d2u_getwc(ipInF, ipFlag->bomtype)) != WEOF) {
464:
465: if ((ipFlag->Force == 0) &&
466:
467: (TempChar < 32) &&
468:
469: (TempChar != 0x0a) && /* Not an LF */
470:
471: (TempChar != 0x0d) && /* Not a CR */
472:
473: (TempChar != 0x09) && /* Not a TAB */
474:
475: (TempChar != 0x0c)) { /* Not a form feed */
476:
477: RetVal = -1;
478:
479: ipFlag->status |= BINARY_FILE ;
480:
481: if (ipFlag->verbose) {
482:
483: if ((ipFlag->stdio_mode) && (!ipFlag->error)) ipFlag->error = 1;
484:
485: d2u_fprintf(stderr, "%s: ", progname);
486:
487: d2u_fprintf(stderr, _("Binary symbol 0x00%02X found at line %u "), TempChar, line_nr);
488:
489: }
490:
491: break;
492:
493: }
494:
495: if ((TempChar != 0x0d)) {
496:
497: if (TempChar == 0x0a) /* Count all DOS and Unix line breaks */
498:
499: ++line_nr;
500:
501: if(d2u_putwc(TempChar, ipOutF, ipFlag, progname) == WEOF) {
502:
503: RetVal = -1;
504:
505: d2u_putwc_error(ipFlag,progname);
506:
507: break;
508:
509: }
510:
511: }
512:
513: else{
514:
515: /* TempChar is a CR */
516:
517: if ( (TempNextChar = d2u_getwc(ipInF, ipFlag->bomtype)) != WEOF) {
518:
519: if (d2u_ungetwc( TempNextChar, ipInF, ipFlag->bomtype) == WEOF) { /* put back peek char */
520:
521: d2u_getc_error(ipFlag,progname);
522:
523: RetVal = -1;
524:
525: break;
526:
527: }
528:
529: /* Don't touch this delimiter if it's a CR,LF pair. */
530:
531: if ( TempNextChar == 0x0a ) {
532:
533: if (d2u_putwc(0x0d, ipOutF, ipFlag, progname) == WEOF) { /* put CR, part of DOS CR-LF */
534:
535: d2u_putwc_error(ipFlag,progname);
536:
537: RetVal = -1;
538:
539: break;
540:
541: }
542:
543: continue;
544:
545: }
546:
547: }
548:
549: if (d2u_putwc(0x0a, ipOutF, ipFlag, progname) == WEOF) { /* MAC line end (CR). Put LF */
550:
551: RetVal = -1;
552:
553: d2u_putwc_error(ipFlag,progname);
554:
555: break;
556:
557: }
558:
559: converted++;
560:
561: line_nr++; /* Count all Mac line breaks */
562:
563: if (ipFlag->NewLine) { /* add additional LF? */
564:
565: if (d2u_putwc(0x0a, ipOutF, ipFlag, progname) == WEOF) {
566:
567: RetVal = -1;
568:
569: d2u_putwc_error(ipFlag,progname);
570:
571: break;
572:
573: }
574:
575: }
576:
577: }
578:
579: }
580:
581: if ((TempChar == WEOF) && ferror(ipInF)) {
582:
583: RetVal = -1;
584:
585: d2u_getc_error(ipFlag,progname);
586:
587: }
588:
589: break;
590:
591: default: /* unknown FromToMode */
592:
593: ;
594:
595: #if DEBUG
596:
597: d2u_fprintf(stderr, "%s: ", progname);
598:
599: d2u_fprintf(stderr, _("program error, invalid conversion mode %d "),ipFlag->FromToMode);
600:
601: exit(1);
602:
603: #endif
604:
605: }
606:
607: if (ipFlag->status & UNICODE_CONVERSION_ERROR)
608:
609: ipFlag->line_nr = line_nr;
610:
611: if ((RetVal == 0) && (ipFlag->verbose > 1)) {
612:
613: d2u_fprintf(stderr, "%s: ", progname);
614:
615: d2u_fprintf(stderr, _("Converted %u out of %u line breaks. "), converted, line_nr -1);
616:
617: }
618:
619: return RetVal;
620:
621: }
622:
623: #endif
624:
625:
626: /* converts stream ipInF to UNIX format text and write to stream ipOutF
627:
628: * RetVal: 0 if success
629:
630: * -1 otherwise
631:
632: */
633:
634: int ConvertDosToUnix(FILE* ipInF, FILE* ipOutF, CFlag *ipFlag, const char *progname)
635:
636: {
637:
638: int RetVal = 0;
639:
640: int TempChar;
641:
642: int TempNextChar;
643:
644: int *ConvTable;
645:
646: unsigned int line_nr = 1;
647:
648: unsigned int converted = 0;
649:
650:
651: ipFlag->status = 0;
652:
653:
654: switch (ipFlag->ConvMode) {
655:
656: case CONVMODE_ASCII: /* ascii */
657:
658: case CONVMODE_UTF16LE: /* Assume UTF-16LE, bomtype = FILE_UTF8 or GB18030 */
659:
660: case CONVMODE_UTF16BE: /* Assume UTF-16BE, bomtype = FILE_UTF8 or GB18030 */
661:
662: ConvTable = D2UAsciiTable;
663:
664: break;
665:
666: case CONVMODE_7BIT: /* 7bit */
667:
668: ConvTable = D2U7BitTable;
669:
670: break;
671:
672: case CONVMODE_437: /* iso */
673:
674: ConvTable = D2UIso437Table;
675:
676: break;
677:
678: case CONVMODE_850: /* iso */
679:
680: ConvTable = D2UIso850Table;
681:
682: break;
683:
684: case CONVMODE_860: /* iso */
685:
686: ConvTable = D2UIso860Table;
687:
688: break;
689:
690: case CONVMODE_863: /* iso */
691:
692: ConvTable = D2UIso863Table;
693:
694: break;
695:
696: case CONVMODE_865: /* iso */
697:
698: ConvTable = D2UIso865Table;
699:
700: break;
701:
702: case CONVMODE_1252: /* iso */
703:
704: ConvTable = D2UIso1252Table;
705:
706: break;
707:
708: default: /* unknown convmode */
709:
710: ipFlag->status |= WRONG_CODEPAGE ;
711:
712: return(-1);
713:
714: }
715:
716: /* Turn off ISO and 7-bit conversion for Unicode text files */
717:
718: if (ipFlag->bomtype > 0)
719:
720: ConvTable = D2UAsciiTable;
721:
722:
723: if ((ipFlag->ConvMode > CONVMODE_7BIT) && (ipFlag->verbose)) { /* not ascii or 7bit */
724:
725: d2u_fprintf(stderr, "%s: ", progname);
726:
727: d2u_fprintf(stderr, _("using code page %d. "), ipFlag->ConvMode);
728:
729: }
730:
731:
732: /* CR-LF -> LF */
733:
734: /* LF -> LF, in case the input file is a Unix text file */
735:
736: /* CR -> CR, in dos2unix mode (don't modify Mac file) */
737:
738: /* CR -> LF, in Mac mode */
739:
740: /* x0a = Newline/Line Feed (LF) */
741:
742: /* x0d = Carriage Return (CR) */
743:
744:
745: switch (ipFlag->FromToMode) {
746:
747: case FROMTO_DOS2UNIX: /* dos2unix */
748:
749: while ((TempChar = fgetc(ipInF)) != EOF) { /* get character */
750:
751: if ((ipFlag->Force == 0) &&
752:
753: (TempChar < 32) &&
754:
755: (TempChar != 'x0a') && /* Not an LF */
756:
757: (TempChar != 'x0d') && /* Not a CR */
758:
759: (TempChar != 'x09') && /* Not a TAB */
760:
761: (TempChar != 'x0c')) { /* Not a form feed */
762:
763: RetVal = -1;
764:
765: ipFlag->status |= BINARY_FILE ;
766:
767: if (ipFlag->verbose) {
768:
769: if ((ipFlag->stdio_mode) && (!ipFlag->error)) ipFlag->error = 1;
770:
771: d2u_fprintf(stderr, "%s: ", progname);
772:
773: d2u_fprintf(stderr, _("Binary symbol 0x%02X found at line %u "),TempChar, line_nr);
774:
775: }
776:
777: break;
778:
779: }
780:
781: if (TempChar != 'x0d') {
782:
783: if (TempChar == 'x0a') /* Count all DOS and Unix line breaks */
784:
785: ++line_nr;
786:
787: if (fputc(ConvTable[TempChar], ipOutF) == EOF) {
788:
789: RetVal = -1;
790:
791: d2u_putc_error(ipFlag,progname);
792:
793: break;
794:
795: }
796:
797: } else {
798:
799: if (StripDelimiter( ipInF, ipOutF, ipFlag, TempChar, &converted, progname) == EOF) {
800:
801: RetVal = -1;
802:
803: break;
804:
805: }
806:
807: }
808:
809: }
810:
811: if ((TempChar == EOF) && ferror(ipInF)) {
812:
813: RetVal = -1;
814:
815: d2u_getc_error(ipFlag,progname);
816:
817: }
818:
819: break;
820:
821: case FROMTO_MAC2UNIX: /* mac2unix */
822:
823: while ((TempChar = fgetc(ipInF)) != EOF) {
824:
825: if ((ipFlag->Force == 0) &&
826:
827: (TempChar < 32) &&
828:
829: (TempChar != 'x0a') && /* Not an LF */
830:
831: (TempChar != 'x0d') && /* Not a CR */
832:
833: (TempChar != 'x09') && /* Not a TAB */
834:
835: (TempChar != 'x0c')) { /* Not a form feed */
836:
837: RetVal = -1;
838:
839: ipFlag->status |= BINARY_FILE ;
840:
841: if (ipFlag->verbose) {
842:
843: if ((ipFlag->stdio_mode) && (!ipFlag->error)) ipFlag->error = 1;
844:
845: d2u_fprintf(stderr, "%s: ", progname);
846:
847: d2u_fprintf(stderr, _("Binary symbol 0x%02X found at line %u "),TempChar, line_nr);
848:
849: }
850:
851: break;
852:
853: }
854:
855: if ((TempChar != 'x0d')) {
856:
857: if (TempChar == 'x0a') /* Count all DOS and Unix line breaks */
858:
859: ++line_nr;
860:
861: if(fputc(ConvTable[TempChar], ipOutF) == EOF) {
862:
863: RetVal = -1;
864:
865: d2u_putc_error(ipFlag,progname);
866:
867: break;
868:
869: }
870:
871: }
872:
873: else{
874:
875: /* TempChar is a CR */
876:
877: if ( (TempNextChar = fgetc(ipInF)) != EOF) {
878:
879: if (ungetc( TempNextChar, ipInF ) == EOF) { /* put back peek char */
880:
881: d2u_getc_error(ipFlag,progname);
882:
883: RetVal = -1;
884:
885: break;
886:
887: }
888:
889: /* Don't touch this delimiter if it's a CR,LF pair. */
890:
891: if ( TempNextChar == 'x0a' ) {
892:
893: if (fputc('x0d', ipOutF) == EOF) { /* put CR, part of DOS CR-LF */
894:
895: RetVal = -1;
896:
897: d2u_putc_error(ipFlag,progname);
898:
899: break;
900:
901: }
902:
903: continue;
904:
905: }
906:
907: }
908:
909: if (fputc('x0a', ipOutF) == EOF) { /* MAC line end (CR). Put LF */
910:
911: RetVal = -1;
912:
913: d2u_putc_error(ipFlag,progname);
914:
915: break;
916:
917: }
918:
919: converted++;
920:
921: line_nr++; /* Count all Mac line breaks */
922:
923: if (ipFlag->NewLine) { /* add additional LF? */
924:
925: if (fputc('x0a', ipOutF) == EOF) {
926:
927: RetVal = -1;
928:
929: d2u_putc_error(ipFlag,progname);
930:
931: break;
932:
933: }
934:
935: }
936:
937: }
938:
939: }
940:
941: if ((TempChar == EOF) && ferror(ipInF)) {
942:
943: RetVal = -1;
944:
945: d2u_getc_error(ipFlag,progname);
946:
947: }
948:
949: break;
950:
951: default: /* unknown FromToMode */
952:
953: ;
954:
955: #if DEBUG
956:
957: d2u_fprintf(stderr, "%s: ", progname);
958:
959: d2u_fprintf(stderr, _("program error, invalid conversion mode %d "),ipFlag->FromToMode);
960:
961: exit(1);
962:
963: #endif
964:
965: }
966:
967: if ((RetVal == 0) && (ipFlag->verbose > 1)) {
968:
969: d2u_fprintf(stderr, "%s: ", progname);
970:
971: d2u_fprintf(stderr, _("Converted %u out of %u line breaks. "),converted, line_nr -1);
972:
973: }
974:
975: return RetVal;
976:
977: }
978:
979:
980:
981: int main (int argc, char *argv[])
982:
983: {
984:
985: /* variable declarations */
986:
987: char progname[9];
988:
989: CFlag *pFlag;
990:
991: char *ptr;
992:
993: char localedir[1024];
994:
995: # ifdef __MINGW64__
996:
997: int _dowildcard = -1; /* enable wildcard expansion for Win64 */
998:
999: # endif
1000:
1001: int argc_new;
1002:
1003: char **argv_new;
1004:
1005:
1006: progname[8] = ' ';
1007:
1008: strcpy(progname,"dos2unix");
1009:
1010:
1011: #ifdef ENABLE_NLS
1012:
1013: ptr = getenv("DOS2UNIX_LOCALEDIR");
1014:
1015: if (ptr == NULL)
1016:
1017: strcpy(localedir,LOCALEDIR);
1018:
1019: else {
1020:
1021: if (strlen(ptr) < sizeof(localedir))
1022:
1023: strcpy(localedir,ptr);
1024:
1025: else {
1026:
1027: d2u_fprintf(stderr,"%s: ",progname);
1028:
1029: d2u_ansi_fprintf(stderr, "%s", _("error: Value of environment variable DOS2UNIX_LOCALEDIR is too long. "));
1030:
1031: strcpy(localedir,LOCALEDIR);
1032:
1033: }
1034:
1035: }
1036:
1037: #endif
1038:
1039:
1040: #if defined(ENABLE_NLS) || (defined(D2U_UNICODE) && !defined(__MSDOS__) && !defined(_WIN32) && !defined(__OS2__))
1041:
1042: /* setlocale() is also needed for nl_langinfo() */
1043:
1044: setlocale (LC_ALL, "");
1045:
1046: #endif
1047:
1048:
1049: #ifdef ENABLE_NLS
1050:
1051: bindtextdomain (PACKAGE, localedir);
1052:
1053: textdomain (PACKAGE);
1054:
1055: #endif
1056:
1057:
1058:
1059: /* variable initialisations */
1060:
1061: pFlag = (CFlag*)malloc(sizeof(CFlag));
1062:
1063: if (pFlag == NULL) {
1064:
1065: d2u_fprintf(stderr, "dos2unix:");
1066:
1067: d2u_ansi_fprintf(stderr, " %s ", strerror(errno));
1068:
1069: return errno;
1070:
1071: }
1072:
1073: pFlag->FromToMode = FROMTO_DOS2UNIX; /* default dos2unix */
1074:
1075: pFlag->keep_bom = 0;
1076:
1077:
1078: if ( ((ptr=strrchr(argv[0],'/')) == NULL) && ((ptr=strrchr(argv[0],'\')) == NULL) )
1079:
1080: ptr = argv[0];
1081:
1082: else
1083:
1084: ptr++;
1085:
1086:
1087: if ((strcmpi("mac2unix", ptr) == 0) || (strcmpi("mac2unix.exe", ptr) == 0)) {
1088:
1089: pFlag->FromToMode = FROMTO_MAC2UNIX;
1090:
1091: strcpy(progname,"mac2unix");
1092:
1093: }
1094:
1095:
1096: #ifdef D2U_UNIFILE
1097:
1098: /* Get arguments in wide Unicode format in the Windows Command Prompt */
1099:
1100: wchar_t **wargv;
1101:
1102: char ***argv_glob;
1103:
1104:
1105: /* This does not support wildcard expansion (globbing) */
1106:
1107: wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
1108:
1109:
1110: argv_glob = (char ***)malloc(sizeof(char***));
1111:
1112: if (argv_glob == NULL) {
1113:
1114: d2u_fprintf(stderr, "%s:", progname);
1115:
1116: d2u_ansi_fprintf(stderr, " %s ", strerror(errno));
1117:
1118: return errno;
1119:
1120: }
1121:
1122: /* Glob the arguments and convert them to UTF-8 */
1123:
1124: argc_new = glob_warg(argc, wargv, argv_glob, pFlag, progname);
1125:
1126: argv_new = *argv_glob;
1127:
1128: #else
1129:
1130: argc_new = argc;
1131:
1132: argv_new = argv;
1133:
1134: #endif
1135:
1136:
1137: #ifdef D2U_UNICODE
1138:
1139: return parse_options(argc_new, argv_new, pFlag, localedir, progname, PrintLicense, ConvertDosToUnix, ConvertDosToUnixW);
1140:
1141: #else
1142:
1143: return parse_options(argc_new, argv_new, pFlag, localedir, progname, PrintLicense, ConvertDosToUnix);
1144:
1145: #endif
1146:
1147: }
1148: