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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [newlib-1.17.0/] [newlib/] [libc/] [stdio/] [sprintf.c] - Blame information for rev 864

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

Line No. Rev Author Line
1 148 jeremybenn
/*
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
FUNCTION
20
<<sprintf>>, <<fprintf>>, <<printf>>, <<snprintf>>, <<asprintf>>, <<asnprintf>>---format output
21
 
22
INDEX
23
        fprintf
24
INDEX
25
        _fprintf_r
26
INDEX
27
        printf
28
INDEX
29
        _printf_r
30
INDEX
31
        asprintf
32
INDEX
33
        _asprintf_r
34
INDEX
35
        sprintf
36
INDEX
37
        _sprintf_r
38
INDEX
39
        snprintf
40
INDEX
41
        _snprintf_r
42
INDEX
43
        asnprintf
44
INDEX
45
        _asnprintf_r
46
 
47
ANSI_SYNOPSIS
48
        #include <stdio.h>
49
 
50
        int printf(const char *<[format]>, ...);
51
        int fprintf(FILE *<[fd]>, const char *<[format]>, ...);
52
        int sprintf(char *<[str]>, const char *<[format]>, ...);
53
        int snprintf(char *<[str]>, size_t <[size]>, const char *<[format]>,
54
                     ...);
55
        int asprintf(char **<[strp]>, const char *<[format]>, ...);
56
        char *asnprintf(char *<[str]>, size_t *<[size]>, const char *<[format]>,
57
                        ...);
58
 
59
        int _printf_r(struct _reent *<[ptr]>, const char *<[format]>, ...);
60
        int _fprintf_r(struct _reent *<[ptr]>, FILE *<[fd]>,
61
                       const char *<[format]>, ...);
62
        int _sprintf_r(struct _reent *<[ptr]>, char *<[str]>,
63
                       const char *<[format]>, ...);
64
        int _snprintf_r(struct _reent *<[ptr]>, char *<[str]>, size_t <[size]>,
65
                        const char *<[format]>, ...);
66
        int _asprintf_r(struct _reent *<[ptr]>, char **<[strp]>,
67
                        const char *<[format]>, ...);
68
        char *_asnprintf_r(struct _reent *<[ptr]>, char *<[str]>,
69
                           size_t *<[size]>, const 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>>, without a terminating NUL
75
        character.  The behavior of <<printf>> is undefined if there
76
        are not enough arguments for the format.  <<printf>> returns
77
        when it reaches the end of the format string.  If there are
78
        more arguments than the format requires, excess arguments are
79
        ignored.
80
 
81
        <<fprintf>> is like <<printf>>, except that output is directed
82
        to the stream <[fd]> rather than <<stdout>>.
83
 
84
        <<sprintf>> is like <<printf>>, except that output is directed
85
        to the buffer <[str]>, and a terminating NUL is output.
86
        Behavior is undefined if more output is generated than the
87
        buffer can hold.
88
 
89
        <<snprintf>> is like <<sprintf>>, except that output is
90
        limited to at most <[size]> bytes, including the terminating
91
        <<NUL>>.  As a special case, if <[size]> is 0, <[str]> can be
92
        NULL, and <<snprintf>> merely calculates how many bytes would
93
        be printed.
94
 
95
        <<asprintf>> is like <<sprintf>>, except that the output is
96
        stored in a dynamically allocated buffer, <[pstr]>, which
97
        should be freed later with <<free>>.
98
 
99
        <<asnprintf>> is like <<sprintf>>, except that the return type
100
        is either the original <[str]> if it was large enough, or a
101
        dynamically allocated string if the output exceeds *<[size]>;
102
        the length of the result is returned in *<[size]>.  When
103
        dynamic allocation occurs, the contents of the original
104
        <[str]> may have been modified.
105
 
106
        For <<sprintf>>, <<snprintf>>, and <<asnprintf>>, the behavior
107
        is undefined if the output <<*<[str]>>> overlaps with one of
108
        the arguments.  Behavior is also undefined if the argument for
109
        <<%n>> within <<*<[format]>>> overlaps another argument.
110
 
111
        <[format]> is a pointer to a character string containing two
112
        types of objects: ordinary characters (other than <<%>>),
113
        which are copied unchanged to the output, and conversion
114
        specifications, each of which is introduced by <<%>>. (To
115
        include <<%>> in the output, use <<%%>> in the format string.)
116
        A conversion specification has the following form:
117
 
118
.       %[<[pos]>][<[flags]>][<[width]>][.<[prec]>][<[size]>]<[type]>
119
 
120
        The fields of the conversion specification have the following
121
        meanings:
122
 
123
        O+
124
        o <[pos]>
125
 
126
        Conversions normally consume arguments in the order that they
127
        are presented.  However, it is possible to consume arguments
128
        out of order, and reuse an argument for more than one
129
        conversion specification (although the behavior is undefined
130
        if the same argument is requested with different types), by
131
        specifying <[pos]>, which is a decimal integer followed by
132
        '$'.  The integer must be between 1 and <NL_ARGMAX> from
133
        limits.h, and if argument <<%n$>> is requested, all earlier
134
        arguments must be requested somewhere within <[format]>.  If
135
        positional parameters are used, then all conversion
136
        specifications except for <<%%>> must specify a position.
137
 
138
        o <[flags]>
139
 
140
        <[flags]> is an optional sequence of characters which control
141
        output justification, numeric signs, decimal points, trailing
142
        zeros, and octal and hex prefixes.  The flag characters are
143
        minus (<<->>), plus (<<+>>), space ( ), zero (<<0>>), sharp
144
        (<<#>>), and quote (<<'>>).  They can appear in any
145
        combination, although not all flags can be used for all
146
        conversion specification types.
147
 
148
                o+
149
                o '
150
                        Since newlib only supports the C locale, this
151
                        flag has no effect in this implementation.
152
                        But in other locales, when <[type]> is <<i>>,
153
                        <<d>>, <<u>>, <<f>>, <<F>>, <<g>>, or <<G>>,
154
                        the locale-dependent thousand's separator is
155
                        inserted prior to zero padding.
156
 
157
                o -
158
                        The result of the conversion is left
159
                        justified, and the right is padded with
160
                        blanks.  If you do not use this flag, the
161
                        result is right justified, and padded on the
162
                        left.
163
 
164
                o +
165
                        The result of a signed conversion (as
166
                        determined by <[type]> of <<d>>, <<i>>, <<a>>,
167
                        <<A>>, <<e>>, <<E>>, <<f>>, <<F>>, <<g>>, or
168
                        <<G>>) will always begin with a plus or minus
169
                        sign.  (If you do not use this flag, positive
170
                        values do not begin with a plus sign.)
171
 
172
                o " " (space)
173
                        If the first character of a signed conversion
174
                        specification is not a sign, or if a signed
175
                        conversion results in no characters, the
176
                        result will begin with a space.  If the space
177
                        ( ) flag and the plus (<<+>>) flag both
178
                        appear, the space flag is ignored.
179
 
180
                o 0
181
                        If the <[type]> character is <<d>>, <<i>>,
182
                        <<o>>, <<u>>, <<x>>, <<X>>, <<a>>, <<A>>,
183
                        <<e>>, <<E>>, <<f>>, <<g>>, or <<G>>: leading
184
                        zeros are used to pad the field width
185
                        (following any indication of sign or base); no
186
                        spaces are used for padding.  If the zero
187
                        (<<0>>) and minus (<<->>) flags both appear,
188
                        the zero (<<0>>) flag will be ignored.  For
189
                        <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, and <<X>>
190
                        conversions, if a precision <[prec]> is
191
                        specified, the zero (<<0>>) flag is ignored.
192
 
193
                        Note that <<0>> is interpreted as a flag, not
194
                        as the beginning of a field width.
195
 
196
                o #
197
                        The result is to be converted to an
198
                        alternative form, according to the <[type]>
199
                        character:
200
 
201
                        o+
202
                        o o
203
                                Increases precision to force the first
204
                                digit of the result to be a zero.
205
 
206
                        o x
207
                                A non-zero result will have a <<0x>>
208
                                prefix.
209
 
210
                        o X
211
                                A non-zero result will have a <<0X>>
212
                                prefix.
213
 
214
                        o a, A, e, E, f, or F
215
                                The result will always contain a
216
                                decimal point even if no digits follow
217
                                the point.  (Normally, a decimal point
218
                                appears only if a digit follows it.)
219
                                Trailing zeros are removed.
220
 
221
                        o g or G
222
                                The result will always contain a
223
                                decimal point even if no digits follow
224
                                the point.  Trailing zeros are not
225
                                removed.
226
 
227
                        o all others
228
                                Undefined.
229
 
230
                        o-
231
                o-
232
 
233
        o <[width]>
234
 
235
                <[width]> is an optional minimum field width.  You can
236
                either specify it directly as a decimal integer, or
237
                indirectly by using instead an asterisk (<<*>>), in
238
                which case an <<int>> argument is used as the field
239
                width.  If positional arguments are used, then the
240
                width must also be specified positionally as <<*m$>>,
241
                with m as a decimal integer.  Negative field widths
242
                are treated as specifying the minus (<<->>) flag for
243
                left justfication, along with a positive field width.
244
                The resulting format may be wider than the specified
245
                width.
246
 
247
        o <[prec]>
248
 
249
                <[prec]> is an optional field; if present, it is
250
                introduced with `<<.>>' (a period). You can specify
251
                the precision either directly as a decimal integer or
252
                indirectly by using an asterisk (<<*>>), in which case
253
                an <<int>> argument is used as the precision.  If
254
                positional arguments are used, then the precision must
255
                also be specified positionally as <<*m$>>, with m as a
256
                decimal integer.  Supplying a negative precision is
257
                equivalent to omitting the precision.  If only a
258
                period is specified the precision is zero. The effect
259
                depends on the conversion <[type]>.
260
 
261
                o+
262
                o d, i, o, u, x, or X
263
                        Minimum number of digits to appear.  If no
264
                        precision is given, defaults to 1.
265
 
266
                o a or A
267
                        Number of digits to appear after the decimal
268
                        point.  If no precision is given, the
269
                        precision defaults to the minimum needed for
270
                        an exact representation.
271
 
272
                o e, E, f or F
273
                        Number of digits to appear after the decimal
274
                        point.  If no precision is given, the
275
                        precision defaults to 6.
276
 
277
                o g or G
278
                        Maximum number of significant digits.  A
279
                        precision of 0 is treated the same as a
280
                        precision of 1.  If no precision is given, the
281
                        precision defaults to 6.
282
 
283
                o s or S
284
                        Maximum number of characters to print from the
285
                        string.  If no precision is given, the entire
286
                        string is printed.
287
 
288
                o all others
289
                        undefined.
290
 
291
                o-
292
 
293
        o <[size]>
294
 
295
                <[size]> is an optional modifier that changes the data
296
                type that the corresponding argument has.  Behavior is
297
                unspecified if a size is given that does not match the
298
                <[type]>.
299
 
300
                o+
301
                o hh
302
                        With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or
303
                        <<X>>, specifies that the argument should be
304
                        converted to a <<signed char>> or <<unsigned
305
                        char>> before printing.
306
 
307
                        With <<n>>, specifies that the argument is a
308
                        pointer to a <<signed char>>.
309
 
310
                o h
311
                        With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or
312
                        <<X>>, specifies that the argument should be
313
                        converted to a <<short>> or <<unsigned short>>
314
                        before printing.
315
 
316
                        With <<n>>, specifies that the argument is a
317
                        pointer to a <<short>>.
318
 
319
                o l
320
                        With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or
321
                        <<X>>, specifies that the argument is a
322
                        <<long>> or <<unsigned long>>.
323
 
324
                        With <<c>>, specifies that the argument has
325
                        type <<wint_t>>.
326
 
327
                        With <<s>>, specifies that the argument is a
328
                        pointer to <<wchar_t>>.
329
 
330
                        With <<n>>, specifies that the argument is a
331
                        pointer to a <<long>>.
332
 
333
                        With <<a>>, <<A>>, <<e>>, <<E>>, <<f>>, <<F>>,
334
                        <<g>>, or <<G>>, has no effect (because of
335
                        vararg promotion rules, there is no need to
336
                        distinguish between <<float>> and <<double>>).
337
 
338
                o ll
339
                        With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or
340
                        <<X>>, specifies that the argument is a
341
                        <<long long>> or <<unsigned long long>>.
342
 
343
                        With <<n>>, specifies that the argument is a
344
                        pointer to a <<long long>>.
345
 
346
                o j
347
                        With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or
348
                        <<X>>, specifies that the argument is an
349
                        <<intmax_t>> or <<uintmax_t>>.
350
 
351
                        With <<n>>, specifies that the argument is a
352
                        pointer to an <<intmax_t>>.
353
 
354
                o z
355
                        With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or
356
                        <<X>>, specifies that the argument is a
357
                        <<ssize_t>> or <<size_t>>.
358
 
359
                        With <<n>>, specifies that the argument is a
360
                        pointer to a <<ssize_t>>.
361
 
362
                o t
363
                        With <<d>>, <<i>>, <<o>>, <<u>>, <<x>>, or
364
                        <<X>>, specifies that the argument is a
365
                        <<ptrdiff_t>>.
366
 
367
                        With <<n>>, specifies that the argument is a
368
                        pointer to a <<ptrdiff_t>>.
369
 
370
                o L
371
                        With <<a>>, <<A>>, <<e>>, <<E>>, <<f>>, <<F>>,
372
                        <<g>>, or <<G>>, specifies that the argument
373
                        is a <<long double>>.
374
 
375
                o-
376
 
377
        o   <[type]>
378
 
379
                <[type]> specifies what kind of conversion <<printf>>
380
                performs.  Here is a table of these:
381
 
382
                o+
383
                o %
384
                        Prints the percent character (<<%>>).
385
 
386
                o c
387
                        Prints <[arg]> as single character.  If the
388
                        <<l>> size specifier is in effect, a multibyte
389
                        character is printed.
390
 
391
                o C
392
                        Short for <<%lc>>.
393
 
394
                o s
395
                        Prints the elements of a pointer to <<char>>
396
                        until the precision or a null character is
397
                        reached.  If the <<l>> size specifier is in
398
                        effect, the pointer is to an array of
399
                        <<wchar_t>>, and the string is converted to
400
                        multibyte characters before printing.
401
 
402
                o S
403
                        Short for <<%ls>>.
404
 
405
                o d or i
406
                        Prints a signed decimal integer; takes an
407
                        <<int>>.  Leading zeros are inserted as
408
                        necessary to reach the precision.  A precision
409
                        of 0 produces an empty string.
410
 
411
                o D
412
                        Newlib extension, short for <<%ld>>.
413
 
414
                o o
415
                        Prints an unsigned octal integer; takes an
416
                        <<unsigned>>.  Leading zeros are inserted as
417
                        necessary to reach the precision.  A precision
418
                        of 0 produces an empty string.
419
 
420
                o O
421
                        Newlib extension, short for <<%lo>>.
422
 
423
                o u
424
                        Prints an unsigned decimal integer; takes an
425
                        <<unsigned>>.  Leading zeros are inserted as
426
                        necessary to reach the precision.  A precision
427
                        of 0 produces an empty string.
428
 
429
                o U
430
                        Newlib extension, short for <<%lu>>.
431
 
432
                o x
433
                        Prints an unsigned hexadecimal integer (using
434
                        <<abcdef>> as digits beyond <<9>>); takes an
435
                        <<unsigned>>.  Leading zeros are inserted as
436
                        necessary to reach the precision.  A precision
437
                        of 0 produces an empty string.
438
 
439
                o X
440
                        Like <<x>>, but uses <<ABCDEF>> as digits
441
                        beyond <<9>>.
442
 
443
                o f
444
                        Prints a signed value of the form
445
                        <<[-]9999.9999>>, with the precision
446
                        determining how many digits follow the decimal
447
                        point; takes a <<double>> (remember that
448
                        <<float>> promotes to <<double>> as a vararg).
449
                        The low order digit is rounded to even.  If
450
                        the precision results in at most DECIMAL_DIG
451
                        digits, the result is rounded correctly; if
452
                        more than DECIMAL_DIG digits are printed, the
453
                        result is only guaranteed to round back to the
454
                        original value.
455
 
456
                        If the value is infinite, the result is
457
                        <<inf>>, and no zero padding is performed.  If
458
                        the value is not a number, the result is
459
                        <<nan>>, and no zero padding is performed.
460
 
461
                o F
462
                        Like <<f>>, but uses <<INF>> and <<NAN>> for
463
                        non-finite numbers.
464
 
465
                o e
466
                        Prints a signed value of the form
467
                        <<[-]9.9999e[+|-]999>>; takes a <<double>>.
468
                        The digit before the decimal point is non-zero
469
                        if the value is non-zero.  The precision
470
                        determines how many digits appear between
471
                        <<.>> and <<e>>, and the exponent always
472
                        contains at least two digits.  The value zero
473
                        has an exponent of zero.  If the value is not
474
                        finite, it is printed like <<f>>.
475
 
476
                o E
477
                        Like <<e>>, but using <<E>> to introduce the
478
                        exponent, and like <<F>> for non-finite
479
                        values.
480
 
481
                o g
482
                        Prints a signed value in either <<f>> or <<e>>
483
                        form, based on the given value and
484
                        precision---an exponent less than -4 or
485
                        greater than the precision selects the <<e>>
486
                        form.  Trailing zeros and the decimal point
487
                        are printed only if necessary; takes a
488
                        <<double>>.
489
 
490
                o G
491
                        Like <<g>>, except use <<F>> or <<E>> form.
492
 
493
                o a
494
                        Prints a signed value of the form
495
                        <<[-]0x1.ffffp[+|-]9>>; takes a <<double>>.
496
                        The letters <<abcdef>> are used for digits
497
                        beyond <<9>>.  The precision determines how
498
                        many digits appear after the decimal point.
499
                        The exponent contains at least one digit, and
500
                        is a decimal value representing the power of
501
                        2; a value of 0 has an exponent of 0.
502
                        Non-finite values are printed like <<f>>.
503
 
504
                o A
505
                        Like <<a>>, except uses <<X>>, <<P>>, and
506
                        <<ABCDEF>> instead of lower case.
507
 
508
                o n
509
                        Takes a pointer to <<int>>, and stores a count
510
                        of the number of bytes written so far.  No
511
                        output is created.
512
 
513
                o p
514
                        Takes a pointer to <<void>>, and prints it in
515
                        an implementation-defined format.  This
516
                        implementation is similar to <<%#tx>>), except
517
                        that <<0x>> appears even for the NULL pointer.
518
 
519
                o-
520
        O-
521
 
522
        <<_printf_r>>, <<_fprintf_r>>, <<_asprintf_r>>,
523
        <<_sprintf_r>>, <<_snprintf_r>>, <<_asnprintf_r>> are simply
524
        reentrant versions of the functions above.
525
 
526
RETURNS
527
On success, <<sprintf>> and <<asprintf>> return the number of bytes in
528
the output string, except the concluding <<NUL>> is not counted.
529
<<snprintf>> returns the number of bytes that would be in the output
530
string, except the concluding <<NUL>> is not counted.  <<printf>> and
531
<<fprintf>> return the number of characters transmitted.
532
<<asnprintf>> returns the original <[str]> if there was enough room,
533
otherwise it returns an allocated string.
534
 
535
If an error occurs, the result of <<printf>>, <<fprintf>>,
536
<<snprintf>>, and <<asprintf>> is a negative value, and the result of
537
<<asnprintf>> is NULL.  No error returns occur for <<sprintf>>.  For
538
<<printf>> and <<fprintf>>, <<errno>> may be set according to
539
<<fputc>>.  For <<asprintf>> and <<asnprintf>>, <<errno>> may be set
540
to ENOMEM if allocation fails, and for <<snprintf>>, <<errno>> may be
541
set to EOVERFLOW if <[size]> or the output length exceeds INT_MAX.
542
 
543
PORTABILITY
544
ANSI C requires <<printf>>, <<fprintf>>, <<sprintf>>, and
545
<<snprintf>>.  <<asprintf>> and <<asnprintf>> are newlib extensions.
546
 
547
The ANSI C standard specifies that implementations must support at
548
least formatted output of up to 509 characters.  This implementation
549
has no inherent limit.
550
 
551
Depending on how newlib was configured, not all format specifiers are
552
supported.
553
 
554
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
555
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
556
*/
557
 
558
#include <_ansi.h>
559
#include <reent.h>
560
#include <stdio.h>
561
#ifdef _HAVE_STDC
562
#include <stdarg.h>
563
#else
564
#include <varargs.h>
565
#endif
566
#include <limits.h>
567
#include "local.h"
568
 
569
int
570
#ifdef _HAVE_STDC
571
_DEFUN(_sprintf_r, (ptr, str, fmt),
572
       struct _reent *ptr _AND
573
       char *str          _AND
574
       _CONST char *fmt _DOTS)
575
#else
576
_sprintf_r(ptr, str, fmt, va_alist)
577
           struct _reent *ptr;
578
           char *str;
579
           _CONST char *fmt;
580
           va_dcl
581
#endif
582
{
583
  int ret;
584
  va_list ap;
585
  FILE f;
586
 
587
  f._flags = __SWR | __SSTR;
588
  f._bf._base = f._p = (unsigned char *) str;
589
  f._bf._size = f._w = INT_MAX;
590
  f._file = -1;  /* No file. */
591
#ifdef _HAVE_STDC
592
  va_start (ap, fmt);
593
#else
594
  va_start (ap);
595
#endif
596
  ret = _svfprintf_r (ptr, &f, fmt, ap);
597
  va_end (ap);
598
  *f._p = 0;
599
  return (ret);
600
}
601
 
602
#ifndef _REENT_ONLY
603
 
604
int
605
#ifdef _HAVE_STDC
606
_DEFUN(sprintf, (str, fmt),
607
       char *str _AND
608
       _CONST char *fmt _DOTS)
609
#else
610
sprintf(str, fmt, va_alist)
611
        char *str;
612
        _CONST char *fmt;
613
        va_dcl
614
#endif
615
{
616
  int ret;
617
  va_list ap;
618
  FILE f;
619
 
620
  f._flags = __SWR | __SSTR;
621
  f._bf._base = f._p = (unsigned char *) str;
622
  f._bf._size = f._w = INT_MAX;
623
  f._file = -1;  /* No file. */
624
#ifdef _HAVE_STDC
625
  va_start (ap, fmt);
626
#else
627
  va_start (ap);
628
#endif
629
  ret = _svfprintf_r (_REENT, &f, fmt, ap);
630
  va_end (ap);
631
  *f._p = 0;
632
  return (ret);
633
}
634
 
635
#endif

powered by: WebSVN 2.1.0

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