-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprintf.h
189 lines (172 loc) · 6.22 KB
/
printf.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* This is free and unencumbered software released into the public domain.
* See the LICENSE file for additional details.
*
* Designed by Chris Hooper in 2020.
*
* ---------------------------------------------------------------------
*
* printf() and other stdio emulation.
*/
#ifndef _PRINTF_H
#define _PRINTF_H
#include <stdint.h>
#include <stddef.h>
#include <stdarg.h>
#ifdef __cplusplus
extern "C" {
#endif
#define fprintf _stdio_fprintf
#define fputs _stdio_fputs
#define fputc _stdio_fputc
#define fread _stdio_fread
#define getchar _stdio_getchar
// #define printf _stdio_printf
#define putchar _stdio_putchar
#define puts _stdio_puts
#define snprintf _stdio_snprintf
#define sprintf _stdio_sprintf
#define ungetc _stdio_ungetc
#define vprintf _stdio_vprintf
#define vsnprintf _stdio_vsnprintf
#if 1
#include <stdio.h>
#endif
#undef fprintf
#undef fputs
#undef fputc
#undef fread
#undef getchar
#undef printf
#undef putchar
#undef puts
#undef snprintf
#undef sprintf
#undef stdin
#undef stdout
#undef stderr
#undef ungetc
#undef vprintf
#undef vsnprintf
#undef __sputc_r
#undef __sputc
/* Redefine stdio functions and definitions */
#define stdin ((FILE *) 0)
#define stdout ((FILE *) 1)
#define stderr ((FILE *) 2)
#define fileno(x) (x)
#define fflush(x)
/**
* vsnprintf() is a stdio compatible function which operates on a buffer, size
* format string, and variable argument list. Output is directed
* to the specified buffer.
*
* @param [out] buf - A pointer to the buffer where output is to be stored.
* @param [in] size - The length of the buffer where output is to be stored.
* @param [in] fmt - A string describing the format of the output. This
* format string is compatible with that of printf().
* @param [in] ap - A pointer to a variable list of arguments.
*
* @return The number of characters (not including the trailing '\\0')
* which would have been printed to the output buffer if enough
* space had been available. Thus, a return value greater than or
* equal to the given size indicates that the output was truncated.
*/
__attribute__((format(__printf__, 3, 0)))
int vsnprintf(char *buf, size_t size, const char *fmt, va_list ap);
/**
* snprintf() is a stdio compatible function which operates on a buffer, size,
* format string, and variable argument list. Output is directed
* to the specified buffer.
*
* @param [out] buf - A pointer to the buffer where output is to be stored.
* @param [in] size - The length of the buffer where output is to be stored.
* @param [in] fmt - A string describing the format of the output. This
* format string is compatible with that of printf().
* @param [in] ... - A variable list of arguments.
*
* @return The number of characters (not including the trailing '\\0')
* which would have been printed to the output buffer if enough
* space had been available. Thus, a return value greater than or
* equal to the given size indicates that the output was truncated.
*/
__attribute__((format(__printf__, 3, 4)))
int snprintf(char *buf, size_t size, const char *fmt, ...);
/**
* sprintf() is a stdio compatible function which operates on a buffer,
* format string, and variable argument list. Output is directed
* to the specified buffer. A maximum of 80 characters (including the
* terminating '\\0') may be written to the buffer. Use snprintf() to
* write larger buffers.
*
* @param [out] buf - A pointer to the buffer where output is to be stored.
* @param [in] fmt - A string describing the format of the output. This
* format string is compatible with that of printf().
* @param [in] ... - A variable list of arguments.
*
* @return The number of characters (not including the trailing '\\0') that
* would have been written to the buffer if the output was not
* limited to 80 bytes.
*
* @see snprintf();
*/
__attribute__((format(__printf__, 2, 3)))
int sprintf(char *buf, const char *fmt, ...);
/**
* vprintf() is a stdio compatible function which operates on a format
* string and variable argument list. Output is directed to
* the serial console.
*
* @param [in] fmt - A string describing the format of the output. This
* format string is compatible with that of printf().
* @param [in] ap - A pointer to a variable list of arguments.
*
* @return The number of characters written to the serial console.
*/
__attribute__((format(__printf__, 1, 0)))
int vprintf(const char *fmt, va_list ap);
/**
* printf() is a stdio compatible function which operates on a format
* string and variable argument list. Output is directed to
* the serial console.
*
* @param [in] fmt - A string describing the format of the output. This
* format string is compatible with that of printf().
* @param [in] ... - A variable list of arguments.
*
* @return The number of characters written to the serial console.
*/
__attribute__((format(__printf__, 1, 2)))
int printf(const char *fmt, ...);
/*
* scanf() uses a format string to derive specified values from input buffer>
*
* @param [in] str - A file pointer which is used to describe the input
* buffer.
* @param [in] fmt - The scanf() format string to process.
* @param [out] ... - A variable list of arguments.
*
* @return The number of format items successfully matched and assigned.
*/
__attribute__((format(__scanf__, 1, 3)))
int sscanf(const char *str, char const *fmt, ...);
int putchar(int ch);
int puts(const char *str);
#ifdef __cplusplus
}
#endif
#undef DO_PRINTF_TEST
#ifdef DO_PRINTF_TEST
/**
* printf_test() is a quick function to test various printf() features.
* Output may be compared against the stdio printf() function
* by defining the CHECK_AGAINST_HOST_PRINTF macro in printf.h.
*
* This function requires no arguments.
*
* @return None.
*/
int printf_test(void);
#endif
#define EXIT_FAILURE 1
#endif /* _PRINTF_H */