Correct printf arguments and use asprintf

Origin: http://marc.info/?l=openbsd-ports&m=142090828929750&w=2

* Str.c: Use asprintf() instead of rolling our own printf string
length detection.

* cookie.c: Pass the char pointer in the string struct to printf %s
instead of the string struct itself.
Print time_t using %lld instead of %ld to allow for 64-bit time_t.

* main.c: Print a long int using the correct format specifier.

* map.c: Print size_t using the correct format specifier.
This commit is contained in:
Scarlett
2015-01-15 18:58:35 +09:00
committed by Tatsuya Kinoshita
parent dae53252fb
commit ec8272d8fe
4 changed files with 24 additions and 100 deletions
+19 -95
View File
@@ -427,103 +427,27 @@ Stralign_center(Str s, int width)
Str Str
Sprintf(char *fmt, ...) Sprintf(char *fmt, ...)
{ {
int len = 0; Str s;
int status = SP_NORMAL; char *cb;
int p = 0; int ret;
char *f; size_t n;
Str s; va_list ap;
va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
for (f = fmt; *f; f++) { ret = vasprintf(&cb, fmt, ap);
redo: if (ret == -1) {
switch (status) { fprintf(stderr,
case SP_NORMAL: "Sprintf: vasprintf failed\n");
if (*f == '%') { exit(1);
status = SP_PREC;
p = 0;
}
else
len++;
break;
case SP_PREC:
if (IS_ALPHA(*f)) {
/* conversion char. */
double vd;
int vi;
char *vs;
void *vp;
switch (*f) {
case 'l':
case 'h':
case 'L':
case 'w':
continue;
case 'd':
case 'i':
case 'o':
case 'x':
case 'X':
case 'u':
vi = va_arg(ap, int);
len += (p > 0) ? p : 10;
break;
case 'f':
case 'g':
case 'e':
case 'G':
case 'E':
vd = va_arg(ap, double);
len += (p > 0) ? p : 15;
break;
case 'c':
len += 1;
vi = va_arg(ap, int);
break;
case 's':
vs = va_arg(ap, char *);
vi = strlen(vs);
len += (p > vi) ? p : vi;
break;
case 'p':
vp = va_arg(ap, void *);
len += 10;
break;
case 'n':
vp = va_arg(ap, void *);
break;
}
status = SP_NORMAL;
}
else if (IS_DIGIT(*f))
p = p * 10 + *f - '0';
else if (*f == '.')
status = SP_PREC2;
else if (*f == '%') {
status = SP_NORMAL;
len++;
}
break;
case SP_PREC2:
if (IS_ALPHA(*f)) {
status = SP_PREC;
goto redo;
}
break;
} }
} va_end(ap);
va_end(ap);
s = Strnew_size(len * 2); n = (size_t) ret + 1;
va_start(ap, fmt); s = Strnew_size(n);
vsprintf(s->ptr, fmt, ap); s->length = ret;
va_end(ap); memcpy(s->ptr, cb, n);
s->length = strlen(s->ptr); free(cb);
if (s->length > len * 2) { return s;
fprintf(stderr, "Sprintf: string too long\n");
exit(1);
}
return s;
} }
Str Str
+3 -3
View File
@@ -247,7 +247,7 @@ find_cookie(ParsedURL *pu)
Strcat(tmp, Sprintf("; $Domain=\"%s\"", p1->domain->ptr)); Strcat(tmp, Sprintf("; $Domain=\"%s\"", p1->domain->ptr));
if (p1->portl) if (p1->portl)
Strcat(tmp, Strcat(tmp,
Sprintf("; $Port=\"%s\"", portlist2str(p1->portl))); Sprintf("; $Port=\"%s\"", portlist2str(p1->portl)->ptr));
} }
} }
return tmp; return tmp;
@@ -461,9 +461,9 @@ save_cookies(void)
for (p = First_cookie; p; p = p->next) { for (p = First_cookie; p; p = p->next) {
if (!(p->flag & COO_USE) || p->flag & COO_DISCARD) if (!(p->flag & COO_USE) || p->flag & COO_DISCARD)
continue; continue;
fprintf(fp, "%s\t%s\t%s\t%ld\t%s\t%s\t%d\t%d\t%s\t%s\t%s\n", fprintf(fp, "%s\t%s\t%s\t%lld\t%s\t%s\t%d\t%d\t%s\t%s\t%s\n",
parsedURL2Str(&p->url)->ptr, parsedURL2Str(&p->url)->ptr,
p->name->ptr, p->value->ptr, p->expires, p->name->ptr, p->value->ptr, (long long) p->expires,
p->domain->ptr, p->path->ptr, p->flag, p->domain->ptr, p->path->ptr, p->flag,
p->version, str2charp(p->comment), p->version, str2charp(p->comment),
(p->portl) ? portlist2str(p->portl)->ptr : "", (p->portl) ? portlist2str(p->portl)->ptr : "",
+1 -1
View File
@@ -5841,7 +5841,7 @@ set_buffer_environ(Buffer *buf)
set_environ("W3M_CURRENT_FORM", form2str((FormItemList *)a->url)); set_environ("W3M_CURRENT_FORM", form2str((FormItemList *)a->url));
else else
set_environ("W3M_CURRENT_FORM", ""); set_environ("W3M_CURRENT_FORM", "");
set_environ("W3M_CURRENT_LINE", Sprintf("%d", set_environ("W3M_CURRENT_LINE", Sprintf("%ld",
l->real_linenumber)->ptr); l->real_linenumber)->ptr);
set_environ("W3M_CURRENT_COLUMN", Sprintf("%d", set_environ("W3M_CURRENT_COLUMN", Sprintf("%d",
buf->currentColumn + buf->currentColumn +
+1 -1
View File
@@ -573,7 +573,7 @@ page_info_panel(Buffer *buf)
"<tr valign=top><td nowrap>Number of lines<td>", "<tr valign=top><td nowrap>Number of lines<td>",
Sprintf("%d", all)->ptr, Sprintf("%d", all)->ptr,
"<tr valign=top><td nowrap>Transferred bytes<td>", "<tr valign=top><td nowrap>Transferred bytes<td>",
Sprintf("%d", buf->trbyte)->ptr, NULL); Sprintf("%zu", buf->trbyte)->ptr, NULL);
a = retrieveCurrentAnchor(buf); a = retrieveCurrentAnchor(buf);
if (a != NULL) { if (a != NULL) {