printf.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2010-11-17 Bernard first version
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <sys/fcntl.h>
  13. #include <finsh.h>
  14. char *format[] =
  15. {
  16. "%",
  17. "%0.",
  18. "%.0",
  19. "%+0.",
  20. "%+.0",
  21. "%.5",
  22. "%+.5",
  23. "%2.5",
  24. "%22.5",
  25. "%022.5",
  26. "%#022.5",
  27. "%-#022.5",
  28. "%+#022.5",
  29. "%-22.5",
  30. "%+22.5",
  31. "%--22.5",
  32. "%++22.5",
  33. "%+-22.5",
  34. "%-+22.5",
  35. "%-#022.5",
  36. "%-#22.5",
  37. "%-2.22",
  38. "%+2.22",
  39. "%-#02.22",
  40. "%-#2.22",
  41. "%-1.5",
  42. "%1.5",
  43. "%-#01.5",
  44. "%-#1.5",
  45. "%-#.5",
  46. "%-#1.",
  47. "%-#.",
  48. NULL
  49. };
  50. static void
  51. intchk(const char *fmt)
  52. {
  53. (void) printf("%15s :, \"", fmt);
  54. (void) printf(fmt, 0);
  55. (void) printf("\", \"");
  56. (void) printf(fmt, 123);
  57. (void) printf("\", \"");
  58. (void) printf(fmt, -18);
  59. (void) printf("\"\n");
  60. }
  61. static void
  62. fltchk(const char *fmt)
  63. {
  64. (void) printf("%15s :, \"", fmt);
  65. (void) printf(fmt, 0.0);
  66. (void) printf("\", \"");
  67. (void) printf(fmt, 123.0001);
  68. (void) printf("\", \"");
  69. (void) printf(fmt, -18.0002301);
  70. (void) printf("\"\n");
  71. }
  72. int printf_test()
  73. {
  74. char buf[256];
  75. int i;
  76. printf("%s\n\n", "# vim:syntax=off:");
  77. /* integers */
  78. for (i = 0; format[i]; i++)
  79. {
  80. strcpy(buf, format[i]);
  81. strcat(buf, "d");
  82. intchk(buf);
  83. }
  84. /* floats */
  85. for (i = 0; format[i]; i++)
  86. {
  87. strcpy(buf, format[i]);
  88. strcat(buf, "f");
  89. fltchk(buf);
  90. }
  91. /* hexa */
  92. for (i = 0; format[i]; i++)
  93. {
  94. strcpy(buf, format[i]);
  95. strcat(buf, "x");
  96. intchk(buf);
  97. }
  98. printf("#%.4x %4x#\n", 4, 88);
  99. printf("#%4x#\n", 4);
  100. printf("#%#22.8x#\n", 1234567);
  101. printf("#%+2i#\n", 18);
  102. printf("#%i#\n", 18);
  103. printf("#%llu#\n", 4294967297ULL);
  104. printf("#%#x#\n", 44444);
  105. printf("#%-8i#\n", 33);
  106. printf("#%i#\n", 18);
  107. printf("#%d#\n", 18);
  108. printf("#%u#\n", 18);
  109. printf("#%lu#\n", 18);
  110. printf("#%li#\n", 18);
  111. printf("#%-+#06d#\n", -123);
  112. printf("#%-+#6d#\n", -123);
  113. printf("#%+#06d#\n", -123);
  114. printf("#%06d#\n", -123);
  115. printf("#%+15s#\n", "ABCDEF");
  116. /* from ncurses make_keys */
  117. printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 16, 16, "KEY_A1", "key_a1");
  118. printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 16, 2, "KEY_A1", "key_a1");
  119. printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 2, 16, "KEY_A1", "key_a1");
  120. printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 16, 0, "KEY_A1", "key_a1");
  121. printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 0, 16, "KEY_A1", "key_a1");
  122. printf("{ %4d, %-*.*s },\t/* %s */\n", 139, 0, 0, "KEY_A1", "key_a1");
  123. printf("{ %4d, %*.*s },\t/* %s */\n", 139, 16, 16, "KEY_A1", "key_a1");
  124. printf("{ %4d, %*.*s },\t/* %s */\n", 139, 16, 2, "KEY_A1", "key_a1");
  125. printf("{ %4d, %*.*s },\t/* %s */\n", 139, 2, 16, "KEY_A1", "key_a1");
  126. printf("{ %4d, %*.*s },\t/* %s */\n", 139, 16, 0, "KEY_A1", "key_a1");
  127. printf("{ %4d, %*.*s },\t/* %s */\n", 139, 0, 16, "KEY_A1", "key_a1");
  128. printf("{ %4d, %*.*s },\t/* %s */\n", 139, 0, 0, "KEY_A1", "key_a1");
  129. printf("%*.*f\n", 0, 16, 0.0);
  130. printf("%*.*f\n", 16, 16, 0.0);
  131. printf("%*.*f\n", 2, 2, -0.0);
  132. printf("%*.*f\n", 20, 0, -123.123);
  133. printf("%*.*f\n", 10, 0, +123.123);
  134. i = printf("\"%s\"\n", "A");
  135. printf("%i\n", i);
  136. /* from glibc's tst-printf.c */
  137. {
  138. char buf[20];
  139. char buf2[512];
  140. int i;
  141. printf("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
  142. snprintf(buf, sizeof(buf), "%30s", "foo"), (int) sizeof(buf),
  143. buf);
  144. memset(buf2, 0, sizeof(buf));
  145. i = snprintf(buf2, 256, "%.9999u", 10);
  146. printf("%i %i\n", i, strlen(buf2));
  147. printf("snprintf (\"%%.999999u\", 10) == %d\n",
  148. snprintf(buf2, sizeof(buf2), "%.999999u", 10));
  149. }
  150. return 0;
  151. }
  152. void libc_printf()
  153. {
  154. printf("stdout test!!\n");
  155. fprintf(stdout, "fprintf test!!\n");
  156. fprintf(stderr, "fprintf test!!\n");
  157. puts("puts test!!\n");
  158. putc('1', stderr);
  159. putc('2', stderr);
  160. putc('\n', stderr);
  161. printf_test();
  162. }
  163. FINSH_FUNCTION_EXPORT(libc_printf, printf test in libc);
  164. void libc_dprintf()
  165. {
  166. int fd;
  167. fd = open("/dev/console", O_WRONLY, 0);
  168. if (fd > 0)
  169. {
  170. dprintf(fd, "fd:%d printf test!!\n", fd);
  171. close(fd);
  172. }
  173. }
  174. FINSH_FUNCTION_EXPORT(libc_dprintf, dprintf test);
  175. void libc_fdopen()
  176. {
  177. int fd;
  178. FILE* fp;
  179. fd = open("/dev/console", O_WRONLY, 0);
  180. if (fd > 0)
  181. {
  182. fp = fdopen(fd, "w");
  183. fprintf(fp, "fdopen test, fd %d!!\n", fileno(fp));
  184. fclose(fp);
  185. }
  186. }
  187. FINSH_FUNCTION_EXPORT(libc_fdopen, fdopen test);