summaryrefslogtreecommitdiff
path: root/static/unix-v10/man3/print.3
blob: b553e4f987cb79dec929bffc44a60fa2f6e3d67f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
.TH PRINT 3
.CT 2 file_io
.SH NAME
print, fprint, sprint, fmtinstall \(mi print formatted output
.SH SYNOPSIS
.nf
.BR "int print(format \fR[\fP , arg \fR]\fP ... )"
.B char \(**format;
.PP
.BR "int fprint(fildes, format \fR[\fP , arg \fR]\fP ... )"
.B int fildes;
.B char \(**format;
.PP
.BR "int sprint(s, format \fR[\fP , arg \fR]\fP ... )"
.B char \(**s, \(**format;
.PP
.B fmtinstall(c, fn)
.B char c;
.B int (*fn)();
.PP
.B strconv(s, f1, f2)
.B char *s;
.PP
.B extern int printcol;
.fi
.SH DESCRIPTION
.I Print
places output on the standard output.
.I Fprint
places output on the named output
file descriptor;
a buffered form
is described in
.IR fio (3).
.I Sprint
places output
followed by the null character
.RB ( \e0 )
in consecutive bytes starting at
.IR s ;
it is the user's responsibility to ensure that
enough storage is available.
Each function returns the number of characters
transmitted (not including the
.B \e0
in the case of
.IR sprint ),
or
a negative value if an output error was encountered.
.PP
Each of these functions
converts, formats, and prints its
trailing arguments
under control of a
.IR format 
string.
The
.I format
contains two types of objects:
plain characters, which are simply copied to the
output stream,
and conversion specifications,
each of which results in fetching of
zero or more
arguments.
The results are undefined if there are arguments of the
wrong type or too few
arguments for the format.
If the format is exhausted while
arguments remain, the excess
are ignored.
.PP
Each conversion specification has the following format
.PP
.B "% [flags] [[\(mi] digits [. digits]] verb
.PP
The
.I flags
modify the meaning of
the conversion verb.
The first (possibly negative) number is called
.IR f1 ,
the second number is
.IR f2 .
The flags and numbers are arguments to
the
.I verb
described below.
.PP
The numeric verbs
.BR d ,
.BR o ,
and
.B x
format their arguments in decimal,
octal and hex respectively.
Each interprets the flags
.BR h ,
.BR l ,
.BR u ,
to mean short,
long,
and unsigned.
If neither
short nor long is specified,
then the arg is an
.BR int .
If unsigned is specified,
then the arg is interpreted as a
positive number and no sign is output.
.I F1
is the minimum field width and,
if negative,
means left justified rather than right justified;
in both cases, padding is done with blanks.
The converted number is padded with
.B 0
on the left to at least
.I f2
characters.
.PP
The floating point verbs
.BR f ,
.BR e ,
and
.B g
take a double argument.
No flags apply to floating point conversions.
.I F1
is the minimum field width and,
if negative,
means left justified.
.I F2
is the number of digits that are converted after the decimal place.
The first unconverted digit has suffered decimal rounding.
The 
.B f
verb produces output of the form
.RB [ - ] digits [ .digits\fR].
.B e
conversion appends an exponent
.BR e [ - ] digits .
The
.B g
verb will output the arg in either
.B e
or
.B f
with the goal of producing the smallest output.
.PP
The
.B s
verb copies a string
(pointer to character)
to the output.
The number of characters copied
.RI ( n )
is the minimum
of the size of the string and
.IR f2 .
These
.I n
characters are justified within a field of
.I f1
characters as described above.
.PP
The
.B c
verb copies a single character (int)
justified within a field of
.I f1
characters as described above.
.PP
.I Fmtinstall
is used to install your own conversions and flags.
.I Fn
should be declared as
.EX
        int fn(o, f1, f2, f3)
        void *o;
        int f1, f2, f3;
.EE
.I Fn
is passed a pointer
.I o
to whatever argument appears in
the list to
.IR print .
.I Fn
should return the size of the argument in bytes so
.I print
can skip over it.
.I F1
and
.I f2
are the decoded numbers in the conversion.
A missing
.I f1
is denoted by the value zero.
A missing
.I f2
is denoted by a negative number.
.I F3
is the logical or of all the
flags seen in the conversion.
If
.I c
is a flag,
.I fn
should return a negative number
that is negated and then logically
.IR or ed
with any other flags
and ultimately
passed to a conversion routine.
All interpretation of
.IR f1 ,
.IR f2 ,
and
.I f3
is left up to the conversion routine.
The standard flags are
.LR h (2),
.LR l (1),
and
.LR u (4).
.PP
.I Sprint
is designed to be recursive in order to
help prepare output in custom conversion routines.
.PP
The output of any conversion routine must be passed through
.IR strconv .
.I S
is the character string,
.I f1
and
.I f2
have the same meaning as above.
.PP
.I Printcol
indicates the position of the next output character.
Tabs, backspaces and carriage returns are interpreted appropriately.
.SH EXAMPLES
This adds a verb to print complex numbers.
.EX
typedef struct {
	double r, i;
} complex;
complex x = { 1.5, -2.3 };
int Xconv();

main()
{

	fmtinstall('X', Xconv);
	print("x = %X\en", x);
}

Xconv(o, f1, f2, f3)
	complex *o;
{
	char str[50];

	sprint(str, "(%g,%g)", o->r, o->i);
	strconv(str, f1, f2);
	return(sizeof(complex));
}
.EE
.SH SEE ALSO
.IR fio (3), 
.IR printf (3)
.SH BUGS
There are internal buffers which may overflow silently.