ref: ff5d67610caab71d38625c3f1788547d00391337
parent: 108224b4845d7ac622cdc3dcbe47b463e4253a4b
author: zoulasc <zoulasc@users.noreply.github.com>
date: Sun Dec 8 09:41:27 EST 2019
Fix printf formats for integers (#57) * More cleanups: - sprinkle const - add a macro (setptr) that cheats const to temporarily NUL terminate strings remove casts from allocations - use strdup instead of strlen+strcpy - use x = malloc(sizeof(*x)) instead of x = malloc(sizeof(type of *x))) - add -Wcast-qual (and casts through unitptr_t in the two macros we cheat (xfree, setptr)). * More cleanups: - add const - use bounded sscanf - use snprintf instead of sprintf * More cleanup: - use snprintf/strlcat instead of sprintf/strcat - use %j instead of %l since we are casting to intmax_t/uintmax_t * Merge the 3 copies of the code that evaluated array strings with separators and convert them to keep track of lengths and use memcpy instead of strcat. * Fix formats for 32 bit machines broken by previous commit. We use intmax_t to provide maximum range for both 32 and 64 bit machines.
--- a/run.c
+++ b/run.c
@@ -898,7 +898,7 @@
break;
case 'o': case 'x': case 'X': case 'u':
flag = *(s-1) == 'l' ? 'd' : 'u';
- *(t-1) = 'l';
+ *(t-1) = 'j';
*t = *s;
*++t = '\0';
break;
@@ -934,8 +934,8 @@
case 'a':
case 'A':
case 'f': snprintf(p, BUFSZ(p), fmt, getfval(x)); break;
- case 'd': snprintf(p, BUFSZ(p), fmt, (long) getfval(x)); break;
- case 'u': snprintf(p, BUFSZ(p), fmt, (int) getfval(x)); break;
+ case 'd': snprintf(p, BUFSZ(p), fmt, (intmax_t) getfval(x)); break;
+ case 'u': snprintf(p, BUFSZ(p), fmt, (uintmax_t) getfval(x)); break;
case 's':
t = getsval(x);
n = strlen(t);
--
⑨