fmtcheck(3bsd)
π fmtcheck β the girl who sanitizes your user-supplied printf(3)-style format strings π
okay so bestie, welcome to fmtcheck(3bsd), a lil utility straight out of the BSD world, living in the LOCAL section. she's here to stop you from getting absolutely wrecked by a format string you didn't write yourself. π«Ά
π LIBRARY
Utility functions from BSD systems β that's libbsd, and you link her with -lbsd. periodt.
β¨ SYNOPSIS β¨
#include <stdio.h>
(See libbsd(7) for include usage.)
const char *
fmtcheck(const char *fmt_suspect, const char *fmt_default);
yes, that include comment is really in there, i'm not being quirky β go read libbsd(7) for the include usage deets.
π DESCRIPTION π
so what's her job? fmtcheck scans fmt_suspect and fmt_default and figures out two things:
- will
fmt_suspecteat the same argument types asfmt_default? π - is
fmt_suspecteven a valid format string at all?
because here's the tea: the printf(3) family of functions can not verify the types of arguments that they are passed at run-time. like, they literally cannot. so in some cases β think catgets(3) β it's useful or even necessary to use a user-supplied format string with no guarantee that the format string matches the specified parameters. π
and that's exactly the situation fmtcheck was designed for, as in:
printf(fmtcheck(user_format, standard_format), arg1, arg2);
iconic. she checks the vibe of your format string before it ever reaches printf.
now, in the check, field widths, fillers, precisions, etc. are ignored β unless the field width or precision is an asterisk β*β instead of a digit string. π also, any text other than the format specifiers is completely ignored. so don't stress about the prose, she only cares about the specs.
note that the formats may be quite different as long as they accept the same parameters. π
for example, this:
"%ld %o %30s %#llx %-10.*e %n"
is compatible with this:
"This number %lu %d%% and string %s has %qd numbers and %.*g floats (%n)."
slay. but watch out, because these are NOT equivalent:
"%o"is not equivalent to"%lx"β the first requires anintegerand the second requires along. different vibes entirely. π"%p"is not equivalent to"%lu"β the first requires apointerand the second requires along. also not the same bestie!! π
π RETURN VALUES π
here's the moment of truth:
- if
fmt_suspectis a valid format and consumes the same argument types asfmt_defaultβ she returnsfmt_suspect. β you're safe, main character energy. - otherwise β she returns
fmt_default. π the fallback comes in clutch and saves your whole program.
π SEE ALSO
and that's the whole page, bestie!! π¦πΈ this one's from the Debian crew, dated June 14, 2014, and yes, it's filed right there under fmtcheck(3bsd) in the LOCAL section. mwah. π
β¨π
π¦ where this page comes from
- alpine 3.22: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- alpine 3.23: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- alpine 3.24: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- arch rolling: libbsd 0.12.2-2 Β· custom
- debian bookworm: libbsd-dev 0.11.7-2
- debian trixie: libbsd-dev 0.12.2-2
- epel 10: libbsd-devel 0.12.2-4.el10_0 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- epel 9: libbsd-devel 0.12.2-1.el9 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 43: libbsd-devel 0.12.2-6.fc43 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 43: libbsd-devel 0.12.2-6.fc43 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 44: libbsd-devel 0.12.2-7.fc44 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 44: libbsd-devel 0.12.2-7.fc44 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- nixos 26.05: libbsd 0.12.2
- opensuse-leap 16.0: libbsd-devel 0.11.7-160000.3.2 Β· BSD-3-Clause
- opensuse-leap 16.0: libbsd-devel 0.11.7-160000.3.2 Β· BSD-3-Clause
- opensuse-leap 16.0: libbsd-devel 0.11.7-160000.3.2 Β· BSD-3-Clause
- opensuse-leap 16.0: libbsd-devel 0.11.7-160000.3.2 Β· BSD-3-Clause
- ubuntu noble: libbsd-dev 0.12.1-1build1
- ubuntu noble: libbsd-dev 0.12.1-1build1.1
- ubuntu questing: libbsd-dev 0.12.2-2build1
- ubuntu resolute: libbsd-dev 0.12.2-2build2
- ubuntu stonking: libbsd-dev 0.12.2-3
- Copyright (c) 2000 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms