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