stdio.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt =='Z') {
  176. qualifier = *fmt;
  177. ++fmt;
  178. }
  179. /* default base */
  180. base = 10;
  181. switch (*fmt) {
  182. case 'c':
  183. if (!(flags & LEFT))
  184. while (--field_width > 0)
  185. *str++ = ' ';
  186. *str++ = (unsigned char) va_arg(args, int);
  187. while (--field_width > 0)
  188. *str++ = ' ';
  189. continue;
  190. case 's':
  191. s = va_arg(args, char *);
  192. if (!s)
  193. s = "<NULL>";
  194. len = strnlen(s, precision);
  195. if (!(flags & LEFT))
  196. while (len < field_width--)
  197. *str++ = ' ';
  198. for (i = 0; i < len; ++i)
  199. *str++ = *s++;
  200. while (len < field_width--)
  201. *str++ = ' ';
  202. continue;
  203. case 'p':
  204. if (field_width == -1) {
  205. field_width = 2*sizeof(void *);
  206. flags |= ZEROPAD;
  207. }
  208. str = number(str,
  209. (unsigned long) va_arg(args, void *), 16,
  210. field_width, precision, flags);
  211. continue;
  212. case 'n':
  213. if (qualifier == 'l') {
  214. long * ip = va_arg(args, long *);
  215. *ip = (str - buf);
  216. } else if (qualifier == 'Z') {
  217. size_t * ip = va_arg(args, size_t *);
  218. *ip = (str - buf);
  219. } else {
  220. int * ip = va_arg(args, int *);
  221. *ip = (str - buf);
  222. }
  223. continue;
  224. case '%':
  225. *str++ = '%';
  226. continue;
  227. /* integer number formats - set up the flags and "break" */
  228. case 'o':
  229. base = 8;
  230. break;
  231. case 'X':
  232. flags |= LARGE;
  233. case 'x':
  234. base = 16;
  235. break;
  236. case 'd':
  237. case 'i':
  238. flags |= SIGN;
  239. case 'u':
  240. break;
  241. default:
  242. *str++ = '%';
  243. if (*fmt)
  244. *str++ = *fmt;
  245. else
  246. --fmt;
  247. continue;
  248. }
  249. if (qualifier == 'l') {
  250. num = va_arg(args, unsigned long);
  251. if (flags & SIGN)
  252. num = (signed long) num;
  253. } else if (qualifier == 'Z') {
  254. num = va_arg(args, size_t);
  255. } else if (qualifier == 'h') {
  256. num = (unsigned short) va_arg(args, int);
  257. if (flags & SIGN)
  258. num = (signed short) num;
  259. } else {
  260. num = va_arg(args, unsigned int);
  261. if (flags & SIGN)
  262. num = (signed int) num;
  263. }
  264. str = number(str, num, base, field_width, precision, flags);
  265. }
  266. *str = '\0';
  267. return str-buf;
  268. }
  269. int sprintf(char * buf, const char *fmt, ...)
  270. {
  271. va_list args;
  272. int i;
  273. va_start(args, fmt);
  274. i=vsprintf(buf,fmt,args);
  275. va_end(args);
  276. return i;
  277. }
  278. static char sprint_buf[1024];
  279. int
  280. printf(const char *fmt, ...)
  281. {
  282. va_list args;
  283. int n;
  284. va_start(args, fmt);
  285. n = vsprintf(sprint_buf, fmt, args);
  286. va_end(args);
  287. if (console_ops.write)
  288. console_ops.write(sprint_buf, n);
  289. return n;
  290. }