summaryrefslogtreecommitdiff
path: root/common.c
diff options
context:
space:
mode:
authorDongHun Kwak <dh0128.kwak@samsung.com>2016-10-20 13:30:18 +0900
committerDongHun Kwak <dh0128.kwak@samsung.com>2016-10-20 13:30:21 +0900
commit2fb41ec277dc25c2a3988fc0b3fc8dde07d36062 (patch)
treeb1da5572b512c3aac8140907103a0fd7af5a33d7 /common.c
parentff02b77ca132c1ed60c503dceda5305738128bf2 (diff)
downloaddos2unix-2fb41ec277dc25c2a3988fc0b3fc8dde07d36062.tar.gz
dos2unix-2fb41ec277dc25c2a3988fc0b3fc8dde07d36062.tar.bz2
dos2unix-2fb41ec277dc25c2a3988fc0b3fc8dde07d36062.zip
Imported Upstream version 7.3.4upstream/7.3.4
Change-Id: I2a4662ebe90fafe4d22e8c9abcd81b99f3a700e6 Signed-off-by: DongHun Kwak <dh0128.kwak@samsung.com>
Diffstat (limited to 'common.c')
-rw-r--r--common.c379
1 files changed, 234 insertions, 145 deletions
diff --git a/common.c b/common.c
index b96a8aa..fcd1b42 100644
--- a/common.c
+++ b/common.c
@@ -365,7 +365,6 @@ int regfile(char *path, int allowSymlinks, CFlag *ipFlag, const char *progname)
#else
struct stat buf;
#endif
- char *errstr;
#ifdef D2U_UNIFILE
d2u_MultiByteToWideChar(CP_UTF8, 0, path, -1, pathw, D2U_MAX_PATH);
@@ -409,8 +408,8 @@ int regfile(char *path, int allowSymlinks, CFlag *ipFlag, const char *progname)
}
else {
if (ipFlag->verbose) {
+ char *errstr = strerror(errno);
ipFlag->error = errno;
- errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: %s:", progname, path);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
@@ -435,7 +434,6 @@ int regfile_target(char *path, CFlag *ipFlag, const char *progname)
#else
struct stat buf;
#endif
- char *errstr;
#ifdef D2U_UNIFILE
d2u_MultiByteToWideChar(CP_UTF8, 0, path, -1, pathw, D2U_MAX_PATH);
@@ -450,8 +448,8 @@ int regfile_target(char *path, CFlag *ipFlag, const char *progname)
}
else {
if (ipFlag->verbose) {
+ char *errstr = strerror(errno);
ipFlag->error = errno;
- errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: %s:", progname, path);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
@@ -490,7 +488,7 @@ int glob_warg(int argc, wchar_t *wargv[], char ***argv, CFlag *ipFlag, const cha
len = (size_t)d2u_WideCharToMultiByte(CP_UTF8, 0, wargv[0], -1, NULL, 0, NULL, NULL);
arg = (char *)malloc(len);
- if (argv == NULL) goto glob_failed;
+ 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;
@@ -514,6 +512,7 @@ int glob_warg(int argc, wchar_t *wargv[], char ***argv, CFlag *ipFlag, const cha
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;
@@ -528,11 +527,13 @@ int glob_warg(int argc, wchar_t *wargv[], char ***argv, CFlag *ipFlag, const cha
++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 (argv == NULL) goto glob_failed;
+ if (arg == NULL) goto glob_failed;
d2u_WideCharToMultiByte(CP_UTF8, 0, path_and_filename, -1, arg, (int)len, NULL, NULL);
free(path_and_filename);
- argv_new = (char **)realloc(argv_new, (size_t)(argc_glob+1)*sizeof(char**));
- if (argv_new == NULL) goto glob_failed;
+ 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)) {
@@ -543,13 +544,16 @@ int glob_warg(int argc, wchar_t *wargv[], char ***argv, CFlag *ipFlag, const cha
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 (argv == NULL) goto glob_failed;
+ if (arg == NULL) goto glob_failed;
d2u_WideCharToMultiByte(CP_UTF8, 0, warg, -1, arg, (int)len, NULL, NULL);
- argv_new = (char **)realloc(argv_new, (size_t)(argc_glob+1)*sizeof(char**));
- if (argv_new == NULL) goto glob_failed;
+ 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;
}
}
@@ -730,7 +734,6 @@ void PrintVersion(const char *progname, const char *localedir)
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
@@ -748,48 +751,172 @@ FILE* OpenInFile(char *ipFN)
}
-/* opens file of name ipFN in write only mode
+/* opens file of name opFN in write only mode
* RetVal: NULL if failure
* file stream otherwise
*/
-FILE* OpenOutFile(int fd)
+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;
- if (( path == NULL) || (((ptr=strrchr(path,'/')) == NULL) && ((ptr=strrchr(path,'\\')) == NULL)) )
+ /* 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 ".";
- else {
- *ptr = '\0';
- return(path);
- }
+
+ if (strcmp(path,"/") == 0)
+ return "/";
+
+ *ptr = '\0';
+ return path;
+}
+
+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
+/* 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
-FILE* MakeTempFileFrom(const char *OutFN, char **fname_ret)
+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
-int MakeTempFileFrom(const char *OutFN, char **fname_ret)
+ 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;
+ strcpy(fname_str, szTempFileName);
+#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;
- FILE *fd = NULL;
#else
- int fd = -1;
-#endif
-#ifdef D2U_UNIFILE
- wchar_t fname_strw[D2U_MAX_PATH];
- wchar_t *namew;
+ int fd = -1; /* file descriptor */
#endif
*fname_ret = NULL;
@@ -799,11 +926,7 @@ int MakeTempFileFrom(const char *OutFN, char **fname_ret)
dir = dirname(cpy);
-#ifdef D2U_UNIFILE
- fname_len = D2U_MAX_PATH;
-#else
fname_len = strlen(dir) + strlen("/d2utmpXXXXXX") + sizeof (char);
-#endif
if (!(fname_str = (char *)malloc(fname_len)))
goto make_failed;
sprintf(fname_str, "%s%s", dir, "/d2utmpXXXXXX");
@@ -812,34 +935,25 @@ int MakeTempFileFrom(const char *OutFN, char **fname_ret)
free(cpy);
#ifdef NO_MKSTEMP
-#ifdef D2U_UNIFILE
- d2u_MultiByteToWideChar(CP_UTF8, 0, fname_str, -1, fname_strw, D2U_MAX_PATH);
- namew = _wmktemp(fname_strw);
- d2u_WideCharToMultiByte(CP_UTF8, 0, namew, -1, fname_str, (int)fname_len, NULL, NULL);
- *fname_ret = fname_str;
- if ((fd = _wfopen(fname_strw, W_CNTRLW)) == NULL)
+ if ((name = d2u_mktemp(fname_str)) == NULL)
goto make_failed;
-#else
- name = mktemp(fname_str);
*fname_ret = name;
- if ((fd = fopen(fname_str, W_CNTRL)) == NULL)
+ if ((fp = OpenOutFile(name)) == NULL)
goto make_failed;
-#endif
#else
if ((fd = mkstemp(fname_str)) == -1)
goto make_failed;
+
+ if ((fp=OpenOutFiled(fd)) == NULL)
+ goto make_failed;
#endif
- return (fd);
+ return (fp);
- make_failed:
- free(*fname_ret);
- *fname_ret = NULL;
-#ifdef NO_MKSTEMP
- return (NULL);
-#else
- return (-1);
-#endif
+ 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
@@ -936,7 +1050,6 @@ int ResolveSymbolicLink(char *lFN, char **rFN, CFlag *ipFlag, const char *progna
FILE *read_bom (FILE *f, int *bomtype)
{
- int bom[4];
/* BOMs
* UTF16-LE ff fe
* UTF16-BE fe ff
@@ -948,6 +1061,7 @@ FILE *read_bom (FILE *f, int *bomtype)
/* Check for BOM */
if (f != NULL) {
+ int bom[4];
if ((bom[0] = fgetc(f)) == EOF) {
if (ferror(f)) {
return NULL;
@@ -1074,10 +1188,7 @@ FILE *write_bom (FILE *f, CFlag *ipFlag, const char *progname)
void print_bom (const int bomtype, const char *filename, const char *progname)
{
- char informat[64];
-# ifdef D2U_UNIFILE
- wchar_t informatw[64];
-#endif
+ char informat[64];
switch (bomtype) {
case FILE_UTF16LE: /* UTF-16 Little Endian */
@@ -1097,10 +1208,13 @@ void print_bom (const int bomtype, const char *filename, const char *progname)
}
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
+#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. */
@@ -1269,11 +1383,6 @@ int ConvertNewFile(char *ipInFN, char *ipOutFN, CFlag *ipFlag, const char *progn
#ifndef NO_CHMOD
mode_t mask;
#endif
-#ifdef NO_MKSTEMP
- FILE* fd;
-#else
- int fd;
-#endif
char *TargetFN = NULL;
int ResolveSymlinkResult = 0;
@@ -1310,8 +1419,8 @@ int ConvertNewFile(char *ipInFN, char *ipOutFN, CFlag *ipFlag, const char *progn
/* retrieve ipInFN file date stamp */
#ifdef D2U_UNIFILE
- d2u_MultiByteToWideChar(CP_UTF8, 0, ipInFN, -1, pathw, D2U_MAX_PATH);
- if (_wstat(pathw, &StatBuf)) {
+ d2u_MultiByteToWideChar(CP_UTF8, 0, ipInFN, -1, pathw, D2U_MAX_PATH);
+ if (_wstat(pathw, &StatBuf)) {
#else
if (stat(ipInFN, &StatBuf)) {
#endif
@@ -1321,60 +1430,43 @@ int ConvertNewFile(char *ipInFN, char *ipOutFN, CFlag *ipFlag, const char *progn
D2U_UTF8_FPRINTF(stderr, "%s: %s:", progname, ipInFN);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
- RetVal = -1;
+ return -1;
}
-#ifdef NO_MKSTEMP
- if((fd = MakeTempFileFrom(ipOutFN, &TempPath))==NULL) {
-#else
- if((fd = MakeTempFileFrom (ipOutFN, &TempPath)) < 0) {
-#endif
+ /* can open in file? */
+ InF=OpenInFile(ipInFN);
+ if (InF == NULL) {
if (ipFlag->verbose) {
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);
+ D2U_UTF8_FPRINTF(stderr, "%s: %s:", progname, ipInFN);
+ D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
}
- RetVal = -1;
+ return -1;
}
-#if DEBUG
- D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
- D2U_UTF8_FPRINTF(stderr, _("using %s as temporary file\n"), TempPath);
-#endif
-
- /* can open in file? */
- if (!RetVal) {
- InF=OpenInFile(ipInFN);
- if (InF == NULL) {
- if (ipFlag->verbose) {
+ /* 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: %s:", progname, ipInFN);
- D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
+ 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;
}
+ RetVal = -1;
}
- /* can open output file? */
- if ((!RetVal) && (InF)) {
-#ifdef NO_MKSTEMP
- if ((TempF=fd) == NULL) {
-#else
- if ((TempF=OpenOutFile(fd)) == NULL) {
- if (ipFlag->verbose) {
- ipFlag->error = errno;
- errstr = strerror(errno);
- D2U_UTF8_FPRINTF(stderr, "%s:", progname);
- D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
- }
-#endif
- fclose (InF);
- InF = NULL;
- 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))
@@ -1399,31 +1491,31 @@ int ConvertNewFile(char *ipInFN, char *ipOutFN, CFlag *ipFlag, const char *progn
#endif
/* can close in file? */
- if ((InF) && (fclose(InF) == EOF))
+ if (fclose(InF) == EOF) {
+ if (ipFlag->verbose) {
+ ipFlag->error = errno;
+ errstr = strerror(errno);
+ D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
+ D2U_UTF8_FPRINTF(stderr, _("Failed to close input file %s:"), ipInFN);
+ D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
+ }
RetVal = -1;
+ }
/* can close output file? */
if (TempF) {
if (fclose(TempF) == EOF) {
- if (ipFlag->verbose) {
- ipFlag->error = errno;
- errstr = strerror(errno);
- D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
- D2U_UTF8_FPRINTF(stderr, _("Failed to write to temporary output file %s:"), TempPath);
- D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
- }
+ if (ipFlag->verbose) {
+ ipFlag->error = errno;
+ errstr = strerror(errno);
+ D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
+ D2U_UTF8_FPRINTF(stderr, _("Failed to write to temporary output file %s:"), TempPath);
+ D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
+ }
RetVal = -1;
}
}
-#ifdef NO_MKSTEMP
- if(fd!=NULL)
- fclose(fd);
-#else
- if(fd>=0)
- close(fd);
-#endif
-
#ifndef NO_CHMOD
if (!RetVal)
{
@@ -1851,8 +1943,6 @@ void print_messages_info(const CFlag *pFlag, const char *infile, const char *pro
void printInfo(CFlag *ipFlag, const char *filename, int bomtype, unsigned int lb_dos, unsigned int lb_unix, unsigned int lb_mac)
{
- const char *ptr;
-
if (ipFlag->file_info & INFO_CONVERT) {
if ((ipFlag->FromToMode == FROMTO_DOS2UNIX) && (lb_dos == 0))
return;
@@ -1896,6 +1986,7 @@ void printInfo(CFlag *ipFlag, const char *filename, int bomtype, unsigned int lb
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
@@ -1913,7 +2004,6 @@ void FileInfoW(FILE* ipInF, CFlag *ipFlag, const char *filename, int bomtype, co
unsigned int lb_dos = 0;
unsigned int lb_unix = 0;
unsigned int lb_mac = 0;
- char *errstr;
ipFlag->status = 0;
@@ -1944,7 +2034,7 @@ void FileInfoW(FILE* ipInF, CFlag *ipFlag, const char *filename, int bomtype, co
if ((TempChar == WEOF) && ferror(ipInF)) {
ipFlag->error = errno;
if (ipFlag->verbose) {
- errstr = strerror(errno);
+ 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);
@@ -1964,8 +2054,6 @@ void FileInfo(FILE* ipInF, CFlag *ipFlag, const char *filename, int bomtype, con
unsigned int lb_dos = 0;
unsigned int lb_unix = 0;
unsigned int lb_mac = 0;
- char *errstr;
-
ipFlag->status = 0;
@@ -1996,7 +2084,7 @@ void FileInfo(FILE* ipInF, CFlag *ipFlag, const char *filename, int bomtype, con
if ((TempChar == EOF) && ferror(ipInF)) {
ipFlag->error = errno;
if (ipFlag->verbose) {
- errstr = strerror(errno);
+ 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);
@@ -2011,7 +2099,6 @@ int GetFileInfo(char *ipInFN, CFlag *ipFlag, const char *progname)
{
int RetVal = 0;
FILE *InF = NULL;
- char *errstr;
int bomtype_orig = FILE_MBS; /* messages must print the real bomtype, not the assumed bomtype */
ipFlag->status = 0 ;
@@ -2035,8 +2122,8 @@ int GetFileInfo(char *ipInFN, CFlag *ipFlag, const char *progname)
InF=OpenInFile(ipInFN);
if (InF == NULL) {
if (ipFlag->verbose) {
+ char *errstr = strerror(errno);
ipFlag->error = errno;
- errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: %s: ", progname, ipInFN);
D2U_ANSI_FPRINTF(stderr, "%s\n", errstr);
}
@@ -2062,9 +2149,17 @@ int GetFileInfo(char *ipInFN, CFlag *ipFlag, const char *progname)
FileInfo(InF, ipFlag, ipInFN, bomtype_orig, progname);
#endif
- /* can close in file? */
- if ((InF) && (fclose(InF) == EOF))
+ /* can close in file? */
+ if (fclose(InF) == EOF) {
+ if (ipFlag->verbose) {
+ char *errstr = strerror(errno);
+ ipFlag->error = errno;
+ D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
+ D2U_UTF8_FPRINTF(stderr, _("Failed to close input file %s:"), ipInFN);
+ D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
+ }
RetVal = -1;
+ }
return RetVal;
}
@@ -2452,7 +2547,7 @@ int parse_options(int argc, char *argv[],
/* no file argument, use stdin and stdout */
if ( (argc > 0) && pFlag->stdio_mode) {
if (pFlag->file_info) {
- RetVal = GetFileInfoStdio(pFlag, progname);
+ GetFileInfoStdio(pFlag, progname);
print_messages_info(pFlag, "stdin", progname);
} else {
#ifdef D2U_UNICODE
@@ -2476,11 +2571,9 @@ int parse_options(int argc, char *argv[],
void d2u_getc_error(CFlag *ipFlag, const char *progname)
{
- char *errstr;
-
ipFlag->error = errno;
if (ipFlag->verbose) {
- errstr = strerror(errno);
+ char *errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_ANSI_FPRINTF(stderr, _("can not read from input file: %s\n"), errstr);
}
@@ -2488,11 +2581,9 @@ void d2u_getc_error(CFlag *ipFlag, const char *progname)
void d2u_putc_error(CFlag *ipFlag, const char *progname)
{
- char *errstr;
-
ipFlag->error = errno;
if (ipFlag->verbose) {
- errstr = strerror(errno);
+ char *errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_ANSI_FPRINTF(stderr, _("can not write to output file: %s\n"), errstr);
}
@@ -2501,12 +2592,10 @@ void d2u_putc_error(CFlag *ipFlag, const char *progname)
#ifdef D2U_UNICODE
void d2u_putwc_error(CFlag *ipFlag, const char *progname)
{
- char *errstr;
-
if (!(ipFlag->status & UNICODE_CONVERSION_ERROR)) {
ipFlag->error = errno;
if (ipFlag->verbose) {
- errstr = strerror(errno);
+ char *errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s: ", progname);
D2U_ANSI_FPRINTF(stderr, _("can not write to output file: %s\n"), errstr);
}
@@ -2555,17 +2644,15 @@ wint_t d2u_ungetwc(wint_t wc, FILE *f, int bomtype)
wint_t d2u_putwc(wint_t wc, FILE *f, CFlag *ipFlag, const char *progname)
{
static char mbs[8];
- static wchar_t lead=0x01, trail; /* lead get's invalid value */
+ static wchar_t lead=0x01; /* lead get's invalid value */
static wchar_t wstr[3];
- size_t i,len;
- int c_trail, c_lead;
+ size_t len;
#if (defined(_WIN32) && !defined(__CYGWIN__))
DWORD dwFlags;
-#else
- char *errstr;
#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;
@@ -2596,6 +2683,7 @@ wint_t d2u_putwc(wint_t wc, FILE *f, CFlag *ipFlag, const char *progname)
return(wc);
}
if ((wc >= 0xdc00) && (wc < 0xe000)) { /* Surrogate trail */
+ static wchar_t trail;
/* check for trail without a lead */
if ((lead < 0xd800) || (lead >= 0xdc00)) {
@@ -2681,7 +2769,7 @@ wint_t d2u_putwc(wint_t wc, FILE *f, CFlag *ipFlag, const char *progname)
#if (defined(_WIN32) && !defined(__CYGWIN__))
d2u_PrintLastError(progname);
#else
- errstr = strerror(errno);
+ char *errstr = strerror(errno);
D2U_UTF8_FPRINTF(stderr, "%s:", progname);
D2U_ANSI_FPRINTF(stderr, " %s\n", errstr);
#endif
@@ -2689,6 +2777,7 @@ wint_t d2u_putwc(wint_t wc, FILE *f, CFlag *ipFlag, const char *progname)
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);