/*static char *SCCSID = "@(#)debsprtf.c 6.1 90/11/17";*/

/*
 *  DEBSPRTF.C
 *
 *  SCCSID = "@(#)debsprtf.c    13.3 89/10/02"
 *
 *  Simple version of sprintf()
 *
 */

int _sprntf(char *, char *, int *);
int sprintflc(char *, char *, int);
int sprintf(char *, char *, int);

extern char *strcpy(char *, const char *);
extern unsigned int strlen(const char *);
extern char *fmtnum(char *,long,int,int,int,char,int);
extern void lowercase(char *);

int _sprntf( char          *buffer,          /* Destination buffer */
             register char *fmt,             /* Format string */
             int           *parmv )          /* Parameter vector */
{
    long x;                         /* Integer to decode */
    register char *cp;              /* Char pointer */
    char *parm;                     /* Parameter pointer */
    int parmlen;                    /* Parameter length */
    int width;                      /* Field width */
    int precision;                  /* Field precision */
    int ljust;                      /* Left-justify flag */
    int longint;                    /* Long integer flag */
    char pad;                       /* Pad character */
    int r;                          /* Radix */

    cp = buffer;                    /* Initialize pointer */
    while(*fmt != '\0')             /* While not at end of format string */
    {
        if(*fmt++ != '%')           /* If not a parameter specifier */
        {
            *cp++ = fmt[-1];        /* Copy the character */
            continue;               /* Continue */
        }
        if(ljust = (*fmt == '-'))
            ++fmt;                  /* Check for left-justify flag */
        if(*fmt == '0')             /* If zero-padding wanted */
        {
            pad = '0';              /* Set pad character */
            ++fmt;                  /* Skip '0' */
        }
        else
        {
            pad = ' ';              /* Else pad with spaces */
        }
        width = 0;                  /* Initialize field width */
        precision = 32767;          /* Set maximum precision */

        /* Calculate field width */
        while ( *fmt >= '0' && *fmt <= '9' )
            width = width*10 + *fmt++ - '0';

        /* Check if precision field */
        if ( *fmt == '.' )
        {
            ++fmt;
            precision = 0;
            /* Calculate field precision */
            while ( *fmt >= '0' && *fmt <= '9' )
                precision = precision*10 + *fmt++ - '0';
        }

        /* Check for long integer flag */
        if (longint = (*fmt == 'l') )
            ++fmt;
        r = 0;
        switch (*fmt)                       /* Switch on format character */
        {
            default:                        /* Bad format type */

            case '%':                       /* Percent sign */
                parm = fmt;                 /* Set pointer */
                parmlen = 1;                /* Set length */
                break;

            case 'c':                       /* Character */
                parm = (char *) parmv;      /* Set pointer */
                parmlen = 1;                /* Set length */
                ++parmv;                    /* Increment pointer */
                pad = ' ';                  /* Pad always a space */
                break;

            case 's':                       /* String */
                parm = (char *) *parmv;     /* Set pointer */
                parmlen = strlen(parm);     /* Set length */
                ++parmv;                    /* Increment pointer */
                pad = ' ';                  /* Pad always a space */
                break;

            case 'd':                       /* Decimal signed */
            case 'u':                       /* Decimal unsigned */
                r = 10;
                break;

            case 'x':                       /* Hex */
            case 'X':                       /* Hex */
                r = 16;
                break;

            case 'b':                       /* Binary */
                r = 2;
                break;
        }

        if (r != 0)
        {
            if (longint)                     /* If long integer */
            {
                x = *((long *) parmv);
                ++parmv;        /* Skip low word */
            }
            else
            {
                /* Else get short integer */
                x = (long) *parmv & 0xFFFFL;
            }
            ++parmv;                /* Skip integer */
            cp = fmtnum( cp, x, r, width, precision, pad, ljust );
            parmlen = 0;            /* No padding needed */
        }

        if (parmlen)
        {

            /* Calculate needed padding */
            width -= parmlen;

            /* If padding before param */
            if( pad == '0' || !ljust )
            {

                /* Loop to pad field */
                while(width > 0)
                {

                    /* Pad with zeroes or spaces */
                    *cp++ = pad;

                    /* Decrement counter */
                    --width;
                }
            }

            /* Adjust the field width */
            parmlen = parmlen > precision ? precision : parmlen;

            /* Copy the parameter */
            while ( parmlen-- > 0 )
                *cp++ = *parm++;

            /* Pad remainder of field */
            while ( width-- > 0 )
                *cp++ = ' ';
        }
        if ( *fmt != '\0' )
            ++fmt;                      /* Increment pointer */
    }

    *cp = '\0';                             /* Null terminate buffer */

    return(cp - buffer);    /* Return number of characters used */
}




int sprintf( char *buffer,           /* Destination buffer */
             char *fmt,              /* Format string */
             int   parm )            /* First parameter (if any) */
{
        return( _sprntf( buffer, fmt, &parm ) );
}




int sprintflc( char *buffer,            /* Destination buffer */
               char *fmt,               /* Format string */
               int   parm )             /* First parameter (if any) */
{
        int rc = _sprntf( buffer, fmt, &parm );
        lowercase( buffer );              /* Lower case string if option set */
        return( rc );
}
