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