stdio.c 7.2 KB

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