Annotation of prex-old/sys/lib/vsprintf.c, Revision 1.1
1.1 ! nbrk 1: /*-
! 2: * Copyright (c) 2005, Kohsuke Ohtani
! 3: * All rights reserved.
! 4: *
! 5: * Redistribution and use in source and binary forms, with or without
! 6: * modification, are permitted provided that the following conditions
! 7: * are met:
! 8: * 1. Redistributions of source code must retain the above copyright
! 9: * notice, this list of conditions and the following disclaimer.
! 10: * 2. Redistributions in binary form must reproduce the above copyright
! 11: * notice, this list of conditions and the following disclaimer in the
! 12: * documentation and/or other materials provided with the distribution.
! 13: * 3. Neither the name of the author nor the names of any co-contributors
! 14: * may be used to endorse or promote products derived from this software
! 15: * without specific prior written permission.
! 16: *
! 17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
! 18: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 19: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 20: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
! 21: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 27: * SUCH DAMAGE.
! 28: */
! 29:
! 30: /*
! 31: * vsprintf.c - Format and output data to buffer
! 32: */
! 33:
! 34: #include <kernel.h>
! 35: #include <sys/types.h>
! 36:
! 37: #define isdigit(c) ((unsigned)((c) - '0') < 10)
! 38:
! 39: static int
! 40: divide(long *n, int base)
! 41: {
! 42: int res;
! 43:
! 44: res = ((u_long)*n) % base;
! 45: *n = ((u_long)*n) / base;
! 46: return res;
! 47: }
! 48:
! 49: static int
! 50: atoi(const char **s)
! 51: {
! 52: int i = 0;
! 53: while (isdigit((int)**s))
! 54: i = i * 10 + *((*s)++) - '0';
! 55: return i;
! 56: }
! 57:
! 58: /*
! 59: * Print formatted output - scaled down version
! 60: *
! 61: * Identifiers:
! 62: * %d - Decimal signed int
! 63: * %x - Hex integer
! 64: * %u - Unsigned integer
! 65: * %c - Character
! 66: * %s - String
! 67: *
! 68: * Flags:
! 69: * 0 - Zero pad
! 70: */
! 71: int
! 72: vsprintf(char *buf, const char *fmt, va_list args)
! 73: {
! 74: char *p, *str;
! 75: const char *digits = "0123456789abcdef";
! 76: char pad, tmp[16];
! 77: int width, base, sign, i;
! 78: long num;
! 79:
! 80: for (p = buf; *fmt; fmt++) {
! 81: if (*fmt != '%') {
! 82: *p++ = *fmt;
! 83: continue;
! 84: }
! 85: /* get flags */
! 86: ++fmt;
! 87: pad = ' ';
! 88: if (*fmt == '0') {
! 89: pad = '0';
! 90: fmt++;
! 91: }
! 92: /* get width */
! 93: width = -1;
! 94: if (isdigit(*fmt)) {
! 95: width = atoi(&fmt);
! 96: }
! 97: base = 10;
! 98: sign = 0;
! 99: switch (*fmt) {
! 100: case 'c':
! 101: *p++ = (char)va_arg(args, int);
! 102: continue;
! 103: case 's':
! 104: str = va_arg(args, char *);
! 105: if (str == NULL)
! 106: str = "<NULL>";
! 107: for (; *str && width != 0; str++, width--) {
! 108: *p++ = *str;
! 109: }
! 110: while (width-- > 0)
! 111: *p++ = pad;
! 112: continue;
! 113: case 'X':
! 114: case 'x':
! 115: base = 16;
! 116: break;
! 117: case 'd':
! 118: sign = 1;
! 119: break;
! 120: case 'u':
! 121: break;
! 122: default:
! 123: continue;
! 124: }
! 125: num = va_arg(args, long);
! 126: if (sign && num < 0) {
! 127: num = -num;
! 128: *p++ = '-';
! 129: width--;
! 130: }
! 131: i = 0;
! 132: if (num == 0)
! 133: tmp[i++] = '0';
! 134: else
! 135: while (num != 0)
! 136: tmp[i++] = digits[divide(&num, base)];
! 137: width -= i;
! 138: while (width-- > 0)
! 139: *p++ = pad;
! 140: while (i-- > 0)
! 141: *p++ = tmp[i];
! 142: }
! 143: *p = '\0';
! 144: return 0;
! 145: }
CVSweb