Commit Graph

94 Commits

Author SHA1 Message Date
Eitan Adler
2f01c791e9 POSIX requires that non-existent or null arguments be treated as if a
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
2012-12-18 21:02:38 +00:00
Jilles Tjoelker
7cbda73825 sh: Prefer internal nextopt() to libc getopt().
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.
2012-09-15 21:56:30 +00:00
Eitan Adler
50d675f7a9 Remove trailing whitespace per mdoc lint warning
Disussed with:	gavin
No objection from:	doc
Approved by:	joel
MFC after:	3 days
2012-03-29 05:02:12 +00:00
Pedro F. Giffuni
ab71f27172 Style cleanups for printf.
PR:		bin/152934
Approved by:	jhb (mentor)
Obtained from:	Illumos
MFC after:	2 weeks
2012-01-12 20:30:20 +00:00
Jilles Tjoelker
27a43b2e51 printf(1): Document that %c and precision for %b/%s use bytes, not chars.
This means these features do not work as expected with multibyte characters.

This perhaps less than ideal behaviour matches printf(3) and is specified by
POSIX.
2011-05-28 14:32:47 +00:00
Jilles Tjoelker
98102dabd3 printf: Allow multibyte characters for '<char> form, avoid negative codes.
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.
2011-05-28 11:37:47 +00:00
Jilles Tjoelker
1792d820be printf(1): Move non-bugs from the BUGS section to a new section CAVEATS. 2011-04-25 19:15:58 +00:00
Jilles Tjoelker
8a906b236a printf(1): Mention effect of '#' modifier on other conversions. 2011-04-25 12:36:29 +00:00
Jilles Tjoelker
77fae5c16d printf: Note that this is used both as a normal program and a shell builtin. 2011-03-01 21:47:06 +00:00
Jilles Tjoelker
add265c6ba printf: Do not use sh memory functions in sh builtin.
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.
2010-12-29 21:38:00 +00:00
Jilles Tjoelker
6a6760db7f sh: Make warnings in the printf builtin non-fatal, like in the program.
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.
2010-12-20 23:06:57 +00:00
Xin LI
de46de4dd1 Revert r216423 per request from Jilles.
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.
2010-12-14 20:35:08 +00:00
Xin LI
5eccc0009b When printf is being used as a sh(1) builtin, it can not call
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.
2010-12-14 18:23:15 +00:00
Xin LI
69be0c5ea2 We work on ctype's and not only on numbers so set LC_ALL instead of
LC_NUMERIC.

PR:		bin/152934
Submitted by:	Pedro F. Giffuni <giffunip tutopia.com>
Obtained from:	Illumos
2010-12-14 01:28:33 +00:00
Xin LI
02a4a3f56c IEEE Std 1003.1-2008, Section 1.4, Utility Description Defaults says
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.
2010-12-14 01:16:56 +00:00
Xin LI
6715165066 Make use of EX_USAGE for usage(). 2010-12-14 00:21:34 +00:00
Xin LI
9180853b89 The only caller of mknum() provides a char instead of an int, so make it
match the definition.

PR:		bin/152934
Submitted by:	Pedro F. Giffuni <giffunip tutopia.com>
Obtained from:	Illumos
2010-12-13 19:54:42 +00:00
Xin LI
c9aca921b8 Move locale.h include to the beginning header section as pointed out by
style(9)

Submitted by:	Pedro F. Giffuni <giffunip tutopia.com>
2010-12-13 19:50:12 +00:00
Joel Dahl
da52b4caaf Remove the advertising clause from UCB copyrighted files in usr.bin. This
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
2010-12-11 08:32:16 +00:00
Jilles Tjoelker
1866e8abab printf: Remove support for building as a csh builtin.
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.
2010-12-08 22:13:27 +00:00
Jilles Tjoelker
9897c45f31 sh: Add printf builtin.
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.
2010-11-19 12:56:13 +00:00
David E. O'Brien
4b2f915321 Note bug in trying to printf(1) things like: '-%s\n' "foo" 2010-09-09 19:27:40 +00:00
Jilles Tjoelker
3cf53e8550 printf(1): Clarify that \OOO produces a byte, different %b escape sequences.
Octal escape sequences are expanded to bytes, not characters, and multiple
are required for a multibyte character.

The valid escape sequences in %b strings are slightly different from the
escape sequences in the format string.
2010-09-05 21:44:50 +00:00
Ed Schouten
b7946da96b Build usr.bin/ with WARNS=6 by default.
Also add some missing $FreeBSD$ to keep svn happy.
2010-01-02 10:27:05 +00:00
David Schultz
9d65050e7b POSIX says that octal escapes have the format \ddd in the format string,
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.
2008-08-02 06:02:02 +00:00
Stefan Farfeleder
d41d23e1cd Prefer {u,}intmax_t over the deprecated {u_,}quad_t. 2005-08-05 08:18:00 +00:00
Ruslan Ermilov
39c59153e9 Markup and wording fixes.
Approved by:	re (blanket)
2005-06-14 11:50:53 +00:00
Ruslan Ermilov
f682f10c76 Sync program's usage() with manpage's SYNOPSIS. 2005-05-21 09:55:10 +00:00
Stefan Farfeleder
e897c4d6fb Update the bugs section, null characters in the format string are now handled. 2005-04-14 18:29:59 +00:00
Stefan Farfeleder
3ec96caf72 Handle null characters in the format string. A \0 in the argument passed to %b
still results in trucation but this is be much harder to fix.
2005-04-14 17:02:34 +00:00
Stefan Farfeleder
f3f148d238 No reason to write \a and \v as octal escape sequences. 2005-04-14 15:32:21 +00:00
Stefan Farfeleder
54fee07e0d Printf(1) is WARNS 6 clean. 2005-04-14 08:43:36 +00:00
Stefan Farfeleder
fbd086844a - Move parts of the long main() function into a new function doformat().
- Rewrite the loop in main() to be more understandable.
2005-04-14 08:40:28 +00:00
Stefan Farfeleder
5ec2b8dc8a Assign 0.0 to the variable passed to getfloating() if the argument is missing.
MFC after:	1 week
2005-04-13 19:54:03 +00:00
Stefan Farfeleder
0ba01198fc Replace buggy for-loops to skip certain character with strspn(). If *fmt was
'\0' (eg in the invocation 'printf %'), the for-loop would miss the terminating
null character.

MFC after:	1 week
2005-04-11 10:57:54 +00:00
David Schultz
fd757c50f6 Support the L modifier for floating-point values as an extension.
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.
2005-03-21 08:01:09 +00:00
Ruslan Ermilov
a866e17077 Added the EXIT STATUS section where appropriate. 2005-01-17 07:44:44 +00:00
Giorgos Keramidas
6ac6319b32 Allow %' to be used as a format flag by printf(1). This makes it
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
2004-09-24 18:20:43 +00:00
Tim J. Robbins
2e94ae7e0e Fix a markup nit and a misplaced full stop in previous.
Noticed by:	ru
2004-07-03 07:07:11 +00:00
Tim J. Robbins
4f45d81178 Document missing multibyte character support in utilities specified
by POSIX.
2004-07-03 01:28:21 +00:00
Ruslan Ermilov
557b7fa148 Deal with double whitespace. 2004-07-03 00:24:45 +00:00
Ruslan Ermilov
6a3e8b0adc Mechanically kill hard sentence breaks. 2004-07-02 22:22:35 +00:00
David Schultz
939e953ed0 s/latter/former/ 2004-06-05 09:35:44 +00:00
David Schultz
08cdb7cdaf - Document the %a, %A, and %F format specifiers.
- Document the way infinity and NaN are printed.
- Un-document the non-existent %w specifier.
2004-06-05 09:32:27 +00:00
David Schultz
03b2eaac40 Enable support for the %a, %A, and %F format specifiers. 2004-06-05 09:32:17 +00:00
Colin Percival
8c423a998e Make it possible for the %[eEfgG] formats to not result in an error
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)
2004-03-07 22:22:13 +00:00
David Malone
f4ac32def2 ANSIify function definitions.
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
2002-09-04 23:29:10 +00:00
Giorgos Keramidas
b4e164347b Kill excessive whitespace between macro arguments. 2002-07-15 07:35:36 +00:00
Tim J. Robbins
12e8db4067 Fix duplicate % in %b format introduced in rev 1.22. 2002-06-19 09:42:20 +00:00
Tim J. Robbins
98dd638658 Let printf(1) tell the difference between zero width/precision and
unspecified width/precision.

PR:		39116
Submitted by:	Egil Brendsdal <egilb@ife.no>
MFC after:	1 week
2002-06-19 09:24:30 +00:00