Commit Graph

90 Commits

Author SHA1 Message Date
andvar 2773222a6c s/chracter/character/ in documentation. 2023-02-13 23:02:27 +00:00
christos 1fd82bd9ab fix typo 2021-05-20 02:01:07 +00:00
kre 108061b040 Changes for POSIX conformance.
1.  exit(1) with an error message on stderr if an I/O error occurs.
1a. To work properly when built into /bin/sh sprinkle clearerr() at
    appropriate places.

2.  Verify that when a 'X data value is used with one of the numeric
    conversions, that nothing follows the 'X'.   It used to be unclear
    in the standard whether this was required or not, it is clear that
    with numeric conversions the entire data value must be used, or an
    error must result.   But with string conversions, that isn't the case
    and unused parts are simply ignored.   This one is a numeric conversion
    with a string value, so which applies?   The standard used to contain
    an example of '+3 being converted, producing the same as '+ ignoring
    the '3' with no mention of any error, so that's the approach we adopted,
    The forthcoming version now explicitly states that an error would also
    be generated from that case, as the '3' was not used by the numeric
    conversion.

2a. We support those conversions with floating as well as integer conversions,
    as the standard used to suggest that was required (but it makes no sense,
    the values are always integers, printing them in a floating format is
    dumb).  The standard has been revised to make it clear that only the
    integer numeric conversions %d %u %x (etc) are supposed to handle the 'X
    form of data value.   We still allow it with the floating formats as an
    extension, for backward compat, just in case someone (other than the ATF
    tests) is using it.   It might go away.

2b. These formats are sypposed to convert 'X where 'X' is a character
    (perhaps multibyte encoded) in the current LC_CTYPE locale category.
    We don't handle that, only 1 byte characters are handled currently.
    However the framework is now there to allow code to (one hopes, easily)
    be added to handle multi-byte locales.   (Note that for the purposes of
    #2 above, 'X' must be a single character, not a single byte.)
2021-05-19 22:41:19 +00:00
kre 6db93d573b Don't describe "%%: in the format as a format conversion, it isn't,
it is an escaped literal '%' character.   None of what applies to
format conversions applies to this.
2021-05-19 22:29:18 +00:00
christos 4a8f7c3df0 make value an int to avoid all the casts and conversion warnings. 2021-04-16 18:31:28 +00:00
christos f1d025f4e1 Change octal and hex parsing to not use strtoul so that they don't handle
'-'. From Martijn van Duren.
Also add a warning if the conversion fails (like the gnu printf does)
2021-04-16 15:10:18 +00:00
uwe e78db94ba3 Add quotes around command substitution in the example
so that it works regardless of IFS and buts.
Requested by kre@
2020-06-29 22:50:11 +00:00
uwe 7a8bed0a49 Drop redundant quoting in the nested printf example. 2020-06-26 22:05:05 +00:00
uwe 0e9ddc2d8b Use ASCII single quote. 2020-06-26 21:03:55 +00:00
uwe 0f2a822331 Try to improve markup to get a nicer PostScript output.
While here edit slightly for consistency: get the octal character
ranges in order, use \num to refer to octal everywhere, etc.
2020-06-26 20:16:55 +00:00
kre 8ad10c91e9 Amend the previous change: we can have (almost) the best of both
worlds, as when the first arg (which should be the format) contains
no % conversions, and there are more args, the results are unspecified
(according to POSIX).

We can use this so the previous usage
	printf -- format arg...
(which is stupid, and pointless, but used to work) continues to
simply ignore the -- (unspecified results mean we can do whatever
feels good...)

This brings back the #if 0'd block from the previous modification
(so there is no longer anything that needs cleaning up later) but runs
the getopt() loop it contained only when there are at least 2 args
(so any 1 arg printf always uses that arg as the format string,
whatever it contains, including just "--") and also only when the
first (format) arg contains no '%' characters (which guarantees no %
conversions without needing to actually parse the arg).  This is the
(or a) "unspecified results" case from POSIX, so we are free to do
anything we like - including assuming that we might have options
(we don't) and pretending to process them.
2019-07-22 17:34:31 +00:00
kre b6a771f35e Stop assuming that printf handles options in any way at all
(it doesn't - that is, shouldn't) which includes processing -- as an
"end of options".  The first arg is (always) the format string.

Remove call to getopt() (but still do associated changes to argc/argv)

Note: for now this is #if 0's out instead of being deleted, the old
code should be fully removed sometime soon.

Problem pointed out on tech-userlevel by Thierry Laronde.
2019-07-21 15:25:39 +00:00
kre d5635413aa Revert previous, it was based upon a misreading of the POSIX
spec.   POSIX requires "as if by calling strtod()" which we
did already ... by calling strtod().   Go back to doing that.
2019-01-27 12:03:09 +00:00
kre 4ca169f245 Always convert input numbers (from the command line) in the C
locale, not as set in the environment.   Conforms with POSIX spec.
2019-01-26 15:22:54 +00:00
kre f910883c96 A truly ancient bug found by Edgar Fuss
When printf is running builtin in a sh, global vars aren't reset to
0 between invocations.   This affects "rval" which remembers state
from a previous %b \c and thereafter always exits after the first
format conversion, until we get a conversion that generates an
error (which resets the flag almost by accident)

	printf %b abc\\c
	abc				(no \n)
	printf %s%s hello world
	hello				(no \n, of course, no world ...)
	printf %s%s hello world
	hello
	printf %s%s hello world
	hello
	printf %d hello
	printf: hello: expected numeric value
	0				(no \n)
	printf %s%s hello world
	helloworld			(no \n, and we are back!)

This affects both /bin/sh and /bin/csh (and has for a very long time).

XXX pullup -8
2018-09-10 14:42:29 +00:00
kre 8b37a6dbec Printf's that support \e for escape all seem to also support \E.
Except us.   Now we do as well.
2018-09-04 01:13:50 +00:00
kre 68b40b6c11 Tighten syntax a little (no more %*4.*2d nonsense).
Include the format collected so far in "missing format char" err message.
Minor KNF and whitespace.
2018-09-03 04:10:20 +00:00
kre 24f342dfd6 PR standards/53563
POSIX requires that signed numbers (strings preceded by '+' or '-')
be allowed as inputs to all of the integer format conversions, including
those which treat the data as unsigned.

Hence we do not need a variant function whose only difference from its
companion is to reject strings starting with '-' - instead we use
the primary function (getintmax()) for everything and remove getuintmax().

Minor update to the man page to indicate that the arg to all of the
integer conversions (diouxX) must be an integer constant (with an
optional sign) and to make it blatantly clear that %o is octal and
%u is unsigned decimal (for some reason those weren't explicitly stated
unlike d i x and X).  Delete "respectively", it is not needed (and does
not really apply).

XXX pullup -8
2018-08-31 17:27:35 +00:00
kre 47ea218fd1 NFC: More KNF (remove () around returned constants). 2018-07-25 15:35:27 +00:00
kre 5755a4ea22 NFC: whitespace & KNF. 2018-07-25 14:41:52 +00:00
kre 2fb3e9fabf Add the new formats to the list of format cracters (oops...) 2018-07-24 20:58:39 +00:00
kre ddc0976a72 Add support for F a and A formats (which go with the eEfgG formats
already supported.)
2018-07-24 20:49:19 +00:00
kre 520d24e3eb Correct a typo (off by one (key)) ...
There is no 'w' format, but there is an 'e'
2018-07-24 19:49:33 +00:00
kre 4bcba09122 Avoid printing error messages twice when an invalid
escape sequence (\ sequence) is present in an arg to a %b
conversion.
2018-07-03 01:56:39 +00:00
kre a78ff1f90a From leot@ on tech-userlevel:
Avoid running off into oblivion when a format string,
or arg to a %b conversion ends in an unescaped backslash.

Patch from Leo slightly modified by me.
2018-07-03 01:54:42 +00:00
wiz 2458e695f0 Remove Tn. 2018-03-12 09:29:43 +00:00
dholland ab87b60207 Explicitly mention use of -- for format strings beginning with -,
per PR 21970.
2018-03-12 00:42:05 +00:00
christos ff53269704 fix some error handling. 2015-06-16 22:54:10 +00:00
snj fc99b6a4d6 Change some standard exit status text to ".Ex -std"
From Eitan Adler in PR bin/47790.
2014-04-13 01:45:34 +00:00
christos b294e9655c WARNS=6 2013-07-16 17:48:22 +00:00
christos 5353040f23 document non-literal format strings 2011-08-16 10:35:03 +00:00
christos ca5bce8901 support grouping format. 2011-03-15 23:11:49 +00:00
joerg 57e0101802 \\*(Pm -> \*(Pm 2010-04-05 21:24:14 +00:00
christos a49ff4deac Avoid segv on "printf '%*********s' 666", from Maksymilian Arciemowicz 2009-10-13 19:28:31 +00:00
lukem c1ceae17f0 Enable WARNS=4 by default for usr.bin, except for:
awk  bdes  checknr  compile_et  error  gss  hxtool  kgetcred  kinit
	klist  ldd  less  lex  locale  login  m4  man  menuc  mk_cmds
	mklocale  msgc  openssl  rpcgen  rpcinfo  sdiff  spell  ssh
	string2key  telnet  tn3270  verify_krb5_conf  xlint
2009-04-14 22:15:16 +00:00
joerg 31ea1ec643 Use semantic markup 2009-03-11 13:57:11 +00:00
dholland 0a268c3ebf Fix description of %b format. PR 39111 from Paul Goyette. 2008-09-01 09:20:41 +00:00
lukem 98e5374ccb Remove the \n and tabs from the __COPYRIGHT() strings.
Tweak to use a consistent format.
2008-07-21 14:19:20 +00:00
apb 76807ad91e Add a note about the stupid behaviour of "%c" format. 2008-05-06 12:01:35 +00:00
christos 56fd57d26f detect more errors from printf/malloc. 2008-03-28 18:05:39 +00:00
dsl a2a01de947 Remember to consume input bytes when processing '\0nnn' for %b formats 2005-03-22 23:55:46 +00:00
christos 175795754a WARNS=3, and remove comment about WFORMAT issues. 2004-10-30 19:28:35 +00:00
christos a7e7123b05 - KNF, WARNS=3, pass lint.
- Simplify octal parsing code.
2004-10-30 19:28:10 +00:00
agc 89aaa1bb64 Move UCB-licensed code from 4-clause to 3-clause licence.
Patches provided by Joel Baker in PR 22365, verified by myself.
2003-08-07 11:13:06 +00:00
dsl 630ca1a3e3 Revert previous. 'None' means that the "Utility Syntax Guidlines" apply. 2003-06-25 12:56:59 +00:00
dsl 0ac8c637fd Remove getopt() loop, IEEE 1003.1 doesn't say that printf(1) should conform
to the "Utility Syntax Guidlines".
Fixes PR 21970.
2003-06-25 09:54:15 +00:00
wiz abe0b8ad64 Use .Aq instead of \*[Lt]...\*[Gt]; sort SEE ALSO; add some more commas;
new sentence, new line; fix some mdoc nits.
2003-04-28 09:38:19 +00:00
augustss 86be450da2 Add missing comma. 2003-04-28 09:26:34 +00:00
dsl a5f7364038 Fix the output of NUL bytes within %b formats.
(Approved by Christos)
2003-02-24 14:42:27 +00:00
christos c02b3bbdf4 Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
  (so I could unset -e PS1 before running the test shell...)
2002-11-24 22:35:38 +00:00