summaryrefslogtreecommitdiff
path: root/unix2dos.c
diff options
context:
space:
mode:
Diffstat (limited to 'unix2dos.c')
-rw-r--r--unix2dos.c260
1 files changed, 154 insertions, 106 deletions
diff --git a/unix2dos.c b/unix2dos.c
index d26404d..791ee6c 100644
--- a/unix2dos.c
+++ b/unix2dos.c
@@ -55,6 +55,9 @@
#include "common.h"
#include "unix2dos.h"
+# if (defined(_WIN32) && !defined(__CYGWIN__))
+#include <windows.h>
+#endif
#ifdef D2U_UNICODE
#if !defined(__MSDOS__) && !defined(_WIN32) && !defined(__OS2__) /* Unix, Cygwin */
# include <langinfo.h>
@@ -71,27 +74,41 @@ All rights reserved.\n\n"));
}
#ifdef D2U_UNICODE
-void AddDOSNewLineW(FILE* ipOutF, CFlag *ipFlag, wint_t CurChar, wint_t PrevChar)
+wint_t AddDOSNewLineW(FILE* ipOutF, CFlag *ipFlag, wint_t CurChar, wint_t PrevChar, const char *progname)
{
if (ipFlag->NewLine) { /* add additional CR-LF? */
/* Don't add line ending if it is a DOS line ending. Only in case of Unix line ending. */
if ((CurChar == 0x0a) && (PrevChar != 0x0d)) {
- d2u_putwc(0x0d, ipOutF, ipFlag);
- d2u_putwc(0x0a, ipOutF, ipFlag);
+ if (d2u_putwc(0x0d, ipOutF, ipFlag, progname) == WEOF) {
+ d2u_putwc_error(ipFlag,progname);
+ return WEOF;
+ }
+ if (d2u_putwc(0x0a, ipOutF, ipFlag, progname) == WEOF) {
+ d2u_putwc_error(ipFlag,progname);
+ return WEOF;
+ }
}
}
+ return CurChar;
}
#endif
-void AddDOSNewLine(FILE* ipOutF, CFlag *ipFlag, int CurChar, int PrevChar)
+int AddDOSNewLine(FILE* ipOutF, CFlag *ipFlag, int CurChar, int PrevChar, const char *progname)
{
if (ipFlag->NewLine) { /* add additional CR-LF? */
/* Don't add line ending if it is a DOS line ending. Only in case of Unix line ending. */
if ((CurChar == '\x0a') && (PrevChar != '\x0d')) {
- fputc('\x0d', ipOutF);
- fputc('\x0a', ipOutF);
+ if (fputc('\x0d', ipOutF) == EOF) {
+ d2u_putc_error(ipFlag,progname);
+ return EOF;
+ }
+ if (fputc('\x0a', ipOutF) == EOF) {
+ d2u_putc_error(ipFlag,progname);
+ return EOF;
+ }
}
}
+ return CurChar;
}
/* converts stream ipInF to DOS format text and write to stream ipOutF
@@ -106,7 +123,6 @@ int ConvertUnixToDosW(FILE* ipInF, FILE* ipOutF, CFlag *ipFlag, const char *prog
wint_t PreviousChar = 0;
unsigned int line_nr = 1;
unsigned int converted = 0;
- char *errstr;
ipFlag->status = 0;
@@ -129,43 +145,53 @@ int ConvertUnixToDosW(FILE* ipInF, FILE* ipOutF, CFlag *ipFlag, const char *prog
ipFlag->status |= BINARY_FILE ;
if (ipFlag->verbose) {
if ((ipFlag->stdio_mode) && (!ipFlag->error)) ipFlag->error = 1;
- fprintf(stderr, "%s: ", progname);
- fprintf(stderr, _("Binary symbol 0x00%02X found at line %u\n"), TempChar, line_nr);
+ d2u_fprintf(stderr, "%s: ", progname);
+ d2u_fprintf(stderr, _("Binary symbol 0x00%02X found at line %u\n"), TempChar, line_nr);
}
break;
}
if (TempChar == 0x0a) {
- d2u_putwc(0x0d, ipOutF, ipFlag); /* got LF, put extra CR */
+ if (d2u_putwc(0x0d, ipOutF, ipFlag, progname) == WEOF) { /* got LF, put extra CR */
+ RetVal = -1;
+ d2u_putwc_error(ipFlag,progname);
+ break;
+ }
converted++;
} else {
if (TempChar == 0x0d) { /* got CR */
- if ((TempChar = d2u_getwc(ipInF, ipFlag->bomtype)) == WEOF) /* get next char (possibly LF) */
- TempChar = 0x0d; /* Read error, or end of file. */
- else {
- d2u_putwc(0x0d, ipOutF, ipFlag); /* put CR */
+ if ((TempChar = d2u_getwc(ipInF, ipFlag->bomtype)) == WEOF) { /* get next char (possibly LF) */
+ if (ferror(ipInF)) /* Read error */
+ break;
+ TempChar = 0x0d; /* end of file. */
+ } else {
+ if (d2u_putwc(0x0d, ipOutF, ipFlag, progname) == WEOF) { /* put CR */
+ RetVal = -1;
+ d2u_putwc_error(ipFlag,progname);
+ break;
+ }
PreviousChar = 0x0d;
}
}
}
if (TempChar == 0x0a) /* Count all DOS and Unix line breaks */
++line_nr;
- if (d2u_putwc(TempChar, ipOutF, ipFlag) == WEOF)
+ if (d2u_putwc(TempChar, ipOutF, ipFlag, progname) == WEOF)
{
RetVal = -1;
- if (ipFlag->verbose) {
- if (!(ipFlag->status & UNICODE_CONVERSION_ERROR)) {
- ipFlag->error = errno;
- errstr = strerror(errno);
- fprintf(stderr, "%s: ", progname);
- fprintf(stderr, _("can not write to output file: %s\n"), errstr);
- }
- }
+ d2u_putwc_error(ipFlag,progname);
break;
} else {
- AddDOSNewLineW( ipOutF, ipFlag, TempChar, PreviousChar);
+ if (AddDOSNewLineW( ipOutF, ipFlag, TempChar, PreviousChar, progname) == WEOF) {
+ RetVal = -1;
+ break;
+ }
}
PreviousChar = TempChar;
}
+ if ((TempChar == WEOF) && ferror(ipInF)) {
+ RetVal = -1;
+ d2u_getc_error(ipFlag,progname);
+ }
break;
case FROMTO_UNIX2MAC: /* unix2mac */
while ((TempChar = d2u_getwc(ipInF, ipFlag->bomtype)) != WEOF) {
@@ -179,22 +205,15 @@ int ConvertUnixToDosW(FILE* ipInF, FILE* ipOutF, CFlag *ipFlag, const char *prog
ipFlag->status |= BINARY_FILE ;
if (ipFlag->verbose) {
if ((ipFlag->stdio_mode) && (!ipFlag->error)) ipFlag->error = 1;
- fprintf(stderr, "%s: ", progname);
- fprintf(stderr, _("Binary symbol 0x00%02X found at line %u\n"), TempChar, line_nr);
+ d2u_fprintf(stderr, "%s: ", progname);
+ d2u_fprintf(stderr, _("Binary symbol 0x00%02X found at line %u\n"), TempChar, line_nr);
}
break;
}
if (TempChar != 0x0a) { /* Not an LF */
- if(d2u_putwc(TempChar, ipOutF, ipFlag) == WEOF) {
+ if(d2u_putwc(TempChar, ipOutF, ipFlag, progname) == WEOF) {
RetVal = -1;
- if (ipFlag->verbose) {
- if (!(ipFlag->status & UNICODE_CONVERSION_ERROR)) {
- ipFlag->error = errno;
- errstr = strerror(errno);
- fprintf(stderr, "%s: ", progname);
- fprintf(stderr, _("can not write to output file: %s\n"), errstr);
- }
- }
+ d2u_putwc_error(ipFlag,progname);
break;
}
PreviousChar = TempChar;
@@ -206,52 +225,48 @@ int ConvertUnixToDosW(FILE* ipInF, FILE* ipOutF, CFlag *ipFlag, const char *prog
++line_nr;
/* Don't touch this delimiter if it's a CR,LF pair. */
if ( PreviousChar == 0x0d ) {
- if (d2u_putwc(0x0a, ipOutF, ipFlag) == WEOF) { /* CR,LF pair. Put LF */
+ if (d2u_putwc(0x0a, ipOutF, ipFlag, progname) == WEOF) { /* CR,LF pair. Put LF */
RetVal = -1;
- if (ipFlag->verbose) {
- if (!(ipFlag->status & UNICODE_CONVERSION_ERROR)) {
- ipFlag->error = errno;
- errstr = strerror(errno);
- fprintf(stderr, "%s: ", progname);
- fprintf(stderr, _("can not write to output file: %s\n"), errstr);
- }
- }
+ d2u_putwc_error(ipFlag,progname);
break;
}
PreviousChar = TempChar;
continue;
}
PreviousChar = TempChar;
- if (d2u_putwc(0x0d, ipOutF, ipFlag) == WEOF) { /* Unix line end (LF). Put CR */
+ if (d2u_putwc(0x0d, ipOutF, ipFlag, progname) == WEOF) { /* Unix line end (LF). Put CR */
RetVal = -1;
- if (ipFlag->verbose) {
- if (!(ipFlag->status & UNICODE_CONVERSION_ERROR)) {
- ipFlag->error = errno;
- errstr = strerror(errno);
- fprintf(stderr, "%s: ", progname);
- fprintf(stderr, _("can not write to output file: %s\n"), errstr);
- }
- }
+ d2u_putwc_error(ipFlag,progname);
break;
}
converted++;
if (ipFlag->NewLine) { /* add additional CR? */
- d2u_putwc(0x0d, ipOutF, ipFlag);
+ if (d2u_putwc(0x0d, ipOutF, ipFlag, progname) == WEOF) {
+ RetVal = -1;
+ d2u_putwc_error(ipFlag,progname);
+ break;
+ }
}
}
}
+ if ((TempChar == WEOF) && ferror(ipInF)) {
+ RetVal = -1;
+ d2u_getc_error(ipFlag,progname);
+ }
break;
default: /* unknown FromToMode */
;
#if DEBUG
- fprintf(stderr, "%s: ", progname);
- fprintf(stderr, _("program error, invalid conversion mode %d\n"),ipFlag->FromToMode);
+ d2u_fprintf(stderr, "%s: ", progname);
+ d2u_fprintf(stderr, _("program error, invalid conversion mode %d\n"),ipFlag->FromToMode);
exit(1);
#endif
}
- if (ipFlag->verbose > 1) {
- fprintf(stderr, "%s: ", progname);
- fprintf(stderr, _("Converted %u out of %u line breaks.\n"), converted, line_nr -1);
+ if (ipFlag->status & UNICODE_CONVERSION_ERROR)
+ ipFlag->line_nr = line_nr;
+ if ((RetVal == 0) && (ipFlag->verbose > 1)) {
+ d2u_fprintf(stderr, "%s: ", progname);
+ d2u_fprintf(stderr, _("Converted %u out of %u line breaks.\n"), converted, line_nr -1);
}
return RetVal;
}
@@ -269,7 +284,6 @@ int ConvertUnixToDos(FILE* ipInF, FILE* ipOutF, CFlag *ipFlag, const char *progn
int *ConvTable;
unsigned int line_nr = 1;
unsigned int converted = 0;
- char *errstr;
ipFlag->status = 0;
@@ -309,8 +323,8 @@ int ConvertUnixToDos(FILE* ipInF, FILE* ipOutF, CFlag *ipFlag, const char *progn
ConvTable = U2DAsciiTable;
if ((ipFlag->ConvMode > CONVMODE_7BIT) && (ipFlag->verbose)) { /* not ascii or 7bit */
- fprintf(stderr, "%s: ", progname);
- fprintf(stderr, _("using code page %d.\n"), ipFlag->ConvMode);
+ d2u_fprintf(stderr, "%s: ", progname);
+ d2u_fprintf(stderr, _("using code page %d.\n"), ipFlag->ConvMode);
}
/* LF -> CR-LF */
@@ -331,21 +345,31 @@ int ConvertUnixToDos(FILE* ipInF, FILE* ipOutF, CFlag *ipFlag, const char *progn
ipFlag->status |= BINARY_FILE ;
if (ipFlag->verbose) {
if ((ipFlag->stdio_mode) && (!ipFlag->error)) ipFlag->error = 1;
- fprintf(stderr, "%s: ", progname);
- fprintf(stderr, _("Binary symbol 0x%02X found at line %u\n"), TempChar, line_nr);
+ d2u_fprintf(stderr, "%s: ", progname);
+ d2u_fprintf(stderr, _("Binary symbol 0x%02X found at line %u\n"), TempChar, line_nr);
}
break;
}
if (TempChar == '\x0a')
{
- fputc('\x0d', ipOutF); /* got LF, put extra CR */
+ if (fputc('\x0d', ipOutF) == EOF) { /* got LF, put extra CR */
+ RetVal = -1;
+ d2u_putc_error(ipFlag,progname);
+ break;
+ }
converted++;
} else {
if (TempChar == '\x0d') { /* got CR */
- if ((TempChar = fgetc(ipInF)) == EOF) /* get next char (possibly LF) */
- TempChar = '\x0d'; /* Read error, or end of file. */
- else {
- fputc('\x0d', ipOutF); /* put CR */
+ if ((TempChar = fgetc(ipInF)) == EOF) { /* get next char (possibly LF) */
+ if (ferror(ipInF)) /* Read error */
+ break;
+ TempChar = '\x0d'; /* end of file. */
+ } else {
+ if (fputc('\x0d', ipOutF) == EOF) { /* put CR */
+ RetVal = -1;
+ d2u_putc_error(ipFlag,progname);
+ break;
+ }
PreviousChar = '\x0d';
}
}
@@ -354,18 +378,20 @@ int ConvertUnixToDos(FILE* ipInF, FILE* ipOutF, CFlag *ipFlag, const char *progn
++line_nr;
if (fputc(ConvTable[TempChar], ipOutF) == EOF) { /* put LF or other char */
RetVal = -1;
- if (ipFlag->verbose) {
- ipFlag->error = errno;
- errstr = strerror(errno);
- fprintf(stderr, "%s: ", progname);
- fprintf(stderr, _("can not write to output file: %s\n"), errstr);
- }
+ d2u_putc_error(ipFlag,progname);
break;
} else {
- AddDOSNewLine( ipOutF, ipFlag, TempChar, PreviousChar);
+ if (AddDOSNewLine( ipOutF, ipFlag, TempChar, PreviousChar, progname) == EOF) {
+ RetVal = -1;
+ break;
+ }
}
PreviousChar = TempChar;
}
+ if ((TempChar == EOF) && ferror(ipInF)) {
+ RetVal = -1;
+ d2u_getc_error(ipFlag,progname);
+ }
break;
case FROMTO_UNIX2MAC: /* unix2mac */
while ((TempChar = fgetc(ipInF)) != EOF) {
@@ -379,20 +405,15 @@ int ConvertUnixToDos(FILE* ipInF, FILE* ipOutF, CFlag *ipFlag, const char *progn
ipFlag->status |= BINARY_FILE ;
if (ipFlag->verbose) {
if ((ipFlag->stdio_mode) && (!ipFlag->error)) ipFlag->error = 1;
- fprintf(stderr, "%s: ", progname);
- fprintf(stderr, _("Binary symbol 0x%02X found at line %u\n"), TempChar, line_nr);
+ d2u_fprintf(stderr, "%s: ", progname);
+ d2u_fprintf(stderr, _("Binary symbol 0x%02X found at line %u\n"), TempChar, line_nr);
}
break;
}
if (TempChar != '\x0a') { /* Not an LF */
if(fputc(ConvTable[TempChar], ipOutF) == EOF) {
RetVal = -1;
- if (ipFlag->verbose) {
- ipFlag->error = errno;
- errstr = strerror(errno);
- fprintf(stderr, "%s: ", progname);
- fprintf(stderr, _("can not write to output file: %s\n"), errstr);
- }
+ d2u_putc_error(ipFlag,progname);
break;
}
PreviousChar = TempChar;
@@ -406,12 +427,7 @@ int ConvertUnixToDos(FILE* ipInF, FILE* ipOutF, CFlag *ipFlag, const char *progn
if ( PreviousChar == '\x0d' ) {
if (fputc('\x0a', ipOutF) == EOF) { /* CR,LF pair. Put LF */
RetVal = -1;
- if (ipFlag->verbose) {
- ipFlag->error = errno;
- errstr = strerror(errno);
- fprintf(stderr, "%s: ", progname);
- fprintf(stderr, _("can not write to output file: %s\n"), errstr);
- }
+ d2u_putc_error(ipFlag,progname);
break;
}
PreviousChar = TempChar;
@@ -420,32 +436,35 @@ int ConvertUnixToDos(FILE* ipInF, FILE* ipOutF, CFlag *ipFlag, const char *progn
PreviousChar = TempChar;
if (fputc('\x0d', ipOutF) == EOF) { /* Unix line end (LF). Put CR */
RetVal = -1;
- if (ipFlag->verbose) {
- ipFlag->error = errno;
- errstr = strerror(errno);
- fprintf(stderr, "%s: ", progname);
- fprintf(stderr, _("can not write to output file: %s\n"), errstr);
- }
+ d2u_putc_error(ipFlag,progname);
break;
}
converted++;
if (ipFlag->NewLine) { /* add additional CR? */
- fputc('\x0d', ipOutF);
+ if (fputc('\x0d', ipOutF) == EOF) {
+ RetVal = -1;
+ d2u_putc_error(ipFlag,progname);
+ break;
+ }
}
}
}
+ if ((TempChar == EOF) && ferror(ipInF)) {
+ RetVal = -1;
+ d2u_getc_error(ipFlag,progname);
+ }
break;
default: /* unknown FromToMode */
;
#if DEBUG
- fprintf(stderr, "%s: ", progname);
- fprintf(stderr, _("program error, invalid conversion mode %d\n"),ipFlag->FromToMode);
+ d2u_fprintf(stderr, "%s: ", progname);
+ d2u_fprintf(stderr, _("program error, invalid conversion mode %d\n"),ipFlag->FromToMode);
exit(1);
#endif
}
- if (ipFlag->verbose > 1) {
- fprintf(stderr, "%s: ", progname);
- fprintf(stderr, _("Converted %u out of %u line breaks.\n"), converted, line_nr -1);
+ if ((RetVal == 0) && (ipFlag->verbose > 1)) {
+ d2u_fprintf(stderr, "%s: ", progname);
+ d2u_fprintf(stderr, _("Converted %u out of %u line breaks.\n"), converted, line_nr -1);
}
return RetVal;
}
@@ -461,6 +480,8 @@ int main (int argc, char *argv[])
# ifdef __MINGW64__
int _dowildcard = -1; /* enable wildcard expansion for Win64 */
# endif
+ int argc_new;
+ char **argv_new;
progname[8] = '\0';
strcpy(progname,"unix2dos");
@@ -473,8 +494,8 @@ int main (int argc, char *argv[])
if (strlen(ptr) < sizeof(localedir))
strcpy(localedir,ptr);
else {
- fprintf(stderr,"%s: ",progname);
- fprintf(stderr, "%s", _("error: Value of environment variable DOS2UNIX_LOCALEDIR is too long.\n"));
+ d2u_fprintf(stderr,"%s: ",progname);
+ d2u_ansi_fprintf(stderr, "%s", _("error: Value of environment variable DOS2UNIX_LOCALEDIR is too long.\n"));
strcpy(localedir,LOCALEDIR);
}
}
@@ -493,6 +514,11 @@ int main (int argc, char *argv[])
/* variable initialisations */
pFlag = (CFlag*)malloc(sizeof(CFlag));
+ if (pFlag == NULL) {
+ d2u_fprintf(stderr, "unix2dos:");
+ d2u_ansi_fprintf(stderr, " %s\n", strerror(errno));
+ return errno;
+ }
pFlag->FromToMode = FROMTO_UNIX2DOS; /* default unix2dos */
pFlag->keep_bom = 1;
@@ -506,10 +532,32 @@ int main (int argc, char *argv[])
strcpy(progname,"unix2mac");
}
+#ifdef D2U_UNIFILE
+ /* Get arguments in wide Unicode format in the Windows Command Prompt */
+ wchar_t **wargv;
+ char ***argv_glob;
+
+ /* This does not support wildcard expansion (globbing) */
+ wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
+
+ argv_glob = (char ***)malloc(sizeof(char***));
+ if (argv_glob == NULL) {
+ d2u_fprintf(stderr, "%s:", progname);
+ d2u_ansi_fprintf(stderr, " %s\n", strerror(errno));
+ return errno;
+ }
+ /* Glob the arguments and convert them to UTF-8 */
+ argc_new = glob_warg(argc, wargv, argv_glob, pFlag, progname);
+ argv_new = *argv_glob;
+#else
+ argc_new = argc;
+ argv_new = argv;
+#endif
+
#ifdef D2U_UNICODE
- return parse_options(argc, argv, pFlag, localedir, progname, PrintLicense, ConvertUnixToDos, ConvertUnixToDosW);
+ return parse_options(argc_new, argv_new, pFlag, localedir, progname, PrintLicense, ConvertUnixToDos, ConvertUnixToDosW);
#else
- return parse_options(argc, argv, pFlag, localedir, progname, PrintLicense, ConvertUnixToDos);
+ return parse_options(argc_new, argv_new, pFlag, localedir, progname, PrintLicense, ConvertUnixToDos);
#endif
}