1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-01 11:32:48 -05:00

Replace more ad-hoc growing char buffers with strbuf.

I've fixed a handful of these where I found them in passing, but when
I went systematically looking, there were a lot more that I hadn't
found!

A particular highlight of this collection is the code that formats
Windows clipboard data in RTF, which was absolutely crying out for
strbuf_catf, and now it's got it.
This commit is contained in:
Simon Tatham
2019-02-11 06:58:07 +00:00
parent e3e4315033
commit d07d7d66f6
13 changed files with 194 additions and 311 deletions

View File

@ -679,32 +679,21 @@ int main(int argc, char **argv)
!strcmp(p, "-no-sanitize-stderr")) {
sanitise_stderr = FORCE_OFF;
} else if (*p != '-') {
char *command;
int cmdlen, cmdsize;
cmdlen = cmdsize = 0;
command = NULL;
strbuf *cmdbuf = strbuf_new();
while (argc) {
while (*p) {
if (cmdlen >= cmdsize) {
cmdsize = cmdlen + 512;
command = sresize(command, cmdsize, char);
}
command[cmdlen++]=*p++;
}
if (cmdlen >= cmdsize) {
cmdsize = cmdlen + 512;
command = sresize(command, cmdsize, char);
}
command[cmdlen++]=' '; /* always add trailing space */
if (--argc) p = *++argv;
while (argc > 0) {
if (cmdbuf->len > 0)
put_byte(cmdbuf, ' '); /* add space separator */
put_datapl(cmdbuf, ptrlen_from_asciz(p));
if (--argc > 0)
p = *++argv;
}
if (cmdlen) command[--cmdlen]='\0';
/* change trailing blank to NUL */
conf_set_str(conf, CONF_remote_cmd, command);
conf_set_str(conf, CONF_remote_cmd, cmdbuf->s);
conf_set_str(conf, CONF_remote_cmd2, "");
conf_set_bool(conf, CONF_nopty, true); /* command => no tty */
strbuf_free(cmdbuf);
break; /* done with cmdline */
} else {
fprintf(stderr, "plink: unknown option \"%s\"\n", p);

View File

@ -548,34 +548,33 @@ bool enum_settings_next(settings_e *handle, strbuf *out)
{
struct dirent *de;
struct stat st;
char *fullpath;
int maxlen, thislen, len;
strbuf *fullpath;
if (!handle->dp)
return NULL;
fullpath = make_filename(INDEX_SESSIONDIR, NULL);
maxlen = len = strlen(fullpath);
fullpath = strbuf_new();
char *sessiondir = make_filename(INDEX_SESSIONDIR, NULL);
put_datapl(fullpath, ptrlen_from_asciz(sessiondir));
sfree(sessiondir);
put_byte(fullpath, '/');
size_t baselen = fullpath->len;
while ( (de = readdir(handle->dp)) != NULL ) {
thislen = len + 1 + strlen(de->d_name);
if (maxlen < thislen) {
maxlen = thislen;
fullpath = sresize(fullpath, maxlen+1, char);
}
fullpath[len] = '/';
strncpy(fullpath+len+1, de->d_name, thislen - (len+1));
fullpath[thislen] = '\0';
fullpath->len = baselen;
put_datapl(fullpath, ptrlen_from_asciz(de->d_name));
if (stat(fullpath, &st) < 0 || !S_ISREG(st.st_mode))
if (stat(fullpath->s, &st) < 0 || !S_ISREG(st.st_mode))
continue; /* try another one */
decode_session_filename(de->d_name, out);
sfree(fullpath);
strbuf_free(fullpath);
return true;
}
sfree(fullpath);
strbuf_free(fullpath);
return false;
}