root/kernel/vsprintf.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. simple_strtoul
  2. skip_atoi
  3. number
  4. vsprintf
  5. sprintf

   1 /*
   2  *  linux/kernel/vsprintf.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
   8 /*
   9  * Wirzenius wrote this portably, Torvalds fucked it up :-)
  10  */
  11 
  12 #include <stdarg.h>
  13 #include <linux/types.h>
  14 #include <linux/string.h>
  15 #include <linux/ctype.h>
  16 
  17 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
     /* [previous][next][first][last][top][bottom][index][help] */
  18 {
  19         unsigned long result = 0,value;
  20 
  21         if (!base) {
  22                 base = 10;
  23                 if (*cp == '0') {
  24                         base = 8;
  25                         cp++;
  26                         if ((*cp == 'x') && isxdigit(cp[1])) {
  27                                 cp++;
  28                                 base = 16;
  29                         }
  30                 }
  31         }
  32         while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
  33             ? toupper(*cp) : *cp)-'A'+10) < base) {
  34                 result = result*base + value;
  35                 cp++;
  36         }
  37         if (endp)
  38                 *endp = (char *)cp;
  39         return result;
  40 }
  41 
  42 /* we use this so that we can do without the ctype library */
  43 #define is_digit(c)     ((c) >= '0' && (c) <= '9')
  44 
  45 static int skip_atoi(const char **s)
     /* [previous][next][first][last][top][bottom][index][help] */
  46 {
  47         int i=0;
  48 
  49         while (is_digit(**s))
  50                 i = i*10 + *((*s)++) - '0';
  51         return i;
  52 }
  53 
  54 #define ZEROPAD 1               /* pad with zero */
  55 #define SIGN    2               /* unsigned/signed long */
  56 #define PLUS    4               /* show plus */
  57 #define SPACE   8               /* space if plus */
  58 #define LEFT    16              /* left justified */
  59 #define SPECIAL 32              /* 0x */
  60 #define SMALL   64              /* use 'abcdef' instead of 'ABCDEF' */
  61 
  62 #define do_div(n,base) ({ \
  63 int __res; \
  64 __asm__("divl %4":"=a" (n),"=d" (__res):"0" (n),"1" (0),"r" (base)); \
  65 __res; })
  66 
  67 static char * number(char * str, int num, int base, int size, int precision
     /* [previous][next][first][last][top][bottom][index][help] */
  68         ,int type)
  69 {
  70         char c,sign,tmp[36];
  71         const char *digits="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  72         int i;
  73 
  74         if (type&SMALL) digits="0123456789abcdefghijklmnopqrstuvwxyz";
  75         if (type&LEFT) type &= ~ZEROPAD;
  76         if (base<2 || base>36)
  77                 return 0;
  78         c = (type & ZEROPAD) ? '0' : ' ' ;
  79         if (type&SIGN && num<0) {
  80                 sign='-';
  81                 num = -num;
  82         } else
  83                 sign=(type&PLUS) ? '+' : ((type&SPACE) ? ' ' : 0);
  84         if (sign) size--;
  85         if (type&SPECIAL)
  86                 if (base==16) size -= 2;
  87                 else if (base==8) size--;
  88         i=0;
  89         if (num==0)
  90                 tmp[i++]='0';
  91         else while (num!=0)
  92                 tmp[i++]=digits[do_div(num,base)];
  93         if (i>precision) precision=i;
  94         size -= precision;
  95         if (!(type&(ZEROPAD+LEFT)))
  96                 while(size-->0)
  97                         *str++ = ' ';
  98         if (sign)
  99                 *str++ = sign;
 100         if (type&SPECIAL)
 101                 if (base==8)
 102                         *str++ = '0';
 103                 else if (base==16) {
 104                         *str++ = '0';
 105                         *str++ = digits[33];
 106                 }
 107         if (!(type&LEFT))
 108                 while(size-->0)
 109                         *str++ = c;
 110         while(i<precision--)
 111                 *str++ = '0';
 112         while(i-->0)
 113                 *str++ = tmp[i];
 114         while(size-->0)
 115                 *str++ = ' ';
 116         return str;
 117 }
 118 
 119 int vsprintf(char *buf, const char *fmt, va_list args)
     /* [previous][next][first][last][top][bottom][index][help] */
 120 {
 121         int len;
 122         int i;
 123         char * str;
 124         char *s;
 125         int *ip;
 126 
 127         int flags;              /* flags to number() */
 128 
 129         int field_width;        /* width of output field */
 130         int precision;          /* min. # of digits for integers; max
 131                                    number of chars for from string */
 132         int qualifier;          /* 'h', 'l', or 'L' for integer fields */
 133 
 134         for (str=buf ; *fmt ; ++fmt) {
 135                 if (*fmt != '%') {
 136                         *str++ = *fmt;
 137                         continue;
 138                 }
 139                         
 140                 /* process flags */
 141                 flags = 0;
 142                 repeat:
 143                         ++fmt;          /* this also skips first '%' */
 144                         switch (*fmt) {
 145                                 case '-': flags |= LEFT; goto repeat;
 146                                 case '+': flags |= PLUS; goto repeat;
 147                                 case ' ': flags |= SPACE; goto repeat;
 148                                 case '#': flags |= SPECIAL; goto repeat;
 149                                 case '0': flags |= ZEROPAD; goto repeat;
 150                                 }
 151                 
 152                 /* get field width */
 153                 field_width = -1;
 154                 if (is_digit(*fmt))
 155                         field_width = skip_atoi(&fmt);
 156                 else if (*fmt == '*') {
 157                         /* it's the next argument */
 158                         field_width = va_arg(args, int);
 159                         if (field_width < 0) {
 160                                 field_width = -field_width;
 161                                 flags |= LEFT;
 162                         }
 163                 }
 164 
 165                 /* get the precision */
 166                 precision = -1;
 167                 if (*fmt == '.') {
 168                         ++fmt;  
 169                         if (is_digit(*fmt))
 170                                 precision = skip_atoi(&fmt);
 171                         else if (*fmt == '*') {
 172                                 /* it's the next argument */
 173                                 precision = va_arg(args, int);
 174                         }
 175                         if (precision < 0)
 176                                 precision = 0;
 177                 }
 178 
 179                 /* get the conversion qualifier */
 180                 qualifier = -1;
 181                 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
 182                         qualifier = *fmt;
 183                         ++fmt;
 184                 }
 185 
 186                 switch (*fmt) {
 187                 case 'c':
 188                         if (!(flags & LEFT))
 189                                 while (--field_width > 0)
 190                                         *str++ = ' ';
 191                         *str++ = (unsigned char) va_arg(args, int);
 192                         while (--field_width > 0)
 193                                 *str++ = ' ';
 194                         break;
 195 
 196                 case 's':
 197                         s = va_arg(args, char *);
 198                         len = strlen(s);
 199                         if (precision < 0)
 200                                 precision = len;
 201                         else if (len > precision)
 202                                 len = precision;
 203 
 204                         if (!(flags & LEFT))
 205                                 while (len < field_width--)
 206                                         *str++ = ' ';
 207                         for (i = 0; i < len; ++i)
 208                                 *str++ = *s++;
 209                         while (len < field_width--)
 210                                 *str++ = ' ';
 211                         break;
 212 
 213                 case 'o':
 214                         str = number(str, va_arg(args, unsigned long), 8,
 215                                 field_width, precision, flags);
 216                         break;
 217 
 218                 case 'p':
 219                         if (field_width == -1) {
 220                                 field_width = 8;
 221                                 flags |= ZEROPAD;
 222                         }
 223                         str = number(str,
 224                                 (unsigned long) va_arg(args, void *), 16,
 225                                 field_width, precision, flags);
 226                         break;
 227 
 228                 case 'x':
 229                         flags |= SMALL;
 230                 case 'X':
 231                         str = number(str, va_arg(args, unsigned long), 16,
 232                                 field_width, precision, flags);
 233                         break;
 234 
 235                 case 'd':
 236                 case 'i':
 237                         flags |= SIGN;
 238                 case 'u':
 239                         str = number(str, va_arg(args, unsigned long), 10,
 240                                 field_width, precision, flags);
 241                         break;
 242 
 243                 case 'n':
 244                         ip = va_arg(args, int *);
 245                         *ip = (str - buf);
 246                         break;
 247 
 248                 default:
 249                         if (*fmt != '%')
 250                                 *str++ = '%';
 251                         if (*fmt)
 252                                 *str++ = *fmt;
 253                         else
 254                                 --fmt;
 255                         break;
 256                 }
 257         }
 258         *str = '\0';
 259         return str-buf;
 260 }
 261 
 262 int sprintf(char * buf, const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 263 {
 264         va_list args;
 265         int i;
 266 
 267         va_start(args, fmt);
 268         i=vsprintf(buf,fmt,args);
 269         va_end(args);
 270         return i;
 271 }
 272 

/* [previous][next][first][last][top][bottom][index][help] */