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

Subversion Repositories or1k

[/] [or1k/] [branches/] [newlib/] [newlib/] [newlib/] [libc/] [stdio/] [sprintf.c] - Blame information for rev 39

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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