From 5e9545756b8387338febc48874c93df7e0b8e8ee Mon Sep 17 00:00:00 2001 From: Tatsuya Kinoshita Date: Thu, 15 Jan 2015 19:13:51 +0900 Subject: Do not use C99 printf format specifiers and asprintf --- Str.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++----------- cookie.c | 4 +-- map.c | 2 +- 3 files changed, 99 insertions(+), 23 deletions(-) diff --git a/Str.c b/Str.c index 8e4b667..eff82a4 100644 --- a/Str.c +++ b/Str.c @@ -427,27 +427,103 @@ Stralign_center(Str s, int width) Str Sprintf(char *fmt, ...) { - Str s; - char *cb; - int ret; - size_t n; - va_list ap; - - va_start(ap, fmt); - ret = vasprintf(&cb, fmt, ap); - if (ret == -1) { - fprintf(stderr, - "Sprintf: vasprintf failed\n"); - exit(1); + int len = 0; + int status = SP_NORMAL; + int p = 0; + char *f; + Str s; + va_list ap; + + va_start(ap, fmt); + for (f = fmt; *f; f++) { + redo: + switch (status) { + case SP_NORMAL: + if (*f == '%') { + 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); - - n = (size_t) ret + 1; - s = Strnew_size(n); - s->length = ret; - memcpy(s->ptr, cb, n); - free(cb); - return s; + } + va_end(ap); + s = Strnew_size(len * 2); + va_start(ap, fmt); + vsprintf(s->ptr, fmt, ap); + va_end(ap); + s->length = strlen(s->ptr); + if (s->length > len * 2) { + fprintf(stderr, "Sprintf: string too long\n"); + exit(1); + } + return s; } Str diff --git a/cookie.c b/cookie.c index dbd698b..c11649c 100644 --- a/cookie.c +++ b/cookie.c @@ -461,9 +461,9 @@ save_cookies(void) for (p = First_cookie; p; p = p->next) { if (!(p->flag & COO_USE) || p->flag & COO_DISCARD) continue; - fprintf(fp, "%s\t%s\t%s\t%lld\t%s\t%s\t%d\t%d\t%s\t%s\t%s\n", + fprintf(fp, "%s\t%s\t%s\t%ld\t%s\t%s\t%d\t%d\t%s\t%s\t%s\n", parsedURL2Str(&p->url)->ptr, - p->name->ptr, p->value->ptr, (long long) p->expires, + p->name->ptr, p->value->ptr, (long)p->expires, p->domain->ptr, p->path->ptr, p->flag, p->version, str2charp(p->comment), (p->portl) ? portlist2str(p->portl)->ptr : "", diff --git a/map.c b/map.c index 116e71c..bb240ea 100644 --- a/map.c +++ b/map.c @@ -573,7 +573,7 @@ page_info_panel(Buffer *buf) "Number of lines", Sprintf("%d", all)->ptr, "Transferred bytes", - Sprintf("%zu", buf->trbyte)->ptr, NULL); + Sprintf("%lu", (unsigned long)buf->trbyte)->ptr, NULL); a = retrieveCurrentAnchor(buf); if (a != NULL) { -- cgit v1.2.3