From b7d80edae3e889fbd2f25c9c9c5f4efd4a912c45 Mon Sep 17 00:00:00 2001 From: Ray Hayes Date: Fri, 21 Oct 2016 11:43:09 -0700 Subject: [PATCH] SFTP issues. --- contrib/win32/win32compat/fileio.c | 12 ++++---- contrib/win32/win32compat/w32fd.c | 5 +--- sftp.c | 46 +++++++++++++++++++++++------- win32_dirent.c | 8 ++++-- 4 files changed, 47 insertions(+), 24 deletions(-) diff --git a/contrib/win32/win32compat/fileio.c b/contrib/win32/win32compat/fileio.c index 64dc065..e9136f1 100644 --- a/contrib/win32/win32compat/fileio.c +++ b/contrib/win32/win32compat/fileio.c @@ -559,13 +559,13 @@ fileio_fstat(struct w32_io* pio, struct _stat64 *buf) { int fileio_stat(const char *path, struct _stat64 *buf) { - wchar_t wpath[MAX_PATH]; + wchar_t* wpath[MAX_PATH]; + wchar_t* wtmp = NULL; - if (MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, MAX_PATH) == 0) { - errno = EFAULT; - debug("WideCharToMultiByte failed - ERROR:%d", GetLastError()); - return GetLastError(); - } + if ((wtmp = utf8_to_utf16(path)) == NULL) + fatal("failed to covert input arguments"); + strcpy(wpath, wtmp); + free(wtmp); return _wstat64(wpath, buf); } diff --git a/contrib/win32/win32compat/w32fd.c b/contrib/win32/win32compat/w32fd.c index 993758a..deda2b0 100644 --- a/contrib/win32/win32compat/w32fd.c +++ b/contrib/win32/win32compat/w32fd.c @@ -438,13 +438,10 @@ int w32_chdir(const char *dirname_utf8) { char *w32_getcwd(char *buffer, int maxlen) { wchar_t wdirname[MAX_PATH]; - int needed; wchar_t *wpwd = _wgetcwd(wdirname, MAX_PATH); - if ((needed = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wdirname, -1, NULL, 0, NULL, NULL)) == 0 || - (needed > MAX_PATH) || - (WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wdirname, -1, buffer, needed, NULL, NULL) != needed)) + if (buffer = utf16_to_utf8(wpwd)) fatal("failed to convert input arguments"); return buffer; diff --git a/sftp.c b/sftp.c index 6b1af98..b72704d 100644 --- a/sftp.c +++ b/sftp.c @@ -76,6 +76,7 @@ typedef void EditLine; #ifdef WIN32_VS #include "win32_dirent.h" +extern int ScreenX; #endif /* File to read commands from */ @@ -816,7 +817,8 @@ do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag) m += strlen(tmp); free(tmp); #ifdef WINDOWS - width = ConSetScreenX(); + ConSetScreenX(); + width = ScreenX ; #else if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) width = ws.ws_col; @@ -853,12 +855,33 @@ do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag) attrib_to_stat(&d[n]->a, &sb); lname = ls_file(fname, &sb, 1, (lflag & LS_SI_UNITS)); - printf("%s\n", lname); +#ifdef WINDOWS + wchar_t* wtmp = utf8_to_utf16(lname); + wprintf_s(L"%ls\n", wtmp); + free(tmp); +#else + printf("%s\n", lname); +#endif free(lname); - } else - printf("%s\n", d[n]->longname); - } else { - printf("%-*s", colspace, fname); + } + else { +#ifdef WINDOWS + wchar_t* wtmp = utf8_to_utf16(d[n]->longname); + wprintf_s(L"%ls\n", wtmp); + free(wtmp); +#else + printf("%s\n", d[n]->longname); +#endif + } + } + else { +#ifdef WINDOWS + wchar_t* wtmp = utf8_to_utf16(fname); + wprintf_s(L"%-*ls", colspace, wtmp); + free(wtmp); +#else + printf("%-*s", colspace, fname); +#endif if (c >= columns) { printf("\n"); c = 1; @@ -918,7 +941,8 @@ do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path, } #ifdef WINDOWS - width = ConSetScreenX(); + ConSetScreenX(); + width = ScreenX; #else if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) width = ws.ws_col; @@ -2128,11 +2152,11 @@ interactive_loop(struct sftp_conn *conn, char *file1, char *file2) break; } else { - int needed; - if ((needed = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wcmd, -1, NULL, 0, NULL, NULL)) == 0 || - (needed > MAX_COMMAND_LINE) || - (WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wcmd, -1, cmd, needed, NULL, NULL) != needed)) + char *pcmd = NULL; + if ((pcmd = utf16_to_utf8(wcmd)) == NULL) fatal("failed to convert input arguments"); + strcpy(cmd, pcmd); + free(pcmd); } } else { diff --git a/win32_dirent.c b/win32_dirent.c index 31827ee..9812252 100644 --- a/win32_dirent.c +++ b/win32_dirent.c @@ -73,6 +73,7 @@ struct dirent *readdir(void *avp) struct dirent *pdirentry; struct _wfinddata_t c_file; DIR *dirp = (DIR *)avp; + char *tmp = NULL; for (;;) { if ( _wfindnext( dirp->hFile, &c_file ) == 0 ) { @@ -82,10 +83,11 @@ struct dirent *readdir(void *avp) } pdirentry = (struct dirent *) malloc( sizeof(struct dirent) ); - if ((needed = WideCharToMultiByte(CP_UTF8, 0, c_file.name, -1, NULL, 0, NULL, NULL)) == 0 || - (pdirentry->d_name = malloc(needed)) == NULL || - WideCharToMultiByte(CP_UTF8, 0, c_file.name, -1, pdirentry->d_name, needed, NULL, NULL) != needed) + if ((tmp = utf16_to_utf8(pdirentry->d_name)) == NULL) fatal("failed to covert input arguments"); + strcpy(c_file.name[0], tmp); + free(tmp); + tmp = NULL; pdirentry->d_ino = 1; // a fictious one like UNIX to say it is nonzero return pdirentry ;