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