3 ms·
> It appears that bash `printf` wraps coreutils printf "with ARGUMENTs converted to proper type first," according to the manual. I guess that's how the "%509s"
by kevinoid 8y ago
> It appears that bash `printf` wraps coreutils printf "with ARGUMENTs converted to proper type first," according to the manual. I guess that's how the "%509s" specifier is able to work without any args.
Actually, coreutils printf follows POSIX/SUS which states that "Any extra b, c, or s conversion specifiers shall be evaluated as if a null string argument were supplied".[1] So that trick will work for many printf(1) implementations (including both bash and coreutils).
Bash printf(1) doesn't have any hooks or wrappers, but it does parse the format string to determine how to convert arguments before calling libc vsnprintf(3). If you are interested in the details, check out how %d/%i is handled in bash[2] and coreutils[3].
1. http://pubs.opengroup.org/onlinepubs/9699919799/utilities/printf.html http://pubs.opengroup.org/onlinepubs/9699919799/utilities/pr... (item 9 under Extended Description)
2. https://git.savannah.gnu.org/cgit/bash.git/tree/builtins/printf.def?h=bash-4.4#n598 https://git.savannah.gnu.org/cgit/bash.git/tree/builtins/pri...
3. https://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=blob;f=src/printf.c;hb=v8.30#l374 https://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=blob;...
- jancsika 8y agoThanks for the links, those are really helpful! Why didn't libc include a printf function that expands a format specifier with a pointer to a byte array (and/or possibly an array of c strings)? That way there would at least be a standard way of handling arbitrary input and erroring out with less risk of buffer overflows and stuff.