X-Git-Url: https://git.distorted.org.uk/u/mdw/putty/blobdiff_plain/f133db8e0a997ab97b0b68a870b56f49501fcfae..3d88e64dfcf5dc0fd361ce0c504c67a9196ce44c:/psftp.c diff --git a/psftp.c b/psftp.c index 6f849d4d..b9f63709 100644 --- a/psftp.c +++ b/psftp.c @@ -32,6 +32,9 @@ static int do_sftp_init(void); */ char *pwd, *homedir; +static Backend *back; +static void *backhandle; +static Config cfg; /* ---------------------------------------------------------------------- * Higher-level helper functions used in commands. @@ -242,8 +245,7 @@ int sftp_cmd_ls(struct sftp_command *cmd) if (nnames + names->nnames >= namesize) { namesize += names->nnames + 128; - ournames = - srealloc(ournames, namesize * sizeof(*ournames)); + ournames = sresize(ournames, namesize, struct fxp_name *); } for (i = 0; i < names->nnames; i++) @@ -830,7 +832,7 @@ int sftp_cmd_chmod(struct sftp_command *cmd) if (!(subset & 06777) && (perms &~ subset)) { printf("chmod: file mode '%.*s' contains no user/group/other" " specifier and permissions other than 't' \n", - strcspn(modebegin, ","), modebegin, *mode); + strcspn(modebegin, ","), modebegin); return 0; } perms &= subset; @@ -931,10 +933,10 @@ static int sftp_cmd_lcd(struct sftp_command *cmd) return 0; } - currdir = smalloc(256); + currdir = snewn(256, char); len = GetCurrentDirectory(256, currdir); if (len > 256) - currdir = srealloc(currdir, len); + currdir = sresize(currdir, len, char); GetCurrentDirectory(len, currdir); printf("New local directory is %s\n", currdir); sfree(currdir); @@ -947,10 +949,10 @@ static int sftp_cmd_lpwd(struct sftp_command *cmd) char *currdir; int len; - currdir = smalloc(256); + currdir = snewn(256, char); len = GetCurrentDirectory(256, currdir); if (len > 256) - currdir = srealloc(currdir, len); + currdir = sresize(currdir, len, char); GetCurrentDirectory(len, currdir); printf("Current local directory is %s\n", currdir); sfree(currdir); @@ -1251,7 +1253,7 @@ struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags) } fflush(stdout); - cmd = smalloc(sizeof(struct sftp_command)); + cmd = snew(struct sftp_command); cmd->words = NULL; cmd->nwords = 0; cmd->wordssize = 0; @@ -1263,7 +1265,7 @@ struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags) char *ret; linesize += 512; - line = srealloc(line, linesize); + line = sresize(line, linesize, char); ret = fgets(line + linelen, linesize - linelen, fp); if (!ret || (linelen == 0 && line[0] == '\0')) { @@ -1295,7 +1297,7 @@ struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags) * containing everything else on the line. */ cmd->nwords = cmd->wordssize = 2; - cmd->words = srealloc(cmd->words, cmd->wordssize * sizeof(char *)); + cmd->words = sresize(cmd->words, cmd->wordssize, char *); cmd->words[0] = "!"; cmd->words[1] = p+1; } else { @@ -1338,8 +1340,7 @@ struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags) *r = '\0'; if (cmd->nwords >= cmd->wordssize) { cmd->wordssize = cmd->nwords + 16; - cmd->words = - srealloc(cmd->words, cmd->wordssize * sizeof(char *)); + cmd->words = sresize(cmd->words, cmd->wordssize, char *); } cmd->words[cmd->nwords++] = q; } @@ -1446,32 +1447,48 @@ static int verbose = 0; */ void fatalbox(char *fmt, ...) { - char str[0x100]; /* Make the size big enough */ + char *str, *str2; + va_list ap; + va_start(ap, fmt); + str = dupvprintf(fmt, ap); + str2 = dupcat("Fatal: ", str, "\n", NULL); + sfree(str); + va_end(ap); + fputs(str2, stderr); + sfree(str2); + + cleanup_exit(1); +} +void modalfatalbox(char *fmt, ...) +{ + char *str, *str2; va_list ap; va_start(ap, fmt); - strcpy(str, "Fatal:"); - vsprintf(str + strlen(str), fmt, ap); + str = dupvprintf(fmt, ap); + str2 = dupcat("Fatal: ", str, "\n", NULL); + sfree(str); va_end(ap); - strcat(str, "\n"); - fputs(str, stderr); + fputs(str2, stderr); + sfree(str2); cleanup_exit(1); } -void connection_fatal(char *fmt, ...) +void connection_fatal(void *frontend, char *fmt, ...) { - char str[0x100]; /* Make the size big enough */ + char *str, *str2; va_list ap; va_start(ap, fmt); - strcpy(str, "Fatal:"); - vsprintf(str + strlen(str), fmt, ap); + str = dupvprintf(fmt, ap); + str2 = dupcat("Fatal: ", str, "\n", NULL); + sfree(str); va_end(ap); - strcat(str, "\n"); - fputs(str, stderr); + fputs(str2, stderr); + sfree(str2); cleanup_exit(1); } -void ldisc_send(char *buf, int len, int interactive) +void ldisc_send(void *handle, char *buf, int len, int interactive) { /* * This is only here because of the calls to ldisc_send(NULL, @@ -1509,7 +1526,7 @@ static unsigned char *outptr; /* where to put the data */ static unsigned outlen; /* how much data required */ static unsigned char *pending = NULL; /* any spare data */ static unsigned pendlen = 0, pendsize = 0; /* length and phys. size of buffer */ -int from_backend(int is_stderr, char *data, int datalen) +int from_backend(void *frontend, int is_stderr, const char *data, int datalen) { unsigned char *p = (unsigned char *) data; unsigned len = (unsigned) datalen; @@ -1545,10 +1562,7 @@ int from_backend(int is_stderr, char *data, int datalen) if (len > 0) { if (pendsize < pendlen + len) { pendsize = pendlen + len + 4096; - pending = (pending ? srealloc(pending, pendsize) : - smalloc(pendsize)); - if (!pending) - fatalbox("Out of memory"); + pending = sresize(pending, pendsize, unsigned char); } memcpy(pending + pendlen, p, len); pendlen += len; @@ -1597,7 +1611,7 @@ int sftp_recvdata(char *buf, int len) } int sftp_senddata(char *buf, int len) { - back->send((unsigned char *) buf, len); + back->send(backhandle, (unsigned char *) buf, len); return 1; } @@ -1608,7 +1622,7 @@ static void ssh_sftp_init(void) { if (sftp_ssh_socket == INVALID_SOCKET) return; - while (!back->sendok()) { + while (!back->sendok(backhandle)) { fd_set readfds; FD_ZERO(&readfds); FD_SET(sftp_ssh_socket, &readfds); @@ -1704,7 +1718,7 @@ static int psftp_connect(char *userhost, char *user, int portnumber) /* * Enact command-line overrides. */ - cmdline_run_saved(); + cmdline_run_saved(&cfg); /* * Trim leading whitespace off the hostname if it's there. @@ -1732,6 +1746,21 @@ static int psftp_connect(char *userhost, char *user, int portnumber) */ cfg.host[strcspn(cfg.host, ":")] = '\0'; + /* + * Remove any remaining whitespace from the hostname. + */ + { + int p1 = 0, p2 = 0; + while (cfg.host[p2] != '\0') { + if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') { + cfg.host[p1] = cfg.host[p2]; + p1++; + } + p2++; + } + cfg.host[p1] = '\0'; + } + /* Set username */ if (user != NULL && user[0] != '\0') { strncpy(cfg.username, user, sizeof(cfg.username) - 1); @@ -1739,6 +1768,7 @@ static int psftp_connect(char *userhost, char *user, int portnumber) } if (!cfg.username[0]) { printf("login as: "); + fflush(stdout); if (!fgets(cfg.username, sizeof(cfg.username), stdin)) { fprintf(stderr, "psftp: aborting\n"); cleanup_exit(1); @@ -1794,11 +1824,14 @@ static int psftp_connect(char *userhost, char *user, int portnumber) back = &ssh_backend; - err = back->init(cfg.host, cfg.port, &realhost, 0); + err = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port, &realhost,0); if (err != NULL) { fprintf(stderr, "ssh_init: %s\n", err); return 1; } + logctx = log_init(NULL, &cfg); + back->provide_logctx(backhandle, logctx); + console_provide_logctx(logctx); ssh_sftp_init(); if (verbose && realhost != NULL) printf("Connected to %s\n", realhost); @@ -1808,11 +1841,11 @@ static int psftp_connect(char *userhost, char *user, int portnumber) void cmdline_error(char *p, ...) { va_list ap; - fprintf(stderr, "pscp: "); + fprintf(stderr, "psftp: "); va_start(ap, p); vfprintf(stderr, p, ap); va_end(ap); - fputc('\n', stderr); + fprintf(stderr, "\n try typing \"psftp -h\" for help\n"); exit(1); } @@ -1827,6 +1860,7 @@ int main(int argc, char *argv[]) int mode = 0; int modeflags = 0; char *batchfile = NULL; + int errors = 0; flags = FLAG_STDERR | FLAG_INTERACTIVE; cmdline_tooltype = TOOLTYPE_FILETRANSFER; @@ -1836,6 +1870,7 @@ int main(int argc, char *argv[]) userhost = user = NULL; + errors = 0; for (i = 1; i < argc; i++) { int ret; if (argv[i][0] != '-') { @@ -1845,7 +1880,7 @@ int main(int argc, char *argv[]) userhost = dupstr(argv[i]); continue; } - ret = cmdline_process_param(argv[i], i+1socket() != NULL) { + if (back != NULL && back->socket(backhandle) != NULL) { char ch; - back->special(TS_EOF); + back->special(backhandle, TS_EOF); sftp_recvdata(&ch, 1); } WSACleanup();