stdio.c 6.2 KB

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