summaryrefslogtreecommitdiff
path: root/usr.bin/printf/printf.c
AgeCommit message (Collapse)Author
2023-11-26Remove copyright strings ifdef'd outWarner Losh
We've ifdef'd out the copyright strings for some time now. Go ahead and remove the ifdefs. Plus whatever other detritis was left over from other recent removals. These copyright strings are present in the comments and are largely from CSRG's attempt at adding their copyright to every binary file (which modern interpretations of the license doesn't require). Sponsored by: Netflix
2023-11-26usr.bin: Remove ancient SCCS tags.Warner Losh
Remove ancient SCCS tags from the tree, automated scripting, with two minor fixup to keep things compiling. All the common forms in the tree were removed with a perl script. Sponsored by: Netflix
2023-09-25Purge more stray embedded $FreeBSD$ stringsJohn Baldwin
These do not use __FBSDID but instead use bare char arrays. Reviewed by: imp, emaste Differential Revision: https://reviews.freebsd.org/D41957
2018-08-11printf: Fix \c in %b in printf builtin exiting the shell after r337458Jilles Tjoelker
SVN r337458 erroneously partially reverted r265885. This is immediately visible when running the Kyua/ATF tests for usr.bin/printf, which actually test sh's printf builtin. PR: 229641 Notes: svn path=/head/; revision=337618
2018-08-08Fix printf(1) ignores width and precision in %b format.Pedro F. Giffuni
The precision with the conversion specifier b is specified by POSIX: see point 7 in the reference documentation. This corrects previous wrong log in r337440. Reference: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/printf.html PR: 229641 Reported by: Rudolf Cejka Submitted by: Garrett D'Amore (illumos) MFC after: 1 week Notes: svn path=/head/; revision=337458
2018-08-08Revert r337440: the log message is wrong.Pedro F. Giffuni
Notes: svn path=/head/; revision=337457
2018-08-07Fix printf(1) ignores width and precision in %b format.Pedro F. Giffuni
The precision with behavior is "unspecified" by POSIX (as of 2018), but most implementations seem to have taken it to be treated the same as for "s"; applied after the unescaping. Adopt the same treatment on our printf. PR: 229641 Submitted by: Garrett D'Amore (illumos) Notes: svn path=/head/; revision=337440
2017-11-20General further adoption of SPDX licensing ID tags.Pedro F. Giffuni
Mainly focus on files that use BSD 3-Clause license. The Software Package Data Exchange (SPDX) group provides a specification to make it easier for automated tools to detect and summarize well known opensource licenses. We are gradually adopting the specification, noting that the tags are considered only advisory and do not, in any way, superceed or replace the license texts. Special thanks to Wind River for providing access to "The Duke of Highlander" tool: an older (2014) run over FreeBSD tree was useful as a starting point. Notes: svn path=/head/; revision=326025
2017-04-29printf: Output formatted data directly, instead of via asprintf.Jilles Tjoelker
Long ago, sh used to have its own optimized and restricted string formatting implementation, which the printf builtin had to bypass via asprintf() to a temporary buffer. Since sh has used libc's string formatting implementation for a long time, remove the workaround. Add a check to keep printf %c '' working the same way (output nothing); POSIX allows both outputting nothing and outputting a NUL byte. Also, this change avoids silently discarding format directives for whose output asprintf() cannot allocate memory. Notes: svn path=/head/; revision=317598
2017-02-28Renumber copyright clause 4Warner Losh
Renumber cluase 4 to 3, per what everybody else did when BSD granted them permission to remove clause 3. My insistance on keeping the same numbering for legal reasons is too pedantic, so give up on that point. Submitted by: Jan Schaumann <jschauma@stevens.edu> Pull Request: https://github.com/freebsd/freebsd/pull/96 Notes: svn path=/head/; revision=314436
2015-03-01sh: Fix compiler warnings related to duplicate or missing declarations.Jilles Tjoelker
Notes: svn path=/head/; revision=279503
2014-05-29Minor style knit.Pedro F. Giffuni
Notes: svn path=/head/; revision=266853
2014-05-26printf(1): warn about incomplete uses n$Pedro F. Giffuni
Reviewed by: jilles Obtained from: Illumos MFC after: 2 weeks Notes: svn path=/head/; revision=266690
2014-05-13printf: Fix missing arguments for %u/%o/%x/%X after r265592.Jilles Tjoelker
If a numeric argument is missing, zero should be assumed, for signed as well as unsigned conversions. This fixes the 'zero' regression tests. r265592 erroneously reverted r244407. Notes: svn path=/head/; revision=265977
2014-05-11printf: fix regression after illumos merges.Pedro F. Giffuni
The "bltin/bltin.h" wrappers do not support exit() and attempting to call it will exit sh completely. Note that errx() is acceptable but will always return with status 2. Reported by: jilles (and the testing framework) Fix by: jilles Pointyhat: pfg Notes: svn path=/head/; revision=265885
2014-05-10prinf: replace use of alloca with variable length array.Pedro F. Giffuni
Use of alloca(3) is discouraged in FreeBSD. Using a VLA is simple and should be more portable. Requested by: jilles Notes: svn path=/head/; revision=265860
2014-05-08Fix the incorrect handling of %b and \c in printf(1)Pedro F. Giffuni
This is required for POSIX compliance. Obtained from: Garrett D'Amore (Illumos) MFC after: 4 days Notes: svn path=/head/; revision=265706
2014-05-07Add width and precision specifiers to printf(1) %n$.Pedro F. Giffuni
This actually completes r264743 so that width and precision specifiers work properly with %n$. These keeps consistency with ksh93 and zsh. Requested by: jilles Obtained from: Garrett D'Amore (Illumos) MFC after: 4 days Notes: svn path=/head/; revision=265592
2014-04-21Add parameterized position handling to printf(1).Pedro F. Giffuni
Add a new %n$ option to change the order of the parameters as done in the ksh93 builtin printf (among others). For example: %printf '%2$1d %1$s\n' one 2 three 4 2 one 4 three The feature was written by Garret D'Amore under a BSD license for Nexenta/illumos. Reference: http://garrett.damore.org/2010/10/new-implementation-of-printf.html PR: bin/152934 Obtained from: Illumos MFC after: 2 weeks Notes: svn path=/head/; revision=264743
2014-04-17Minor style(9) cleanupsPedro F. Giffuni
#define should be followed by a tab. MFC after: 3 days Notes: svn path=/head/; revision=264581
2012-12-18POSIX requires that non-existent or null arguments be treated as if aEitan Adler
zero argument were supplied. Add a regression test to catch this case as well. PR: bin/174521 Submitted by: Daniel Shahaf <danielsh@elego.de> (pr) Submitted by: Mark Johnston <markjdb@gmail.com> (initial patch) Reviewed by: jilles Approved by: cperciva (implicit) MFC after: 3 weeks Notes: svn path=/head/; revision=244407
2012-09-15sh: Prefer internal nextopt() to libc getopt().Jilles Tjoelker
This reduces code duplication and code size. /usr/bin/printf is not affected. Side effect: different error messages when certain builtins are passed invalid options. Notes: svn path=/head/; revision=240541
2012-01-12Style cleanups for printf.Pedro F. Giffuni
PR: bin/152934 Approved by: jhb (mentor) Obtained from: Illumos MFC after: 2 weeks Notes: svn path=/head/; revision=230027
2011-05-28printf: Allow multibyte characters for '<char> form, avoid negative codes.Jilles Tjoelker
Examples: LC_ALL=en_US.UTF-8 printf '%d\n' $(printf \'\\303\\244) LC_ALL=en_US.ISO8859-1 printf '%d\n' $(printf \'\\344) Both of these should print 228. Like some other shells, incomplete or invalid multibyte characters yield the value of the first byte without a warning. Note that there is no general way to go back from the character code to the character. Notes: svn path=/head/; revision=222418
2011-03-01printf: Note that this is used both as a normal program and a shell builtin.Jilles Tjoelker
Notes: svn path=/head/; revision=219153
2010-12-29printf: Do not use sh memory functions in sh builtin.Jilles Tjoelker
These functions throw exceptions if they fail, possibly causing memory leaks. The normal out-of-memory handling suffices. The INTOFF around almost all of printf prevents memory leaks due to SIGINT. Notes: svn path=/head/; revision=216809
2010-12-20sh: Make warnings in the printf builtin non-fatal, like in the program.Jilles Tjoelker
The #define for warnx now behaves much like the libc function (except that it uses sh command name and output). Also, it now uses C99 __VA_ARGS__ so there is no need for three different macros for 0, 1 or 2 parameters. Notes: svn path=/head/; revision=216606
2010-12-14Revert r216423 per request from Jilles.Xin LI
The new behavior prevents us from being able to bail out explicitly on unknown options that we have not implemented. BASH for instance have introduced a '-v' for printf(1) builtin and it seems to be bad to pretend that we supported it and have a script break silently. Notes: svn path=/head/; revision=216447
2010-12-14When printf is being used as a sh(1) builtin, it can not callXin LI
exit(3) as pointed out by jilles@ so revert to using return(), also change the return value back to 1 as requested by bde@. This is logically a revert of revision 216422. Notes: svn path=/head/; revision=216439
2010-12-14We work on ctype's and not only on numbers so set LC_ALL instead ofXin LI
LC_NUMERIC. PR: bin/152934 Submitted by: Pedro F. Giffuni <giffunip tutopia.com> Obtained from: Illumos Notes: svn path=/head/; revision=216424
2010-12-14IEEE Std 1003.1-2008, Section 1.4, Utility Description Defaults saysXin LI
that when the options section is listed as "None", utility shall recognize "--" as a first argument to be discarded. This implementation is largely based on OpenBSD implementation but we do slightly differently: a) We skip argv[0] as the first step; b) We test whether the next argument is "--" and ignore it. With this change one will get: %printf usage: printf format [arguments ...] %printf -v -v%printf -- -v -v% %printf -- usage: printf format [arguments ...] Which matches the behavior observed on a Debian system but different from the Illumos change. Notes: svn path=/head/; revision=216423
2010-12-14Make use of EX_USAGE for usage().Xin LI
Notes: svn path=/head/; revision=216422
2010-12-13The only caller of mknum() provides a char instead of an int, so make itXin LI
match the definition. PR: bin/152934 Submitted by: Pedro F. Giffuni <giffunip tutopia.com> Obtained from: Illumos Notes: svn path=/head/; revision=216418
2010-12-13Move locale.h include to the beginning header section as pointed out byXin LI
style(9) Submitted by: Pedro F. Giffuni <giffunip tutopia.com> Notes: svn path=/head/; revision=216417
2010-12-11Remove the advertising clause from UCB copyrighted files in usr.bin. ThisJoel Dahl
is in accordance with the information provided at ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change Also add $FreeBSD$ to a few files to keep svn happy. Discussed with: imp, rwatson Notes: svn path=/head/; revision=216370
2010-12-08printf: Remove support for building as a csh builtin.Jilles Tjoelker
The #define BUILTIN was for building as a csh (not tcsh) builtin. Given that csh was replaced by tcsh years ago there is no point in keeping this. The #define SHELL is for building as an sh builtin and is in active use. This commit does not change the /bin/sh and /usr/bin/printf binaries. Notes: svn path=/head/; revision=216310
2010-11-19sh: Add printf builtin.Jilles Tjoelker
This was removed in 2001 but I think it is appropriate to add it back: * I do not want to encourage people to write fragile and non-portable echo commands by making printf much slower than echo. * Recent versions of Autoconf use it a lot. * Almost no software still wants to support systems that do not have printf(1) at all. * In many other shells printf is already a builtin. Side effect: printf is now always the builtin version (which behaves identically to /usr/bin/printf) and cannot be overridden via PATH (except via the undocumented %builtin mechanism). Code size increases about 5K on i386. Embedded folks might want to replace /usr/bin/printf with a hard link to /usr/bin/alias. Notes: svn path=/head/; revision=215520
2008-08-02POSIX says that octal escapes have the format \ddd in the format string,David Schultz
but \0ddd in a %b argument, with a length restriction of 3 octal digits in either case. This seems silly, but it needs to be right so it's possible to write an octal escape followed by an ordinary digit. Solaris printf(1) and GNU printf(1) also behave this way. Example: "printf '\0752'" now produces "=2" instead of garbage. Notes: svn path=/head/; revision=181153
2005-08-05Prefer {u,}intmax_t over the deprecated {u_,}quad_t.Stefan Farfeleder
Notes: svn path=/head/; revision=148721
2005-05-21Sync program's usage() with manpage's SYNOPSIS.Ruslan Ermilov
Notes: svn path=/head/; revision=146466
2005-04-14Handle null characters in the format string. A \0 in the argument passed to %bStefan Farfeleder
still results in trucation but this is be much harder to fix. Notes: svn path=/head/; revision=145078
2005-04-14No reason to write \a and \v as octal escape sequences.Stefan Farfeleder
Notes: svn path=/head/; revision=145074
2005-04-14- Move parts of the long main() function into a new function doformat().Stefan Farfeleder
- Rewrite the loop in main() to be more understandable. Notes: svn path=/head/; revision=145061
2005-04-13Assign 0.0 to the variable passed to getfloating() if the argument is missing.Stefan Farfeleder
MFC after: 1 week Notes: svn path=/head/; revision=145027
2005-04-11Replace buggy for-loops to skip certain character with strspn(). If *fmt wasStefan Farfeleder
'\0' (eg in the invocation 'printf %'), the for-loop would miss the terminating null character. MFC after: 1 week Notes: svn path=/head/; revision=144902
2005-03-21Support the L modifier for floating-point values as an extension.David Schultz
When L is omitted, double precision is used, so printf(1) gives reproducable results. When L is specified, long double precision is used, which may improve precision, depending on the machine. Notes: svn path=/head/; revision=143906
2004-09-24Allow %' to be used as a format flag by printf(1). This makes itGiorgos Keramidas
possible to print the thousands separator in the locale setups that have one, by something like this: $ env -i LC_NUMERIC=en_US.ISO8859-1 ./printf "%'0.2f\n" 12345 12,345.00 Reviewed by: das Notes: svn path=/head/; revision=135751
2004-06-05Enable support for the %a, %A, and %F format specifiers.David Schultz
Notes: svn path=/head/; revision=130115
2004-03-07Make it possible for the %[eEfgG] formats to not result in an errorColin Percival
being reported by /usr/bin/printf. This bug has been around for 22 months... either nobody uses printf with floating-point values, or people are forgetting to check their return codes. Approved by: rwatson (mentor) Notes: svn path=/head/; revision=126729
2002-09-04ANSIify function definitions.David Malone
Add some constness to avoid some warnings. Remove use register keyword. Deal with missing/unneeded extern/prototypes. Some minor type changes/casts to avoid warnings. Reviewed by: md5 Notes: svn path=/head/; revision=102944