Skip to content

Commit 76ca43f

Browse files
committed
Rewrite with GNU C
1 parent 49aa479 commit 76ca43f

File tree

7 files changed

+812
-361
lines changed

7 files changed

+812
-361
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ $(error config.mk is missing. Please run gen-config step before building.)
33
endif
44
include config.mk
55
all :
6-
$(CC) $(CFLAGS) -std=c11 cprintf.c main.c -o cprintf
6+
$(CC) $(CFLAGS) -std=gnu11 cprintf.c main.c -o cprintf
77
$(STRIP) cprintf
88
format :
99
clang-format -i include/cprintf.h cprintf.c test.c main.c
1010
test :
11-
$(CC) -O0 -Wall -Wextra -Wgnu -std=c11 -pedantic -ggdb -fsanitize=address cprintf.c test.c
11+
$(CC) -O0 -Wall -Wno-format-extra-args -Wextra -Wno-format-security -std=gnu11 -Wno-gnu-zero-variadic-macro-arguments -Wno-gnu-statement-expression-from-macro-expansion -pedantic -ggdb -fsanitize=address cprintf.c test.c

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ cprintf("{}{}{}{}","xxxxx"); // Also boom!!!!!!
1717
printf("%s","xxxxx"); // Good!
1818
cprintf("{}","xxxxxx"); // Also good :)
1919
```
20+
# About GNU C:
21+
cprintf uses GNU C11, port/cprintf.c is in pure C std.
2022
# Let's make a colorful world!
2123
# What's this?
2224
We often use ASCII color like `\033[0m` when developing. But it's hard to remember them.

cprintf.c

Lines changed: 25 additions & 247 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,28 @@
2828
*
2929
*/
3030
#include "include/cprintf.h"
31-
#define cprintf_strlen(f) (f == NULL ? 0 : strlen(f))
32-
#define cprintf_avoid_null(f) (f == NULL ? "" : f)
33-
#define cprintf_buf_len(f, d) (f != NULL ? (size_t)snprintf(NULL, 0, f, d) : 0)
31+
32+
//
33+
// Generic support.
34+
//
35+
36+
// For marking the buffer, so that it can be free() later.
3437
static thread_local char **cprintf_buffer = NULL;
3538
static thread_local size_t cprintf_buf_count = 0;
36-
static void cprintf_mark_buf(char *b)
39+
void cprintf_mark_buf(char *b)
3740
{
41+
/*
42+
* Mark the buffer, so that it can be free() later.
43+
*/
3844
cprintf_buffer = realloc(cprintf_buffer, (cprintf_buf_count + 1) * sizeof(char *));
3945
cprintf_buffer[cprintf_buf_count] = b;
4046
cprintf_buf_count++;
4147
}
4248
void cprintf_free_buf(void)
4349
{
50+
/*
51+
* Free all the buffers that have been marked.
52+
*/
4453
for (size_t i = 0; i < cprintf_buf_count; i++) {
4554
free(cprintf_buffer[i]);
4655
}
@@ -50,6 +59,11 @@ void cprintf_free_buf(void)
5059
}
5160
char *cprintf_regen_format(const char *f)
5261
{
62+
/*
63+
* This function will regenerate the format string
64+
* to replace all '{}' with '%s'.
65+
* If the input is NULL, it will return an empty string.
66+
*/
5367
char *ret = strdup(cprintf_avoid_null(f));
5468
int j = 0;
5569
size_t len = cprintf_strlen(f);
@@ -76,246 +90,11 @@ char *cprintf_regen_format(const char *f)
7690
cprintf_mark_buf(ret);
7791
return ret;
7892
}
79-
char *cprintf_get_fmt_unknown(const char *f)
80-
{
81-
(void)f; // Unused parameter
82-
return NULL;
83-
}
84-
char *cprintf_get_fmt_bool(const char *f)
85-
{
86-
(void)f; // Unused parameter
87-
return NULL;
88-
}
89-
char *cprintf_get_fmt_char(const char *f)
90-
{
91-
char *b = malloc(cprintf_strlen(f) + 16);
92-
sprintf(b, "%%%sc", cprintf_avoid_null(f));
93-
return b;
94-
}
95-
char *cprintf_get_fmt_schar(const char *f)
96-
{
97-
char *b = malloc(cprintf_strlen(f) + 16);
98-
sprintf(b, "%%%sd", cprintf_avoid_null(f));
99-
return b;
100-
}
101-
char *cprintf_get_fmt_uchar(const char *f)
102-
{
103-
char *b = malloc(cprintf_strlen(f) + 16);
104-
sprintf(b, "%%%sd", cprintf_avoid_null(f));
105-
return b;
106-
}
107-
char *cprintf_get_fmt_short(const char *f)
108-
{
109-
char *b = malloc(cprintf_strlen(f) + 16);
110-
sprintf(b, "%%%shd", cprintf_avoid_null(f));
111-
return b;
112-
}
113-
char *cprintf_get_fmt_ushort(const char *f)
114-
{
115-
char *b = malloc(cprintf_strlen(f) + 16);
116-
sprintf(b, "%%%shu", cprintf_avoid_null(f));
117-
return b;
118-
}
119-
char *cprintf_get_fmt_int(const char *f)
120-
{
121-
char *b = malloc(cprintf_strlen(f) + 16);
122-
sprintf(b, "%%%sd", cprintf_avoid_null(f));
123-
return b;
124-
}
125-
char *cprintf_get_fmt_uint(const char *f)
126-
{
127-
char *b = malloc(cprintf_strlen(f) + 16);
128-
sprintf(b, "%%%su", cprintf_avoid_null(f));
129-
return b;
130-
}
131-
char *cprintf_get_fmt_long(const char *f)
132-
{
133-
char *b = malloc(cprintf_strlen(f) + 16);
134-
sprintf(b, "%%%sld", cprintf_avoid_null(f));
135-
return b;
136-
}
137-
char *cprintf_get_fmt_ulong(const char *f)
138-
{
139-
char *b = malloc(cprintf_strlen(f) + 16);
140-
sprintf(b, "%%%slu", cprintf_avoid_null(f));
141-
return b;
142-
}
143-
char *cprintf_get_fmt_llong(const char *f)
144-
{
145-
char *b = malloc(cprintf_strlen(f) + 16);
146-
sprintf(b, "%%%slld", cprintf_avoid_null(f));
147-
return b;
148-
}
149-
char *cprintf_get_fmt_ullong(const char *f)
150-
{
151-
char *b = malloc(cprintf_strlen(f) + 16);
152-
sprintf(b, "%%%sllu", cprintf_avoid_null(f));
153-
return b;
154-
}
155-
char *cprintf_get_fmt_float(const char *f)
156-
{
157-
char *b = malloc(cprintf_strlen(f) + 16);
158-
sprintf(b, "%%%sf", cprintf_avoid_null(f));
159-
return b;
160-
}
161-
char *cprintf_get_fmt_double(const char *f)
162-
{
163-
char *b = malloc(cprintf_strlen(f) + 16);
164-
sprintf(b, "%%%sf", cprintf_avoid_null(f));
165-
return b;
166-
}
167-
char *cprintf_get_fmt_ldouble(const char *f)
168-
{
169-
char *b = malloc(cprintf_strlen(f) + 16);
170-
sprintf(b, "%%%sLf", cprintf_avoid_null(f));
171-
return b;
172-
}
173-
char *cprintf_get_fmt_ptr(const char *f)
174-
{
175-
char *b = malloc(cprintf_strlen(f) + 16);
176-
sprintf(b, "%%%sp", cprintf_avoid_null(f));
177-
return b;
178-
}
179-
180-
char *cprintf_get_string_bool(char *f, bool d)
181-
{
182-
char *buf = malloc(32);
183-
sprintf(buf, "%s", d ? "true" : "false");
184-
free(f);
185-
cprintf_mark_buf(buf);
186-
return buf;
187-
}
188-
char *cprintf_get_string_char(char *f, char d)
189-
{
190-
char *buf = malloc(cprintf_buf_len(f, d) + 32);
191-
sprintf(buf, f, d);
192-
free(f);
193-
cprintf_mark_buf(buf);
194-
return buf;
195-
}
196-
char *cprintf_get_string_schar(char *f, signed char d)
197-
{
198-
char *buf = malloc(cprintf_buf_len(f, d) + 32);
199-
sprintf(buf, f, d);
200-
free(f);
201-
cprintf_mark_buf(buf);
202-
return buf;
203-
}
204-
char *cprintf_get_string_uchar(char *f, unsigned char d)
205-
{
206-
char *buf = malloc(cprintf_buf_len(f, d) + 32);
207-
sprintf(buf, f, d);
208-
free(f);
209-
cprintf_mark_buf(buf);
210-
return buf;
211-
}
212-
char *cprintf_get_string_short(char *f, short d)
213-
{
214-
char *buf = malloc(cprintf_buf_len(f, d) + 32);
215-
sprintf(buf, f, d);
216-
free(f);
217-
cprintf_mark_buf(buf);
218-
return buf;
219-
}
220-
char *cprintf_get_string_ushort(char *f, unsigned short d)
221-
{
222-
char *buf = malloc(cprintf_buf_len(f, d) + 32);
223-
sprintf(buf, f, d);
224-
free(f);
225-
cprintf_mark_buf(buf);
226-
return buf;
227-
}
228-
char *cprintf_get_string_int(char *f, int d)
229-
{
230-
char *buf = malloc(cprintf_buf_len(f, d) + 32);
231-
sprintf(buf, f, d);
232-
free(f);
233-
cprintf_mark_buf(buf);
234-
return buf;
235-
}
236-
char *cprintf_get_string_uint(char *f, unsigned int d)
237-
{
238-
char *buf = malloc(cprintf_buf_len(f, d) + 32);
239-
sprintf(buf, f, d);
240-
free(f);
241-
cprintf_mark_buf(buf);
242-
return buf;
243-
}
244-
char *cprintf_get_string_long(char *f, long d)
245-
{
246-
char *buf = malloc(cprintf_buf_len(f, d) + 32);
247-
sprintf(buf, f, d);
248-
free(f);
249-
cprintf_mark_buf(buf);
250-
return buf;
251-
}
252-
char *cprintf_get_string_ulong(char *f, unsigned long d)
253-
{
254-
char *buf = malloc(cprintf_buf_len(f, d) + 32);
255-
sprintf(buf, f, d);
256-
free(f);
257-
cprintf_mark_buf(buf);
258-
return buf;
259-
}
260-
char *cprintf_get_string_llong(char *f, long long d)
261-
{
262-
char *buf = malloc(cprintf_buf_len(f, d) + 32);
263-
sprintf(buf, f, d);
264-
free(f);
265-
cprintf_mark_buf(buf);
266-
return buf;
267-
}
268-
char *cprintf_get_string_ullong(char *f, unsigned long long d)
269-
{
270-
char *buf = malloc(cprintf_buf_len(f, d) + 32);
271-
sprintf(buf, f, d);
272-
free(f);
273-
cprintf_mark_buf(buf);
274-
return buf;
275-
}
276-
char *cprintf_get_string_float(char *f, float d)
277-
{
278-
char *buf = malloc(cprintf_buf_len(f, d) + 32);
279-
sprintf(buf, f, d);
280-
free(f);
281-
cprintf_mark_buf(buf);
282-
return buf;
283-
}
284-
char *cprintf_get_string_double(char *f, double d)
285-
{
286-
char *buf = malloc(cprintf_buf_len(f, d) + 32);
287-
sprintf(buf, f, d);
288-
free(f);
289-
cprintf_mark_buf(buf);
290-
return buf;
291-
}
292-
char *cprintf_get_string_ldouble(char *f, long double d)
293-
{
294-
char *buf = malloc(cprintf_buf_len(f, d) + 32);
295-
sprintf(buf, f, d);
296-
free(f);
297-
cprintf_mark_buf(buf);
298-
return buf;
299-
}
300-
char *cprintf_get_string_ptr(char *f, void *d)
301-
{
302-
char *buf = malloc(cprintf_buf_len(f, d) + 32);
303-
sprintf(buf, f, d);
304-
free(f);
305-
cprintf_mark_buf(buf);
306-
return buf;
307-
}
308-
char *cprintf_get_string_unknown(char *f, ...)
309-
{
310-
char *buf = malloc(32);
311-
sprintf(buf, "%s", "{unknown type}");
312-
free(f);
313-
cprintf_mark_buf(buf);
314-
return buf;
315-
}
93+
//
94+
// Color support.
95+
//
31696
char *cprintf_base_color = "254;228;208";
31797
bool cprintf_print_color_only_tty = true;
318-
#ifdef _GNU_SOURCE
31998
#define fprintf_only_tty(stream, ...) \
32099
{ \
321100
if (!cprintf_print_color_only_tty) { \
@@ -327,9 +106,6 @@ bool cprintf_print_color_only_tty = true;
327106
} \
328107
} \
329108
}
330-
#else
331-
#define fprintf_if_not_fifo(stream, ...) fprintf(stream, __VA_ARGS__)
332-
#endif
333109
static void fprint_rgb_fg_color(FILE *_Nonnull stream, const char *_Nonnull color)
334110
{
335111
/*
@@ -491,7 +267,7 @@ static const char *cfprintf_print_bg_color(FILE *_Nonnull stream, const char *_N
491267
}
492268
return ret;
493269
}
494-
void cprintf__(const char *_Nonnull buf)
270+
int cprintf__(const char *_Nonnull buf)
495271
{
496272
const char *p = NULL;
497273
p = buf;
@@ -514,8 +290,9 @@ void cprintf__(const char *_Nonnull buf)
514290
// We will always reset the color in the end.
515291
fprintf_only_tty(stdout, "\033[0m");
516292
fflush(stdout);
293+
return 114514;
517294
}
518-
void cfprintf__(FILE *_Nonnull stream, const char *_Nonnull buf)
295+
int cfprintf__(FILE *_Nonnull stream, const char *_Nonnull buf)
519296
{
520297
const char *p = NULL;
521298
p = buf;
@@ -538,4 +315,5 @@ void cfprintf__(FILE *_Nonnull stream, const char *_Nonnull buf)
538315
// We will always reset the color in the end.
539316
fprintf_only_tty(stream, "\033[0m");
540317
fflush(stream);
318+
return 114514;
541319
}

0 commit comments

Comments
 (0)