printf.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * printf.c
  3. *
  4. * Copyright (C) 2018 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <monolithium.h>
  23. #include "io_priv.h"
  24. #define __CRT_PRINTF_FLAG_ALIGN_LEFT (1 << 0)
  25. #define __CRT_PRINTF_FLAG_PLUS (1 << 1)
  26. #define __CRT_PRINTF_FLAG_SPACE (1 << 2)
  27. #define __CRT_PRINTF_FLAG_EXTRA (1 << 3)
  28. #define __CRT_PRINTF_FLAG_ZERO_PAD (1 << 4)
  29. #define __CRT_PRINTF_FLAG_EXTERNAL_WIDTH (1 << 5)
  30. #define __CRT_PRINTF_FLAG_EXTERNAL_PRECISION (1 << 6)
  31. #define __CRT_PRINTF_FLAG_DEFAULT_WIDTH (1 << 7)
  32. #define __CRT_PRINTF_FLAG_DEFAULT_PRECISION (1 << 8)
  33. typedef struct
  34. {
  35. FILE *stream;
  36. char *string;
  37. size_t size;
  38. } __crt_stream_or_string_t;
  39. static int __crt_strputc(__crt_stream_or_string_t *str, char c)
  40. {
  41. if (str->stream)
  42. {
  43. return fputc_unlocked(c, str->stream) != EOF;
  44. }
  45. else
  46. {
  47. if (str->size > 1)
  48. {
  49. *str->string++ = c;
  50. str->size--;
  51. }
  52. return 1;
  53. }
  54. }
  55. static int __crt_strputs(__crt_stream_or_string_t *str, const char *s)
  56. {
  57. if (str->stream)
  58. {
  59. return fputs_unlocked(s, str->stream) != EOF ? strlen(s) : 0;
  60. }
  61. else
  62. {
  63. int length = strlen(s);
  64. if (str->size > 1)
  65. {
  66. strncpy(str->string, s, str->size - 1);
  67. if (length < str->size)
  68. {
  69. str->string += length;
  70. str->size -= length;
  71. }
  72. else
  73. {
  74. str->string += str->size - 1;
  75. str->size = 0;
  76. }
  77. }
  78. return length;
  79. }
  80. }
  81. static inline int __crt_vstrprintf(__crt_stream_or_string_t *str, const char *format, va_list ap)
  82. {
  83. int ret = 0;
  84. if (str->stream) syscall_wait_mutex(str->stream->mutex, NO_TIMEOUT);
  85. const char *ptr;
  86. for (ptr = format; *ptr; ptr++)
  87. {
  88. if (*ptr == '%')
  89. {
  90. unsigned long flags = 0;
  91. unsigned int width = 0, precision = 0;
  92. ptr++;
  93. while (*ptr)
  94. {
  95. if (*ptr == '-') flags |= __CRT_PRINTF_FLAG_ALIGN_LEFT;
  96. else if (*ptr == '+') flags |= __CRT_PRINTF_FLAG_PLUS;
  97. else if (*ptr == ' ') flags |= __CRT_PRINTF_FLAG_SPACE;
  98. else if (*ptr == '#') flags |= __CRT_PRINTF_FLAG_EXTRA;
  99. else if (*ptr == '0') flags |= __CRT_PRINTF_FLAG_ZERO_PAD;
  100. else break;
  101. ptr++;
  102. }
  103. if (*ptr == '*') flags |= __CRT_PRINTF_FLAG_EXTERNAL_WIDTH;
  104. else if (isdigit(*ptr)) width = strtoul(ptr, (char**)&ptr, 10);
  105. else flags |= __CRT_PRINTF_FLAG_DEFAULT_WIDTH;
  106. if (*ptr == '.')
  107. {
  108. ptr++;
  109. if (*ptr == '*') flags |= __CRT_PRINTF_FLAG_EXTERNAL_PRECISION;
  110. else if (isdigit(*ptr)) precision = strtoul(ptr, (char**)&ptr, 10);
  111. else flags |= __CRT_PRINTF_FLAG_DEFAULT_PRECISION;
  112. }
  113. else
  114. {
  115. flags |= __CRT_PRINTF_FLAG_DEFAULT_PRECISION;
  116. }
  117. int variable_size = 0;
  118. if (*ptr == 'h')
  119. {
  120. variable_size--;
  121. if (*++ptr == 'h')
  122. {
  123. variable_size--;
  124. ptr++;
  125. }
  126. }
  127. else if (*ptr == 'l')
  128. {
  129. variable_size++;
  130. if (*++ptr == 'l')
  131. {
  132. variable_size++;
  133. ptr++;
  134. }
  135. }
  136. const char *data = NULL;
  137. char buffer[512];
  138. int radix = 10;
  139. switch (*ptr)
  140. {
  141. case 's':
  142. data = va_arg(ap, const char*);
  143. break;
  144. case 'c':
  145. data = buffer;
  146. buffer[0] = (char)va_arg(ap, int);
  147. buffer[1] = '\0';
  148. break;
  149. case 'd':
  150. case 'i':
  151. if (flags & __CRT_PRINTF_FLAG_DEFAULT_WIDTH) width = 0;
  152. if (flags & __CRT_PRINTF_FLAG_DEFAULT_PRECISION) precision = 1;
  153. data = buffer;
  154. switch (variable_size)
  155. {
  156. case -2: itoa((char)va_arg(ap, int), buffer, 10); break;
  157. case -1: itoa((short)va_arg(ap, int), buffer, 10); break;
  158. case 0: itoa(va_arg(ap, int), buffer, 10); break;
  159. case 1: ltoa(va_arg(ap, long), buffer, 10); break;
  160. case 2: lltoa(va_arg(ap, long long), buffer, 10); break;
  161. }
  162. break;
  163. case 'o':
  164. radix = 8;
  165. goto read_variable;
  166. case 'x':
  167. case 'X':
  168. radix = 16;
  169. case 'u':
  170. read_variable:
  171. if (flags & __CRT_PRINTF_FLAG_DEFAULT_WIDTH) width = 0;
  172. if (flags & __CRT_PRINTF_FLAG_DEFAULT_PRECISION) precision = 1;
  173. data = buffer;
  174. switch (variable_size)
  175. {
  176. case -2: uitoa((unsigned char)va_arg(ap, unsigned int), buffer, radix); break;
  177. case -1: uitoa((unsigned short)va_arg(ap, unsigned int), buffer, radix); break;
  178. case 0: uitoa(va_arg(ap, unsigned int), buffer, radix); break;
  179. case 1: ultoa(va_arg(ap, unsigned long), buffer, radix); break;
  180. case 2: ulltoa(va_arg(ap, unsigned long long), buffer, radix); break;
  181. }
  182. break;
  183. case '%':
  184. data = buffer;
  185. strcpy(buffer, "%");
  186. break;
  187. }
  188. if (flags & __CRT_PRINTF_FLAG_EXTERNAL_WIDTH) width = va_arg(ap, unsigned int);
  189. if (flags & __CRT_PRINTF_FLAG_EXTERNAL_PRECISION) precision = va_arg(ap, unsigned int);
  190. int length = strlen(data);
  191. char padding = (flags &__CRT_PRINTF_FLAG_ZERO_PAD) ? '0' : ' ';
  192. switch (*ptr)
  193. {
  194. case 's':
  195. if (!(flags & __CRT_PRINTF_FLAG_ALIGN_LEFT))
  196. {
  197. while (length < width)
  198. {
  199. ret += __crt_strputc(str, padding);
  200. length++;
  201. }
  202. }
  203. if (precision)
  204. {
  205. while (*data && precision > 0)
  206. {
  207. ret += __crt_strputc(str, *data++);
  208. precision--;
  209. }
  210. }
  211. else
  212. {
  213. ret += __crt_strputs(str, data ? data : "(null)");
  214. }
  215. break;
  216. case 'd':
  217. case 'i':
  218. case 'o':
  219. case 'u':
  220. case 'x':
  221. case 'X':
  222. if (flags & (__CRT_PRINTF_FLAG_PLUS | __CRT_PRINTF_FLAG_SPACE))
  223. {
  224. if (*data == '-')
  225. {
  226. ret += __crt_strputc(str, '-');
  227. data++;
  228. }
  229. else
  230. {
  231. ret += __crt_strputc(str, (flags & __CRT_PRINTF_FLAG_PLUS) ? '+' : ' ');
  232. length++;
  233. }
  234. }
  235. if (!(flags & __CRT_PRINTF_FLAG_ALIGN_LEFT) && width > precision)
  236. {
  237. while (length < width - precision)
  238. {
  239. ret += __crt_strputc(str, padding);
  240. length++;
  241. }
  242. while (length < width)
  243. {
  244. ret += __crt_strputc(str, '0');
  245. length++;
  246. }
  247. }
  248. else
  249. {
  250. while (length < precision)
  251. {
  252. ret += __crt_strputc(str, '0');
  253. length++;
  254. }
  255. }
  256. ret += __crt_strputs(str, data);
  257. break;
  258. }
  259. while (length < width)
  260. {
  261. ret += __crt_strputc(str, padding);
  262. length++;
  263. }
  264. }
  265. else
  266. {
  267. ret += __crt_strputc(str, *ptr);
  268. }
  269. }
  270. if (str->string) *str->string = '\0';
  271. if (str->stream) syscall_release_mutex(str->stream->mutex);
  272. return ret;
  273. }
  274. int vsnprintf(char *string, size_t size, const char *format, va_list ap)
  275. {
  276. __crt_stream_or_string_t str = { .stream = NULL, .string = string, .size = size };
  277. return __crt_vstrprintf(&str, format, ap);
  278. }
  279. int vsprintf(char *str, const char *format, va_list ap)
  280. {
  281. return vsnprintf(str, -1, format, ap);
  282. }
  283. int vprintf(const char *format, va_list ap)
  284. {
  285. return vfprintf(stdout, format, ap);
  286. }
  287. int vfprintf(FILE *stream, const char *format, va_list ap)
  288. {
  289. __crt_stream_or_string_t str = { .stream = stream, .string = NULL, .size = 0 };
  290. return __crt_vstrprintf(&str, format, ap);
  291. }
  292. int fprintf(FILE *stream, const char *format, ...)
  293. {
  294. va_list ap;
  295. va_start(ap, format);
  296. int ret = vfprintf(stream, format, ap);
  297. va_end(ap);
  298. return ret;
  299. }
  300. int snprintf(char *str, size_t size, const char *format, ...)
  301. {
  302. va_list ap;
  303. va_start(ap, format);
  304. int ret = vsnprintf(str, size, format, ap);
  305. va_end(ap);
  306. return ret;
  307. }
  308. int sprintf(char *str, const char *format, ...)
  309. {
  310. va_list ap;
  311. va_start(ap, format);
  312. int ret = vsprintf(str, format, ap);
  313. va_end(ap);
  314. return ret;
  315. }
  316. int printf(const char *format, ...)
  317. {
  318. va_list ap;
  319. va_start(ap, format);
  320. int ret = vprintf(format, ap);
  321. va_end(ap);
  322. return ret;
  323. }