vsprintf.c 6.1 KB

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