OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [stdio/] [sprintf.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.