Commit | Line | Data |
---|---|---|
1968fc94 LM |
1 | /* |
2 | File name : fprintf.c | |
3 | Date of creation : 05/12/2022 | |
4 | Version : 1.0 | |
5 | Copyright : Soft'n'design | |
6 | Author : Laurent Mazet <mazet@softndesign.org> | |
7 | ||
8 | Description : This file contains embedded printf | |
9 | ||
10 | History : | |
11 | - initial version | |
12 | */ | |
13 | ||
14 | #include <stdarg.h> | |
15 | #include <stdint.h> | |
16 | #include <unistd.h> | |
17 | ||
18 | #include "fdprintf.h" | |
19 | ||
01f02c41 | 20 | int stdfdin = STDIN_FILENO; |
a8476deb LM |
21 | int stdfdout = STDOUT_FILENO; |
22 | int stdfderr = STDERR_FILENO; | |
1968fc94 LM |
23 | |
24 | unsigned int nextpow (unsigned int x, int base) { | |
25 | unsigned int n = 0; | |
26 | while (x) { | |
27 | n++; | |
28 | x = x / base; | |
29 | } | |
30 | return (n == 0) ? 1 : n; | |
31 | } | |
32 | ||
95834c7b LM |
33 | char *itoa (char *str, unsigned u, unsigned int sz) |
34 | { | |
35 | unsigned int i = (sz == 0 ) ? nextpow (u, 10) : sz; | |
36 | char *s = str; | |
37 | while (i > 0) { | |
38 | str[i - 1] = '0' + (u % 10); | |
39 | u /= 10; | |
40 | i--; | |
41 | s++; | |
42 | } | |
43 | return s; | |
44 | } | |
45 | ||
8148aaf1 LM |
46 | double tenpower(int n) |
47 | { | |
48 | double t = 1.0; | |
49 | int i; | |
50 | for (i = 0; i < n; i++) { | |
51 | t *= 10; | |
52 | } | |
53 | for (i = 0; i > n; i--) { | |
54 | t /= 10; | |
55 | } | |
56 | return t; | |
57 | } | |
58 | ||
59 | int getexponant (double *f, int maxexp) | |
60 | { | |
61 | int exp = 0; | |
62 | while (*f > 10) { | |
63 | *f /= 10; | |
64 | exp++; | |
65 | } | |
66 | while (*f < 1) { | |
67 | *f *= 10; | |
68 | exp--; | |
69 | } | |
70 | *f += tenpower (maxexp - 1); | |
71 | if (*f >= 10) { | |
72 | *f /= 10; | |
73 | exp++; | |
74 | } | |
75 | ||
76 | return exp; | |
77 | } | |
1968fc94 LM |
78 | /* simple fprintf function */ |
79 | ||
80 | int fdprintf (int fd, const char *fmt, ...) | |
81 | { | |
82 | char buffer[1024 + 1] = { 0 }; | |
83 | char *str = buffer; | |
84 | ||
85 | va_list ap; | |
86 | va_start (ap, fmt); | |
87 | while (*fmt) { | |
88 | char *s; | |
95834c7b | 89 | double f = 0.0; |
1968fc94 LM |
90 | int d = 0; |
91 | unsigned int u; | |
92 | char c = *fmt++; | |
93 | ||
94 | /* copy standard char */ | |
95 | if (c != '%') { | |
96 | *str++ = c; | |
97 | } else { | |
98 | int t = 0; | |
c04d8fdf | 99 | char w = '0'; |
1968fc94 LM |
100 | int i, sz = 0; |
101 | void *p = NULL; | |
102 | ||
103 | /* stamp */ | |
104 | if ((*fmt == ' ') || (*fmt == '0')) { | |
105 | w = *fmt++; | |
106 | } | |
107 | ||
108 | /* size */ | |
109 | if ((*fmt >= '1') && (*fmt <= '9')) { | |
110 | sz = *fmt++ - '0'; | |
111 | } | |
112 | ||
113 | /* process format char */ | |
114 | switch (*fmt++) { | |
115 | case 'c': /* char */ | |
116 | c = (char) va_arg (ap, int); | |
117 | *str++ = c; | |
118 | break; | |
119 | case 'd': /* int */ | |
120 | d = va_arg (ap, int); | |
121 | if (d < 0) { | |
122 | *str++ = '-'; | |
123 | d = -d; | |
124 | } | |
125 | t = 1; | |
126 | /* fall through */ | |
127 | case 'u': /* unsigned int */ | |
95834c7b LM |
128 | str = itoa (str, (t) ? (unsigned int)d : va_arg (ap, unsigned int), 0); |
129 | break; | |
130 | case 'f': /* float */ | |
131 | f = va_arg (ap, double); | |
c04d8fdf LM |
132 | if (f == 0) { |
133 | *str++ = '0'; | |
134 | break; | |
135 | } | |
95834c7b LM |
136 | if (f < 0) { |
137 | *str++ = '-'; | |
138 | f = -f; | |
139 | } | |
8148aaf1 | 140 | t = getexponant (&f, -6); |
95834c7b LM |
141 | u = (int)f; |
142 | str = itoa (str, u, 0); | |
8148aaf1 | 143 | d = (int)((f - u) * tenpower (6)); |
95834c7b LM |
144 | if (d > 0) { |
145 | *str++ = '.'; | |
146 | str = itoa (str, d, 6); | |
147 | } | |
8148aaf1 LM |
148 | while (*(str - 1) == '0') { |
149 | str--; | |
150 | } | |
95834c7b LM |
151 | if (t != 0) { |
152 | *str++ = 'e'; | |
153 | if (t < 0) { | |
154 | *str++ = '-'; | |
155 | t = -t; | |
156 | } | |
157 | str = itoa (str, t, 0); | |
1968fc94 | 158 | } |
1968fc94 LM |
159 | break; |
160 | case 'p': /* pointer */ | |
161 | *str++ = '0'; | |
162 | *str++ = 'x'; | |
163 | w = '0'; | |
164 | sz = sizeof (void *) * 2; | |
165 | p = va_arg (ap, void *); | |
166 | /* fall through */ | |
167 | case 'x': /* integer hexa */ | |
168 | if (!p) { | |
169 | u = va_arg (ap, unsigned int); | |
170 | if (sz == 0) { | |
171 | sz = nextpow (u, 16); | |
172 | } | |
173 | } else { | |
174 | u = (uintptr_t)p; | |
175 | } | |
176 | for (i = sz, t = 1; i > 0; i--) { | |
177 | char x = (char)((u >> (i * 4 - 4)) & 0xf); | |
178 | if ((t == 1) && (x == 0)) { | |
179 | *str++ = w; | |
180 | } else { | |
181 | *str++ = (x > 9) ? 'a' + x - 10 : '0' + x; | |
182 | t = 0; | |
183 | } | |
184 | } | |
185 | break; | |
186 | case 's': /* string */ | |
187 | s = va_arg (ap, char *); | |
188 | while (s && *s) | |
189 | *str++ = *s++; | |
190 | break; | |
191 | default: | |
192 | *str++ = '?'; | |
193 | } | |
194 | } | |
195 | } | |
196 | va_end (ap); | |
197 | ||
198 | /* output string */ | |
199 | int n = str - buffer; | |
200 | if (n < (int)sizeof (buffer) - 1) { | |
201 | return write (fd, buffer, n); | |
202 | } | |
203 | return 0; | |
204 | } | |
205 | ||
206 | /* vim: set ts=4 sw=4 et: */ |