Subversion Repositories Kolibri OS

Rev

Rev 4921 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4349 Serge 1
/*
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
<>, <>, <>, <>, <>, <>---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 
49
 
4921 Serge 50
        int printf(const char *restrict <[format]>, ...);
51
        int fprintf(FILE *restrict <[fd]>, const char *restrict <[format]>, ...);
52
        int sprintf(char *restrict <[str]>, const char *restrict <[format]>, ...);
53
        int snprintf(char *restrict <[str]>, size_t <[size]>, const char *restrict <[format]>,
4349 Serge 54
                     ...);
4921 Serge 55
        int asprintf(char **restrict <[strp]>, const char *restrict <[format]>, ...);
56
        char *asnprintf(char *restrict <[str]>, size_t *restrict <[size]>, const char *restrict <[format]>,
4349 Serge 57
                        ...);
58
 
4921 Serge 59
        int _printf_r(struct _reent *<[ptr]>, const char *restrict <[format]>, ...);
60
        int _fprintf_r(struct _reent *<[ptr]>, FILE *restrict <[fd]>,
61
                       const char *restrict <[format]>, ...);
62
        int _sprintf_r(struct _reent *<[ptr]>, char *restrict <[str]>,
63
                       const char *restrict <[format]>, ...);
64
        int _snprintf_r(struct _reent *<[ptr]>, char *restrict <[str]>, size_t <[size]>,
65
                        const char *restrict <[format]>, ...);
66
        int _asprintf_r(struct _reent *<[ptr]>, char **restrict <[strp]>,
67
                        const char *restrict <[format]>, ...);
68
        char *_asnprintf_r(struct _reent *<[ptr]>, char *restrict <[str]>,
69
                           size_t *restrict <[size]>, const char *restrict <[format]>, ...);
4349 Serge 70
 
71
DESCRIPTION
72
        <> accepts a series of arguments, applies to each a
73
        format specifier from <<*<[format]>>>, and writes the
74
        formatted data to <>, without a terminating NUL
75
        character.  The behavior of <> is undefined if there
76
        are not enough arguments for the format.  <> 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
        <> is like <>, except that output is directed
82
        to the stream <[fd]> rather than <>.
83
 
84
        <> is like <>, 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
        <> is like <>, except that output is
90
        limited to at most <[size]> bytes, including the terminating
91
        <>.  As a special case, if <[size]> is 0, <[str]> can be
92
        NULL, and <> merely calculates how many bytes would
93
        be printed.
94
 
95
        <> is like <>, except that the output is
96
        stored in a dynamically allocated buffer, <[pstr]>, which
97
        should be freed later with <>.
98
 
99
        <> is like <>, 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 <>, <>, and <>, 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  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
	This positional parameters method is a POSIX extension to the C
138
	standard definition for the functions.
139
 
140
	o <[flags]>
141
 
142
	<[flags]> is an optional sequence of characters which control
143
	output justification, numeric signs, decimal points, trailing
144
	zeros, and octal and hex prefixes.  The flag characters are
145
	minus (<<->>), plus (<<+>>), space ( ), zero (<<0>>), sharp
146
	(<<#>>), and quote (<<'>>).  They can appear in any
147
	combination, although not all flags can be used for all
148
	conversion specification types.
149
 
150
		o+
151
		o '
152
			A POSIX extension to the C standard.  However, this
153
			implementation presently treats it as a no-op, which
154
			is the default behavior for the C locale, anyway.  (If
155
			it did what it is supposed to, when <[type]> were <>,
156
			<>, <>, <>, <>, <>, or <>, the
157
			integer portion of the conversion would be formatted
158
			with thousands' grouping wide characters.)
159
 
160
		o -
161
			The result of the conversion is left
162
			justified, and the right is padded with
163
			blanks.  If you do not use this flag, the
164
			result is right justified, and padded on the
165
			left.
166
 
167
	        o +
168
			The result of a signed conversion (as
169
			determined by <[type]> of <>, <>, <>,
170
			<>, <>, <>, <>, <>, <>, or
171
			<>) will always begin with a plus or minus
172
			sign.  (If you do not use this flag, positive
173
			values do not begin with a plus sign.)
174
 
175
		o " " (space)
176
			If the first character of a signed conversion
177
		        specification is not a sign, or if a signed
178
		        conversion results in no characters, the
179
		        result will begin with a space.  If the space
180
		        ( ) flag and the plus (<<+>>) flag both
181
		        appear, the space flag is ignored.
182
 
183
	        o 0
184
			If the <[type]> character is <>, <>,
185
			<>, <>, <>, <>, <>, <>,
186
			<>, <>, <>, <>, <>, or <>:  leading
187
			zeros are used to pad the field width
188
			(following any indication of sign or base); no
189
			spaces are used for padding.  If the zero
190
			(<<0>>) and minus (<<->>) flags both appear,
191
			the zero (<<0>>) flag will be ignored.  For
192
			<>, <>, <>, <>, <>, and <>
193
			conversions, if a precision <[prec]> is
194
			specified, the zero (<<0>>) flag is ignored.
195
 
196
			Note that <<0>> is interpreted as a flag, not
197
		        as the beginning of a field width.
198
 
199
	        o #
200
			The result is to be converted to an
201
			alternative form, according to the <[type]>
6099 serge 202
			character.
203
		o-
4349 Serge 204
 
6099 serge 205
	The alternative form output with the # flag depends on the <[type]>
206
	character:
4349 Serge 207
 
6099 serge 208
		o+
209
		o o
210
			Increases precision to force the first
211
			digit of the result to be a zero.
4349 Serge 212
 
6099 serge 213
		o x
214
			A non-zero result will have a <<0x>>
215
			prefix.
4349 Serge 216
 
6099 serge 217
		o X
218
			A non-zero result will have a <<0X>>
219
			prefix.
4349 Serge 220
 
6099 serge 221
		o a, A, e, E, f, or F
222
			The result will always contain a
223
			decimal point even if no digits follow
224
			the point.  (Normally, a decimal point
225
			appears only if a digit follows it.)
226
			Trailing zeros are removed.
4349 Serge 227
 
6099 serge 228
		o g or G
229
			The result will always contain a
230
			decimal point even if no digits follow
231
			the point.  Trailing zeros are not
232
			removed.
4349 Serge 233
 
6099 serge 234
		o all others
235
			Undefined.
236
 
4349 Serge 237
		o-
238
 
239
	o <[width]>
240
 
241
		<[width]> is an optional minimum field width.  You can
242
		either specify it directly as a decimal integer, or
243
		indirectly by using instead an asterisk (<<*>>), in
244
		which case an <> argument is used as the field
245
		width.  If positional arguments are used, then the
246
		width must also be specified positionally as <<*m$>>,
247
		with m as a decimal integer.  Negative field widths
248
		are treated as specifying the minus (<<->>) flag for
249
		left justfication, along with a positive field width.
250
		The resulting format may be wider than the specified
251
		width.
252
 
253
	o <[prec]>
254
 
255
		<[prec]> is an optional field; if present, it is
256
		introduced with `<<.>>' (a period). You can specify
257
		the precision either directly as a decimal integer or
258
		indirectly by using an asterisk (<<*>>), in which case
259
		an <> argument is used as the precision.  If
260
		positional arguments are used, then the precision must
261
		also be specified positionally as <<*m$>>, with m as a
262
		decimal integer.  Supplying a negative precision is
263
		equivalent to omitting the precision.  If only a
264
		period is specified the precision is zero. The effect
265
		depends on the conversion <[type]>.
266
 
267
		o+
268
		o d, i, o, u, x, or X
269
			Minimum number of digits to appear.  If no
270
			precision is given, defaults to 1.
271
 
272
		o a or A
273
			Number of digits to appear after the decimal
274
			point.  If no precision is given, the
275
			precision defaults to the minimum needed for
276
			an exact representation.
277
 
278
		o e, E, f or F
279
			Number of digits to appear after the decimal
280
			point.  If no precision is given, the
281
			precision defaults to 6.
282
 
283
		o g or G
284
			Maximum number of significant digits.  A
285
			precision of 0 is treated the same as a
286
			precision of 1.  If no precision is given, the
287
			precision defaults to 6.
288
 
289
		o s or S
290
			Maximum number of characters to print from the
291
			string.  If no precision is given, the entire
292
			string is printed.
293
 
294
		o all others
295
			undefined.
296
 
297
		o-
298
 
299
	o <[size]>
300
 
301
		<[size]> is an optional modifier that changes the data
302
		type that the corresponding argument has.  Behavior is
303
		unspecified if a size is given that does not match the
304
		<[type]>.
305
 
306
		o+
307
		o hh
308
			With <>, <>, <>, <>, <>, or
309
			<>, specifies that the argument should be
310
			converted to a <> or <
311
			char>> before printing.
312
 
313
			With <>, specifies that the argument is a
314
			pointer to a <>.
315
 
316
		o h
317
			With <>, <>, <>, <>, <>, or
318
			<>, specifies that the argument should be
319
			converted to a <> or <>
320
			before printing.
321
 
322
			With <>, specifies that the argument is a
323
			pointer to a <>.
324
 
325
		o l
326
			With <>, <>, <>, <>, <>, or
327
			<>, specifies that the argument is a
328
			<> or <>.
329
 
330
			With <>, specifies that the argument has
331
			type <>.
332
 
333
			With <>, specifies that the argument is a
334
			pointer to <>.
335
 
336
			With <>, specifies that the argument is a
337
			pointer to a <>.
338
 
339
			With <>, <>, <>, <>, <>, <>,
340
			<>, or <>, has no effect (because of
341
			vararg promotion rules, there is no need to
342
			distinguish between <> and <>).
343
 
344
		o ll
345
			With <>, <>, <>, <>, <>, or
346
			<>, specifies that the argument is a
347
			<> or <>.
348
 
349
			With <>, specifies that the argument is a
350
			pointer to a <>.
351
 
352
		o j
353
			With <>, <>, <>, <>, <>, or
354
			<>, specifies that the argument is an
355
			<> or <>.
356
 
357
			With <>, specifies that the argument is a
358
			pointer to an <>.
359
 
360
		o z
361
			With <>, <>, <>, <>, <>, or
362
			<>, specifies that the argument is a <>.
363
 
364
			With <>, specifies that the argument is a
365
			pointer to a <>.
366
 
367
		o t
368
			With <>, <>, <>, <>, <>, or
369
			<>, specifies that the argument is a
370
			<>.
371
 
372
			With <>, specifies that the argument is a
373
			pointer to a <>.
374
 
375
		o L
376
			With <>, <>, <>, <>, <>, <>,
377
			<>, or <>, specifies that the argument
378
			is a <>.
379
 
380
		o-
381
 
382
	o   <[type]>
383
 
384
		<[type]> specifies what kind of conversion <>
385
		performs.  Here is a table of these:
386
 
387
		o+
388
		o %
389
			Prints the percent character (<<%>>).
390
 
391
		o c
392
			Prints <[arg]> as single character.  If the
393
			<> size specifier is in effect, a multibyte
394
			character is printed.
395
 
396
		o C
397
			Short for <<%lc>>.  A POSIX extension to the C standard.
398
 
399
		o s
400
			Prints the elements of a pointer to <>
401
			until the precision or a null character is
402
			reached.  If the <> size specifier is in
403
			effect, the pointer is to an array of
404
			<>, and the string is converted to
405
			multibyte characters before printing.
406
 
407
		o S
408
			Short for <<%ls>>.  A POSIX extension to the C standard.
409
 
410
		o d or i
411
			Prints a signed decimal integer; takes an
412
			<>.  Leading zeros are inserted as
413
			necessary to reach the precision.  A value of 0 with
414
			a precision of 0 produces an empty string.
415
 
416
		o D
417
			Newlib extension, short for <<%ld>>.
418
 
419
		o o
420
			Prints an unsigned octal integer; takes an
421
			<>.  Leading zeros are inserted as
422
			necessary to reach the precision.  A value of 0 with
423
			a precision of 0 produces an empty string.
424
 
425
		o O
426
			Newlib extension, short for <<%lo>>.
427
 
428
		o u
429
			Prints an unsigned decimal integer; takes an
430
			<>.  Leading zeros are inserted as
431
			necessary to reach the precision.  A value of 0 with
432
			a precision of 0 produces an empty string.
433
 
434
		o U
435
			Newlib extension, short for <<%lu>>.
436
 
437
		o x
438
			Prints an unsigned hexadecimal integer (using
439
			<> as digits beyond <<9>>); takes an
440
			<>.  Leading zeros are inserted as
441
			necessary to reach the precision.  A value of 0 with
442
			a precision of 0 produces an empty string.
443
 
444
		o X
445
			Like <>, but uses <> as digits
446
			beyond <<9>>.
447
 
448
		o f
449
			Prints a signed value of the form
450
			<<[-]9999.9999>>, with the precision
451
			determining how many digits follow the decimal
452
			point; takes a <> (remember that
453
			<> promotes to <> as a vararg).
454
			The low order digit is rounded to even.  If
455
			the precision results in at most DECIMAL_DIG
456
			digits, the result is rounded correctly; if
457
			more than DECIMAL_DIG digits are printed, the
458
			result is only guaranteed to round back to the
459
			original value.
460
 
461
			If the value is infinite, the result is
462
			<>, and no zero padding is performed.  If
463
			the value is not a number, the result is
464
			<>, and no zero padding is performed.
465
 
466
		o F
467
			Like <>, but uses <> and <> for
468
			non-finite numbers.
469
 
470
		o e
471
			Prints a signed value of the form
472
			<<[-]9.9999e[+|-]999>>; takes a <>.
473
			The digit before the decimal point is non-zero
474
			if the value is non-zero.  The precision
475
			determines how many digits appear between
476
			<<.>> and <>, and the exponent always
477
			contains at least two digits.  The value zero
478
			has an exponent of zero.  If the value is not
479
			finite, it is printed like <>.
480
 
481
		o E
482
			Like <>, but using <> to introduce the
483
			exponent, and like <> for non-finite
484
			values.
485
 
486
		o g
487
			Prints a signed value in either <> or <>
488
			form, based on the given value and
489
			precision---an exponent less than -4 or
490
			greater than the precision selects the <>
491
			form.  Trailing zeros and the decimal point
492
			are printed only if necessary; takes a
493
			<>.
494
 
495
		o G
496
			Like <>, except use <> or <> form.
497
 
498
		o a
499
			Prints a signed value of the form
500
			<<[-]0x1.ffffp[+|-]9>>; takes a <>.
501
			The letters <> are used for digits
502
			beyond <<9>>.  The precision determines how
503
			many digits appear after the decimal point.
504
			The exponent contains at least one digit, and
505
			is a decimal value representing the power of
506
			2; a value of 0 has an exponent of 0.
507
			Non-finite values are printed like <>.
508
 
509
		o A
510
			Like <>, except uses <>, <

>, and

511
			<> instead of lower case.
512
 
513
		o n
514
			Takes a pointer to <>, and stores a count
515
			of the number of bytes written so far.  No
516
			output is created.
517
 
518
		o p
519
			Takes a pointer to <>, and prints it in
520
			an implementation-defined format.  This
521
			implementation is similar to <<%#tx>>), except
522
			that <<0x>> appears even for the NULL pointer.
523
 
4921 Serge 524
		o m
525
			Prints the output of <>; no
526
			argument is required.  A GNU extension.
527
 
4349 Serge 528
		o-
529
	O-
530
 
531
        <<_printf_r>>, <<_fprintf_r>>, <<_asprintf_r>>,
532
        <<_sprintf_r>>, <<_snprintf_r>>, <<_asnprintf_r>> are simply
533
        reentrant versions of the functions above.
534
 
535
RETURNS
536
On success, <> and <> return the number of bytes in
537
the output string, except the concluding <> is not counted.
538
<> returns the number of bytes that would be in the output
539
string, except the concluding <> is not counted.  <> and
540
<> return the number of characters transmitted.
541
<> returns the original <[str]> if there was enough room,
542
otherwise it returns an allocated string.
543
 
544
If an error occurs, the result of <>, <>,
545
<>, and <> is a negative value, and the result of
546
<> is NULL.  No error returns occur for <>.  For
547
<> and <>, <> may be set according to
548
<>.  For <> and <>, <> may be set
549
to ENOMEM if allocation fails, and for <>, <> may be
550
set to EOVERFLOW if <[size]> or the output length exceeds INT_MAX.
551
 
552
BUGS
553
The ``''' (quote) flag does not work when locale's thousands_sep is not empty.
554
 
555
PORTABILITY
556
ANSI C requires <>, <>, <>, and
557
<>.  <> and <> are newlib extensions.
558
 
559
The ANSI C standard specifies that implementations must support at
560
least formatted output of up to 509 characters.  This implementation
561
has no inherent limit.
562
 
563
Depending on how newlib was configured, not all format specifiers are
564
supported.
565
 
566
Supporting OS subroutines required: <>, <>, <>,
567
<>, <>, <>, <>.
568
*/
569
 
570
#include <_ansi.h>
571
#include 
572
#include 
573
#ifdef _HAVE_STDC
574
#include 
575
#else
576
#include 
577
#endif
578
#include 
579
#include "local.h"
580
 
581
int
582
#ifdef _HAVE_STDC
583
_DEFUN(_sprintf_r, (ptr, str, fmt),
584
       struct _reent *ptr _AND
4921 Serge 585
       char *__restrict str          _AND
586
       _CONST char *__restrict fmt _DOTS)
4349 Serge 587
#else
588
_sprintf_r(ptr, str, fmt, va_alist)
589
           struct _reent *ptr;
4921 Serge 590
           char *__restrict str;
591
           _CONST char *__restrict fmt;
4349 Serge 592
           va_dcl
593
#endif
594
{
595
  int ret;
596
  va_list ap;
597
  FILE f;
598
 
599
  f._flags = __SWR | __SSTR;
600
  f._bf._base = f._p = (unsigned char *) str;
601
  f._bf._size = f._w = INT_MAX;
602
  f._file = -1;  /* No file. */
603
#ifdef _HAVE_STDC
604
  va_start (ap, fmt);
605
#else
606
  va_start (ap);
607
#endif
608
  ret = _svfprintf_r (ptr, &f, fmt, ap);
609
  va_end (ap);
610
  *f._p = '\0';	/* terminate the string */
611
  return (ret);
612
}
613
 
6099 serge 614
#ifdef _NANO_FORMATTED_IO
615
int
616
_EXFUN(_siprintf_r, (struct _reent *, char *, const char *, ...)
617
       _ATTRIBUTE ((__alias__("_sprintf_r"))));
618
#endif
619
 
4349 Serge 620
#ifndef _REENT_ONLY
621
 
622
int
623
#ifdef _HAVE_STDC
624
_DEFUN(sprintf, (str, fmt),
4921 Serge 625
       char *__restrict str _AND
626
       _CONST char *__restrict fmt _DOTS)
4349 Serge 627
#else
628
sprintf(str, fmt, va_alist)
629
        char *str;
630
        _CONST char *fmt;
631
        va_dcl
632
#endif
633
{
634
  int ret;
635
  va_list ap;
636
  FILE f;
637
 
638
  f._flags = __SWR | __SSTR;
639
  f._bf._base = f._p = (unsigned char *) str;
640
  f._bf._size = f._w = INT_MAX;
641
  f._file = -1;  /* No file. */
642
#ifdef _HAVE_STDC
643
  va_start (ap, fmt);
644
#else
645
  va_start (ap);
646
#endif
647
  ret = _svfprintf_r (_REENT, &f, fmt, ap);
648
  va_end (ap);
649
  *f._p = '\0';	/* terminate the string */
650
  return (ret);
651
}
652
 
6099 serge 653
#ifdef _NANO_FORMATTED_IO
654
int
655
_EXFUN(siprintf, (char *, const char *, ...)
656
       _ATTRIBUTE ((__alias__("sprintf"))));
4349 Serge 657
#endif
6099 serge 658
#endif