Skip to content

Commit 78306c9

Browse files
committed
Issues with chdir and input conversion.
1 parent 2a8c0fe commit 78306c9

File tree

6 files changed

+67
-29
lines changed

6 files changed

+67
-29
lines changed

contrib/win32/win32compat/inc/fcntl.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@
1111
#define O_TRUNC 0x0200 // open and truncate
1212
#define O_EXCL 0x0400 // open only if file doesn't already exist
1313

14-
#define O_BINARY 0x8000 // file mode is binary (untranslated)
14+
#define O_TEXT 0x4000 /* file mode is text (translated) */
15+
#define O_BINARY 0x8000 /* file mode is binary (untranslated) */
16+
#define O_WTEXT 0x10000 /* file mode is UTF16 (translated) */
17+
#define O_U16TEXT 0x20000 /* file mode is UTF16 no BOM (translated) */
18+
#define O_U8TEXT 0x40000 /* file mode is UTF8 no BOM (translated) */

contrib/win32/win32compat/inc/sys/stat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#define stat w32_stat
3030
#define lstat w32_stat
3131
#define mkdir w32_mkdir
32+
#define chdir w32_chdir
3233

3334
struct w32_stat {
3435
dev_t st_dev; /* ID of device containing file */

contrib/win32/win32compat/inc/w32posix.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ long w32_lseek( int fd, long offset, int origin);
5252
int w32_isatty(int fd);
5353
FILE* w32_fdopen(int fd, const char *mode);
5454
int w32_mkdir(const char *pathname, unsigned short mode);
55+
int w32_chdir(const char *dirname);
5556

5657
/*common i/o*/
5758
#define fcntl(a,b,...) w32_fcntl((a), (b), __VA_ARGS__)
@@ -72,7 +73,6 @@ int w32_raise(int sig);
7273
int w32_kill(int pid, int sig);
7374
FILE* w32_fopen_utf8(const char *, const char *);
7475

75-
7676
/* Shutdown constants */
7777
#define SHUT_WR SD_SEND
7878
#define SHUT_RD SD_RECEIVE

contrib/win32/win32compat/w32fd.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ int w32_writev(int fd, const struct iovec *iov, int iovcnt) {
389389
CHECK_FD(fd);
390390

391391
for (i = 0; i < iovcnt; i++) {
392-
int ret = write(fd, iov[i].iov_base, iov[i].iov_len);
392+
int ret = w32_write(fd, iov[i].iov_base, iov[i].iov_len);
393393

394394
if (ret > 0) {
395395
written += ret;
@@ -418,7 +418,23 @@ w32_lseek(int fd, long offset, int origin) {
418418

419419
int
420420
w32_mkdir(const char *pathname, unsigned short mode) {
421-
return _mkdir(pathname);
421+
wchar_t wdirname[MAX_PATH];
422+
423+
if (MultiByteToWideChar(CP_UTF8, 0, pathname, -1, wdirname, MAX_PATH)) {
424+
return _wmkdir(wdirname);
425+
}
426+
427+
return 0;
428+
}
429+
430+
int w32_chdir(const char *dirname) {
431+
wchar_t wdirname[MAX_PATH];
432+
433+
if (MultiByteToWideChar(CP_UTF8, 0, dirname, -1, wdirname, MAX_PATH)) {
434+
return _wchdir(wdirname);
435+
}
436+
437+
return 0;
422438
}
423439

424440
int

scp.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,9 +1092,9 @@ main(int argc, char **argv)
10921092
args.list = remote_remote_args.list = NULL;
10931093
addargs(&args, "%s", ssh_program);
10941094
addargs(&args, "-x");
1095-
addargs(&args, "-oForwardAgent=no");
1096-
addargs(&args, "-oPermitLocalCommand=no");
1097-
addargs(&args, "-oClearAllForwardings=yes");
1095+
addargs(&args, "\"-oForwardAgent no\"");
1096+
addargs(&args, "\"-oPermitLocalCommand no\"");
1097+
addargs(&args, "\"-oClearAllForwardings yes\"");
10981098

10991099
fflag = tflag = 0;
11001100
while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q12346S:o:F:")) != -1)
@@ -1127,8 +1127,8 @@ main(int argc, char **argv)
11271127
addargs(&args, "%s", optarg);
11281128
break;
11291129
case 'B':
1130-
addargs(&remote_remote_args, "-oBatchmode=yes");
1131-
addargs(&args, "-oBatchmode=yes");
1130+
addargs(&remote_remote_args, "\"-oBatchmode yes\"");
1131+
addargs(&args, "\"-oBatchmode yes\"");
11321132
break;
11331133
case 'l':
11341134
limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
@@ -1347,7 +1347,7 @@ toremote(char *targ, int argc, char **argv)
13471347
freeargs(&alist);
13481348
addargs(&alist, "%s", ssh_program);
13491349
addargs(&alist, "-x");
1350-
addargs(&alist, "-oClearAllForwardings=yes");
1350+
addargs(&alist, "-\"oClearAllForwardings yes\"");
13511351
addargs(&alist, "-n");
13521352
for (j = 0; j < remote_remote_args.num; j++) {
13531353
addargs(&alist, "%s",

sftp.c

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ typedef void EditLine;
7373
#define DEFAULT_COPY_BUFLEN 32768 /* Size of buffer for up/download */
7474
#define DEFAULT_NUM_REQUESTS 64 /* # concurrent outstanding requests */
7575

76-
#ifdef WIN32_VS
76+
#ifdef WINDOWS
77+
#include <io.h>
78+
#include <fcntl.h>
7779
#include "win32_dirent.h"
7880
#endif
7981

@@ -1425,6 +1427,7 @@ parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
14251427
glob_t g;
14261428

14271429
path1 = path2 = NULL;
1430+
14281431
cmdnum = parse_args(&cmd, &ignore_errors, &aflag, &fflag, &hflag,
14291432
&iflag, &lflag, &pflag, &rflag, &sflag, &n_arg, &path1, &path2);
14301433
if (ignore_errors != 0)
@@ -2104,7 +2107,7 @@ interactive_loop(struct sftp_conn *conn, char *file1, char *file2)
21042107
free(dir);
21052108
}
21062109

2107-
#ifndef WIN32_VS
2110+
#ifndef WINDOWS
21082111
setvbuf(stdout, NULL, _IOLBF, 0);
21092112
setvbuf(infile, NULL, _IOLBF, 0);
21102113
#endif
@@ -2117,13 +2120,26 @@ interactive_loop(struct sftp_conn *conn, char *file1, char *file2)
21172120
signal(SIGINT, SIG_IGN);
21182121

21192122
if (el == NULL) {
2120-
if (interactive)
2121-
printf("sftp> ");
2122-
if (fgets(cmd, sizeof(cmd), infile) == NULL) {
2123-
if (interactive)
2124-
printf("\n");
2125-
break;
2126-
}
2123+
if (interactive) {
2124+
wchar_t wcmd[2048];
2125+
printf("sftp> ");
2126+
if (fgetws(wcmd, sizeof(cmd)/sizeof(wchar_t), infile) == NULL) {
2127+
if (interactive)
2128+
printf("\n");
2129+
break;
2130+
}
2131+
else {
2132+
int needed;
2133+
if ((needed = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wcmd, -1, NULL, 0, NULL, NULL)) == 0 ||
2134+
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wcmd, -1, cmd, needed, NULL, NULL) != needed)
2135+
fatal("failed to covert input arguments");
2136+
}
2137+
}
2138+
else {
2139+
if (fgets(cmd, sizeof(cmd), infile) == NULL) {
2140+
break;
2141+
}
2142+
}
21272143
if (!interactive) { /* Echo command */
21282144
printf("sftp> %s", cmd);
21292145
if (strlen(cmd) > 0 &&
@@ -2335,13 +2351,14 @@ main(int argc, char **argv)
23352351
args.list = NULL;
23362352
addargs(&args, "%s", ssh_program);
23372353

2338-
addargs(&args, "-oForwardX11 no");
2339-
addargs(&args, "-oForwardAgent no");
2340-
addargs(&args, "-oPermitLocalCommand no");
2341-
addargs(&args, "-oClearAllForwardings yes");
2354+
addargs(&args, "\"-oForwardX11 no\"");
2355+
addargs(&args, "\"-oForwardAgent no\"");
2356+
addargs(&args, "\"-oPermitLocalCommand no\"");
2357+
addargs(&args, "\"-oClearAllForwardings yes\"");
23422358

23432359
ll = SYSLOG_LEVEL_INFO;
23442360
infile = stdin;
2361+
_setmode(_fileno(infile), O_U16TEXT);
23452362

23462363
while ((ch = getopt(argc, argv,
23472364
"1246afhpqrvCc:D:i:l:o:s:S:b:B:F:P:R:")) != -1) {
@@ -2367,8 +2384,8 @@ main(int argc, char **argv)
23672384
addargs(&args, "-%c", ch);
23682385
break;
23692386
case 'P':
2370-
addargs(&args, "-oPort %s", optarg);
2371-
break;
2387+
addargs(&args, "\"-oPort %s\"", optarg);
2388+
break;
23722389
case 'v':
23732390
if (debug_level < 3) {
23742391
addargs(&args, "-v");
@@ -2402,8 +2419,8 @@ main(int argc, char **argv)
24022419
fatal("%s (%s).", strerror(errno), optarg);
24032420
showprogress = 0;
24042421
quiet = batchmode = 1;
2405-
addargs(&args, "-obatchmode yes");
2406-
break;
2422+
addargs(&args, "\"-obatchmode yes\"");
2423+
break;
24072424
case 'f':
24082425
global_fflag = 1;
24092426
break;
@@ -2476,8 +2493,8 @@ main(int argc, char **argv)
24762493
fprintf(stderr, "Missing hostname\n");
24772494
usage();
24782495
}
2479-
addargs(&args, "-oProtocol %d", sshver);
2480-
2496+
addargs(&args, "\"-oProtocol %d\"", sshver);
2497+
24812498
/* no subsystem if the server-spec contains a '/' */
24822499
if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
24832500
addargs(&args, "-s");

0 commit comments

Comments
 (0)