vsprintf.c 6.0 KB

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