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