#define PLANCK_FEATURE_FMT 1 /* * planck-fmt.c -- string building without stdio. ALWAYS COMPILED IN; there is no manifest switch, * because every other component needs it and the whole point is that printf never gets linked. * * WHY THIS EXISTS. One snprintf anywhere pulls musl's entire printf engine into a static binary. * Measured on this exact toolchain (riscv32-linux-musl-gcc 11.2.1, -static -Os -Wl,-s): * * write() only 5,028 bytes * + one snprintf("%lu") 37,908 bytes <- +32,880 for one integer * * That is not a rounding error, it is the measurement. The 480K planck basis had a 13,540-byte * init; the rebuilt one reached 54,444 for the SAME components, and this is nearly all of the * difference. The reconstruction note in planck-mem.c even observed that the original's pieces * (" mem used=", " free=", " total=") are SEPARATE strings in the old image's string table rather * than one format string -- the original had no printf and assembled the line by concatenation. * The reconstruction then used snprintf anyway. This puts that back. * * Everything appends to a caller-owned buffer and TRUNCATES rather than overflowing. Return value * is the new length, so appends chain without re-scanning. */ #include #include /* Append iStr. Returns the new length, always < iNumCap. */ static size_t planck_cat(char *oStr, size_t iNumCap, size_t iNumAt, const char *iStr) { if (iNumAt + 1 >= iNumCap) return iNumAt; while (*iStr && iNumAt + 1 < iNumCap) oStr[iNumAt++] = *iStr++; oStr[iNumAt] = '\0'; return iNumAt; } /* * Append an unsigned value, optionally right-padded to iNumWidth with iChPad. * Digits are produced backwards into a scratch buffer -- 20 covers 64-bit. */ static size_t planck_catu(char *oStr, size_t iNumCap, size_t iNumAt, unsigned long long iNumVal, unsigned iNumWidth, char iChPad) { char lStrDigits[20]; unsigned lNumLen = 0; do { lStrDigits[lNumLen++] = (char)('0' + (iNumVal % 10)); iNumVal /= 10; } while (iNumVal && lNumLen < sizeof(lStrDigits)); while (lNumLen < iNumWidth && lNumLen < sizeof(lStrDigits)) lStrDigits[lNumLen++] = iChPad; while (lNumLen && iNumAt + 1 < iNumCap) oStr[iNumAt++] = lStrDigits[--lNumLen]; oStr[iNumAt] = '\0'; return iNumAt; } /* * Join a NULL-terminated list of strings into oStr, replacing the "%s%s%s" shape that most call * sites here actually wanted. stdarg.h is the COMPILER's header, not libc's -- it costs nothing, * unlike the printf that used to be reached for. * * planck_cats(cmd, sizeof(cmd), "/sbin/ifconfig ", iface, " up", NULL); */ static size_t planck_cats(char *oStr, size_t iNumCap, ...) { va_list lObjAp; const char *lStr; size_t lNumAt = 0; oStr[0] = '\0'; va_start(lObjAp, iNumCap); while ((lStr = va_arg(lObjAp, const char *)) != NULL) lNumAt = planck_cat(oStr, iNumCap, lNumAt, lStr); va_end(lObjAp); return lNumAt; } /* Signed convenience -- only used for signal numbers, which are never LLONG_MIN. */ static size_t planck_cati(char *oStr, size_t iNumCap, size_t iNumAt, long long iNumVal) { if (iNumVal < 0) { iNumAt = planck_cat(oStr, iNumCap, iNumAt, "-"); iNumVal = -iNumVal; } return planck_catu(oStr, iNumCap, iNumAt, (unsigned long long)iNumVal, 0, '0'); }