xref: /openbmc/u-boot/include/vsprintf.h (revision 60570df1)
1 /*
2  * (C) Copyright 2000-2009
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #ifndef __VSPRINTF_H
9 #define __VSPRINTF_H
10 
11 ulong simple_strtoul(const char *cp, char **endp, unsigned int base);
12 
13 /**
14  * strict_strtoul - convert a string to an unsigned long strictly
15  * @param cp	The string to be converted
16  * @param base	The number base to use
17  * @param res	The converted result value
18  * @return 0 if conversion is successful and *res is set to the converted
19  * value, otherwise it returns -EINVAL and *res is set to 0.
20  *
21  * strict_strtoul converts a string to an unsigned long only if the
22  * string is really an unsigned long string, any string containing
23  * any invalid char at the tail will be rejected and -EINVAL is returned,
24  * only a newline char at the tail is acceptible because people generally
25  * change a module parameter in the following way:
26  *
27  *      echo 1024 > /sys/module/e1000/parameters/copybreak
28  *
29  * echo will append a newline to the tail.
30  *
31  * simple_strtoul just ignores the successive invalid characters and
32  * return the converted value of prefix part of the string.
33  *
34  * Copied this function from Linux 2.6.38 commit ID:
35  * 521cb40b0c44418a4fd36dc633f575813d59a43d
36  *
37  */
38 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
39 unsigned long long simple_strtoull(const char *cp, char **endp,
40 					unsigned int base);
41 long simple_strtol(const char *cp, char **endp, unsigned int base);
42 
43 /**
44  * panic() - Print a message and reset/hang
45  *
46  * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
47  * defined, then it will hang instead of reseting.
48  *
49  * @param fmt:	printf() format string for message, which should not include
50  *		\n, followed by arguments
51  */
52 void panic(const char *fmt, ...)
53 		__attribute__ ((format (__printf__, 1, 2), noreturn));
54 
55 /**
56  * panic_str() - Print a message and reset/hang
57  *
58  * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
59  * defined, then it will hang instead of reseting.
60  *
61  * This function can be used instead of panic() when your board does not
62  * already use printf(), * to keep code size small.
63  *
64  * @param fmt:	string to display, which should not include \n
65  */
66 void panic_str(const char *str) __attribute__ ((noreturn));
67 
68 /**
69  * Format a string and place it in a buffer
70  *
71  * @param buf	The buffer to place the result into
72  * @param fmt	The format string to use
73  * @param ...	Arguments for the format string
74  *
75  * The function returns the number of characters written
76  * into @buf.
77  *
78  * See the vsprintf() documentation for format string extensions over C99.
79  */
80 int sprintf(char *buf, const char *fmt, ...)
81 		__attribute__ ((format (__printf__, 2, 3)));
82 
83 /**
84  * Format a string and place it in a buffer (va_list version)
85  *
86  * @param buf	The buffer to place the result into
87  * @param size	The size of the buffer, including the trailing null space
88  * @param fmt	The format string to use
89  * @param args	Arguments for the format string
90  * @return the number of characters which have been written into
91  * the @buf not including the trailing '\0'. If @size is == 0 the function
92  * returns 0.
93  *
94  * If you're not already dealing with a va_list consider using scnprintf().
95  *
96  * See the vsprintf() documentation for format string extensions over C99.
97  */
98 int vsprintf(char *buf, const char *fmt, va_list args);
99 char *simple_itoa(ulong i);
100 
101 #ifdef CONFIG_SYS_VSNPRINTF
102 /**
103  * Format a string and place it in a buffer
104  *
105  * @param buf	The buffer to place the result into
106  * @param size	The size of the buffer, including the trailing null space
107  * @param fmt	The format string to use
108  * @param ...	Arguments for the format string
109  * @return the number of characters which would be
110  * generated for the given input, excluding the trailing null,
111  * as per ISO C99.  If the return is greater than or equal to
112  * @size, the resulting string is truncated.
113  *
114  * See the vsprintf() documentation for format string extensions over C99.
115  */
116 int snprintf(char *buf, size_t size, const char *fmt, ...)
117 		__attribute__ ((format (__printf__, 3, 4)));
118 
119 /**
120  * Format a string and place it in a buffer
121  *
122  * @param buf	The buffer to place the result into
123  * @param size	The size of the buffer, including the trailing null space
124  * @param fmt	The format string to use
125  * @param ...	Arguments for the format string
126  *
127  * The return value is the number of characters written into @buf not including
128  * the trailing '\0'. If @size is == 0 the function returns 0.
129  *
130  * See the vsprintf() documentation for format string extensions over C99.
131  */
132 int scnprintf(char *buf, size_t size, const char *fmt, ...)
133 		__attribute__ ((format (__printf__, 3, 4)));
134 
135 /**
136  * Format a string and place it in a buffer (base function)
137  *
138  * @param buf	The buffer to place the result into
139  * @param size	The size of the buffer, including the trailing null space
140  * @param fmt	The format string to use
141  * @param args	Arguments for the format string
142  * @return The number characters which would be generated for the given
143  * input, excluding the trailing '\0', as per ISO C99. Note that fewer
144  * characters may be written if this number of characters is >= size.
145  *
146  * This function follows C99 vsnprintf, but has some extensions:
147  * %pS output the name of a text symbol
148  * %pF output the name of a function pointer
149  * %pR output the address range in a struct resource
150  *
151  * The function returns the number of characters which would be
152  * generated for the given input, excluding the trailing '\0',
153  * as per ISO C99.
154  *
155  * Call this function if you are already dealing with a va_list.
156  * You probably want snprintf() instead.
157  */
158 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
159 
160 /**
161  * Format a string and place it in a buffer (va_list version)
162  *
163  * @param buf	The buffer to place the result into
164  * @param size	The size of the buffer, including the trailing null space
165  * @param fmt	The format string to use
166  * @param args	Arguments for the format string
167  * @return the number of characters which have been written into
168  * the @buf not including the trailing '\0'. If @size is == 0 the function
169  * returns 0.
170  *
171  * If you're not already dealing with a va_list consider using scnprintf().
172  *
173  * See the vsprintf() documentation for format string extensions over C99.
174  */
175 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
176 #else
177 /*
178  * Use macros to silently drop the size parameter. Note that the 'cn'
179  * versions are the same as the 'n' versions since the functions assume
180  * there is always enough buffer space when !CONFIG_SYS_VSNPRINTF
181  */
182 #define snprintf(buf, size, fmt, args...) sprintf(buf, fmt, ##args)
183 #define scnprintf(buf, size, fmt, args...) sprintf(buf, fmt, ##args)
184 #define vsnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args)
185 #define vscnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args)
186 #endif /* CONFIG_SYS_VSNPRINTF */
187 
188 /**
189  * print_grouped_ull() - print a value with digits grouped by ','
190  *
191  * This prints a value with grouped digits, like 12,345,678 to make it easier
192  * to read.
193  *
194  * @val:	Value to print
195  * @digits:	Number of digiits to print
196  */
197 void print_grouped_ull(unsigned long long int_val, int digits);
198 
199 #endif
200