home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / compiler / clib / printf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-28  |  10.6 KB  |  287 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: printf.c,v 1.7 1997/01/28 17:03:00 digulla Exp $
  4.  
  5.     Desc: ANSI C function printf()
  6.     Lang: english
  7. */
  8.  
  9. /*****************************************************************************
  10.  
  11.     NAME */
  12. #include <stdio.h>
  13. #include <stdarg.h>
  14.  
  15.     int printf (
  16.  
  17. /*  SYNOPSIS */
  18.     const char * format,
  19.     ...)
  20.  
  21. /*  FUNCTION
  22.     Formats a list of arguments and prints them to standard out.
  23.  
  24.     The format string is composed of zero or more directives: ordinary
  25.     characters (not %), which are copied unchanged to the output
  26.     stream; and conversion specifications, each of which results in
  27.     fetching zero or more subsequent arguments Each conversion
  28.     specification is introduced by the character %. The arguments must
  29.     correspond properly (after type promotion) with the conversion
  30.     specifier. After the %, the following appear in sequence:
  31.  
  32.     \begin{itemize}
  33.     \item Zero or more of the following flags:
  34.  
  35.     \begin{description}
  36.     \item{#} specifying that the value should be converted to an
  37.     ``alternate form''. For c, d, i, n, p, s, and u conversions, this
  38.     option has no effect. For o conversions, the precision of the
  39.     number is increased to force the first character of the output
  40.     string to a zero (except if a zero value is printed with an
  41.     explicit precision of zero). For x and X conversions, a non-zero
  42.     result has the string `0x' (or `0X' for X conversions) prepended to
  43.     it. For e, E, f, g, and G conversions, the result will always
  44.     contain a decimal point, even if no digits follow it (normally, a
  45.     decimal point appears in the results of those conversions only if a
  46.     digit follows). For g and G conversions, trailing zeros are not
  47.     removed from the result as they would otherwise be.
  48.  
  49.     \item{0} specifying zero padding. For all conversions except n, the
  50.     converted value is padded on the left with zeros rather than
  51.     blanks. If a precision is given with a numeric conversion (d, i, o,
  52.     u, i, x, and X), the 0 flag is ignored.
  53.  
  54.     \item{-} (a negative field width flag) indicates the converted
  55.     value is to be left adjusted on the field boundary. Except for n
  56.     conversions, the converted value is padded on the right with
  57.     blanks, rather than on the left with blanks or zeros. A -
  58.     overrides a 0 if both are given.
  59.  
  60.     \item{ } (a space) specifying that a blank should be left before a
  61.     positive number produced by a signed conversion (d, e, E, f, g, G,
  62.     or i). + specifying that a sign always be placed before a number
  63.     produced by a signed conversion. A + overrides a space if both are
  64.     used.
  65.  
  66.     \item{'} specifying that in a numerical argument the output is to
  67.     be grouped if the locale information indicates any. Note that many
  68.     versions of gcc cannot parse this option and will issue a warning.
  69.  
  70.     \end{description}
  71.  
  72.     \item An optional decimal digit string specifying a minimum field
  73.     width. If the converted value has fewer characters than the field
  74.     width, it will be padded with spaces on the left (or right, if the
  75.     left-adjustment flag has been given) to fill out the field width.
  76.  
  77.     \item An optional precision, in the form of a period (`.') followed
  78.     by an optional digit string. If the digit string is omitted, the
  79.     precision is taken as zero. This gives the minimum number of digits
  80.     to appear for d, i, o, u, x, and X conversions, the number of
  81.     digits to appear after the decimal-point for e, E, and f
  82.     conversions, the maximum number of significant digits for g and G
  83.     conversions, or the maximum number of characters to be printed from
  84.     a string for s conversions.
  85.  
  86.     \item The optional character h, specifying that a following d, i,
  87.     o, u, x, or X conversion corresponds to a short int or unsigned
  88.     short int argument, or that a following n conversion corresponds to
  89.     a pointer to a short int argument.
  90.  
  91.     \item The optional character l (ell) specifying that a following d,
  92.     i, o, u, x, or X conversion applies to a pointer to a long int or
  93.     unsigned long int argument, or that a following n conversion
  94.     corresponds to a pointer to a long int argument. Linux provides a
  95.     non ANSI compliant use of two l flags as a synonym to q or L. Thus
  96.     ll can be used in combination with float conversions. This usage
  97.     is, however, strongly discouraged.
  98.  
  99.     \item The character L specifying that a following e, E,
  100.     f, g, or G conversion corresponds to a long double
  101.     argument, or a following d, i, o, u, x, or X conversion corresponds to a long long argument. Note
  102.     that long long is not specified in ANSI C and
  103.     therefore not portable to all architectures.
  104.  
  105.     \item The optional character q. This is equivalent to L. See the
  106.     STANDARDS and BUGS sections for comments on the use of ll, L, and
  107.     q.
  108.  
  109.     \item A Z character specifying that the following integer (d, i, o,
  110.     u, i, x, and X), conversion corresponds to a size_t argument.
  111.  
  112.     \item A character that specifies the type of conversion to be
  113.     applied.
  114.  
  115.     A field width or precision, or both, may be indicated by an
  116.     asterisk `*' instead of a digit string. In this case, an int
  117.     argument supplies the field width or precision. A negative field
  118.     width is treated as a left adjustment flag followed by a positive
  119.     field width; a negative precision is treated as though it were
  120.     missing.
  121.  
  122.     The conversion specifiers and their meanings are:
  123.  
  124.     \begin{description}
  125.     \item{diouxX} The int (or appropriate variant) argument is
  126.     converted to signed decimal (d and i), unsigned octal (o, unsigned
  127.     decimal (u, or unsigned hexadecimal (x and X) notation. The letters
  128.     abcdef are used for x conversions; the letters ABCDEF are used for
  129.     X conversions. The precision, if any, gives the minimum number of
  130.     digits that must appear; if the converted value requires fewer
  131.     digits, it is padded on the left with zeros.
  132.  
  133.     \item{eE} The double argument is rounded and converted in the style
  134.     [<->]d.dddedd where there is one digit before the decimal-point
  135.     character and the number of digits after it is equal to the
  136.     precision; if the precision is missing, it is taken as 6; if the
  137.     precision is zero, no decimal-point character appears. An E
  138.     conversion uses the letter E (rather than e) to introduce the
  139.     exponent. The exponent always contains at least two digits; if the
  140.     value is zero, the exponent is 00.
  141.  
  142.     \item{f} The double argument is rounded and converted to decimal
  143.     notation in the style [-]ddd.ddd, where the number of digits after
  144.     the decimal-point character is equal to the precision
  145.     specification. If the precision is missing, it is taken as 6; if
  146.     the precision is explicitly zero, no decimal-point character
  147.     appears. If a decimal point appears, at least one digit appears
  148.     before it.
  149.  
  150.     \item{g} The double argument is converted in style f or e (or E for
  151.     G conversions). The precision specifies the number of significant
  152.     digits. If the precision is missing, 6 digits are given; if the
  153.     precision is zero, it is treated as 1. Style e is used if the
  154.     exponent from its conversion is less than -4 or greater than or
  155.     equal to the precision. Trailing zeros are removed from the
  156.     fractional part of the result; a decimal point appears only if it
  157.     is followed by at least one digit.
  158.  
  159.     \item{c} The int argument is converted to an unsigned char, and the
  160.     resulting character is written.
  161.  
  162.     \item{s} The ``char *'' argument is expected to be a pointer to an
  163.     array of character type (pointer to a string). Characters from the
  164.     array are written up to (but not including) a terminating NUL
  165.     character; if a precision is specified, no more than the number
  166.     specified are written. If a precision is given, no null character
  167.     need be present; if the precision is not specified, or is greater
  168.     than the size of the array, the array must contain a terminating
  169.     NUL character.
  170.  
  171.     \item{p} The ``void *'' pointer argument is printed in hexadecimal
  172.     (as if by %#x or %#lx).
  173.  
  174.     \item{n} The number of characters written so far is stored into the
  175.     integer indicated by the ``int *'' (or variant) pointer argument.
  176.     No argument is converted.
  177.  
  178.     \item{%} A `%' is written. No argument is converted. The complete
  179.     conversion specification is `%%'.
  180.  
  181.     \end{description}
  182.     \end{itemize}
  183.  
  184.     In no case does a non-existent or small field width cause
  185.     truncation of a field; if the result of a conversion is wider than
  186.     the field width, the field is expanded to contain the conversion
  187.     result.
  188.  
  189.     INPUTS
  190.     format - Format string as described above
  191.     ... - Arguments for the format string
  192.  
  193.     RESULT
  194.     The number of characters written to stdout or EOF on error.
  195.  
  196.     NOTES
  197.  
  198.     EXAMPLE
  199.     To print a date and time in the form `Sunday, July 3,
  200.     10:02', where weekday and month are pointers to strings:
  201.  
  202.         #include <stdio.h>
  203.  
  204.         fprintf (stdout, "%s, %s %d, %.2d:%.2d\n",
  205.             weekday, month, day, hour, min);
  206.  
  207.     To print to five decimal places:
  208.  
  209.         #include <math.h>
  210.         #include <stdio.h>
  211.  
  212.         fprintf (stdout, "pi = %.5f\n", 4 * atan(1.0));
  213.  
  214.     To allocate a 128 byte string and print into it:
  215.  
  216.         #include <stdio.h>
  217.         #include <stdlib.h>
  218.         #include <stdarg.h>
  219.  
  220.         char *newfmt(const char *fmt, ...)
  221.         {
  222.         char *p;
  223.         va_list ap;
  224.  
  225.         if ((p = malloc(128)) == NULL)
  226.             return (NULL);
  227.  
  228.         va_start(ap, fmt);
  229.  
  230.         (void) vsnprintf(p, 128, fmt, ap);
  231.  
  232.         va_end(ap);
  233.  
  234.         return (p);
  235.         }
  236.  
  237.     BUGS
  238.     All functions are fully ANSI C3.159-1989 conformant, but provide
  239.     the additional flags q, Z and ' as well as an additional behaviour
  240.     of the L and l flags. The latter may be considered to be a bug, as
  241.     it changes the behaviour of flags defined in ANSI C3.159-1989.
  242.  
  243.     The effect of padding the %p format with zeros (either by the 0
  244.     flag or by specifying a precision), and the benign effect (i.e.,
  245.     none) of the # flag on %n and %p conversions, as well as
  246.     nonsensical combinations such as are not standard; such
  247.     combinations should be avoided.
  248.  
  249.     Some combinations of flags defined by ANSI C are not making sense
  250.     in ANSI C (e.g. %Ld). While they may have a well-defined behaviour
  251.     on Linux, this need not to be so on other architectures. Therefore
  252.     it usually is better to use flags that are not defined by ANSI C at
  253.     all, i.e. use q instead of L in combination with diouxX conversions
  254.     or ll. The usage of q is not the same as on BSD 4.4, as it may be
  255.     used in float conversions equivalently to L.
  256.  
  257.     Because sprintf and vsprintf assume an infinitely long string,
  258.     callers must be careful not to overflow the actual space; this is
  259.     often impossible to assure.
  260.  
  261.     SEE ALSO
  262.     fprintf(), vprintf(), vfprintf(), sprintf(), vsprintf(),
  263.     vnsprintf()
  264.  
  265.     INTERNALS
  266.  
  267.     HISTORY
  268.     15.10.1996 digulla created
  269.     11.12.1996 digulla added manual after printf(3C) man page
  270.  
  271. ******************************************************************************/
  272. {
  273.     int     retval;
  274.     va_list args;
  275.  
  276.     va_start (args, format);
  277.  
  278.     retval = vfprintf (stdout, format, args);
  279.  
  280.     va_end (args);
  281.  
  282.     fflush (stdout);
  283.  
  284.     return retval;
  285. } /* printf */
  286.  
  287.