home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mesa5.zip / mesa5src.zip / MesaDLL / snprintf.cpp < prev    next >
Text File  |  2002-11-27  |  30KB  |  1,054 lines

  1. /*
  2.  * $Id: snprintf.c,v 1.18 2001/01/07 10:57:14 hno Exp $
  3.  */
  4.  
  5. #include "config.h"
  6. #if !HAVE_SNPRINTF || !HAVE_VSNPRINTF
  7.  
  8. //#include "defines.h"
  9.  
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <sys/types.h>
  13. #include <stdarg.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <math.h>
  17. #include <assert.h>
  18.  
  19.  
  20. /* #Default:
  21.    # request_header_max_size 10 KB
  22. */
  23. #define MAX_S_STRING_LENGTH 4096 /* 12k */
  24.  
  25. void fatal(const char *message)
  26. {  printf("\a%s\n",message);
  27. }
  28.  
  29. #define xisascii(x) isascii((unsigned char)x)
  30. #define xisalpha(x) isalpha((unsigned char)x)
  31. #define xisdigit(x) isdigit((unsigned char)x)
  32. #define xislower(x) islower((unsigned char)x)
  33.  
  34. //extern int debugLevels[MAX_DEBUG_SECTIONS];
  35. //extern int _db_level;
  36. //extern void _db_print(const char *,...) PRINTF_FORMAT_ARG1;
  37.  
  38. #ifdef HAVE_CVT
  39.  
  40. #define ap_ecvt ecvt
  41. #define ap_fcvt fcvt
  42. #define ap_gcvt gcvt
  43.  
  44. #else
  45.  
  46. /*
  47.  * cvt.c - IEEE floating point formatting routines for FreeBSD
  48.  * from GNU libc-4.6.27
  49.  */
  50.  
  51. /*
  52.  *    ap_ecvt converts to decimal
  53.  *      the number of digits is specified by ndigit
  54.  *      decpt is set to the position of the decimal point
  55.  *      sign is set to 0 for positive, 1 for negative
  56.  */
  57.  
  58. #define        NDIG    80
  59.  
  60. static char *
  61. ap_cvt(double arg, int ndigits, int *decpt, int *sign, int eflag)
  62. {
  63.     register int r2;
  64.     double fi, fj;
  65.     register char *p, *p1;
  66.     static char buf[NDIG];
  67.  
  68.     if (ndigits >= NDIG - 1)
  69.        ndigits = NDIG - 2;
  70.     r2 = 0;
  71.     *sign = 0;
  72.     p = &buf[0];
  73.     if (arg < 0) {
  74.        *sign = 1;
  75.        arg = -arg;
  76.     }
  77.     arg = modf(arg, &fi);
  78.     p1 = &buf[NDIG];
  79.     /*
  80.      * Do integer part
  81.      */
  82.     if (fi != 0) {
  83.        p1 = &buf[NDIG];
  84.        while (fi != 0) {
  85.            fj = modf(fi / 10, &fi);
  86.            *--p1 = (int) ((fj + .03) * 10) + '0';
  87.            r2++;
  88.        }
  89.        while (p1 < &buf[NDIG])
  90.            *p++ = *p1++;
  91.     } else if (arg > 0) {
  92.        while ((fj = arg * 10) < 1) {
  93.            arg = fj;
  94.            r2--;
  95.        }
  96.     }
  97.     p1 = &buf[ndigits];
  98.     if (eflag == 0)
  99.        p1 += r2;
  100.     *decpt = r2;
  101.     if (p1 < &buf[0]) {
  102.        buf[0] = '\0';
  103.        return (buf);
  104.     }
  105.     while (p <= p1 && p < &buf[NDIG]) {
  106.        arg *= 10;
  107.        arg = modf(arg, &fj);
  108.        *p++ = (int) fj + '0';
  109.     }
  110.     if (p1 >= &buf[NDIG]) {
  111.        buf[NDIG - 1] = '\0';
  112.        return (buf);
  113.     }
  114.     p = p1;
  115.     *p1 += 5;
  116.     while (*p1 > '9') {
  117.        *p1 = '0';
  118.        if (p1 > buf)
  119.            ++ * --p1;
  120.        else {
  121.            *p1 = '1';
  122.            (*decpt)++;
  123.            if (eflag == 0) {
  124.                if (p > buf)
  125.                    *p = '0';
  126.                p++;
  127.            }
  128.        }
  129.     }
  130.     *p = '\0';
  131.     return (buf);
  132. }
  133.  
  134. static char *
  135. ap_ecvt(double arg, int ndigits, int *decpt, int *sign)
  136. {
  137.     return (ap_cvt(arg, ndigits, decpt, sign, 1));
  138. }
  139.  
  140. static char *
  141. ap_fcvt(double arg, int ndigits, int *decpt, int *sign)
  142. {
  143.     return (ap_cvt(arg, ndigits, decpt, sign, 0));
  144. }
  145.  
  146. /*
  147.  * ap_gcvt  - Floating output conversion to
  148.  * minimal length string
  149.  */
  150.  
  151. static char *
  152. ap_gcvt(double number, int ndigit, char *buf)
  153. {
  154.     int sign, decpt;
  155.     register char *p1, *p2;
  156.     register int i;
  157.  
  158.     p1 = ap_ecvt(number, ndigit, &decpt, &sign);
  159.     p2 = buf;
  160.     if (sign)
  161.        *p2++ = '-';
  162.     for (i = ndigit - 1; i > 0 && p1[i] == '0'; i--)
  163.        ndigit--;
  164.     if ((decpt >= 0 && decpt - ndigit > 4)
  165.        || (decpt < 0 && decpt < -3)) {         /* use E-style */
  166.        decpt--;
  167.        *p2++ = *p1++;
  168.        *p2++ = '.';
  169.        for (i = 1; i < ndigit; i++)
  170.            *p2++ = *p1++;
  171.        *p2++ = 'e';
  172.        if (decpt < 0) {
  173.            decpt = -decpt;
  174.            *p2++ = '-';
  175.        } else
  176.            *p2++ = '+';
  177.        if (decpt / 100 > 0)
  178.            *p2++ = decpt / 100 + '0';
  179.        if (decpt / 10 > 0)
  180.            *p2++ = (decpt % 100) / 10 + '0';
  181.        *p2++ = decpt % 10 + '0';
  182.     } else {
  183.        if (decpt <= 0) {
  184.            if (*p1 != '0')
  185.                *p2++ = '.';
  186.            while (decpt < 0) {
  187.                decpt++;
  188.                *p2++ = '0';
  189.            }
  190.        }
  191.        for (i = 1; i <= ndigit; i++) {
  192.            *p2++ = *p1++;
  193.            if (i == decpt)
  194.                *p2++ = '.';
  195.        }
  196.        if (ndigit < decpt) {
  197.            while (ndigit++ < decpt)
  198.                *p2++ = '0';
  199.            *p2++ = '.';
  200.        }
  201.     }
  202.     if (p2[-1] == '.')
  203.        p2--;
  204.     *p2 = '\0';
  205.     return (buf);
  206. }
  207.  
  208. #endif /* HAVE_CVT */
  209.  
  210. typedef enum {
  211.     NO = 0, YES = 1
  212. } boolean_e;
  213.  
  214. #define FALSE                  0
  215. #define TRUE                   1
  216. #define NUL                    '\0'
  217. #define INT_NULL               ((int *)0)
  218. #define WIDE_INT               long
  219.  
  220. typedef WIDE_INT wide_int;
  221. typedef unsigned WIDE_INT u_wide_int;
  222. typedef int bool_int;
  223.  
  224. #define S_NULL                 "(null)"
  225. #define S_NULL_LEN             6
  226.  
  227. #define FLOAT_DIGITS           6
  228. #define EXPONENT_LENGTH                10
  229.  
  230. /*
  231.  * NUM_BUF_SIZE is the size of the buffer used for arithmetic conversions
  232.  *
  233.  * XXX: this is a magic number; do not decrease it
  234.  */
  235. #define NUM_BUF_SIZE           512
  236.  
  237.  
  238. /*
  239.  * Descriptor for buffer area
  240.  */
  241. struct buf_area {
  242.     char *buf_end;
  243.     char *nextb;               /* pointer to next byte to read/write   */
  244. };
  245.  
  246. typedef struct buf_area buffy;
  247.  
  248. /*
  249.  * The INS_CHAR macro inserts a character in the buffer and writes
  250.  * the buffer back to disk if necessary
  251.  * It uses the char pointers sp and bep:
  252.  *      sp points to the next available character in the buffer
  253.  *      bep points to the end-of-buffer+1
  254.  * While using this macro, note that the nextb pointer is NOT updated.
  255.  *
  256.  * NOTE: Evaluation of the c argument should not have any side-effects
  257.  */
  258. #define INS_CHAR( c, sp, bep, cc )     \
  259.            {                           \
  260.                if ( sp < bep )         \
  261.                {                       \
  262.                    *sp++ = c ;         \
  263.                    cc++ ;              \
  264.                }                       \
  265.            }
  266.  
  267. #define NUM( c )                       ( c - '0' )
  268.  
  269. #define STR_TO_DEC( str, num )         \
  270.     num = NUM( *str++ ) ;              \
  271.     while ( xisdigit( *str ) )         \
  272.     {                                  \
  273.        num *= 10 ;                     \
  274.        num += NUM( *str++ ) ;          \
  275.     }
  276.  
  277. /*
  278.  * This macro does zero padding so that the precision
  279.  * requirement is satisfied. The padding is done by
  280.  * adding '0's to the left of the string that is going
  281.  * to be printed.
  282.  */
  283. #define FIX_PRECISION( adjust, precision, s, s_len )   \
  284.     if ( adjust )                                      \
  285.        while ( s_len < precision )                     \
  286.        {                                               \
  287.            *--s = '0' ;                                \
  288.            s_len++ ;                                   \
  289.        }
  290.  
  291. /*
  292.  * Macro that does padding. The padding is done by printing
  293.  * the character ch.
  294.  */
  295. #define PAD( width, len, ch )  do              \
  296.        {                                       \
  297.            INS_CHAR( ch, sp, bep, cc ) ;       \
  298.            width-- ;                           \
  299.        }                                       \
  300.        while ( width > len )
  301.  
  302. /*
  303.  * Prefix the character ch to the string str
  304.  * Increase length
  305.  * Set the has_prefix flag
  306.  */
  307. #define PREFIX( str, length, ch )       *--str = ch ; length++ ; has_prefix = YES
  308.  
  309.  
  310. /*
  311.  * Convert num to its decimal format.
  312.  * Return value:
  313.  *   - a pointer to a string containing the number (no sign)
  314.  *   - len contains the length of the string
  315.  *   - is_negative is set to TRUE or FALSE depending on the sign
  316.  *     of the number (always set to FALSE if is_unsigned is TRUE)
  317.  *
  318.  * The caller provides a buffer for the string: that is the buf_end argument
  319.  * which is a pointer to the END of the buffer + 1 (i.e. if the buffer
  320.  * is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
  321.  */
  322. static char *
  323. conv_10(register wide_int num, register bool_int is_unsigned,
  324.     register bool_int * is_negative, char *buf_end, register int *len)
  325. {
  326.     register char *p = buf_end;
  327.     register u_wide_int magnitude;
  328.  
  329.     if (is_unsigned) {
  330.        magnitude = (u_wide_int) num;
  331.        *is_negative = FALSE;
  332.     } else {
  333.        *is_negative = (num < 0);
  334.  
  335.        /*
  336.         * On a 2's complement machine, negating the most negative integer
  337.         * results in a number that cannot be represented as a signed integer.
  338.         * Here is what we do to obtain the number's magnitude:
  339.         *      a. add 1 to the number
  340.         *      b. negate it (becomes positive)
  341.         *      c. convert it to unsigned
  342.         *      d. add 1
  343.         */
  344.        if (*is_negative) {
  345.            wide_int t = num + 1;
  346.  
  347.            magnitude = ((u_wide_int) - t) + 1;
  348.        } else
  349.            magnitude = (u_wide_int) num;
  350.     }
  351.  
  352.     /*
  353.      * We use a do-while loop so that we write at least 1 digit
  354.      */
  355.     do {
  356.        register u_wide_int new_magnitude = magnitude / 10;
  357.  
  358.        *--p = (char) (magnitude - new_magnitude * 10 + '0');
  359.        magnitude = new_magnitude;
  360.     }
  361.     while (magnitude);
  362.  
  363.     *len = buf_end - p;
  364.     return (p);
  365. }
  366.  
  367.  
  368.  
  369. /*
  370.  * Convert a floating point number to a string formats 'f', 'e' or 'E'.
  371.  * The result is placed in buf, and len denotes the length of the string
  372.  * The sign is returned in the is_negative argument (and is not placed
  373.  * in buf).
  374.  */
  375. static char *
  376. conv_fp(register char format, register double num,
  377.     boolean_e add_dp, int precision, bool_int * is_negative, char *buf, int *len)
  378. {
  379.     register char *s = buf;
  380.     register char *p;
  381.     int decimal_point;
  382.  
  383.     if (format == 'f')
  384.        p = ap_fcvt(num, precision, &decimal_point, is_negative);
  385.     else                       /* either e or E format */
  386.        p = ap_ecvt(num, precision + 1, &decimal_point, is_negative);
  387.  
  388.     /*
  389.      * Check for Infinity and NaN
  390.      */
  391.     if (xisalpha(*p)) {
  392.        *len = strlen(strcpy(buf, p));
  393.        *is_negative = FALSE;
  394.        return (buf);
  395.     }
  396.     if (format == 'f') {
  397.        if (decimal_point <= 0) {
  398.            *s++ = '0';
  399.            if (precision > 0) {
  400.                *s++ = '.';
  401.                while (decimal_point++ < 0)
  402.                    *s++ = '0';
  403.            } else if (add_dp)
  404.                *s++ = '.';
  405.        } else {
  406.            while (decimal_point-- > 0)
  407.                *s++ = *p++;
  408.            if (precision > 0 || add_dp)
  409.                *s++ = '.';
  410.        }
  411.     } else {
  412.        *s++ = *p++;
  413.        if (precision > 0 || add_dp)
  414.            *s++ = '.';
  415.     }
  416.  
  417.     /*
  418.      * copy the rest of p, the NUL is NOT copied
  419.      */
  420.     while (*p)
  421.        *s++ = *p++;
  422.  
  423.     if (format != 'f') {
  424.        char temp[EXPONENT_LENGTH];     /* for exponent conversion */
  425.        int t_len;
  426.        bool_int exponent_is_negative;
  427.  
  428.        *s++ = format;          /* either e or E */
  429.        decimal_point--;
  430.        if (decimal_point != 0) {
  431.            p = conv_10((wide_int) decimal_point, FALSE, &exponent_is_negative,
  432.                &temp[EXPONENT_LENGTH], &t_len);
  433.            *s++ = exponent_is_negative ? '-' : '+';
  434.  
  435.            /*
  436.             * Make sure the exponent has at least 2 digits
  437.             */
  438.            if (t_len == 1)
  439.                *s++ = '0';
  440.            while (t_len--)
  441.                *s++ = *p++;
  442.        } else {
  443.            *s++ = '+';
  444.            *s++ = '0';
  445.            *s++ = '0';
  446.        }
  447.     }
  448.     *len = s - buf;
  449.     return (buf);
  450. }
  451.  
  452.  
  453. /*
  454.  * Convert num to a base X number where X is a power of 2. nbits determines X.
  455.  * For example, if nbits is 3, we do base 8 conversion
  456.  * Return value:
  457.  *      a pointer to a string containing the number
  458.  *
  459.  * The caller provides a buffer for the string: that is the buf_end argument
  460.  * which is a pointer to the END of the buffer + 1 (i.e. if the buffer
  461.  * is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
  462.  */
  463. static char *
  464. conv_p2(register u_wide_int num, register int nbits,
  465.     char format, char *buf_end, register int *len)
  466. {
  467.     register int mask = (1 << nbits) - 1;
  468.     register char *p = buf_end;
  469.     static char low_digits[] = "0123456789abcdef";
  470.     static char upper_digits[] = "0123456789ABCDEF";
  471.     register char *digits = (format == 'X') ? upper_digits : low_digits;
  472.  
  473.     do {
  474.        *--p = digits[num & mask];
  475.        num >>= nbits;
  476.     }
  477.     while (num);
  478.  
  479.     *len = buf_end - p;
  480.     return (p);
  481. }
  482.  
  483.  
  484. /*
  485.  * Do format conversion placing the output in buffer
  486.  */
  487. static int
  488. format_converter(register buffy * odp,  char *fmt,  va_list ap)
  489. {
  490.     char *sp;
  491.     char *bep;
  492.     int cc = 0;
  493.     int i;
  494.  
  495.     char *s = NULL;
  496.     char *q;
  497.     int s_len;
  498.  
  499.     int min_width = 0;
  500.     int precision = 0;
  501.     enum {
  502.        LEFT, RIGHT
  503.     } adjust;
  504.     char pad_char;
  505.     char prefix_char;
  506.  
  507.     double fp_num;
  508.     wide_int i_num = (wide_int) 0;
  509.     u_wide_int ui_num;
  510.  
  511.     char num_buf[NUM_BUF_SIZE];
  512.     char char_buf[2];          /* for printing %% and %<unknown> */
  513. int razIncr=0;
  514. //static int onjopa=0;
  515.     /*
  516.      * Flag variables
  517.      */
  518.     boolean_e is_long;
  519.     boolean_e alternate_form;
  520.     boolean_e print_sign;
  521.     boolean_e print_blank;
  522.     boolean_e adjust_precision;
  523.     boolean_e adjust_width;
  524.     bool_int is_negative;
  525.  
  526.     sp = odp->nextb;
  527.     bep = odp->buf_end;
  528.  
  529. if(fmt == NULL)
  530. {
  531. // if(!onjopa)
  532.   { // printf("\aª«»á ¡«¼Ñα 1\n");
  533.     // onjopa  = 1;
  534.      fatal("snprintf.:format_converter: ª«»á ¡«¼Ñα 1");
  535.   }
  536.   abort();
  537. }
  538.  
  539.     while (*fmt) {
  540.        if (*fmt != '%') {
  541.            INS_CHAR(*fmt, sp, bep, cc);
  542.        } else {
  543.            /*
  544.             * Default variable settings
  545.             */
  546.            adjust = RIGHT;
  547.            alternate_form = print_sign = print_blank = NO;
  548.            pad_char = ' ';
  549.            prefix_char = NUL;
  550.  
  551.  
  552.            fmt++;
  553.            razIncr++;
  554. if(fmt == NULL)
  555. {
  556. // if(!onjopa)
  557.   { // printf("\aª«»á ¡«¼Ñα 2\n");
  558.     // onjopa = 1;
  559.      fatal("snprintf.:format_converter: ª«»á ¡«¼Ñα 2");
  560.   }
  561.    abort();
  562. }
  563. if(razIncr > 10000)
  564. { //if(!onjopa)
  565.    { // printf("ª«»á ¡«¼Ñα 3\n");
  566.      // onjopa = 1;
  567.       fatal("snprintf.:format_converter: ª«»á ¡«¼Ñα 3");
  568.    }
  569.    abort();
  570. }
  571.  
  572.            /*
  573.             * Try to avoid checking for flags, width or precision
  574.             */
  575.            if (xisascii(*fmt) && !xislower(*fmt)) {
  576.                /*
  577.                 * Recognize flags: -, #, BLANK, +
  578.                 */
  579.                for (;; fmt++) {
  580.                    if (*fmt == '-')
  581.                        adjust = LEFT;
  582.                    else if (*fmt == '+')
  583.                        print_sign = YES;
  584.                    else if (*fmt == '#')
  585.                        alternate_form = YES;
  586.                    else if (*fmt == ' ')
  587.                        print_blank = YES;
  588.                    else if (*fmt == '0')
  589.                        pad_char = '0';
  590.                    else
  591.                        break;
  592.                }
  593.  
  594.                /*
  595.                 * Check if a width was specified
  596.                 */
  597.                if (xisdigit(*fmt)) {
  598.                    STR_TO_DEC(fmt, min_width);
  599.                    adjust_width = YES;
  600.                } else if (*fmt == '*') {
  601.                    min_width = va_arg(ap, int);
  602.                    fmt++;
  603.                    adjust_width = YES;
  604.                    if (min_width < 0) {
  605.                        adjust = LEFT;
  606.                        min_width = -min_width;
  607.                    }
  608.                } else
  609.                    adjust_width = NO;
  610.  
  611.                /*
  612.                 * Check if a precision was specified
  613.                 *
  614.                 * XXX: an unreasonable amount of precision may be specified
  615.                 * resulting in overflow of num_buf. Currently we
  616.                 * ignore this possibility.
  617.                 */
  618.                if (*fmt == '.') {
  619.                    adjust_precision = YES;
  620.                    fmt++;
  621.                    if (xisdigit(*fmt)) {
  622.                        STR_TO_DEC(fmt, precision);
  623.                    } else if (*fmt == '*') {
  624.                        precision = va_arg(ap, int);
  625.                        fmt++;
  626.                        if (precision < 0)
  627.                            precision = 0;
  628.                    } else
  629.                        precision = 0;
  630.                } else
  631.                    adjust_precision = NO;
  632.            } else
  633.                adjust_precision = adjust_width = NO;
  634.  
  635.            /*
  636.             * Modifier check
  637.             */
  638.            if (*fmt == 'l') {
  639.                is_long = YES;
  640.                fmt++;
  641.            } else
  642.                is_long = NO;
  643.  
  644.            /*
  645.             * Argument extraction and printing.
  646.             * First we determine the argument type.
  647.             * Then, we convert the argument to a string.
  648.             * On exit from the switch, s points to the string that
  649.             * must be printed, s_len has the length of the string
  650.             * The precision requirements, if any, are reflected in s_len.
  651.             *
  652.             * NOTE: pad_char may be set to '0' because of the 0 flag.
  653.             *   It is reset to ' ' by non-numeric formats
  654.             */
  655.            switch (*fmt) {
  656.            case 'u':
  657.                if (is_long)
  658.                    i_num = va_arg(ap, u_wide_int);
  659.                else
  660.                    i_num = (wide_int) va_arg(ap, unsigned int);
  661.                /*
  662.                 * The rest also applies to other integer formats, so fall
  663.                 * into that case.
  664.                 */
  665.            case 'd':
  666.            case 'i':
  667.                /*
  668.                 * Get the arg if we haven't already.
  669.                 */
  670.                if ((*fmt) != 'u') {
  671.                    if (is_long)
  672.                        i_num = va_arg(ap, wide_int);
  673.                    else
  674.                        i_num = (wide_int) va_arg(ap, int);
  675.                };
  676.                s = conv_10(i_num, (*fmt) == 'u', &is_negative,
  677.                    &num_buf[NUM_BUF_SIZE], &s_len);
  678.                FIX_PRECISION(adjust_precision, precision, s, s_len);
  679.  
  680.                if (*fmt != 'u') {
  681.                    if (is_negative)
  682.                        prefix_char = '-';
  683.                    else if (print_sign)
  684.                        prefix_char = '+';
  685.                    else if (print_blank)
  686.                        prefix_char = ' ';
  687.                }
  688.                break;
  689.  
  690.  
  691.            case 'o':
  692.                if (is_long)
  693.                    ui_num = va_arg(ap, u_wide_int);
  694.                else
  695.                    ui_num = (u_wide_int) va_arg(ap, unsigned int);
  696.                s = conv_p2(ui_num, 3, *fmt,
  697.                    &num_buf[NUM_BUF_SIZE], &s_len);
  698.                FIX_PRECISION(adjust_precision, precision, s, s_len);
  699.                if (alternate_form && *s != '0') {
  700.                    *--s = '0';
  701.                    s_len++;
  702.                }
  703.                break;
  704.  
  705.  
  706.            case 'x':
  707.            case 'X':
  708.                if (is_long)
  709.                    ui_num = (u_wide_int) va_arg(ap, u_wide_int);
  710.                else
  711.                    ui_num = (u_wide_int) va_arg(ap, unsigned int);
  712.                s = conv_p2(ui_num, 4, *fmt,
  713.                    &num_buf[NUM_BUF_SIZE], &s_len);
  714.                FIX_PRECISION(adjust_precision, precision, s, s_len);
  715.                if (alternate_form && i_num != 0) {
  716.                    *--s = *fmt;        /* 'x' or 'X' */
  717.                    *--s = '0';
  718.                    s_len += 2;
  719.                }
  720.                break;
  721.  
  722.  
  723.            case 's':
  724.                s = va_arg(ap, char *);
  725.                if (s != NULL) {
  726. /* --- */
  727. /* #Default:
  728.    # request_header_max_size 10 KB
  729. */
  730.  
  731. /* -- */
  732. //if(!onjopa)
  733.       {    int ii, len;
  734.            for(ii=0;ii<MAX_S_STRING_LENGTH;ii++)
  735.            {  if(s[ii] == 0)
  736.                 break;
  737.            }
  738.            if(ii >= MAX_S_STRING_LENGTH) /* XXX! but what to do ? EK */
  739.            {
  740. //              s_len = MAX_S_STRING_LENGTH-1;
  741. //              s[s_len] = 0;
  742. //              printf("\aª«»á ¡«¼Ñα 5\n");
  743. //              onjopa = 1;
  744. //              debug(1, 0) ("snprintf.:format_converter:WARNING: j#5, first %i bytes of buffer:\n>%s<\n",s_len,s);
  745. ////          debug(1, 0) ("snprintf.:format_converter::WARNING: ª«»á ¡«¼Ñα 5. ¡Ñ óδ»«½¡Ñ¡¡« πß½«ó¿Ñ %p <= %p < %p",odp->nextb,s,odp->buf_end);
  746.           fatal("snprintf.:format_converter: ª«»á ¡«¼Ñα 5");
  747.            } else {
  748.                s_len = ii;
  749.            }
  750.        }
  751. /* -- */
  752. //                   s_len = strlen(s);
  753.                    if (adjust_precision && precision < s_len)
  754.                        s_len = precision;
  755.                } else {
  756.                    s = S_NULL;
  757.                    s_len = S_NULL_LEN;
  758.                }
  759.                pad_char = ' ';
  760.                break;
  761.  
  762.  
  763.            case 'f':
  764.            case 'e':
  765.            case 'E':
  766.                fp_num = va_arg(ap, double);
  767.  
  768.                s = conv_fp(*fmt, fp_num, alternate_form,
  769.                    (adjust_precision == NO) ? FLOAT_DIGITS : precision,
  770.                    &is_negative, &num_buf[1], &s_len);
  771.                if (is_negative)
  772.                    prefix_char = '-';
  773.                else if (print_sign)
  774.                    prefix_char = '+';
  775.                else if (print_blank)
  776.                    prefix_char = ' ';
  777.                break;
  778.  
  779.  
  780.            case 'g':
  781.            case 'G':
  782.                if (adjust_precision == NO)
  783.                    precision = FLOAT_DIGITS;
  784.                else if (precision == 0)
  785.                    precision = 1;
  786.                /* We use &num_buf[ 1 ], so that we have room for the sign */
  787.                s = ap_gcvt(va_arg(ap, double), precision, &num_buf[1]);
  788.                if (*s == '-')
  789.                    prefix_char = *s++;
  790.                else if (print_sign)
  791.                    prefix_char = '+';
  792.                else if (print_blank)
  793.                    prefix_char = ' ';
  794. if(s == NULL)
  795. {
  796. //  if(!onjopa)
  797.   { // printf("ª«»á ¡«¼Ñα 6\n");
  798.     // onjopa = 1;
  799.      fatal("snprintf.:format_converter: ª«»á ¡«¼Ñα 6");
  800.   }
  801.    abort();
  802. }
  803. /* -- */
  804. {  int ii;
  805.    for(ii=0;ii<1024;ii++)
  806.    {  if(s[ii] == 0)
  807.                 break;
  808.    }
  809. if(ii >= 1000)
  810. {
  811. //   if(!onjopa)
  812.    {  //  printf("ª«»á ¡«¼Ñα 7\n");
  813.       //  onjopa = 1;
  814.         fatal("snprintf.:format_converter: ª«»á ¡«¼Ñα 7");
  815.    }
  816.    abort();
  817. }
  818.  
  819. }
  820. /* -- */
  821.  
  822.                s_len = strlen(s);
  823.  
  824.                if (alternate_form && (q = strchr(s, '.')) == NULL)
  825.                    s[s_len++] = '.';
  826.                if (*fmt == 'G' && (q = strchr(s, 'e')) != NULL)
  827.                    *q = 'E';
  828.                break;
  829.  
  830.  
  831.            case 'c':
  832.                char_buf[0] = (char) (va_arg(ap, int));
  833.                s = &char_buf[0];
  834.                s_len = 1;
  835.                pad_char = ' ';
  836.                break;
  837.  
  838.  
  839.            case '%':
  840.                char_buf[0] = '%';
  841.                s = &char_buf[0];
  842.                s_len = 1;
  843.                pad_char = ' ';
  844.                break;
  845.  
  846.  
  847.            case 'n':
  848.                *(va_arg(ap, int *)) = cc;
  849.                break;
  850.  
  851.                /*
  852.                 * Always extract the argument as a "char *" pointer. We
  853.                 * should be using "void *" but there are still machines
  854.                 * that don't understand it.
  855.                 * If the pointer size is equal to the size of an unsigned
  856.                 * integer we convert the pointer to a hex number, otherwise
  857.                 * we print "%p" to indicate that we don't handle "%p".
  858.                 */
  859.            case 'p':
  860.                ui_num = (u_wide_int) va_arg(ap, char *);
  861.  
  862.                if (sizeof(char *) <= sizeof(u_wide_int))
  863.                         s = conv_p2(ui_num, 4, 'x',
  864.                        &num_buf[NUM_BUF_SIZE], &s_len);
  865.                else {
  866.                    s = "%p";
  867.                    s_len = 2;
  868.                }
  869.                pad_char = ' ';
  870.                break;
  871.  
  872.  
  873.            case NUL:
  874.                /*
  875.                 * The last character of the format string was %.
  876.                 * We ignore it.
  877.                 */
  878.                continue;
  879.  
  880.  
  881.                /*
  882.                 * The default case is for unrecognized %'s.
  883.                 * We print %<char> to help the user identify what
  884.                 * option is not understood.
  885.                 * This is also useful in case the user wants to pass
  886.                 * the output of format_converter to another function
  887.                 * that understands some other %<char> (like syslog).
  888.                 * Note that we can't point s inside fmt because the
  889.                 * unknown <char> could be preceded by width etc.
  890.                 */
  891.            default:
  892.                char_buf[0] = '%';
  893.                char_buf[1] = *fmt;
  894.                s = char_buf;
  895.                s_len = 2;
  896.                pad_char = ' ';
  897.                break;
  898.            }
  899.  
  900.            if (prefix_char != NUL) {
  901.                *--s = prefix_char;
  902.                s_len++;
  903.            }
  904.            if (adjust_width && adjust == RIGHT && min_width > s_len) {
  905.                if (pad_char == '0' && prefix_char != NUL) {
  906.                    INS_CHAR(*s, sp, bep, cc)
  907.                        s++;
  908.                    s_len--;
  909.                    min_width--;
  910.                }
  911.                PAD(min_width, s_len, pad_char);
  912.            }
  913.            /*
  914.             * Print the string s.
  915.             */
  916.            for (i = s_len; i != 0; i--) {
  917.                INS_CHAR(*s, sp, bep, cc);
  918.                s++;
  919.            }
  920.  
  921.            if (adjust_width && adjust == LEFT && min_width > s_len)
  922.                PAD(min_width, s_len, pad_char);
  923.        }
  924.        fmt++;
  925.     }
  926.     odp->nextb = sp;
  927.     return (cc);
  928. }
  929.  
  930.  
  931. /*
  932.  * This is the general purpose conversion function.
  933.  * Must be called with len >= 0, but we cannot assert() that
  934.  * because size_t is unsigned on some platforms
  935.  */
  936. static void
  937. strx_printv(int *ccp, char *buf, size_t len, const char *format,
  938.     va_list ap)
  939. {
  940.     buffy od;
  941.     int cc;
  942.  
  943. #if OLD_CODE
  944.     /*
  945.      * First initialize the descriptor
  946.      * Notice that if no length is given, we initialize buf_end to the
  947.      * highest possible address.
  948.      */
  949.     od.buf_end = len ? &buf[len] : (char *) ~0;
  950. #else
  951.     od.buf_end = &buf[len];
  952. #endif
  953.     od.nextb = buf;
  954.  
  955.     /*
  956.      * Do the conversion
  957.      */
  958.     cc = format_converter(&od, (char *)format, ap);
  959.     if (len == 0 || od.nextb <= od.buf_end)
  960.        *(od.nextb) = '\0';
  961.     if (ccp)
  962.        *ccp = cc;
  963. }
  964. #endif
  965.  
  966. #if !HAVE_SNPRINTF
  967. /*
  968.  * if len == 0, silently return
  969.  */
  970. int
  971. snprintf(char *buf, size_t len, const char *format,...)
  972. {
  973.     int cc = 0;
  974.     va_list ap;
  975.     va_start(ap, format);
  976.     if (len > 0)
  977.        strx_printv(&cc, buf, (len - 1), format, ap);
  978.     va_end(ap);
  979.     return (cc);
  980. }
  981. #endif
  982.  
  983. #if !HAVE_VSNPRINTF
  984. /*
  985.  * if len == 0, silently return
  986.  */
  987. int
  988. vsnprintf(char *buf, size_t len, const char *format, va_list ap)
  989. {
  990.     int cc = 0;
  991.     if (len > 0)
  992.        strx_printv(&cc, buf, (len - 1), format, ap);
  993.     return (cc);
  994. }
  995. #endif
  996.  
  997.  
  998. /* ====================================================================
  999.  * Copyright (c) 1995-1997 The Apache Group.  All rights reserved.
  1000.  *
  1001.  * Redistribution and use in source and binary forms, with or without
  1002.  * modification, are permitted provided that the following conditions
  1003.  * are met:
  1004.  *
  1005.  * 1. Redistributions of source code must retain the above copyright
  1006.  *    notice, this list of conditions and the following disclaimer.
  1007.  *
  1008.  * 2. Redistributions in binary form must reproduce the above copyright
  1009.  *    notice, this list of conditions and the following disclaimer in
  1010.  *    the documentation and/or other materials provided with the
  1011.  *    distribution.
  1012.  *
  1013.  * 3. All advertising materials mentioning features or use of this
  1014.  *    software must display the following acknowledgment:
  1015.  *    "This product includes software developed by the Apache Group
  1016.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  1017.  *
  1018.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  1019.  *    endorse or promote products derived from this software without
  1020.  *    prior written permission.
  1021.  *
  1022.  * 5. Redistributions of any form whatsoever must retain the following
  1023.  *    acknowledgment:
  1024.  *    "This product includes software developed by the Apache Group
  1025.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  1026.  *
  1027.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  1028.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1029.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  1030.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  1031.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  1032.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  1033.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  1034.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1035.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  1036.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  1037.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  1038.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  1039.  * ====================================================================
  1040.  *
  1041.  * This software consists of voluntary contributions made by many
  1042.  * individuals on behalf of the Apache Group and was originally based
  1043.  * on public domain software written at the National Center for
  1044.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  1045.  * For more information on the Apache Group and the Apache HTTP server
  1046.  * project, please see <http://www.apache.org/>.
  1047.  *
  1048.  * This code is based on, and used with the permission of, the
  1049.  * SIO stdio-replacement strx_* functions by Panos Tsirigotis
  1050.  * <panos@alumni.cs.colorado.edu> for xinetd.
  1051.  */
  1052.  
  1053.  
  1054.