stdio.c 7.1 KB

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