fix dependency check logic cause config always disabled
[lunaix-os.git] / lunaix-os / usr / libc / src / _vprintf.c
1 #include "_mystdio.h"
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdio.h>
6
7 #define NUMBUFSIZ 24
8
9 static const char flag_chars[] = "#0- +";
10
11 #define FLAG_ALT (1 << 0)
12 #define FLAG_ZERO (1 << 1)
13 #define FLAG_LEFTJUSTIFY (1 << 2)
14 #define FLAG_SPACEPOSITIVE (1 << 3)
15 #define FLAG_PLUSPOSITIVE (1 << 4)
16 #define FLAG_NUMERIC (1 << 5)
17 #define FLAG_SIGNED (1 << 6)
18 #define FLAG_NEGATIVE (1 << 7)
19 #define FLAG_ALT2 (1 << 8)
20 #define FLAG_CAPS (1 << 9)
21
22 int
23 __vprintf_internal(char* buffer, const char* fmt, size_t max_len, va_list vargs)
24 {
25     // This sprintf just a random implementation I found it on Internet . lol.
26     //      Of course, with some modifications for porting to LunaixOS :)
27
28     char numbuf[NUMBUFSIZ];
29     uintptr_t ptr = 0;
30     for (; *fmt; ++fmt) {
31         if (max_len && ptr >= max_len - 1) {
32             break;
33         }
34
35         if (*fmt != '%') {
36             buffer[ptr++] = *fmt;
37             continue;
38         }
39
40         // process flags
41         int flags = 0;
42         for (++fmt; *fmt; ++fmt) {
43             const char* flagc = strchr(flag_chars, *fmt);
44             if (flagc) {
45                 flags |= 1 << (flagc - flag_chars);
46             } else {
47                 break;
48             }
49         }
50
51         // process width
52         int width = -1;
53         if (*fmt >= '1' && *fmt <= '9') {
54             for (width = 0; *fmt >= '0' && *fmt <= '9';) {
55                 width = 10 * width + *fmt++ - '0';
56             }
57         } else if (*fmt == '*') {
58             width = va_arg(vargs, int);
59             ++fmt;
60         }
61
62         // process precision
63         int precision = -1;
64         if (*fmt == '.') {
65             ++fmt;
66             if (*fmt >= '0' && *fmt <= '9') {
67                 for (precision = 0; *fmt >= '0' && *fmt <= '9';) {
68                     precision = 10 * precision + *fmt++ - '0';
69                 }
70             } else if (*fmt == '*') {
71                 precision = va_arg(vargs, int);
72                 ++fmt;
73             }
74             if (precision < 0) {
75                 precision = 0;
76             }
77         }
78
79         // process main conversion character
80         int base = 10;
81         unsigned long num = 0;
82         int length = 0;
83         char* data = "";
84     again:
85         switch (*fmt) {
86             case 'l':
87             case 'z':
88                 length = 1;
89                 ++fmt;
90                 goto again;
91             case 'd':
92             case 'i': {
93                 long x = length ? va_arg(vargs, long) : va_arg(vargs, int);
94                 int negative = x < 0 ? FLAG_NEGATIVE : 0;
95                 num = negative ? -x : x;
96                 flags |= FLAG_NUMERIC | FLAG_SIGNED | negative;
97                 break;
98             }
99             case 'u':
100             format_unsigned:
101                 num = length ? va_arg(vargs, unsigned long)
102                              : va_arg(vargs, unsigned);
103                 flags |= FLAG_NUMERIC;
104                 break;
105             case 'b':
106                 base = 2;
107                 goto format_unsigned;
108             case 'x':
109                 base = 16;
110                 goto format_unsigned;
111             case 'X':
112                 flags = flags | FLAG_CAPS;
113                 base = 16;
114                 goto format_unsigned;
115             case 'o':
116                 base = 8;
117                 goto format_unsigned;
118             case 'p':
119                 num = (uintptr_t)va_arg(vargs, void*);
120                 base = 16;
121                 flags |= FLAG_ALT | FLAG_ALT2 | FLAG_NUMERIC;
122                 break;
123             case 's':
124                 data = va_arg(vargs, char*);
125                 break;
126             case 'c':
127                 data = numbuf;
128                 numbuf[0] = va_arg(vargs, int);
129                 numbuf[1] = '\0';
130                 break;
131             default:
132                 data = numbuf;
133                 numbuf[0] = (*fmt ? *fmt : '%');
134                 numbuf[1] = '\0';
135                 if (!*fmt) {
136                     fmt--;
137                 }
138                 break;
139         }
140
141         if (flags & FLAG_NUMERIC) {
142             data = itoa(num, numbuf, base);
143             int i = 0;
144             char c;
145             while ((flags & FLAG_CAPS) && (c = data[i])) {
146                 data[i] = c & ~((c & 0x40) >> 1);
147                 i++;
148             }
149         }
150
151         const char* prefix = "";
152         if ((flags & FLAG_NUMERIC) && (flags & FLAG_SIGNED)) {
153             if (flags & FLAG_NEGATIVE) {
154                 prefix = "-";
155             } else if (flags & FLAG_PLUSPOSITIVE) {
156                 prefix = "+";
157             } else if (flags & FLAG_SPACEPOSITIVE) {
158                 prefix = " ";
159             }
160         } else if ((flags & FLAG_NUMERIC) && (flags & FLAG_ALT) &&
161                    (base == 16 || base == -16) &&
162                    (num || (flags & FLAG_ALT2))) {
163             prefix = "0x";
164         }
165
166         int len;
167         if (precision >= 0 && !(flags & FLAG_NUMERIC)) {
168             len = strnlen(data, precision);
169         } else {
170             len = strlen(data);
171         }
172         int zeros;
173         if ((flags & FLAG_NUMERIC) && precision >= 0) {
174             zeros = precision > len ? precision - len : 0;
175         } else if ((flags & FLAG_NUMERIC) && (flags & FLAG_ZERO) &&
176                    !(flags & FLAG_LEFTJUSTIFY) &&
177                    len + (int)strlen(prefix) < width) {
178             zeros = width - len - strlen(prefix);
179         } else {
180             zeros = 0;
181         }
182         width -= len + zeros + strlen(prefix);
183         for (; !(flags & FLAG_LEFTJUSTIFY) && width > 0; --width) {
184             buffer[ptr++] = ' ';
185         }
186         for (; *prefix; ++prefix) {
187             buffer[ptr++] = *prefix;
188         }
189         for (; zeros > 0; --zeros) {
190             buffer[ptr++] = '0';
191         }
192         for (; len > 0; ++data, --len) {
193             buffer[ptr++] = *data;
194         }
195         for (; width > 0; --width) {
196             buffer[ptr++] = ' ';
197         }
198     }
199     buffer[ptr++] = '\0';
200
201     return ptr;
202 }
203
204 int
205 vsnprintf(char* buffer, unsigned int size, const char* fmt, va_list ap)
206 {
207     return __vprintf_internal(buffer, fmt, size, ap);
208 }
209
210 int
211 snprintf(char* buffer, unsigned int size, const char* fmt, ...)
212 {
213     va_list l;
214     va_start(l, fmt);
215     int r = __vprintf_internal(buffer, fmt, size, l);
216     va_end(l);
217     return r;
218 }