X-Git-Url: https://git.distorted.org.uk/u/mdw/putty/blobdiff_plain/a832773496d46caa5e328d36a15b4918f24a804e..7bd029472d96162f71f61df67baedc00fc0bb1a8:/scp.c diff --git a/scp.c b/scp.c index 069f7ef9..8f21a7f1 100644 --- a/scp.c +++ b/scp.c @@ -101,6 +101,7 @@ static int using_sftp = 0; static Backend *back; static void *backhandle; +static Config cfg; static void source(char *src); static void rsource(char *src); @@ -160,13 +161,15 @@ static void tell_str(FILE * stream, char *str) static void tell_user(FILE * stream, char *fmt, ...) { - char str[0x100]; /* Make the size big enough */ + char *str, *str2; va_list ap; va_start(ap, fmt); - vsprintf(str, fmt, ap); + str = dupvprintf(fmt, ap); va_end(ap); - strcat(str, "\n"); - tell_str(stream, str); + str2 = dupcat(str, "\n", NULL); + sfree(str); + tell_str(stream, str2); + sfree(str2); } static void gui_update_stats(char *name, unsigned long size, @@ -216,14 +219,15 @@ static void gui_update_stats(char *name, unsigned long size, */ void fatalbox(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"); - tell_str(stderr, str); + tell_str(stderr, str2); + sfree(str2); errs++; if (gui_mode) { @@ -239,14 +243,15 @@ void fatalbox(char *fmt, ...) } void modalfatalbox(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"); - tell_str(stderr, str); + tell_str(stderr, str2); + sfree(str2); errs++; if (gui_mode) { @@ -262,14 +267,15 @@ void modalfatalbox(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"); - tell_str(stderr, str); + tell_str(stderr, str2); + sfree(str2); errs++; if (gui_mode) { @@ -311,7 +317,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(void *frontend, 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; @@ -347,8 +353,7 @@ int from_backend(void *frontend, 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)); + pending = sresize(pending, pendsize, unsigned char); if (!pending) fatalbox("Out of memory"); } @@ -427,14 +432,15 @@ static void ssh_scp_init(void) */ static void bump(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); va_end(ap); - strcat(str, "\n"); - tell_str(stderr, str); + str2 = dupcat(str, "\n", NULL); + sfree(str); + tell_str(stderr, str2); + sfree(str2); errs++; if (back != NULL && back->socket(backhandle) != NULL) { @@ -487,7 +493,7 @@ static void do_cmd(char *host, char *user, char *cmd) /* * Enact command-line overrides. */ - cmdline_run_saved(); + cmdline_run_saved(&cfg); /* * Trim leading whitespace off the hostname if it's there. @@ -538,7 +544,7 @@ static void do_cmd(char *host, char *user, char *cmd) namelen = 0; if (GetUserName(user, &namelen) == FALSE) bump("Empty user name"); - user = smalloc(namelen * sizeof(char)); + user = snewn(namelen, char); GetUserName(user, &namelen); if (verbose) tell_user(stderr, "Guessing user name: %s", user); @@ -568,11 +574,12 @@ static void do_cmd(char *host, char *user, char *cmd) back = &ssh_backend; - err = back->init(NULL, &backhandle, cfg.host, cfg.port, &realhost, 0); + err = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port, &realhost,0); if (err != NULL) bump("ssh_init: %s", err); - logctx = log_init(NULL); + logctx = log_init(NULL, &cfg); back->provide_logctx(backhandle, logctx); + console_provide_logctx(logctx); ssh_scp_init(); if (verbose && realhost != NULL) tell_user(stderr, "Connected to %s\n", realhost); @@ -769,8 +776,7 @@ void scp_sftp_listdir(char *dirname) 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++) @@ -1056,7 +1062,7 @@ int scp_sink_setup(char *source, int preserve, int recursive) * wildcardness comes before the final slash) and arrange * things so that a dirstack entry will be set up. */ - newsource = smalloc(1+strlen(source)); + newsource = snewn(1+strlen(source), char); if (!wc_unescape(newsource, source)) { /* Yes, here we go; it's a wildcard. Bah. */ char *dupsource, *lastpart, *dirpart, *wildcard; @@ -1089,7 +1095,7 @@ int scp_sink_setup(char *source, int preserve, int recursive) * wildcard escapes from the directory part, throwing * an error if it contains a real wildcard. */ - dirpart = smalloc(1+strlen(dupsource)); + dirpart = snewn(1+strlen(dupsource), char); if (!wc_unescape(dirpart, dupsource)) { tell_user(stderr, "%s: multiple-level wildcards unsupported", source); @@ -1298,8 +1304,7 @@ int scp_get_sink_action(struct scp_sink_action *act) } 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++) ournames[nnames++] = names->names[i]; @@ -1308,7 +1313,7 @@ int scp_get_sink_action(struct scp_sink_action *act) } fxp_close(dirhandle); - newitem = smalloc(sizeof(struct scp_sftp_dirstack)); + newitem = snew(struct scp_sftp_dirstack); newitem->next = scp_sftp_dirstack_head; newitem->names = ournames; newitem->namepos = 0; @@ -1396,7 +1401,7 @@ int scp_get_sink_action(struct scp_sink_action *act) bump("Lost connection"); if (i >= bufsize) { bufsize = i + 128; - act->buf = srealloc(act->buf, bufsize); + act->buf = sresize(act->buf, bufsize, char); } act->buf[i++] = ch; } while (ch != '\n'); @@ -1503,16 +1508,17 @@ int scp_finish_filerecv(void) */ static void run_err(const char *fmt, ...) { - char str[2048]; + char *str, *str2; va_list ap; va_start(ap, fmt); errs++; - strcpy(str, "scp: "); - vsprintf(str + strlen(str), fmt, ap); - strcat(str, "\n"); - scp_send_errmsg(str); - tell_user(stderr, "%s", str); + str = dupvprintf(fmt, ap); + str2 = dupcat("scp: ", str, "\n", NULL); + sfree(str); + scp_send_errmsg(str2); + tell_user(stderr, "%s", str2); va_end(ap); + sfree(str2); } /* @@ -1921,12 +1927,11 @@ static void toremote(int argc, char *argv[]) FindClose(fh); } - cmd = smalloc(strlen(targ) + 100); - sprintf(cmd, "scp%s%s%s%s -t %s", - verbose ? " -v" : "", - recursive ? " -r" : "", - preserve ? " -p" : "", - targetshouldbedirectory ? " -d" : "", targ); + cmd = dupprintf("scp%s%s%s%s -t %s", + verbose ? " -v" : "", + recursive ? " -r" : "", + preserve ? " -p" : "", + targetshouldbedirectory ? " -d" : "", targ); do_cmd(host, user, cmd); sfree(cmd); @@ -2025,12 +2030,11 @@ static void tolocal(int argc, char *argv[]) user = NULL; } - cmd = smalloc(strlen(src) + 100); - sprintf(cmd, "scp%s%s%s%s -f %s", - verbose ? " -v" : "", - recursive ? " -r" : "", - preserve ? " -p" : "", - targetshouldbedirectory ? " -d" : "", src); + cmd = dupprintf("scp%s%s%s%s -f %s", + verbose ? " -v" : "", + recursive ? " -r" : "", + preserve ? " -p" : "", + targetshouldbedirectory ? " -d" : "", src); do_cmd(host, user, cmd); sfree(cmd); @@ -2073,7 +2077,7 @@ static void get_dir_list(int argc, char *argv[]) user = NULL; } - cmd = smalloc(4 * strlen(src) + 100); + cmd = snewn(4 * strlen(src) + 100, char); strcpy(cmd, "ls -la '"); p = cmd + strlen(cmd); for (q = src; *q; q++) { @@ -2161,7 +2165,7 @@ void cmdline_error(char *p, ...) va_start(ap, p); vfprintf(stderr, p, ap); va_end(ap); - fputc('\n', stderr); + fprintf(stderr, "\n try typing just \"pscp\" for help\n"); exit(1); } @@ -2184,7 +2188,7 @@ int main(int argc, char *argv[]) int ret; if (argv[i][0] != '-') break; - ret = cmdline_process_param(argv[i], i+1