stdio.c 6.8 KB

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