OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [lib/] [vsprintf.c] - Diff between revs 1275 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 1275 Rev 1765
/*
/*
 *  linux/lib/vsprintf.c
 *  linux/lib/vsprintf.c
 *
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *  Copyright (C) 1991, 1992  Linus Torvalds
 */
 */
 
 
/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
/*
/*
 * Wirzenius wrote this portably, Torvalds fucked it up :-)
 * Wirzenius wrote this portably, Torvalds fucked it up :-)
 */
 */
 
 
/*
/*
 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
 * - changed to provide snprintf and vsnprintf functions
 * - changed to provide snprintf and vsnprintf functions
 */
 */
 
 
#include <stdarg.h>
#include <stdarg.h>
#include <linux/types.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/ctype.h>
#include <linux/kernel.h>
#include <linux/kernel.h>
 
 
#include <asm/div64.h>
#include <asm/div64.h>
#include <asm/page.h>
#include <asm/page.h>
 
 
/**
/**
 * simple_strtoul - convert a string to an unsigned long
 * simple_strtoul - convert a string to an unsigned long
 * @cp: The start of the string
 * @cp: The start of the string
 * @endp: A pointer to the end of the parsed string will be placed here
 * @endp: A pointer to the end of the parsed string will be placed here
 * @base: The number base to use
 * @base: The number base to use
 */
 */
unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
{
{
        unsigned long result = 0,value;
        unsigned long result = 0,value;
 
 
        if (!base) {
        if (!base) {
                base = 10;
                base = 10;
                if (*cp == '0') {
                if (*cp == '0') {
                        base = 8;
                        base = 8;
                        cp++;
                        cp++;
                        if ((*cp == 'x') && isxdigit(cp[1])) {
                        if ((*cp == 'x') && isxdigit(cp[1])) {
                                cp++;
                                cp++;
                                base = 16;
                                base = 16;
                        }
                        }
                }
                }
        }
        }
        while (isxdigit(*cp) &&
        while (isxdigit(*cp) &&
               (value = isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
               (value = isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
                result = result*base + value;
                result = result*base + value;
                cp++;
                cp++;
        }
        }
        if (endp)
        if (endp)
                *endp = (char *)cp;
                *endp = (char *)cp;
        return result;
        return result;
}
}
 
 
/**
/**
 * simple_strtol - convert a string to a signed long
 * simple_strtol - convert a string to a signed long
 * @cp: The start of the string
 * @cp: The start of the string
 * @endp: A pointer to the end of the parsed string will be placed here
 * @endp: A pointer to the end of the parsed string will be placed here
 * @base: The number base to use
 * @base: The number base to use
 */
 */
long simple_strtol(const char *cp,char **endp,unsigned int base)
long simple_strtol(const char *cp,char **endp,unsigned int base)
{
{
        if(*cp=='-')
        if(*cp=='-')
                return -simple_strtoul(cp+1,endp,base);
                return -simple_strtoul(cp+1,endp,base);
        return simple_strtoul(cp,endp,base);
        return simple_strtoul(cp,endp,base);
}
}
 
 
/**
/**
 * simple_strtoull - convert a string to an unsigned long long
 * simple_strtoull - convert a string to an unsigned long long
 * @cp: The start of the string
 * @cp: The start of the string
 * @endp: A pointer to the end of the parsed string will be placed here
 * @endp: A pointer to the end of the parsed string will be placed here
 * @base: The number base to use
 * @base: The number base to use
 */
 */
unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
{
{
        unsigned long long result = 0,value;
        unsigned long long result = 0,value;
 
 
        if (!base) {
        if (!base) {
                base = 10;
                base = 10;
                if (*cp == '0') {
                if (*cp == '0') {
                        base = 8;
                        base = 8;
                        cp++;
                        cp++;
                        if ((*cp == 'x') && isxdigit(cp[1])) {
                        if ((*cp == 'x') && isxdigit(cp[1])) {
                                cp++;
                                cp++;
                                base = 16;
                                base = 16;
                        }
                        }
                }
                }
        }
        }
        while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
        while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
            ? toupper(*cp) : *cp)-'A'+10) < base) {
            ? toupper(*cp) : *cp)-'A'+10) < base) {
                result = result*base + value;
                result = result*base + value;
                cp++;
                cp++;
        }
        }
        if (endp)
        if (endp)
                *endp = (char *)cp;
                *endp = (char *)cp;
        return result;
        return result;
}
}
 
 
/**
/**
 * simple_strtoll - convert a string to a signed long long
 * simple_strtoll - convert a string to a signed long long
 * @cp: The start of the string
 * @cp: The start of the string
 * @endp: A pointer to the end of the parsed string will be placed here
 * @endp: A pointer to the end of the parsed string will be placed here
 * @base: The number base to use
 * @base: The number base to use
 */
 */
long long simple_strtoll(const char *cp,char **endp,unsigned int base)
long long simple_strtoll(const char *cp,char **endp,unsigned int base)
{
{
        if(*cp=='-')
        if(*cp=='-')
                return -simple_strtoull(cp+1,endp,base);
                return -simple_strtoull(cp+1,endp,base);
        return simple_strtoull(cp,endp,base);
        return simple_strtoull(cp,endp,base);
}
}
 
 
static int skip_atoi(const char **s)
static int skip_atoi(const char **s)
{
{
        int i=0;
        int i=0;
 
 
        while (isdigit(**s))
        while (isdigit(**s))
                i = i*10 + *((*s)++) - '0';
                i = i*10 + *((*s)++) - '0';
        return i;
        return i;
}
}
 
 
#define ZEROPAD 1               /* pad with zero */
#define ZEROPAD 1               /* pad with zero */
#define SIGN    2               /* unsigned/signed long */
#define SIGN    2               /* unsigned/signed long */
#define PLUS    4               /* show plus */
#define PLUS    4               /* show plus */
#define SPACE   8               /* space if plus */
#define SPACE   8               /* space if plus */
#define LEFT    16              /* left justified */
#define LEFT    16              /* left justified */
#define SPECIAL 32              /* 0x */
#define SPECIAL 32              /* 0x */
#define LARGE   64              /* use 'ABCDEF' instead of 'abcdef' */
#define LARGE   64              /* use 'ABCDEF' instead of 'abcdef' */
 
 
static char * number(char * buf, char * end, long long num, int base, int size, int precision, int type)
static char * number(char * buf, char * end, long long num, int base, int size, int precision, int type)
{
{
        char c,sign,tmp[66];
        char c,sign,tmp[66];
        const char *digits;
        const char *digits;
        static const char small_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
        static const char small_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
        static const char large_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        static const char large_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int i;
        int i;
 
 
        digits = (type & LARGE) ? large_digits : small_digits;
        digits = (type & LARGE) ? large_digits : small_digits;
        if (type & LEFT)
        if (type & LEFT)
                type &= ~ZEROPAD;
                type &= ~ZEROPAD;
        if (base < 2 || base > 36)
        if (base < 2 || base > 36)
                return 0;
                return 0;
        c = (type & ZEROPAD) ? '0' : ' ';
        c = (type & ZEROPAD) ? '0' : ' ';
        sign = 0;
        sign = 0;
        if (type & SIGN) {
        if (type & SIGN) {
                if (num < 0) {
                if (num < 0) {
                        sign = '-';
                        sign = '-';
                        num = -num;
                        num = -num;
                        size--;
                        size--;
                } else if (type & PLUS) {
                } else if (type & PLUS) {
                        sign = '+';
                        sign = '+';
                        size--;
                        size--;
                } else if (type & SPACE) {
                } else if (type & SPACE) {
                        sign = ' ';
                        sign = ' ';
                        size--;
                        size--;
                }
                }
        }
        }
        if (type & SPECIAL) {
        if (type & SPECIAL) {
                if (base == 16)
                if (base == 16)
                        size -= 2;
                        size -= 2;
                else if (base == 8)
                else if (base == 8)
                        size--;
                        size--;
        }
        }
        i = 0;
        i = 0;
        if (num == 0)
        if (num == 0)
                tmp[i++]='0';
                tmp[i++]='0';
        else while (num != 0)
        else while (num != 0)
                tmp[i++] = digits[do_div(num,base)];
                tmp[i++] = digits[do_div(num,base)];
        if (i > precision)
        if (i > precision)
                precision = i;
                precision = i;
        size -= precision;
        size -= precision;
        if (!(type&(ZEROPAD+LEFT))) {
        if (!(type&(ZEROPAD+LEFT))) {
                while(size-->0) {
                while(size-->0) {
                        if (buf <= end)
                        if (buf <= end)
                                *buf = ' ';
                                *buf = ' ';
                        ++buf;
                        ++buf;
                }
                }
        }
        }
        if (sign) {
        if (sign) {
                if (buf <= end)
                if (buf <= end)
                        *buf = sign;
                        *buf = sign;
                ++buf;
                ++buf;
        }
        }
        if (type & SPECIAL) {
        if (type & SPECIAL) {
                if (base==8) {
                if (base==8) {
                        if (buf <= end)
                        if (buf <= end)
                                *buf = '0';
                                *buf = '0';
                        ++buf;
                        ++buf;
                } else if (base==16) {
                } else if (base==16) {
                        if (buf <= end)
                        if (buf <= end)
                                *buf = '0';
                                *buf = '0';
                        ++buf;
                        ++buf;
                        if (buf <= end)
                        if (buf <= end)
                                *buf = digits[33];
                                *buf = digits[33];
                        ++buf;
                        ++buf;
                }
                }
        }
        }
        if (!(type & LEFT)) {
        if (!(type & LEFT)) {
                while (size-- > 0) {
                while (size-- > 0) {
                        if (buf <= end)
                        if (buf <= end)
                                *buf = c;
                                *buf = c;
                        ++buf;
                        ++buf;
                }
                }
        }
        }
        while (i < precision--) {
        while (i < precision--) {
                if (buf <= end)
                if (buf <= end)
                        *buf = '0';
                        *buf = '0';
                ++buf;
                ++buf;
        }
        }
        while (i-- > 0) {
        while (i-- > 0) {
                if (buf <= end)
                if (buf <= end)
                        *buf = tmp[i];
                        *buf = tmp[i];
                ++buf;
                ++buf;
        }
        }
        while (size-- > 0) {
        while (size-- > 0) {
                if (buf <= end)
                if (buf <= end)
                        *buf = ' ';
                        *buf = ' ';
                ++buf;
                ++buf;
        }
        }
        return buf;
        return buf;
}
}
 
 
/**
/**
* vsnprintf - Format a string and place it in a buffer
* vsnprintf - Format a string and place it in a buffer
* @buf: The buffer to place the result into
* @buf: The buffer to place the result into
* @size: The size of the buffer, including the trailing null space
* @size: The size of the buffer, including the trailing null space
* @fmt: The format string to use
* @fmt: The format string to use
* @args: Arguments for the format string
* @args: Arguments for the format string
*
*
* Call this function if you are already dealing with a va_list.
* Call this function if you are already dealing with a va_list.
* You probably want snprintf instead.
* You probably want snprintf instead.
 */
 */
int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
{
{
        int len;
        int len;
        unsigned long long num;
        unsigned long long num;
        int i, base;
        int i, base;
        char *str, *end, c;
        char *str, *end, c;
        const char *s;
        const char *s;
 
 
        int flags;              /* flags to number() */
        int flags;              /* flags to number() */
 
 
        int field_width;        /* width of output field */
        int field_width;        /* width of output field */
        int precision;          /* min. # of digits for integers; max
        int precision;          /* min. # of digits for integers; max
                                   number of chars for from string */
                                   number of chars for from string */
        int qualifier;          /* 'h', 'l', or 'L' for integer fields */
        int qualifier;          /* 'h', 'l', or 'L' for integer fields */
                                /* 'z' support added 23/7/1999 S.H.    */
                                /* 'z' support added 23/7/1999 S.H.    */
                                /* 'z' changed to 'Z' --davidm 1/25/99 */
                                /* 'z' changed to 'Z' --davidm 1/25/99 */
 
 
        /* Reject out-of-range values early */
        /* Reject out-of-range values early */
        if (unlikely((int) size < 0)) {
        if (unlikely((int) size < 0)) {
                /* There can be only one.. */
                /* There can be only one.. */
                static int warn = 1;
                static int warn = 1;
                if (warn) {
                if (warn) {
                        printk(KERN_WARNING "improper call of vsnprintf!\n");
                        printk(KERN_WARNING "improper call of vsnprintf!\n");
                        dump_stack();
                        dump_stack();
                        warn = 0;
                        warn = 0;
                }
                }
                return 0;
                return 0;
        }
        }
 
 
        str = buf;
        str = buf;
        end = buf + size - 1;
        end = buf + size - 1;
 
 
        if (end < buf - 1) {
        if (end < buf - 1) {
                end = ((void *) -1);
                end = ((void *) -1);
                size = end - buf + 1;
                size = end - buf + 1;
        }
        }
 
 
        for (; *fmt ; ++fmt) {
        for (; *fmt ; ++fmt) {
                if (*fmt != '%') {
                if (*fmt != '%') {
                        if (str <= end)
                        if (str <= end)
                                *str = *fmt;
                                *str = *fmt;
                        ++str;
                        ++str;
                        continue;
                        continue;
                }
                }
 
 
                /* process flags */
                /* process flags */
                flags = 0;
                flags = 0;
                repeat:
                repeat:
                        ++fmt;          /* this also skips first '%' */
                        ++fmt;          /* this also skips first '%' */
                        switch (*fmt) {
                        switch (*fmt) {
                                case '-': flags |= LEFT; goto repeat;
                                case '-': flags |= LEFT; goto repeat;
                                case '+': flags |= PLUS; goto repeat;
                                case '+': flags |= PLUS; goto repeat;
                                case ' ': flags |= SPACE; goto repeat;
                                case ' ': flags |= SPACE; goto repeat;
                                case '#': flags |= SPECIAL; goto repeat;
                                case '#': flags |= SPECIAL; goto repeat;
                                case '0': flags |= ZEROPAD; goto repeat;
                                case '0': flags |= ZEROPAD; goto repeat;
                        }
                        }
 
 
                /* get field width */
                /* get field width */
                field_width = -1;
                field_width = -1;
                if (isdigit(*fmt))
                if (isdigit(*fmt))
                        field_width = skip_atoi(&fmt);
                        field_width = skip_atoi(&fmt);
                else if (*fmt == '*') {
                else if (*fmt == '*') {
                        ++fmt;
                        ++fmt;
                        /* it's the next argument */
                        /* it's the next argument */
                        field_width = va_arg(args, int);
                        field_width = va_arg(args, int);
                        if (field_width < 0) {
                        if (field_width < 0) {
                                field_width = -field_width;
                                field_width = -field_width;
                                flags |= LEFT;
                                flags |= LEFT;
                        }
                        }
                }
                }
 
 
                /* get the precision */
                /* get the precision */
                precision = -1;
                precision = -1;
                if (*fmt == '.') {
                if (*fmt == '.') {
                        ++fmt;
                        ++fmt;
                        if (isdigit(*fmt))
                        if (isdigit(*fmt))
                                precision = skip_atoi(&fmt);
                                precision = skip_atoi(&fmt);
                        else if (*fmt == '*') {
                        else if (*fmt == '*') {
                                ++fmt;
                                ++fmt;
                                /* it's the next argument */
                                /* it's the next argument */
                                precision = va_arg(args, int);
                                precision = va_arg(args, int);
                        }
                        }
                        if (precision < 0)
                        if (precision < 0)
                                precision = 0;
                                precision = 0;
                }
                }
 
 
                /* get the conversion qualifier */
                /* get the conversion qualifier */
                qualifier = -1;
                qualifier = -1;
                if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
                if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
                    *fmt =='Z' || *fmt == 'z') {
                    *fmt =='Z' || *fmt == 'z') {
                        qualifier = *fmt;
                        qualifier = *fmt;
                        ++fmt;
                        ++fmt;
                        if (qualifier == 'l' && *fmt == 'l') {
                        if (qualifier == 'l' && *fmt == 'l') {
                                qualifier = 'L';
                                qualifier = 'L';
                                ++fmt;
                                ++fmt;
                        }
                        }
                }
                }
 
 
                /* default base */
                /* default base */
                base = 10;
                base = 10;
 
 
                switch (*fmt) {
                switch (*fmt) {
                        case 'c':
                        case 'c':
                                if (!(flags & LEFT)) {
                                if (!(flags & LEFT)) {
                                        while (--field_width > 0) {
                                        while (--field_width > 0) {
                                                if (str <= end)
                                                if (str <= end)
                                                        *str = ' ';
                                                        *str = ' ';
                                                ++str;
                                                ++str;
                                        }
                                        }
                                }
                                }
                                c = (unsigned char) va_arg(args, int);
                                c = (unsigned char) va_arg(args, int);
                                if (str <= end)
                                if (str <= end)
                                        *str = c;
                                        *str = c;
                                ++str;
                                ++str;
                                while (--field_width > 0) {
                                while (--field_width > 0) {
                                        if (str <= end)
                                        if (str <= end)
                                                *str = ' ';
                                                *str = ' ';
                                        ++str;
                                        ++str;
                                }
                                }
                                continue;
                                continue;
 
 
                        case 's':
                        case 's':
                                s = va_arg(args, char *);
                                s = va_arg(args, char *);
                                if ((unsigned long)s < PAGE_SIZE)
                                if ((unsigned long)s < PAGE_SIZE)
                                        s = "<NULL>";
                                        s = "<NULL>";
 
 
                                len = strnlen(s, precision);
                                len = strnlen(s, precision);
 
 
                                if (!(flags & LEFT)) {
                                if (!(flags & LEFT)) {
                                        while (len < field_width--) {
                                        while (len < field_width--) {
                                                if (str <= end)
                                                if (str <= end)
                                                        *str = ' ';
                                                        *str = ' ';
                                                ++str;
                                                ++str;
                                        }
                                        }
                                }
                                }
                                for (i = 0; i < len; ++i) {
                                for (i = 0; i < len; ++i) {
                                        if (str <= end)
                                        if (str <= end)
                                                *str = *s;
                                                *str = *s;
                                        ++str; ++s;
                                        ++str; ++s;
                                }
                                }
                                while (len < field_width--) {
                                while (len < field_width--) {
                                        if (str <= end)
                                        if (str <= end)
                                                *str = ' ';
                                                *str = ' ';
                                        ++str;
                                        ++str;
                                }
                                }
                                continue;
                                continue;
 
 
                        case 'p':
                        case 'p':
                                if (field_width == -1) {
                                if (field_width == -1) {
                                        field_width = 2*sizeof(void *);
                                        field_width = 2*sizeof(void *);
                                        flags |= ZEROPAD;
                                        flags |= ZEROPAD;
                                }
                                }
                                str = number(str, end,
                                str = number(str, end,
                                                (unsigned long) va_arg(args, void *),
                                                (unsigned long) va_arg(args, void *),
                                                16, field_width, precision, flags);
                                                16, field_width, precision, flags);
                                continue;
                                continue;
 
 
 
 
                        case 'n':
                        case 'n':
                                /* FIXME:
                                /* FIXME:
                                * What does C99 say about the overflow case here? */
                                * What does C99 say about the overflow case here? */
                                if (qualifier == 'l') {
                                if (qualifier == 'l') {
                                        long * ip = va_arg(args, long *);
                                        long * ip = va_arg(args, long *);
                                        *ip = (str - buf);
                                        *ip = (str - buf);
                                } else if (qualifier == 'Z' || qualifier == 'z') {
                                } else if (qualifier == 'Z' || qualifier == 'z') {
                                        size_t * ip = va_arg(args, size_t *);
                                        size_t * ip = va_arg(args, size_t *);
                                        *ip = (str - buf);
                                        *ip = (str - buf);
                                } else {
                                } else {
                                        int * ip = va_arg(args, int *);
                                        int * ip = va_arg(args, int *);
                                        *ip = (str - buf);
                                        *ip = (str - buf);
                                }
                                }
                                continue;
                                continue;
 
 
                        case '%':
                        case '%':
                                if (str <= end)
                                if (str <= end)
                                        *str = '%';
                                        *str = '%';
                                ++str;
                                ++str;
                                continue;
                                continue;
 
 
                                /* integer number formats - set up the flags and "break" */
                                /* integer number formats - set up the flags and "break" */
                        case 'o':
                        case 'o':
                                base = 8;
                                base = 8;
                                break;
                                break;
 
 
                        case 'X':
                        case 'X':
                                flags |= LARGE;
                                flags |= LARGE;
                        case 'x':
                        case 'x':
                                base = 16;
                                base = 16;
                                break;
                                break;
 
 
                        case 'd':
                        case 'd':
                        case 'i':
                        case 'i':
                                flags |= SIGN;
                                flags |= SIGN;
                        case 'u':
                        case 'u':
                                break;
                                break;
 
 
                        default:
                        default:
                                if (str <= end)
                                if (str <= end)
                                        *str = '%';
                                        *str = '%';
                                ++str;
                                ++str;
                                if (*fmt) {
                                if (*fmt) {
                                        if (str <= end)
                                        if (str <= end)
                                                *str = *fmt;
                                                *str = *fmt;
                                        ++str;
                                        ++str;
                                } else {
                                } else {
                                        --fmt;
                                        --fmt;
                                }
                                }
                                continue;
                                continue;
                }
                }
                if (qualifier == 'L')
                if (qualifier == 'L')
                        num = va_arg(args, long long);
                        num = va_arg(args, long long);
                else if (qualifier == 'l') {
                else if (qualifier == 'l') {
                        num = va_arg(args, unsigned long);
                        num = va_arg(args, unsigned long);
                        if (flags & SIGN)
                        if (flags & SIGN)
                                num = (signed long) num;
                                num = (signed long) num;
                } else if (qualifier == 'Z' || qualifier == 'z') {
                } else if (qualifier == 'Z' || qualifier == 'z') {
                        num = va_arg(args, size_t);
                        num = va_arg(args, size_t);
                } else if (qualifier == 'h') {
                } else if (qualifier == 'h') {
                        num = (unsigned short) va_arg(args, int);
                        num = (unsigned short) va_arg(args, int);
                        if (flags & SIGN)
                        if (flags & SIGN)
                                num = (signed short) num;
                                num = (signed short) num;
                } else {
                } else {
                        num = va_arg(args, unsigned int);
                        num = va_arg(args, unsigned int);
                        if (flags & SIGN)
                        if (flags & SIGN)
                                num = (signed int) num;
                                num = (signed int) num;
                }
                }
                str = number(str, end, num, base,
                str = number(str, end, num, base,
                                field_width, precision, flags);
                                field_width, precision, flags);
        }
        }
        if (str <= end)
        if (str <= end)
                *str = '\0';
                *str = '\0';
        else if (size > 0)
        else if (size > 0)
                /* don't write out a null byte if the buf size is zero */
                /* don't write out a null byte if the buf size is zero */
                *end = '\0';
                *end = '\0';
        /* the trailing null byte doesn't count towards the total
        /* the trailing null byte doesn't count towards the total
        * ++str;
        * ++str;
        */
        */
        return str-buf;
        return str-buf;
}
}
 
 
/**
/**
 * snprintf - Format a string and place it in a buffer
 * snprintf - Format a string and place it in a buffer
 * @buf: The buffer to place the result into
 * @buf: The buffer to place the result into
 * @size: The size of the buffer, including the trailing null space
 * @size: The size of the buffer, including the trailing null space
 * @fmt: The format string to use
 * @fmt: The format string to use
 * @...: Arguments for the format string
 * @...: Arguments for the format string
 */
 */
int snprintf(char * buf, size_t size, const char *fmt, ...)
int snprintf(char * buf, size_t size, const char *fmt, ...)
{
{
        va_list args;
        va_list args;
        int i;
        int i;
 
 
        va_start(args, fmt);
        va_start(args, fmt);
        i=vsnprintf(buf,size,fmt,args);
        i=vsnprintf(buf,size,fmt,args);
        va_end(args);
        va_end(args);
        return i;
        return i;
}
}
 
 
/**
/**
 * vsprintf - Format a string and place it in a buffer
 * vsprintf - Format a string and place it in a buffer
 * @buf: The buffer to place the result into
 * @buf: The buffer to place the result into
 * @fmt: The format string to use
 * @fmt: The format string to use
 * @args: Arguments for the format string
 * @args: Arguments for the format string
 *
 *
 * Call this function if you are already dealing with a va_list.
 * Call this function if you are already dealing with a va_list.
 * You probably want sprintf instead.
 * You probably want sprintf instead.
 */
 */
int vsprintf(char *buf, const char *fmt, va_list args)
int vsprintf(char *buf, const char *fmt, va_list args)
{
{
        return vsnprintf(buf, (~0U)>>1, fmt, args);
        return vsnprintf(buf, (~0U)>>1, fmt, args);
}
}
 
 
 
 
/**
/**
 * sprintf - Format a string and place it in a buffer
 * sprintf - Format a string and place it in a buffer
 * @buf: The buffer to place the result into
 * @buf: The buffer to place the result into
 * @fmt: The format string to use
 * @fmt: The format string to use
 * @...: Arguments for the format string
 * @...: Arguments for the format string
 */
 */
int sprintf(char * buf, const char *fmt, ...)
int sprintf(char * buf, const char *fmt, ...)
{
{
        va_list args;
        va_list args;
        int i;
        int i;
 
 
        va_start(args, fmt);
        va_start(args, fmt);
        i=vsprintf(buf,fmt,args);
        i=vsprintf(buf,fmt,args);
        va_end(args);
        va_end(args);
        return i;
        return i;
}
}
 
 
/**
/**
 * vsscanf - Unformat a buffer into a list of arguments
 * vsscanf - Unformat a buffer into a list of arguments
 * @buf:        input buffer
 * @buf:        input buffer
 * @fmt:        format of buffer
 * @fmt:        format of buffer
 * @args:       arguments
 * @args:       arguments
 */
 */
int vsscanf(const char * buf, const char * fmt, va_list args)
int vsscanf(const char * buf, const char * fmt, va_list args)
{
{
        const char *str = buf;
        const char *str = buf;
        char *next;
        char *next;
        char digit;
        char digit;
        int num = 0;
        int num = 0;
        int qualifier;
        int qualifier;
        int base;
        int base;
        int field_width;
        int field_width;
        int is_sign = 0;
        int is_sign = 0;
 
 
        while(*fmt && *str) {
        while(*fmt && *str) {
                /* skip any white space in format */
                /* skip any white space in format */
                /* white space in format matchs any amount of
                /* white space in format matchs any amount of
                 * white space, including none, in the input.
                 * white space, including none, in the input.
                 */
                 */
                if (isspace(*fmt)) {
                if (isspace(*fmt)) {
                        while (isspace(*fmt))
                        while (isspace(*fmt))
                                ++fmt;
                                ++fmt;
                        while (isspace(*str))
                        while (isspace(*str))
                                ++str;
                                ++str;
                }
                }
 
 
                /* anything that is not a conversion must match exactly */
                /* anything that is not a conversion must match exactly */
                if (*fmt != '%' && *fmt) {
                if (*fmt != '%' && *fmt) {
                        if (*fmt++ != *str++)
                        if (*fmt++ != *str++)
                                break;
                                break;
                        continue;
                        continue;
                }
                }
 
 
                if (!*fmt)
                if (!*fmt)
                        break;
                        break;
                ++fmt;
                ++fmt;
 
 
                /* skip this conversion.
                /* skip this conversion.
                 * advance both strings to next white space
                 * advance both strings to next white space
                 */
                 */
                if (*fmt == '*') {
                if (*fmt == '*') {
                        while (!isspace(*fmt) && *fmt)
                        while (!isspace(*fmt) && *fmt)
                                fmt++;
                                fmt++;
                        while (!isspace(*str) && *str)
                        while (!isspace(*str) && *str)
                                str++;
                                str++;
                        continue;
                        continue;
                }
                }
 
 
                /* get field width */
                /* get field width */
                field_width = -1;
                field_width = -1;
                if (isdigit(*fmt))
                if (isdigit(*fmt))
                        field_width = skip_atoi(&fmt);
                        field_width = skip_atoi(&fmt);
 
 
                /* get conversion qualifier */
                /* get conversion qualifier */
                qualifier = -1;
                qualifier = -1;
                if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
                if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
                    *fmt == 'Z' || *fmt == 'z') {
                    *fmt == 'Z' || *fmt == 'z') {
                        qualifier = *fmt;
                        qualifier = *fmt;
                        fmt++;
                        fmt++;
                }
                }
                base = 10;
                base = 10;
                is_sign = 0;
                is_sign = 0;
 
 
                if (!*fmt || !*str)
                if (!*fmt || !*str)
                        break;
                        break;
 
 
                switch(*fmt++) {
                switch(*fmt++) {
                case 'c':
                case 'c':
                {
                {
                        char *s = (char *) va_arg(args,char*);
                        char *s = (char *) va_arg(args,char*);
                        if (field_width == -1)
                        if (field_width == -1)
                                field_width = 1;
                                field_width = 1;
                        do {
                        do {
                                *s++ = *str++;
                                *s++ = *str++;
                        } while (--field_width > 0 && *str);
                        } while (--field_width > 0 && *str);
                        num++;
                        num++;
                }
                }
                continue;
                continue;
                case 's':
                case 's':
                {
                {
                        char *s = (char *) va_arg(args, char *);
                        char *s = (char *) va_arg(args, char *);
                        if(field_width == -1)
                        if(field_width == -1)
                                field_width = INT_MAX;
                                field_width = INT_MAX;
                        /* first, skip leading white space in buffer */
                        /* first, skip leading white space in buffer */
                        while (isspace(*str))
                        while (isspace(*str))
                                str++;
                                str++;
 
 
                        /* now copy until next white space */
                        /* now copy until next white space */
                        while (*str && !isspace(*str) && field_width--) {
                        while (*str && !isspace(*str) && field_width--) {
                                *s++ = *str++;
                                *s++ = *str++;
                        }
                        }
                        *s = '\0';
                        *s = '\0';
                        num++;
                        num++;
                }
                }
                continue;
                continue;
                case 'n':
                case 'n':
                        /* return number of characters read so far */
                        /* return number of characters read so far */
                {
                {
                        int *i = (int *)va_arg(args,int*);
                        int *i = (int *)va_arg(args,int*);
                        *i = str - buf;
                        *i = str - buf;
                }
                }
                continue;
                continue;
                case 'o':
                case 'o':
                        base = 8;
                        base = 8;
                        break;
                        break;
                case 'x':
                case 'x':
                case 'X':
                case 'X':
                        base = 16;
                        base = 16;
                        break;
                        break;
                case 'i':
                case 'i':
                        base = 0;
                        base = 0;
                case 'd':
                case 'd':
                        is_sign = 1;
                        is_sign = 1;
                case 'u':
                case 'u':
                        break;
                        break;
                case '%':
                case '%':
                        /* looking for '%' in str */
                        /* looking for '%' in str */
                        if (*str++ != '%')
                        if (*str++ != '%')
                                return num;
                                return num;
                        continue;
                        continue;
                default:
                default:
                        /* invalid format; stop here */
                        /* invalid format; stop here */
                        return num;
                        return num;
                }
                }
 
 
                /* have some sort of integer conversion.
                /* have some sort of integer conversion.
                 * first, skip white space in buffer.
                 * first, skip white space in buffer.
                 */
                 */
                while (isspace(*str))
                while (isspace(*str))
                        str++;
                        str++;
 
 
                digit = *str;
                digit = *str;
                if (is_sign && digit == '-')
                if (is_sign && digit == '-')
                        digit = *(str + 1);
                        digit = *(str + 1);
 
 
                if (!digit
                if (!digit
                    || (base == 16 && !isxdigit(digit))
                    || (base == 16 && !isxdigit(digit))
                    || (base == 10 && !isdigit(digit))
                    || (base == 10 && !isdigit(digit))
                    || (base == 8 && (!isdigit(digit) || digit > '7'))
                    || (base == 8 && (!isdigit(digit) || digit > '7'))
                    || (base == 0 && !isdigit(digit)))
                    || (base == 0 && !isdigit(digit)))
                                break;
                                break;
 
 
                switch(qualifier) {
                switch(qualifier) {
                case 'h':
                case 'h':
                        if (is_sign) {
                        if (is_sign) {
                                short *s = (short *) va_arg(args,short *);
                                short *s = (short *) va_arg(args,short *);
                                *s = (short) simple_strtol(str,&next,base);
                                *s = (short) simple_strtol(str,&next,base);
                        } else {
                        } else {
                                unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
                                unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
                                *s = (unsigned short) simple_strtoul(str, &next, base);
                                *s = (unsigned short) simple_strtoul(str, &next, base);
                        }
                        }
                        break;
                        break;
                case 'l':
                case 'l':
                        if (is_sign) {
                        if (is_sign) {
                                long *l = (long *) va_arg(args,long *);
                                long *l = (long *) va_arg(args,long *);
                                *l = simple_strtol(str,&next,base);
                                *l = simple_strtol(str,&next,base);
                        } else {
                        } else {
                                unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
                                unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
                                *l = simple_strtoul(str,&next,base);
                                *l = simple_strtoul(str,&next,base);
                        }
                        }
                        break;
                        break;
                case 'L':
                case 'L':
                        if (is_sign) {
                        if (is_sign) {
                                long long *l = (long long*) va_arg(args,long long *);
                                long long *l = (long long*) va_arg(args,long long *);
                                *l = simple_strtoll(str,&next,base);
                                *l = simple_strtoll(str,&next,base);
                        } else {
                        } else {
                                unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
                                unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
                                *l = simple_strtoull(str,&next,base);
                                *l = simple_strtoull(str,&next,base);
                        }
                        }
                        break;
                        break;
                case 'Z':
                case 'Z':
                case 'z':
                case 'z':
                {
                {
                        size_t *s = (size_t*) va_arg(args,size_t*);
                        size_t *s = (size_t*) va_arg(args,size_t*);
                        *s = (size_t) simple_strtoul(str,&next,base);
                        *s = (size_t) simple_strtoul(str,&next,base);
                }
                }
                break;
                break;
                default:
                default:
                        if (is_sign) {
                        if (is_sign) {
                                int *i = (int *) va_arg(args, int*);
                                int *i = (int *) va_arg(args, int*);
                                *i = (int) simple_strtol(str,&next,base);
                                *i = (int) simple_strtol(str,&next,base);
                        } else {
                        } else {
                                unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
                                unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
                                *i = (unsigned int) simple_strtoul(str,&next,base);
                                *i = (unsigned int) simple_strtoul(str,&next,base);
                        }
                        }
                        break;
                        break;
                }
                }
                num++;
                num++;
 
 
                if (!next)
                if (!next)
                        break;
                        break;
                str = next;
                str = next;
        }
        }
        return num;
        return num;
}
}
 
 
/**
/**
 * sscanf - Unformat a buffer into a list of arguments
 * sscanf - Unformat a buffer into a list of arguments
 * @buf:        input buffer
 * @buf:        input buffer
 * @fmt:        formatting of buffer
 * @fmt:        formatting of buffer
 * @...:        resulting arguments
 * @...:        resulting arguments
 */
 */
int sscanf(const char * buf, const char * fmt, ...)
int sscanf(const char * buf, const char * fmt, ...)
{
{
        va_list args;
        va_list args;
        int i;
        int i;
 
 
        va_start(args,fmt);
        va_start(args,fmt);
        i = vsscanf(buf,fmt,args);
        i = vsscanf(buf,fmt,args);
        va_end(args);
        va_end(args);
        return i;
        return i;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.