printf.c 4.5 KB

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