correct issue when using %f with 0
[calc.git] / fdprintf.c
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
20 int stdfdin = STDIN_FILENO;
21 int stdfdout = STDOUT_FILENO;
22 int stdfderr = STDERR_FILENO;
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
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
46 /* simple fprintf function */
47
48 int fdprintf (int fd, const char *fmt, ...)
49 {
50 char buffer[1024 + 1] = { 0 };
51 char *str = buffer;
52
53 va_list ap;
54 va_start (ap, fmt);
55 while (*fmt) {
56 char *s;
57 double f = 0.0;
58 int d = 0;
59 unsigned int u;
60 char c = *fmt++;
61
62 /* copy standard char */
63 if (c != '%') {
64 *str++ = c;
65 } else {
66 int t = 0;
67 char w = '0';
68 int i, sz = 0;
69 void *p = NULL;
70
71 /* stamp */
72 if ((*fmt == ' ') || (*fmt == '0')) {
73 w = *fmt++;
74 }
75
76 /* size */
77 if ((*fmt >= '1') && (*fmt <= '9')) {
78 sz = *fmt++ - '0';
79 }
80
81 /* process format char */
82 switch (*fmt++) {
83 case 'c': /* char */
84 c = (char) va_arg (ap, int);
85 *str++ = c;
86 break;
87 case 'd': /* int */
88 d = va_arg (ap, int);
89 if (d < 0) {
90 *str++ = '-';
91 d = -d;
92 }
93 t = 1;
94 /* fall through */
95 case 'u': /* unsigned int */
96 str = itoa (str, (t) ? (unsigned int)d : va_arg (ap, unsigned int), 0);
97 break;
98 case 'f': /* float */
99 f = va_arg (ap, double);
100 if (f == 0) {
101 *str++ = '0';
102 break;
103 }
104 if (f < 0) {
105 *str++ = '-';
106 f = -f;
107 }
108 t = 0;
109 while (f > 10) {
110 f /= 10;
111 t++;
112 }
113 while (f < 1) {
114 f *= 10;
115 t--;
116 }
117 f += 1e-7;
118 if (f >= 10) {
119 f /= 10;
120 t++;
121 }
122 u = (int)f;
123 str = itoa (str, u, 0);
124 d = (int)((f - u) * 1e6);
125 if (d > 0) {
126 *str++ = '.';
127 str = itoa (str, d, 6);
128 }
129 if (t != 0) {
130 *str++ = 'e';
131 if (t < 0) {
132 *str++ = '-';
133 t = -t;
134 }
135 str = itoa (str, t, 0);
136 }
137 break;
138 case 'p': /* pointer */
139 *str++ = '0';
140 *str++ = 'x';
141 w = '0';
142 sz = sizeof (void *) * 2;
143 p = va_arg (ap, void *);
144 /* fall through */
145 case 'x': /* integer hexa */
146 if (!p) {
147 u = va_arg (ap, unsigned int);
148 if (sz == 0) {
149 sz = nextpow (u, 16);
150 }
151 } else {
152 u = (uintptr_t)p;
153 }
154 for (i = sz, t = 1; i > 0; i--) {
155 char x = (char)((u >> (i * 4 - 4)) & 0xf);
156 if ((t == 1) && (x == 0)) {
157 *str++ = w;
158 } else {
159 *str++ = (x > 9) ? 'a' + x - 10 : '0' + x;
160 t = 0;
161 }
162 }
163 break;
164 case 's': /* string */
165 s = va_arg (ap, char *);
166 while (s && *s)
167 *str++ = *s++;
168 break;
169 default:
170 *str++ = '?';
171 }
172 }
173 }
174 va_end (ap);
175
176 /* output string */
177 int n = str - buffer;
178 if (n < (int)sizeof (buffer) - 1) {
179 return write (fd, buffer, n);
180 }
181 return 0;
182 }
183
184 /* vim: set ts=4 sw=4 et: */