5172ceedec100ecc495e811fdb59700e46cc89b1
[platform/kernel/u-boot.git] / include / vsprintf.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * (C) Copyright 2000-2009
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6
7 #ifndef __VSPRINTF_H
8 #define __VSPRINTF_H
9
10 #include <stdarg.h>
11 #include <linux/types.h>
12
13 /**
14  * simple_strtoul - convert a string to an unsigned long
15  *
16  * @cp: The string to be converted
17  * @endp: Updated to point to the first character not converted
18  * @base: The number base to use (0 for the default)
19  * Return: value decoded from string (0 if invalid)
20  *
21  * Converts a string to an unsigned long. If there are invalid characters at
22  * the end these are ignored. In the worst case, if all characters are invalid,
23  * 0 is returned
24  *
25  * A hex prefix is supported (e.g. 0x123) regardless of the value of @base.
26  * If found, the base is set to hex (16).
27  *
28  * If @base is 0:
29  *    - an octal '0' prefix (e.g. 0777) sets the base to octal (8).
30  *    - otherwise the base defaults to decimal (10).
31  */
32 ulong simple_strtoul(const char *cp, char **endp, unsigned int base);
33
34 /**
35  * hex_strtoul - convert a string in hex to an unsigned long
36  *
37  * @cp: The string to be converted
38  * @endp: Updated to point to the first character not converted
39  * Return: value decoded from string (0 if invalid)
40  *
41  * Converts a hex string to an unsigned long. If there are invalid characters at
42  * the end these are ignored. In the worst case, if all characters are invalid,
43  * 0 is returned
44  */
45 unsigned long hextoul(const char *cp, char **endp);
46
47 /**
48  * dec_strtoul - convert a string in decimal to an unsigned long
49  *
50  * @cp: The string to be converted
51  * @endp: Updated to point to the first character not converted
52  * Return: value decoded from string (0 if invalid)
53  *
54  * Converts a decimal string to an unsigned long. If there are invalid
55  * characters at the end these are ignored. In the worst case, if all characters
56  * are invalid, 0 is returned
57  */
58 unsigned long dectoul(const char *cp, char **endp);
59
60 /**
61  * strict_strtoul - convert a string to an unsigned long strictly
62  * @cp: The string to be converted
63  * @base: The number base to use (0 for the default)
64  * @res: The converted result value
65  * Return: 0 if conversion is successful and `*res` is set to the converted
66  * value, otherwise it returns -EINVAL and `*res` is set to 0.
67  *
68  * strict_strtoul converts a string to an unsigned long only if the
69  * string is really an unsigned long string, any string containing
70  * any invalid char at the tail will be rejected and -EINVAL is returned,
71  * only a newline char at the tail is acceptible because people generally
72  * change a module parameter in the following way:
73  *
74  *      echo 1024 > /sys/module/e1000/parameters/copybreak
75  *
76  * echo will append a newline to the tail.
77  *
78  * A hex prefix is supported (e.g. 0x123) regardless of the value of @base.
79  * If found, the base is set to hex (16).
80  *
81  * If @base is 0:
82  *    - an octal '0' prefix (e.g. 0777) sets the base to octal (8).
83  *    - otherwise the base defaults to decimal (10).
84  *
85  * Copied this function from Linux 2.6.38 commit ID:
86  * 521cb40b0c44418a4fd36dc633f575813d59a43d
87  *
88  */
89 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
90 unsigned long long simple_strtoull(const char *cp, char **endp,
91                                         unsigned int base);
92 long simple_strtol(const char *cp, char **endp, unsigned int base);
93 long long simple_strtoll(const char *cp, char **endp, unsigned int base);
94
95 /**
96  * trailing_strtol() - extract a trailing integer from a string
97  *
98  * Given a string this finds a trailing number on the string and returns it.
99  * For example, "abc123" would return 123.
100  *
101  * Note that this does not handle a string without a prefix. See dectoul() for
102  * that case.
103  *
104  * @str:        String to examine
105  * Return: trailing number if found, else -1
106  */
107 long trailing_strtol(const char *str);
108
109 /**
110  * trailing_strtoln() - extract a trailing integer from a fixed-length string
111  *
112  * Given a fixed-length string this finds a trailing number on the string
113  * and returns it. For example, "abc123" would return 123. Only the
114  * characters between @str and @end - 1 are examined. If @end is NULL, it is
115  * set to str + strlen(str).
116  *
117  * @str:        String to examine
118  * @end:        Pointer to end of string to examine, or NULL to use the
119  *              whole string
120  * Return: trailing number if found, else -1
121  */
122 long trailing_strtoln(const char *str, const char *end);
123
124 /**
125  * panic() - Print a message and reset/hang
126  *
127  * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
128  * defined, then it will hang instead of resetting.
129  *
130  * @fmt: printf() format string for message, which should not include
131  *              \n, followed by arguments
132  */
133 void panic(const char *fmt, ...)
134                 __attribute__ ((format (__printf__, 1, 2), noreturn));
135
136 /**
137  * panic_str() - Print a message and reset/hang
138  *
139  * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
140  * defined, then it will hang instead of resetting.
141  *
142  * This function can be used instead of panic() when your board does not
143  * already use printf(), * to keep code size small.
144  *
145  * @str: string to display, which should not include \n
146  */
147 void panic_str(const char *str) __attribute__ ((noreturn));
148
149 /**
150  * Format a string and place it in a buffer
151  *
152  * @buf: The buffer to place the result into
153  * @fmt: The format string to use
154  * @...: Arguments for the format string
155  *
156  * The function returns the number of characters written
157  * into @buf.
158  *
159  * See the vsprintf() documentation for format string extensions over C99.
160  */
161 int sprintf(char *buf, const char *fmt, ...)
162                 __attribute__ ((format (__printf__, 2, 3)));
163
164 /**
165  * Format a string and place it in a buffer (va_list version)
166  *
167  * @buf: The buffer to place the result into
168  * @fmt: The format string to use
169  * @args: Arguments for the format string
170  * Return: the number of characters which have been written into
171  * the @buf not including the trailing '\0'.
172  *
173  * If you're not already dealing with a va_list consider using scnprintf().
174  *
175  * See the vsprintf() documentation for format string extensions over C99.
176  */
177 int vsprintf(char *buf, const char *fmt, va_list args);
178
179 /**
180  * simple_itoa() - convert an unsigned integer to a string
181  *
182  * This returns a static string containing the decimal representation of the
183  * given value. The returned value may be overwritten by other calls to other
184  * simple... functions, so should be used immediately
185  *
186  * @val: Value to convert
187  * Return: string containing the decimal representation of @val
188  */
189 char *simple_itoa(ulong val);
190
191 /**
192  * simple_xtoa() - convert an unsigned integer to a hex string
193  *
194  * This returns a static string containing the hexadecimal representation of the
195  * given value. The returned value may be overwritten by other calls to other
196  * simple... functions, so should be used immediately
197  *
198  * @num: Value to convert
199  * Return: string containing the hexecimal representation of @val
200  */
201 char *simple_xtoa(ulong num);
202
203 /**
204  * Format a string and place it in a buffer
205  *
206  * @buf: The buffer to place the result into
207  * @size: The size of the buffer, including the trailing null space
208  * @fmt: The format string to use
209  * @...: Arguments for the format string
210  * Return: the number of characters which would be
211  * generated for the given input, excluding the trailing null,
212  * as per ISO C99.  If the return is greater than or equal to
213  * @size, the resulting string is truncated.
214  *
215  * See the vsprintf() documentation for format string extensions over C99.
216  */
217 int snprintf(char *buf, size_t size, const char *fmt, ...)
218                 __attribute__ ((format (__printf__, 3, 4)));
219
220 /**
221  * Format a string and place it in a buffer
222  *
223  * @buf: The buffer to place the result into
224  * @size: The size of the buffer, including the trailing null space
225  * @fmt: The format string to use
226  * @...: Arguments for the format string
227  *
228  * The return value is the number of characters written into @buf not including
229  * the trailing '\0'. If @size is == 0 the function returns 0.
230  *
231  * See the vsprintf() documentation for format string extensions over C99.
232  */
233 int scnprintf(char *buf, size_t size, const char *fmt, ...)
234                 __attribute__ ((format (__printf__, 3, 4)));
235
236 /**
237  * Format a string and place it in a buffer (base function)
238  *
239  * @buf: The buffer to place the result into
240  * @size: The size of the buffer, including the trailing null space
241  * @fmt: The format string to use
242  * @args: Arguments for the format string
243  * Return: The number characters which would be generated for the given
244  * input, excluding the trailing '\0', as per ISO C99. Note that fewer
245  * characters may be written if this number of characters is >= size.
246  *
247  * This function follows C99 vsnprintf, but has some extensions:
248  * %pS output the name of a text symbol
249  * %pF output the name of a function pointer
250  * %pR output the address range in a struct resource
251  *
252  * The function returns the number of characters which would be
253  * generated for the given input, excluding the trailing '\0',
254  * as per ISO C99.
255  *
256  * Call this function if you are already dealing with a va_list.
257  * You probably want snprintf() instead.
258  */
259 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
260
261 /**
262  * Format a string and place it in a buffer (va_list version)
263  *
264  * @buf: The buffer to place the result into
265  * @size: The size of the buffer, including the trailing null space
266  * @fmt: The format string to use
267  * @args: Arguments for the format string
268  * Return: the number of characters which have been written into
269  * the @buf not including the trailing '\0'. If @size is == 0 the function
270  * returns 0.
271  *
272  * If you're not already dealing with a va_list consider using scnprintf().
273  *
274  * See the vsprintf() documentation for format string extensions over C99.
275  */
276 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
277
278 /**
279  * print_grouped_ull() - print a value with digits grouped by ','
280  *
281  * This prints a value with grouped digits, like 12,345,678 to make it easier
282  * to read.
283  *
284  * @int_val: Value to print
285  * @digits: Number of digiits to print
286  */
287 void print_grouped_ull(unsigned long long int_val, int digits);
288
289 bool str2off(const char *p, loff_t *num);
290 bool str2long(const char *p, ulong *num);
291
292 /**
293  * strmhz() - Convert a value to a Hz string
294  *
295  * This creates a string indicating the number of MHz of a value. For example,
296  * 2700000 produces "2.7".
297  * @buf: Buffer to hold output string, which must be large enough
298  * @hz: Value to convert
299  */
300 char *strmhz(char *buf, unsigned long hz);
301
302 /**
303  * str_to_upper() - Convert a string to upper case
304  *
305  * This simply uses toupper() on each character of the string.
306  *
307  * @in: String to convert (must be large enough to hold the output string)
308  * @out: Buffer to put converted string
309  * @len: Number of bytes available in @out (SIZE_MAX for all)
310  */
311 void str_to_upper(const char *in, char *out, size_t len);
312
313 /**
314  * vsscanf - Unformat a buffer into a list of arguments
315  * @inp: input buffer
316  * @fmt0: format of buffer
317  * @ap: arguments
318  */
319 int vsscanf(const char *inp, char const *fmt0, va_list ap);
320
321 /**
322  * sscanf - Unformat a buffer into a list of arguments
323  * @buf:        input buffer
324  * @fmt:        formatting of buffer
325  * @...:        resulting arguments
326  */
327 int sscanf(const char *buf, const char *fmt, ...);
328
329 #endif