1#! /bin/sh
2#
3# @(#)printf.sh	1.11 18/09/20 Copyright 2016-2018 J. Schilling
4#
5
6# Read printf core functions
7. ../../common/test-common
8
9#
10# Basic printfs to check the printf builtin
11#
12docommand printf01 "$SHELL -c 'printf \"%s\\\n\" \"abc\"'" 0 "abc\n" ""
13docommand printf02 "$SHELL -c 'printf \"%s\" \"abc\"'" 0 "abc" ""
14docommand printf03 "$SHELL -c 'printf \"%s\\\n\" \"abc\" 123'" 0 "abc\n123\n" ""
15docommand printf04 "$SHELL -c 'printf \"%.2s\" \"abc\"'" 0 "ab" ""
16
17docommand printf10 "$SHELL -c 'printf \"%d\\\n\" 123'" 0 "123\n" ""
18docommand printf11 "$SHELL -c 'printf \"%10d\\\n\" 123'" 0 "       123\n" ""
19docommand printf12 "$SHELL -c 'printf \"%-10d\\\n\" 123'" 0 "123       \n" ""
20docommand printf13 "$SHELL -c 'printf \"%d\\\n\" 0xff'" 0 "255\n" ""
21docommand printf14 "$SHELL -c 'printf \"%i\\\n\" 0xff'" 0 "255\n" ""
22docommand printf15 "$SHELL -c 'printf \"%u\\\n\" 0xff'" 0 "255\n" ""
23docommand printf16 "$SHELL -c 'printf \"%x\\\n\" 0xff'" 0 "ff\n" ""
24docommand printf17 "$SHELL -c 'printf \"%X\\\n\" 0xff'" 0 "FF\n" ""
25docommand printf18 "$SHELL -c 'printf \"%o\\\n\" 0xff'" 0 "377\n" ""
26
27docommand printf20 "$SHELL -c 'printf \"%*d\\\n\" 10 123'" 0 "       123\n" ""
28docommand printf21 "$SHELL -c 'printf \"%*d\\\n\" -10 123'" 0 "123       \n" ""
29
30#
31# %b cannot be based on printf() as it needs to support nul bytes.
32# So we need to check the fieldwidth and significance features again
33#
34docommand printf22 "$SHELL -c 'printf \"%10b\\\n\" 123'" 0 "       123\n" ""
35docommand printf23 "$SHELL -c 'printf \"%-10b\\\n\" 123'" 0 "123       \n" ""
36docommand printf24 "$SHELL -c 'printf \"%*b\\\n\" 10 123'" 0 "       123\n" ""
37docommand printf25 "$SHELL -c 'printf \"%*b\\\n\" -10 123'" 0 "123       \n" ""
38docommand printf26 "$SHELL -c 'printf \"%.3b\\\n\" 1234567890'" 0 "123\n" ""
39docommand printf27 "$SHELL -c 'printf \"%.*b\\\n\" 3 1234567890'" 0 "123\n" ""
40
41#
42# Check whether printf '\0123' behaves like the C-syntax and stops after
43# the 3rd octal number even in case that the first number is a '0'
44#
45docommand printf30 "$SHELL -c 'printf \"\\1234\\\n\"'" 0 "S4\n" ""
46docommand printf31 "$SHELL -c 'printf \"\\0123\\\n\"'" 0 "\n3\n" ""
47
48docommand printf40 "$SHELL -c 'printf \"%d\\\n\" -1'" 0 "-1\n" ""
49docommand printf41 "$SHELL -c 'printf \"%u\\\n\" -1'" 0 "18446744073709551615\n" ""
50docommand printf42 "$SHELL -c 'printf \"%o\\\n\" -1'" 0 "1777777777777777777777\n" ""
51docommand printf43 "$SHELL -c 'printf \"%x\\\n\" -1'" 0 "ffffffffffffffff\n" ""
52
53
54cat > x <<"XEOF"
55printf '%b' 'abc'
56XEOF
57docommand printf110 "$SHELL ./x" 0 "abc" ""
58
59cat > x <<"XEOF"
60printf '%b' 'abc\cdef'
61XEOF
62docommand printf111 "$SHELL ./x" 0 "abc" ""
63
64cat > x <<"XEOF"
65printf '%b123' 'abc\cdef'
66XEOF
67docommand printf112 "$SHELL ./x" 0 "abc" ""
68
69cat > x <<"XEOF"
70printf '%b' 'abc\01'
71XEOF
72docommand printf113 "$SHELL ./x" 0 "abc\001" ""
73
74cat > x <<"XEOF"
75printf '%b' 'abc\0'
76XEOF
77docommand printf114 "$SHELL ./x" 0 "abc\000" ""
78
79cat > x <<"XEOF"
80printf '%b' 'abc\0def'
81XEOF
82docommand printf115 "$SHELL ./x" 0 "abc\000def" ""
83
84cat > x <<"XEOF"
85printf 'abc\0def'
86XEOF
87docommand printf116 "$SHELL ./x" 0 "abc\000def" ""
88
89#
90# Tests from Sven Maschek
91#
92docommand -bo printf200 "$SHELL -c 'printf \"\\\a\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "007\n" ""
93docommand -bo printf201 "$SHELL -c 'printf \"\\\b\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "010\n" ""
94docommand -bo printf202 "$SHELL -c 'printf \"\\\t\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "011\n" ""
95docommand -bo printf203 "$SHELL -c 'printf \"\\\n\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "012\n" ""
96docommand -bo printf204 "$SHELL -c 'printf \"\\\v\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "013\n" ""
97docommand -bo printf205 "$SHELL -c 'printf \"\\\f\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "014\n" ""
98docommand -bo printf206 "$SHELL -c 'printf \"\\\r\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "015\n" ""
99
100docommand -bo printf207 "$SHELL -c 'printf \".\\\c.\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "056 134 143 056\n" ""
101docommand -bo printf208 "$SHELL -c 'printf \"%b\" \".\\\c.\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "056\n" ""
102
103docommand -bo printf209 "$SHELL -c 'printf \"\\\d\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 144\n" ""
104docommand -bo printf210 "$SHELL -c 'printf \"\\\e\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 145\n" ""
105docommand -bo printf211 "$SHELL -c 'printf \"\\\g\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 147\n" ""
106docommand -bo printf212 "$SHELL -c 'printf \"\\\h\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 150\n" ""
107docommand -bo printf213 "$SHELL -c 'printf \"\\\i\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 151\n" ""
108docommand -bo printf214 "$SHELL -c 'printf \"\\\j\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 152\n" ""
109docommand -bo printf215 "$SHELL -c 'printf \"\\\k\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 153\n" ""
110docommand -bo printf216 "$SHELL -c 'printf \"\\\l\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 154\n" ""
111docommand -bo printf217 "$SHELL -c 'printf \"\\\m\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 155\n" ""
112docommand -bo printf218 "$SHELL -c 'printf \"\\\o\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 157\n" ""
113docommand -bo printf219 "$SHELL -c 'printf \"\\\p\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 160\n" ""
114docommand -bo printf220 "$SHELL -c 'printf \"\\\q\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 161\n" ""
115docommand -bo printf221 "$SHELL -c 'printf \"\\\s\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 163\n" ""
116docommand -bo printf222 "$SHELL -c 'printf \"\\\u\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 165\n" ""
117docommand -bo printf223 "$SHELL -c 'printf \"\\\w\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 167\n" ""
118docommand -bo printf224 "$SHELL -c 'printf \"\\\x\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 170\n" ""
119docommand -bo printf225 "$SHELL -c 'printf \"\\\y\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 171\n" ""
120docommand -bo printf226 "$SHELL -c 'printf \"\\\z\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 172\n" ""
121
122
123docommand -bo printf227 "$SHELL -c 'printf \"%b\" \"\\\33\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "134 063 063\n" ""
124docommand -bo printf228 "$SHELL -c 'printf \"%b\" \"\\\033\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "033\n" ""
125docommand -bo printf229 "$SHELL -c 'printf \"%b\" \"\\\0033\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "033\n" ""
126
127docommand -bo printf230 "$SHELL -c 'printf \"\\\33\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "033\n" ""
128docommand -bo printf231 "$SHELL -c 'printf \"\\\033\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "033\n" ""
129docommand -bo printf232 "$SHELL -c 'printf \"\\\0033\" | od -b -A n|sed -e 2d -e \"s/^[ 	]*//\"'" 0 "003 063\n" ""
130
131docommand printf233 "$SHELL -c 'printf \"%d\\\n\" \"\\\"a\"'" 0 "97\n" ""
132
133
134#
135# Tests for floating point enhancements.
136#
137expect_fail_save=$expect_fail
138expect_fail=true
139docommand -silent -esilent printf400 "$SHELL -c 'LC_ALL=C;export LC_ALL; printf \"%f\\\n\" 1.234567'" 0 "1.234567\n" ""
140expect_fail=$expect_fail_save
141if [ "$failed" = true ]; then
142	echo
143	echo "Test $cmd_label is a Solaris printf enhancement."
144	echo "Skipping printf400..printf406."
145	echo
146else
147docommand printf400 "$SHELL -c 'LC_ALL=C;export LC_ALL; printf \"%f\\\n\" 1.234567'" 0 "1.234567\n" ""
148docommand printf401 "$SHELL -c 'LC_ALL=C;export LC_ALL; printf \"%F\\\n\" 1.234567'" 0 "1.234567\n" ""
149docommand printf402 "$SHELL -c 'LC_ALL=C;export LC_ALL; printf \"%e\\\n\" 1.234567'" 0 "1.234567e+00\n" ""
150docommand printf403 "$SHELL -c 'LC_ALL=C;export LC_ALL; printf \"%E\\\n\" 1.234567'" 0 "1.234567E+00\n" ""
151docommand printf404 "$SHELL -c 'LC_ALL=C;export LC_ALL; printf \"%g\\\n\" 1.234567'" 0 "1.23457\n" ""
152docommand printf405 "$SHELL -c 'LC_ALL=C;export LC_ALL; printf \"%G\\\n\" 1.234567'" 0 "1.23457\n" ""
153
154docommand printf406 "$SHELL -c 'LC_ALL=C;export LC_ALL; printf \"%*.*f\\\n\" 6 3 1.234567'" 0 " 1.235\n" ""
155fi
156
157#
158# Tests for the Solaris enhancements %n$
159# First test %s only as the closed source Solaris /usr/bin/printf does
160# not support %n$i.
161#
162expect_fail_save=$expect_fail
163expect_fail=true
164docommand -silent -esilent printf500 "$SHELL -c 'printf \"%2\\\$s %1\\\$s\\\n\" 1 2'" 0 "2 1\n" ""
165expect_fail=$expect_fail_save
166if [ "$failed" = true ]; then
167	echo
168	echo "Test $cmd_label is a Solaris printf enhancement."
169	echo "Skipping printf500..printf501."
170	echo
171else
172docommand printf500 "$SHELL -c 'printf \"%2\\\$s %1\\\$s\\\n\" 1 2'" 0 "2 1\n" ""
173
174cat > x <<"XEOF"
175printf '%3$*2$.*1$s\n' 3 10 abcdefghijk
176XEOF
177docommand printf501 "$SHELL ./x" 0 "       abc\n" ""
178fi
179
180expect_fail_save=$expect_fail
181expect_fail=true
182docommand -silent -esilent printf600 "$SHELL -c 'printf \"%2\\\$i %1\\\$i\\\n\" 1 2'" 0 "2 1\n" ""
183expect_fail=$expect_fail_save
184if [ "$failed" = true ]; then
185	echo
186	echo "Test $cmd_label is a Schily/ksh93 printf enhancement."
187	echo "Skipping printf600..printf604."
188	echo
189else
190docommand printf600 "$SHELL -c 'printf \"%2\\\$i %1\\\$i\\\n\" 1 2'" 0 "2 1\n" ""
191
192cat > x <<"XEOF"
193printf '%3$*2$.*1$i\n' 3 10 1234567
194XEOF
195docommand printf601 "$SHELL ./x" 0 "   1234567\n" ""
196
197cat > x <<"XEOF"
198printf '%3$*2$.*1$i\n' 3 10 1
199XEOF
200docommand printf602 "$SHELL ./x" 0 "       001\n" ""
201
202cat > x <<"XEOF"
203printf '%3$*2$.*1$i\n' 3 10 123
204XEOF
205docommand printf603 "$SHELL ./x" 0 "       123\n" ""
206
207cat > x <<"XEOF"
208printf '%3$*2$.*1$i\n' 3 10 123 2 15 456
209XEOF
210docommand printf604 "$SHELL ./x" 0 "       123\n            456\n" ""
211fi
212
213#exit
214
215remove x
216success
217