From 2854245328ad1c7f29137f6a7fcc31dda1050b4b Mon Sep 17 00:00:00 2001 From: manojampalam Date: Wed, 9 Mar 2016 22:39:27 -0800 Subject: [PATCH] Made sftp.exe functional --- contrib/win32/win32compat/fileio.c | 13 +++++ contrib/win32/win32compat/inc/unistd.h | 1 + contrib/win32/win32compat/inc/w32posix.h | 2 + contrib/win32/win32compat/w32fd.c | 6 +++ contrib/win32/win32compat/w32fd.h | 1 + readpass.c | 68 +++++++++++++++--------- sftp-client.c | 2 +- sftp.c | 8 ++- 8 files changed, 69 insertions(+), 32 deletions(-) diff --git a/contrib/win32/win32compat/fileio.c b/contrib/win32/win32compat/fileio.c index 5aac018..4804da4 100644 --- a/contrib/win32/win32compat/fileio.c +++ b/contrib/win32/win32compat/fileio.c @@ -397,6 +397,7 @@ VOID CALLBACK WriteCompletionRoutine( abort(); pio->write_details.remaining -= dwNumberOfBytesTransfered; pio->write_details.pending = FALSE; + *((__int64*)&lpOverlapped->Offset) += dwNumberOfBytesTransfered; } /* write() implementation */ @@ -507,6 +508,18 @@ fileio_stat(const char *path, struct _stat64 *buf) { return _stat64(path, buf); } +long +fileio_lseek(struct w32_io* pio, long offset, int origin) { + int fd = _open_osfhandle((intptr_t)pio->handle, 0); + debug2("pio:%p", pio); + if (fd == -1) { + errno = EOTHER; + return -1; + } + + return _lseek(fd, offset, origin); +} + /* isatty() implementation */ int fileio_isatty(struct w32_io* pio) { diff --git a/contrib/win32/win32compat/inc/unistd.h b/contrib/win32/win32compat/inc/unistd.h index dcdb912..cf3a4df 100644 --- a/contrib/win32/win32compat/inc/unistd.h +++ b/contrib/win32/win32compat/inc/unistd.h @@ -31,6 +31,7 @@ #define sleep(sec) Sleep(1000 * sec) #define alarm w32_alarm +#define lseek w32_lseek /* Compatibility header to avoid lots of #ifdefs in includes.h on Win32 */ diff --git a/contrib/win32/win32compat/inc/w32posix.h b/contrib/win32/win32compat/inc/w32posix.h index db18da4..6ed1db2 100644 --- a/contrib/win32/win32compat/inc/w32posix.h +++ b/contrib/win32/win32compat/inc/w32posix.h @@ -48,6 +48,8 @@ int w32_read(int fd, void *dst, unsigned int max); int w32_write(int fd, const void *buf, unsigned int max); int w32_fstat(int fd, struct w32_stat *buf); int w32_stat(const char *path, struct w32_stat *buf); +long w32_lseek( int fd, long offset, int origin); + int w32_isatty(int fd); FILE* w32_fdopen(int fd, const char *mode); int w32_mkdir(const char *pathname, unsigned short mode); diff --git a/contrib/win32/win32compat/w32fd.c b/contrib/win32/win32compat/w32fd.c index 570a4af..1756c43 100644 --- a/contrib/win32/win32compat/w32fd.c +++ b/contrib/win32/win32compat/w32fd.c @@ -353,6 +353,12 @@ w32_stat(const char *path, struct w32_stat *buf) { return fileio_stat(path, (struct _stat64*)buf); } +long +w32_lseek(int fd, long offset, int origin) { + CHECK_FD(fd); + return fileio_lseek(fd_table.w32_ios[fd], offset, origin); +} + int w32_mkdir(const char *pathname, unsigned short mode) { return _mkdir(pathname); diff --git a/contrib/win32/win32compat/w32fd.h b/contrib/win32/win32compat/w32fd.h index a91f137..7fad895 100644 --- a/contrib/win32/win32compat/w32fd.h +++ b/contrib/win32/win32compat/w32fd.h @@ -116,6 +116,7 @@ int fileio_read(struct w32_io* pio, void *dst, unsigned int max); int fileio_write(struct w32_io* pio, const void *buf, unsigned int max); int fileio_fstat(struct w32_io* pio, struct _stat64 *buf); int fileio_stat(const char *path, struct _stat64 *buf); +long fileio_lseek(struct w32_io* pio, long offset, int origin); int fileio_isatty(struct w32_io* pio); FILE* fileio_fdopen(struct w32_io* pio, const char *mode); diff --git a/readpass.c b/readpass.c index 47d0b38..f8aae68 100644 --- a/readpass.c +++ b/readpass.c @@ -334,41 +334,57 @@ read_passphrase(const char *prompt, int flags) /* * Show prompt for user. */ + HANDLE c_in = CreateFileA("CONIN$", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL); + HANDLE c_out = CreateFileA("CONOUT$", GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL); + int tmp; + int originalMode, tmpMode; - write(STDOUT_FILENO, prompt, strlen(prompt)); + GetConsoleMode(c_in, &originalMode); + SetConsoleMode(c_in, ENABLE_LINE_INPUT); + //SetConsoleMode(c_in, (originalMode & ~(ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT))); - len = retr = 0; - int bufsize = sizeof(buf); + GetConsoleMode(c_in, &tmpMode); - while (_kbhit()) - _getch(); + WriteFile(c_out, prompt, strlen(prompt), &tmp, NULL); + // write(STDOUT_FILENO, prompt, strlen(prompt)); + ReadFile(c_in, buf, 1024, &tmp, NULL); + buf[tmp - 2] = '\0'; + SetConsoleMode(c_in, originalMode); + CloseHandle(c_in); + CloseHandle(c_out); - while ( len < bufsize ) { + // len = retr = 0; + // int bufsize = sizeof(buf); - buf[len] = (unsigned char) _getch() ; + //while (_kbhit()) + // _getch(); + + //while ( len < bufsize ) { + + // buf[len] = (unsigned char) _getch() ; - if ( buf[len] == '\r' ) { - if (_kbhit() ) - _getch(); // read linefeed if its there - break; - } - else if ( buf[len] == '\n' ) { - break; - } - else if ( buf[len] == '\b' ) { // backspace - if (len > 0 ) - len--; // overwrite last character - } - else { + // if ( buf[len] == '\r' ) { + // if (_kbhit() ) + // _getch(); // read linefeed if its there + // break; + // } + // else if ( buf[len] == '\n' ) { + // break; + // } + // else if ( buf[len] == '\b' ) { // backspace + // if (len > 0 ) + // len--; // overwrite last character + // } + // else { - //_putch( (int) '*' ); // show a star in place of what is typed - len++; // keep reading in the loop - } - } + // //_putch( (int) '*' ); // show a star in place of what is typed + // len++; // keep reading in the loop + // } + //} - buf[len] = '\0' ; // get rid of the cr/lf - write(STDOUT_FILENO,"\n", strlen("\n")); // show a newline as we do not echo password or the line + //buf[len] = '\0' ; // get rid of the cr/lf + //write(STDOUT_FILENO,"\n", strlen("\n")); // show a newline as we do not echo password or the line ret = xstrdup(buf); diff --git a/sftp-client.c b/sftp-client.c index 38fa323..5d88443 100644 --- a/sftp-client.c +++ b/sftp-client.c @@ -1556,7 +1556,7 @@ do_download(struct sftp_conn *conn, const char *remote_path, } #endif } - #ifdef WIN32_FIXME + #if(0)//def WIN32_FIXME _close(local_fd); diff --git a/sftp.c b/sftp.c index b6afdeb..210b029 100644 --- a/sftp.c +++ b/sftp.c @@ -75,11 +75,11 @@ typedef void EditLine; #ifdef WIN32_FIXME - #define mkdir(a, b) _mkdir(a) +// #define mkdir(a, b) _mkdir(a) #define FAIL(X) if (X) goto fail - extern int sfd_start; + //extern int sfd_start; #endif @@ -2308,9 +2308,7 @@ connect_to_server(char *path, char **args, int *in, int *out) si.cb = sizeof(STARTUPINFO); si.hStdInput = sfd_to_handle(sockout[0]); si.hStdOutput = sfd_to_handle(sockin[1]);//GetStdHandle(STD_OUTPUT_HANDLE);//sfd_to_handle(sockin[1]); - si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); - si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); - si.hStdError = GetStdHandle(STD_ERROR_HANDLE); + si.hStdError = GetStdHandle(STD_ERROR_HANDLE); si.wShowWindow = SW_HIDE; si.dwFlags = STARTF_USESTDHANDLES; si.lpDesktop = NULL;