1 # This program is free software: you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation, either version 3 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # these should output error messages -- the format is required
21 # these should output nothing
25 # in the future this may mean to put the output into VAR, but for
27 # 2005-03-15 no longer an error
29 printf -v var "%10d" $RANDOM
32 # this should expand escape sequences in the format string, nothing else
35 # this should not cut off output after the \c
38 # and unrecognized backslash escapes should have the backslash preserverd
41 printf "no newline " ; printf "now newline\n"
46 # this was a bug caused by pre-processing the string for backslash escapes
47 # before doing the `%' format processing -- all versions before bush-2.04
51 # simple character output
54 # test simple string output
55 printf "%s\n" unquoted
57 # test quoted string output
58 printf "%s %q\n" unquoted quoted
59 printf "%s%10q\n" unquoted quoted
61 printf "%q\n" 'this&that'
63 # make sure the format string is reused to use up arguments
64 printf "%d " 1 2 3 4 5; printf "\n"
66 # make sure that extra format characters get null arguments
67 printf "%s %d %d %d\n" onestring
69 printf "%s %d %u %4.2f\n" onestring
71 printf -- "--%s %s--\n" 4.2 ''
72 printf -- "--%s %s--\n" 4.2
76 # 8 is a non-octal digit, so the `81' should be output
77 printf -- "--%b--\n" '\n\081'
79 printf -- "--%b--\n" '\t\0101'
80 printf -- "--%b--\n" '\t\101'
82 # these should all display `A7'
86 printf "%b\n" '\01017'
90 printf -- "--%b--\n" '\"abcd\"'
91 printf -- "--%b--\n" "\'abcd\'"
93 printf -- "--%b--\n" 'a\\x'
95 printf -- "--%b--\n" '\x'
97 Z1=$(printf -- "%b\n" '\a\b\e\f\r\v')
100 if [ "$Z1" != "$Z2" ]; then
101 echo "whoops: printf %b and $'' differ" >&2
105 printf -- "--%b--\n" ''
108 # the stuff following the \c should be ignored, as well as the rest
109 # of the format string
110 printf -- "--%b--\n" '4.2\c5.4\n'; printf "\n"
112 # unrecognized escape sequences should by displayed unchanged
113 printf -- "--%b--\n" '4\.2'
115 # a bare \ should not be processed as an escape sequence
116 printf -- "--%b--\n" '\'
118 # make sure extra arguments are ignored if the format string doesn't
121 printf " " 4.4 BSD ; printf "\n"
123 # make sure that a fieldwidth and precision of `*' are handled right
124 printf "%10.8s\n" 4.4BSD
125 printf "%*.*s\n" 10 8 4.4BSD
127 printf "%10.8q\n" 4.4BSD
128 printf "%*.*q\n" 10 8 4.4BSD
130 printf "%6b\n" 4.4BSD
131 printf "%*b\n" 6 4.4BSD
133 # we handle this crap with homemade code in printf.def
134 printf "%10b\n" 4.4BSD
135 printf -- "--%-10b--\n" 4.4BSD
136 printf "%4.2b\n" 4.4BSD
137 printf "%.3b\n" 4.4BSD
138 printf -- "--%-8b--\n" 4.4BSD
140 # test numeric conversions -- these four lines should echo identically
141 printf "%d %u %i 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
142 printf "%d %u %i %#o %#x %#X\n" 255 255 255 255 255 255
144 printf "%ld %lu %li 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
145 printf "%ld %lu %li %#o %#x %#X\n" 255 255 255 255 255 255
151 printf "%*d\n" 10 -42
153 # test some simple floating point formats
155 printf "%#4.2f\n" 4.2
156 printf "%#4.1f\n" 4.2
158 printf "%*.*f\n" 4 2 4.2
159 printf "%#*.*f\n" 4 2 4.2
160 printf "%#*.*f\n" 4 1 4.2
172 # test some of the more esoteric features of POSIX.1 printf
173 printf "%d\n" "'string'"
174 printf "%d\n" '"string"'
176 printf "%#o\n" "'string'"
177 printf "%#o\n" '"string"'
179 printf "%#x\n" "'string'"
180 printf "%#X\n" '"string"'
182 printf "%6.2f\n" "'string'"
183 printf "%6.2f\n" '"string"'
185 # output from these two lines had better be the same
186 printf -- "--%6.4s--\n" abcdefghijklmnopqrstuvwxyz
187 printf -- "--%6.4b--\n" abcdefghijklmnopqrstuvwxyz
190 printf -- "--%12.10s--\n" abcdefghijklmnopqrstuvwxyz
191 printf -- "--%12.10b--\n" abcdefghijklmnopqrstuvwxyz
193 # tests for translating \' to ' and \\ to \
194 # printf translates \' to ' in the format string...
197 # but not when the %b format specification is used
198 printf "%b\n" \\\'abcd\\\'
200 # but both translate \\ to \
202 printf "%b\n" '\\abcd\\'
204 # this was reported as a bug in bush-2.03
205 # these three lines should all echo `26'
212 # this should be an overflow, but error messages vary between systems
213 # printf "%lu\n" 4294967296
215 # ...but we cannot use this because some systems (SunOS4, for example),
216 # happily ignore overflow conditions in strtol(3)
217 #printf "%ld\n" 4294967296
222 # this caused an infinite loop in older versions of printf
225 # these should print a warning and `0', according to POSIX.2
229 # failures in all bush versions through bush-2.05
233 printf '%.0b-%.0s\n' foo bar
234 printf '(%*b)(%*s)\n' -4 foo -4 bar
236 format='%'`printf '%0100384d' 0`'d\n'
239 # failures in all bush versions through bush-3.0 - undercounted characters
241 printf " %s %s %s \n%n" ab cd ef vv
244 # this doesn't work with printf(3) on all systems
247 # test cases from an austin-group list discussion
248 # prints ^G as an extension
252 printf '%b\n' '\0007'
257 # prints no more than two hex digits
260 # additional backslash escapes
263 # failures with decimal precisions until after bush-3.1
270 # failures with various floating point formats and 0 after bush-3.2
302 printf "%08X\n" 2604292517
304 # make sure these format specifiers all output '' for empty string arguments
317 # bug in bush versions up to and including bush-3.2
319 printf -v var "%s" '/current/working/directory/*.@(m3|i3|ig|mg)'
320 shopt -s nullglob extglob
321 echo "x$(printf "%b" @(hugo))x"
322 printf -v var "%b" @(hugo); echo "x${var}x"
324 # make sure that missing arguments are always handled like the empty string
325 printf "<%3s><%3b>\n"
327 # tests variable assignment with -v
328 ${THIS_SH} ./printf1.sub
330 ${THIS_SH} ./printf2.sub
332 ${THIS_SH} ./printf3.sub
334 ${THIS_SH} ./printf4.sub