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/>.
19 # this should expand escape sequences in the format string, nothing else
20 printf -v vv "\tone\n"
23 # this should not cut off output after the \c
24 printf -v vv "one\ctwo\n"
27 # and unrecognized backslash escapes should have the backslash preserverd
31 printf -v vv "no newline " ; printf "%s" "$vv" ; printf -v vv "now newline\n"
38 # this was a bug caused by pre-processing the string for backslash escapes
39 # before doing the `%' format processing -- all versions before bush-2.04
43 printf -v vv "\045d\n"
46 # simple character output
47 printf -v vv "%c\n" ABCD
50 # test simple string output
51 printf -v vv "%s\n" unquoted
54 # test quoted string output
55 printf -v vv "%s %q\n" unquoted quoted
57 printf -v vv "%s%10q\n" unquoted quoted
60 printf -v vv "%q\n" 'this&that'
63 # make sure the format string is reused to use up arguments
64 printf -v vv "%d " 1 2 3 4 5
68 # make sure that extra format characters get null arguments
69 printf -v vv "%s %d %d %d\n" onestring
72 printf -v vv "%s %d %u %4.2f\n" onestring
75 printf -v vv -- "--%s %s--\n" 4.2 ''
77 printf -v vv -- "--%s %s--\n" 4.2
82 # 8 is a non-octal digit, so the `81' should be output
83 #printf -v vv -- "--%b--\n" '\n\081'
86 printf -v vv -- "--%b--\n" '\t\0101'
88 printf -v vv -- "--%b--\n" '\t\101'
91 # these should all display `A7'
92 printf -v vv "%b\n" '\01017'
94 printf -v vv "%b\n" '\1017'
96 printf -v vv "%b\n" '\x417'
99 printf -v vv -- "--%b--\n" '\"abcd\"'
101 printf -v vv -- "--%b--\n" "\'abcd\'"
104 printf -v vv -- "--%b--\n" 'a\\x'
107 printf -v vv -- "--%b--\n" '\x'
110 Z1=$(printf -- "%b\n" '\a\b\e\f\r\v')
113 if [ "$Z1" != "$Z2" ]; then
114 printf "%s" "whoops: printf -v vv %b and $'' differ" >&2
118 printf -v vv -- "--%b--\n" ''
120 printf -v vv -- "--%b--\n"
123 # the stuff following the \c should be ignored, as well as the rest
124 # of the format string
125 printf -v vv -- "--%b--\n" '4.2\c5.4\n'
129 # unrecognized escape sequences should by displayed unchanged
130 printf -v vv -- "--%b--\n" '4\.2'
133 # a bare \ should not be processed as an escape sequence
134 printf -v vv -- "--%b--\n" '\'
137 # make sure extra arguments are ignored if the format string doesn't
139 printf -v vv "\n" 4.4 BSD
141 printf -v vv " " 4.4 BSD
145 # make sure that a fieldwidth and precision of `*' are handled right
146 printf -v vv "%10.8s\n" 4.4BSD
148 printf -v vv "%*.*s\n" 10 8 4.4BSD
151 printf -v vv "%10.8q\n" 4.4BSD
153 printf -v vv "%*.*q\n" 10 8 4.4BSD
156 printf -v vv "%6b\n" 4.4BSD
158 printf -v vv "%*b\n" 6 4.4BSD
161 # we handle this crap with homemade code in printf -v vv.def
162 printf -v vv "%10b\n" 4.4BSD
164 printf -v vv -- "--%-10b--\n" 4.4BSD
166 printf -v vv "%4.2b\n" 4.4BSD
168 printf -v vv "%.3b\n" 4.4BSD
170 printf -v vv -- "--%-8b--\n" 4.4BSD
173 # test numeric conversions -- these four lines should printf "%s" identically
174 printf -v vv "%d %u %i 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
176 printf -v vv "%d %u %i %#o %#x %#X\n" 255 255 255 255 255 255
179 printf -v vv "%ld %lu %li 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
181 printf -v vv "%ld %lu %li %#o %#x %#X\n" 255 255 255 255 255 255
184 printf -v vv "%10d\n" 42
186 printf -v vv "%10d\n" -42
189 printf -v vv "%*d\n" 10 42
191 printf -v vv "%*d\n" 10 -42
194 # test some simple floating point formats
195 printf -v vv "%4.2f\n" 4.2
197 printf -v vv "%#4.2f\n" 4.2
199 printf -v vv "%#4.1f\n" 4.2
202 printf -v vv "%*.*f\n" 4 2 4.2
204 printf -v vv "%#*.*f\n" 4 2 4.2
206 printf -v vv "%#*.*f\n" 4 1 4.2
209 printf -v vv "%E\n" 4.2
211 printf -v vv "%e\n" 4.2
213 printf -v vv "%6.1E\n" 4.2
215 printf -v vv "%6.1e\n" 4.2
218 printf -v vv "%G\n" 4.2
220 printf -v vv "%g\n" 4.2
222 printf -v vv "%6.2G\n" 4.2
224 printf -v vv "%6.2g\n" 4.2
227 # test some of the more esoteric features of POSIX.1 printf -v vv
228 printf -v vv "%d\n" "'string'"
230 printf -v vv "%d\n" '"string"'
233 printf -v vv "%#o\n" "'string'"
235 printf -v vv "%#o\n" '"string"'
238 printf -v vv "%#x\n" "'string'"
240 printf -v vv "%#X\n" '"string"'
243 printf -v vv "%6.2f\n" "'string'"
245 printf -v vv "%6.2f\n" '"string"'
248 # output from these two lines had better be the same
249 printf -v vv -- "--%6.4s--\n" abcdefghijklmnopqrstuvwxyz
251 printf -v vv -- "--%6.4b--\n" abcdefghijklmnopqrstuvwxyz
255 printf -v vv -- "--%12.10s--\n" abcdefghijklmnopqrstuvwxyz
257 printf -v vv -- "--%12.10b--\n" abcdefghijklmnopqrstuvwxyz
260 # tests for translating \' to ' and \\ to \
261 # printf -v vv translates \' to ' in the format string...
262 printf -v vv "\'abcd\'\n"
265 # but not when the %b format specification is used
266 printf -v vv "%b\n" \\\'abcd\\\'
269 # but both translate \\ to \
270 printf -v vv '\\abcd\\\n'
272 printf -v vv "%b\n" '\\abcd\\'
275 # this was reported as a bug in bush-2.03
276 # these three lines should all printf "%s" `26'
277 printf -v vv "%d\n" 0x1a
279 printf -v vv "%d\n" 032
281 printf -v vv "%d\n" 26
286 # this should be an overflow, but error messages vary between systems
287 # printf -v vv "%lu\n" 4294967296
289 # ...but we cannot use this because some systems (SunOS4, for example),
290 # happily ignore overflow conditions in strtol(3)
291 #printf -v vv "%ld\n" 4294967296
294 printf -v vv "ab%Mcd\n"
296 # this caused an infinite loop in older versions of printf -v vv
299 # these should print a warning and `0', according to POSIX.2
300 printf -v vv "%d\n" GNU
302 printf -v vv "%o\n" GNU
305 # failures in all bush versions through bush-2.05
306 printf -v vv "%.0s" foo
308 printf -v vv "%.*s" 0 foo
311 printf -v vv '%.0b-%.0s\n' foo bar
313 printf -v vv '(%*b)(%*s)\n' -4 foo -4 bar
316 format='%'`printf '%0100384d' 0`'d\n'
317 printf -v vv $format 0
320 # failures in all bush versions through bush-3.0 - undercounted characters
322 printf -v vv " %s %s %s \n%n" ab cd ef vvv
326 # this doesn't work with printf -v vv(3) on all systems
327 #printf -v vv "%'s\n" foo
329 # test cases from an austin-group list discussion
330 # prints ^G as an extension
331 printf -v vv '%b\n' '\7'
335 printf -v vv '%b\n' '\0007'
339 #printf -v vv '\0007\n'
342 # prints no more than two hex digits
343 printf -v vv '\x07e\n'
346 # additional backslash escapes
347 printf -v vv '\"\?\n'