sl@0
|
1 |
/* SPRINTF.C
|
sl@0
|
2 |
*
|
sl@0
|
3 |
* Portions Copyright (c) 1990-2006 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
4 |
* All rights reserved.
|
sl@0
|
5 |
*/
|
sl@0
|
6 |
|
sl@0
|
7 |
/*
|
sl@0
|
8 |
* Copyright (c) 1990 The Regents of the University of California.
|
sl@0
|
9 |
* All rights reserved.
|
sl@0
|
10 |
*
|
sl@0
|
11 |
* Redistribution and use in source and binary forms are permitted
|
sl@0
|
12 |
* provided that the above copyright notice and this paragraph are
|
sl@0
|
13 |
* duplicated in all such forms and that any documentation,
|
sl@0
|
14 |
* advertising materials, and other materials related to such
|
sl@0
|
15 |
* distribution and use acknowledge that the software was developed
|
sl@0
|
16 |
* by the University of California, Berkeley. The name of the
|
sl@0
|
17 |
* University may not be used to endorse or promote products derived
|
sl@0
|
18 |
* from this software without specific prior written permission.
|
sl@0
|
19 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
sl@0
|
20 |
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
sl@0
|
21 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
sl@0
|
22 |
*/
|
sl@0
|
23 |
|
sl@0
|
24 |
/*
|
sl@0
|
25 |
|
sl@0
|
26 |
FUNCTION
|
sl@0
|
27 |
<<printf>>, <<fprintf>>, <<sprintf>>---format output
|
sl@0
|
28 |
INDEX
|
sl@0
|
29 |
fprintf
|
sl@0
|
30 |
INDEX
|
sl@0
|
31 |
printf
|
sl@0
|
32 |
INDEX
|
sl@0
|
33 |
sprintf
|
sl@0
|
34 |
|
sl@0
|
35 |
ANSI_SYNOPSIS
|
sl@0
|
36 |
#include <stdio.h>
|
sl@0
|
37 |
|
sl@0
|
38 |
int printf(const char *<[format]> [, <[arg]>, ...]);
|
sl@0
|
39 |
int fprintf(FILE *<[fd]>, const char *<[format]> [, <[arg]>, ...]);
|
sl@0
|
40 |
int sprintf(char *<[str]>, const char *<[format]> [, <[arg]>, ...]);
|
sl@0
|
41 |
|
sl@0
|
42 |
TRAD_SYNOPSIS
|
sl@0
|
43 |
#include <stdio.h>
|
sl@0
|
44 |
|
sl@0
|
45 |
int printf(<[format]> [, <[arg]>, ...])
|
sl@0
|
46 |
char *<[format]>;
|
sl@0
|
47 |
|
sl@0
|
48 |
int fprintf(<[fd]>, <[format]> [, <[arg]>, ...]);
|
sl@0
|
49 |
FILE *<[fd]>;
|
sl@0
|
50 |
char *<[format]>;
|
sl@0
|
51 |
|
sl@0
|
52 |
int sprintf(<[str]>, <[format]> [, <[arg]>, ...]);
|
sl@0
|
53 |
char *<[str]>;
|
sl@0
|
54 |
char *<[format]>;
|
sl@0
|
55 |
|
sl@0
|
56 |
DESCRIPTION
|
sl@0
|
57 |
<<printf>> accepts a series of arguments, applies to each a
|
sl@0
|
58 |
format specifier from <<*<[format]>>>, and writes the
|
sl@0
|
59 |
formatted data to <<stdout>>, terminated with a null character.
|
sl@0
|
60 |
The behavior of <<printf>> is undefined if there are not enough
|
sl@0
|
61 |
arguments for the format.
|
sl@0
|
62 |
<<printf>> returns when it reaches the end of the format string.
|
sl@0
|
63 |
If there are more arguments than the format requires, excess
|
sl@0
|
64 |
arguments are ignored.
|
sl@0
|
65 |
|
sl@0
|
66 |
<<fprintf>> and <<sprintf>> are identical to <<printf>>, other than the
|
sl@0
|
67 |
destination of the formatted output: <<fprintf>> sends the
|
sl@0
|
68 |
output to a specified file <[fd]>, while <<sprintf>> stores the
|
sl@0
|
69 |
output in the specified char array <[str]>. For <<sprintf>>,
|
sl@0
|
70 |
the behavior is also undefined if the output <<*<[str]>>>
|
sl@0
|
71 |
overlaps with one of the arguments.
|
sl@0
|
72 |
<[format]> is a pointer to a charater string containing two types of
|
sl@0
|
73 |
objects: ordinary characters (other than <<%>>), which are
|
sl@0
|
74 |
copied unchanged to the output, and conversion
|
sl@0
|
75 |
specifications, each of which is introduced by <<%>>.
|
sl@0
|
76 |
(To include <<%>> in the output, use <<%%>> in the format string.)
|
sl@0
|
77 |
A conversion specification has the following form:
|
sl@0
|
78 |
|
sl@0
|
79 |
. %[<[flags]>][<[width]>][.<[prec]>][<[size]>][<[type]>]
|
sl@0
|
80 |
|
sl@0
|
81 |
The fields of the conversion specification have the following meanings:
|
sl@0
|
82 |
|
sl@0
|
83 |
O+
|
sl@0
|
84 |
o <[flags]>
|
sl@0
|
85 |
|
sl@0
|
86 |
an optional sequence of characters which control
|
sl@0
|
87 |
output justification, numeric signs, decimal points,
|
sl@0
|
88 |
trailing zeroes, and octal and hex prefixes.
|
sl@0
|
89 |
The flag characters are minus (<<->>), plus (<<+>>),
|
sl@0
|
90 |
space ( ), zero (<<0>>), and sharp (<<#>>). They can
|
sl@0
|
91 |
appear in any combination.
|
sl@0
|
92 |
|
sl@0
|
93 |
o+
|
sl@0
|
94 |
o -
|
sl@0
|
95 |
The result of the conversion is left justified, and the right is
|
sl@0
|
96 |
padded with blanks. If you do not use this flag, the result is right
|
sl@0
|
97 |
justified, and padded on the left.
|
sl@0
|
98 |
|
sl@0
|
99 |
o +
|
sl@0
|
100 |
The result of a signed conversion (as determined by <[type]>)
|
sl@0
|
101 |
will always begin with a plus or minus sign. (If you do not use
|
sl@0
|
102 |
this flag, positive values do not begin with a plus sign.)
|
sl@0
|
103 |
|
sl@0
|
104 |
o " " (space)
|
sl@0
|
105 |
If the first character of a signed conversion specification
|
sl@0
|
106 |
is not a sign, or if a signed conversion results in no
|
sl@0
|
107 |
characters, the result will begin with a space. If the
|
sl@0
|
108 |
space ( ) flag and the plus (<<+>>) flag both appear,
|
sl@0
|
109 |
the space flag is ignored.
|
sl@0
|
110 |
|
sl@0
|
111 |
o 0
|
sl@0
|
112 |
If the <[type]> character is <<d>>, <<i>>, <<o>>, <<u>>,
|
sl@0
|
113 |
<<x>>, <<X>>, <<e>>, <<E>>, <<f>>, <<g>>, or <<G>>: leading zeroes,
|
sl@0
|
114 |
are used to pad the field width (following any indication of sign or
|
sl@0
|
115 |
base); no spaces are used for padding. If the zero (<<0>>) and
|
sl@0
|
116 |
minus (<<->>) flags both appear, the zero (<<0>>) flag will
|
sl@0
|
117 |
be ignored. For <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, and <<X>>
|
sl@0
|
118 |
conversions, if a precision <[prec]> is specified, the zero (<<0>>)
|
sl@0
|
119 |
flag is ignored.
|
sl@0
|
120 |
|
sl@0
|
121 |
Note that <<0>> is interpreted as a flag, not as the beginning
|
sl@0
|
122 |
of a field width.
|
sl@0
|
123 |
|
sl@0
|
124 |
o #
|
sl@0
|
125 |
The result is to be converted to an alternative form, according
|
sl@0
|
126 |
to the next character:
|
sl@0
|
127 |
|
sl@0
|
128 |
o+
|
sl@0
|
129 |
o 0
|
sl@0
|
130 |
increases precision to force the first digit
|
sl@0
|
131 |
of the result to be a zero.
|
sl@0
|
132 |
|
sl@0
|
133 |
o x
|
sl@0
|
134 |
a non-zero result will have a <<0x>> prefix.
|
sl@0
|
135 |
|
sl@0
|
136 |
o X
|
sl@0
|
137 |
a non-zero result will have a <<0X>> prefix.
|
sl@0
|
138 |
|
sl@0
|
139 |
o e, E or f
|
sl@0
|
140 |
The result will always contain a decimal point
|
sl@0
|
141 |
even if no digits follow the point.
|
sl@0
|
142 |
(Normally, a decimal point appears only if a
|
sl@0
|
143 |
digit follows it.) Trailing zeroes are removed.
|
sl@0
|
144 |
|
sl@0
|
145 |
o g or G
|
sl@0
|
146 |
same as <<e>> or <<E>>, but trailing zeroes
|
sl@0
|
147 |
are not removed.
|
sl@0
|
148 |
|
sl@0
|
149 |
o all others
|
sl@0
|
150 |
undefined.
|
sl@0
|
151 |
|
sl@0
|
152 |
o-
|
sl@0
|
153 |
o-
|
sl@0
|
154 |
|
sl@0
|
155 |
o <[width]>
|
sl@0
|
156 |
|
sl@0
|
157 |
<[width]> is an optional minimum field width. You can either
|
sl@0
|
158 |
specify it directly as a decimal integer, or indirectly by
|
sl@0
|
159 |
using instead an asterisk (<<*>>), in which case an <<int>>
|
sl@0
|
160 |
argument is used as the field width. Negative field widths
|
sl@0
|
161 |
are not supported; if you attempt to specify a negative field
|
sl@0
|
162 |
width, it is interpreted as a minus (<<->>) flag followed by a
|
sl@0
|
163 |
positive field width.
|
sl@0
|
164 |
|
sl@0
|
165 |
o <[prec]>
|
sl@0
|
166 |
|
sl@0
|
167 |
an optional field; if present, it is introduced with `<<.>>'
|
sl@0
|
168 |
(a period). This field gives the maximum number of
|
sl@0
|
169 |
characters to print in a conversion; the minimum number of
|
sl@0
|
170 |
digits of an integer to print, for conversions with <[type]>
|
sl@0
|
171 |
<<d>>, <<i>>, <<o>>, <<u>>, <<x>>, and <<X>>; the maximum number of
|
sl@0
|
172 |
significant digits, for the <<g>> and <<G>> conversions;
|
sl@0
|
173 |
or the number of digits to print after the decimal
|
sl@0
|
174 |
point, for <<e>>, <<E>>, and <<f>> conversions. You can specify
|
sl@0
|
175 |
the precision either directly as a decimal integer or
|
sl@0
|
176 |
indirectly by using an asterisk (<<*>>), in which case
|
sl@0
|
177 |
an <<int>> argument is used as the precision. Supplying a negative
|
sl@0
|
178 |
precision is equivalent to omitting the precision.
|
sl@0
|
179 |
If only a period is specified the precision is zero.
|
sl@0
|
180 |
If a precision appears with any other conversion <[type]>
|
sl@0
|
181 |
than those listed here, the behavior is undefined.
|
sl@0
|
182 |
|
sl@0
|
183 |
o <[size]>
|
sl@0
|
184 |
|
sl@0
|
185 |
<<h>>, <<l>>, and <<L>> are optional size characters which
|
sl@0
|
186 |
override the default way that <<printf>> interprets the
|
sl@0
|
187 |
data type of the corresponding argument. <<h>> forces
|
sl@0
|
188 |
the following <<d>>, <<i>>, <<o>>, <<u>>, <<x>> or <<X>> conversion
|
sl@0
|
189 |
<[type]> to apply to a <<short>> or <<unsigned short>>. <<h>> also
|
sl@0
|
190 |
forces a following <<n>> <[type]> to apply to
|
sl@0
|
191 |
a pointer to a <<short>>. Similarily, an
|
sl@0
|
192 |
<<l>> forces the following <<d>>, <<i>>, <<o>>, <<u>>,
|
sl@0
|
193 |
<<x>> or <<X>> conversion <[type]> to apply to a <<long>> or
|
sl@0
|
194 |
<<unsigned long>>. <<l>> also forces a following <<n>> <[type]> to
|
sl@0
|
195 |
apply to a pointer to a <<long>>. If an <<h>>
|
sl@0
|
196 |
or an <<l>> appears with another conversion
|
sl@0
|
197 |
specifier, the behavior is undefined. <<L>> forces a
|
sl@0
|
198 |
following <<e>>, <<E>>, <<f>>, <<g>> or <<G>> conversion <[type]> to
|
sl@0
|
199 |
apply to a <<long double>> argument. If <<L>> appears with
|
sl@0
|
200 |
any other conversion <[type]>, the behavior is undefined.
|
sl@0
|
201 |
|
sl@0
|
202 |
o <[type]>
|
sl@0
|
203 |
|
sl@0
|
204 |
<[type]> specifies what kind of conversion <<printf>> performs.
|
sl@0
|
205 |
Here is a table of these:
|
sl@0
|
206 |
|
sl@0
|
207 |
o+
|
sl@0
|
208 |
o %
|
sl@0
|
209 |
prints the percent character (<<%>>)
|
sl@0
|
210 |
|
sl@0
|
211 |
o c
|
sl@0
|
212 |
prints <[arg]> as single character
|
sl@0
|
213 |
|
sl@0
|
214 |
o s
|
sl@0
|
215 |
prints characters until precision is reached or a null terminator
|
sl@0
|
216 |
is encountered; takes a string pointer
|
sl@0
|
217 |
|
sl@0
|
218 |
o d
|
sl@0
|
219 |
prints a signed decimal integer; takes an <<int>> (same as <<i>>)
|
sl@0
|
220 |
|
sl@0
|
221 |
o i
|
sl@0
|
222 |
prints a signed decimal integer; takes an <<int>> (same as <<d>>)
|
sl@0
|
223 |
|
sl@0
|
224 |
o o
|
sl@0
|
225 |
prints a signed octal integer; takes an <<int>>
|
sl@0
|
226 |
|
sl@0
|
227 |
o u
|
sl@0
|
228 |
prints an unsigned decimal integer; takes an <<int>>
|
sl@0
|
229 |
|
sl@0
|
230 |
o x
|
sl@0
|
231 |
prints an unsigned hexadecimal integer (using <<abcdef>> as
|
sl@0
|
232 |
digits beyond <<9>>); takes an <<int>>
|
sl@0
|
233 |
|
sl@0
|
234 |
o X
|
sl@0
|
235 |
prints an unsigned hexadecimal integer (using <<ABCDEF>> as
|
sl@0
|
236 |
digits beyond <<9>>); takes an <<int>>
|
sl@0
|
237 |
|
sl@0
|
238 |
o f
|
sl@0
|
239 |
prints a signed value of the form <<[-]9999.9999>>; takes
|
sl@0
|
240 |
a floating point number
|
sl@0
|
241 |
|
sl@0
|
242 |
o e
|
sl@0
|
243 |
prints a signed value of the form <<[-]9.9999e[+|-]999>>; takes a
|
sl@0
|
244 |
floating point number
|
sl@0
|
245 |
|
sl@0
|
246 |
o E
|
sl@0
|
247 |
prints the same way as <<e>>, but using <<E>> to introduce the
|
sl@0
|
248 |
exponent; takes a floating point number
|
sl@0
|
249 |
|
sl@0
|
250 |
o g
|
sl@0
|
251 |
prints a signed value in either <<f>> or <<e>> form, based on given
|
sl@0
|
252 |
value and precision---trailing zeros and the decimal point are
|
sl@0
|
253 |
printed only if necessary; takes a floating point number
|
sl@0
|
254 |
|
sl@0
|
255 |
o G
|
sl@0
|
256 |
prints the same way as <<g>>, but using <<E>> for the exponent if an
|
sl@0
|
257 |
exponent is needed; takes a floating point number
|
sl@0
|
258 |
|
sl@0
|
259 |
o n
|
sl@0
|
260 |
stores (in the same object) a count of the characters written;
|
sl@0
|
261 |
takes a pointer to <<int>>
|
sl@0
|
262 |
|
sl@0
|
263 |
o p
|
sl@0
|
264 |
prints a pointer in an implementation-defined format.
|
sl@0
|
265 |
This implementation treats the pointer as an
|
sl@0
|
266 |
<<unsigned long>> (same as <<Lu>>).
|
sl@0
|
267 |
o-
|
sl@0
|
268 |
O-
|
sl@0
|
269 |
|
sl@0
|
270 |
|
sl@0
|
271 |
RETURNS
|
sl@0
|
272 |
<<sprintf>> returns the number of bytes in the output string,
|
sl@0
|
273 |
save that the concluding <<NULL>> is not counted.
|
sl@0
|
274 |
<<printf>> and <<fprintf>> return the number of characters transmitted.
|
sl@0
|
275 |
If an error occurs, <<printf>> and <<fprintf>> return <<EOF>>.
|
sl@0
|
276 |
Error returns occur for <<sprintf>>.
|
sl@0
|
277 |
|
sl@0
|
278 |
PORTABILITY
|
sl@0
|
279 |
The ANSI C standard specifies that implementations must
|
sl@0
|
280 |
support at least formatted output of up to 509 characters.
|
sl@0
|
281 |
|
sl@0
|
282 |
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
sl@0
|
283 |
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
|
sl@0
|
284 |
*/
|
sl@0
|
285 |
|
sl@0
|
286 |
#include <_ansi.h>
|
sl@0
|
287 |
#include <stdio_r.h>
|
sl@0
|
288 |
#include <stdarg.h>
|
sl@0
|
289 |
#include <limits.h>
|
sl@0
|
290 |
#include "LOCAL.H"
|
sl@0
|
291 |
|
sl@0
|
292 |
/**
|
sl@0
|
293 |
A reentrant version of sprintf().
|
sl@0
|
294 |
*/
|
sl@0
|
295 |
EXPORT_C int _sprintf_r (struct _reent *ptr, char *str, const char *fmt, ...)
|
sl@0
|
296 |
{
|
sl@0
|
297 |
int ret;
|
sl@0
|
298 |
va_list ap;
|
sl@0
|
299 |
FILE f;
|
sl@0
|
300 |
|
sl@0
|
301 |
f._flags = __SWR | __SSTR;
|
sl@0
|
302 |
f._bf._base = f._p = (unsigned char *) str;
|
sl@0
|
303 |
f._bf._size = f._w = INT_MAX;
|
sl@0
|
304 |
f._data = ptr;
|
sl@0
|
305 |
va_start (ap, fmt);
|
sl@0
|
306 |
ret = vfprintf (&f, fmt, ap);
|
sl@0
|
307 |
va_end (ap);
|
sl@0
|
308 |
*f._p = 0;
|
sl@0
|
309 |
return (ret);
|
sl@0
|
310 |
}
|
sl@0
|
311 |
|
sl@0
|
312 |
#ifndef _REENT_ONLY
|
sl@0
|
313 |
|
sl@0
|
314 |
/**
|
sl@0
|
315 |
Print formatted data to a string.
|
sl@0
|
316 |
Writes a sequence of arguments to the given buffer formatted as the format argument specifies.
|
sl@0
|
317 |
|
sl@0
|
318 |
@param str Buffer where to store the resulting formatted string.
|
sl@0
|
319 |
@param fmt String that contains the text to be printed.
|
sl@0
|
320 |
|
sl@0
|
321 |
@return On Success, the total number of characters printed is returned.
|
sl@0
|
322 |
On Failure, a negative number is returned.
|
sl@0
|
323 |
*/
|
sl@0
|
324 |
EXPORT_C int sprintf (char *str, const char *fmt, ...)
|
sl@0
|
325 |
{
|
sl@0
|
326 |
int ret;
|
sl@0
|
327 |
va_list ap;
|
sl@0
|
328 |
FILE f;
|
sl@0
|
329 |
|
sl@0
|
330 |
f._flags = __SWR | __SSTR;
|
sl@0
|
331 |
f._bf._base = f._p = (unsigned char *) str;
|
sl@0
|
332 |
f._bf._size = f._w = INT_MAX;
|
sl@0
|
333 |
f._data = _REENT2;
|
sl@0
|
334 |
if (!f._data)
|
sl@0
|
335 |
return EOF; // Memory for library globals is not allocated (errno not set).
|
sl@0
|
336 |
va_start (ap, fmt);
|
sl@0
|
337 |
ret = vfprintf (&f, fmt, ap);
|
sl@0
|
338 |
va_end (ap);
|
sl@0
|
339 |
*f._p = 0;
|
sl@0
|
340 |
return (ret);
|
sl@0
|
341 |
}
|
sl@0
|
342 |
|
sl@0
|
343 |
|
sl@0
|
344 |
/**
|
sl@0
|
345 |
@param str Buffer where to store the resulting formatted string.
|
sl@0
|
346 |
@param fmt String that contains the text to be printed.
|
sl@0
|
347 |
@param ap
|
sl@0
|
348 |
|
sl@0
|
349 |
@return On Success, the total number of characters printed is returned.
|
sl@0
|
350 |
On Failure, a negative number is returned.
|
sl@0
|
351 |
*/
|
sl@0
|
352 |
EXPORT_C int vsprintf (char *str,char const *fmt, va_list ap)
|
sl@0
|
353 |
{
|
sl@0
|
354 |
int ret;
|
sl@0
|
355 |
FILE f;
|
sl@0
|
356 |
|
sl@0
|
357 |
f._flags = __SWR | __SSTR;
|
sl@0
|
358 |
f._bf._base = f._p = (unsigned char *) str;
|
sl@0
|
359 |
f._bf._size = f._w = INT_MAX;
|
sl@0
|
360 |
f._data = _REENT2;
|
sl@0
|
361 |
if (!f._data)
|
sl@0
|
362 |
return EOF; // Memory for library globals is not allocated (errno not set).
|
sl@0
|
363 |
ret = vfprintf (&f, fmt, ap);
|
sl@0
|
364 |
*f._p = 0;
|
sl@0
|
365 |
return ret;
|
sl@0
|
366 |
}
|
sl@0
|
367 |
|
sl@0
|
368 |
#endif
|