]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
9785c905 SG |
2 | /* |
3 | * (C) Copyright 2000-2009 | |
4 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
9785c905 SG |
5 | */ |
6 | ||
7 | #ifndef __VSPRINTF_H | |
8 | #define __VSPRINTF_H | |
9 | ||
f272f1fc | 10 | #include <stdarg.h> |
f7d6b896 | 11 | #include <linux/types.h> |
f272f1fc | 12 | |
7e5f460e SG |
13 | /** |
14 | * simple_strtoul - convert a string to an unsigned long | |
15 | * | |
4e64cae0 SG |
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) | |
185f812c | 19 | * Return: value decoded from string (0 if invalid) |
7e5f460e SG |
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 | |
18546f29 | 24 | * |
e6951139 SG |
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). | |
7e5f460e | 31 | */ |
9785c905 | 32 | ulong simple_strtoul(const char *cp, char **endp, unsigned int base); |
71ec92b6 | 33 | |
7e5f460e SG |
34 | /** |
35 | * hex_strtoul - convert a string in hex to an unsigned long | |
36 | * | |
4e64cae0 SG |
37 | * @cp: The string to be converted |
38 | * @endp: Updated to point to the first character not converted | |
185f812c | 39 | * Return: value decoded from string (0 if invalid) |
7e5f460e SG |
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 | ||
0b1284eb SG |
47 | /** |
48 | * dec_strtoul - convert a string in decimal to an unsigned long | |
49 | * | |
4e64cae0 SG |
50 | * @cp: The string to be converted |
51 | * @endp: Updated to point to the first character not converted | |
185f812c | 52 | * Return: value decoded from string (0 if invalid) |
0b1284eb SG |
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 | ||
71ec92b6 SG |
60 | /** |
61 | * strict_strtoul - convert a string to an unsigned long strictly | |
4e64cae0 SG |
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. | |
71ec92b6 SG |
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 | * | |
e6951139 SG |
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). | |
18546f29 | 84 | * |
71ec92b6 SG |
85 | * Copied this function from Linux 2.6.38 commit ID: |
86 | * 521cb40b0c44418a4fd36dc633f575813d59a43d | |
87 | * | |
88 | */ | |
9785c905 SG |
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); | |
0b016428 | 93 | long long simple_strtoll(const char *cp, char **endp, unsigned int base); |
66312374 | 94 | |
c4af6732 SG |
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 | * | |
d667a0d8 SG |
101 | * Note that this does not handle a string without a prefix. See dectoul() for |
102 | * that case. | |
103 | * | |
4e64cae0 | 104 | * @str: String to examine |
18436c74 | 105 | * Return: trailing number if found, else -1 |
c4af6732 SG |
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 | * | |
4e64cae0 | 117 | * @str: String to examine |
c4af6732 SG |
118 | * @end: Pointer to end of string to examine, or NULL to use the |
119 | * whole string | |
18436c74 | 120 | * Return: trailing number if found, else -1 |
c4af6732 SG |
121 | */ |
122 | long trailing_strtoln(const char *str, const char *end); | |
123 | ||
8565efd5 SG |
124 | /** |
125 | * trailing_strtoln_end() - extract trailing integer from a fixed-length string | |
126 | * | |
127 | * Given a fixed-length string this finds a trailing number on the string | |
128 | * and returns it. For example, "abc123" would return 123. Only the | |
129 | * characters between @str and @end - 1 are examined. If @end is NULL, it is | |
130 | * set to str + strlen(str). | |
131 | * | |
132 | * @str: String to examine | |
133 | * @end: Pointer to end of string to examine, or NULL to use the | |
134 | * whole string | |
135 | * @endp: If non-NULL, this is set to point to the character where the | |
136 | * number starts, e.g. for "mmc0" this would be point to the '0'; if no | |
137 | * trailing number is found, it is set to the end of the string | |
138 | * Return: training number if found, else -1 | |
139 | */ | |
140 | long trailing_strtoln_end(const char *str, const char *end, char const **endp); | |
141 | ||
66312374 SG |
142 | /** |
143 | * panic() - Print a message and reset/hang | |
144 | * | |
145 | * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is | |
3450a859 | 146 | * defined, then it will hang instead of resetting. |
66312374 | 147 | * |
4e64cae0 | 148 | * @fmt: printf() format string for message, which should not include |
66312374 SG |
149 | * \n, followed by arguments |
150 | */ | |
9785c905 SG |
151 | void panic(const char *fmt, ...) |
152 | __attribute__ ((format (__printf__, 1, 2), noreturn)); | |
71ec92b6 | 153 | |
66312374 SG |
154 | /** |
155 | * panic_str() - Print a message and reset/hang | |
156 | * | |
157 | * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is | |
3450a859 | 158 | * defined, then it will hang instead of resetting. |
66312374 SG |
159 | * |
160 | * This function can be used instead of panic() when your board does not | |
161 | * already use printf(), * to keep code size small. | |
162 | * | |
4e64cae0 | 163 | * @str: string to display, which should not include \n |
66312374 SG |
164 | */ |
165 | void panic_str(const char *str) __attribute__ ((noreturn)); | |
166 | ||
71ec92b6 SG |
167 | /** |
168 | * Format a string and place it in a buffer | |
169 | * | |
4e64cae0 SG |
170 | * @buf: The buffer to place the result into |
171 | * @fmt: The format string to use | |
172 | * @...: Arguments for the format string | |
71ec92b6 SG |
173 | * |
174 | * The function returns the number of characters written | |
175 | * into @buf. | |
176 | * | |
177 | * See the vsprintf() documentation for format string extensions over C99. | |
178 | */ | |
9785c905 SG |
179 | int sprintf(char *buf, const char *fmt, ...) |
180 | __attribute__ ((format (__printf__, 2, 3))); | |
71ec92b6 SG |
181 | |
182 | /** | |
183 | * Format a string and place it in a buffer (va_list version) | |
184 | * | |
4e64cae0 SG |
185 | * @buf: The buffer to place the result into |
186 | * @fmt: The format string to use | |
187 | * @args: Arguments for the format string | |
185f812c | 188 | * Return: the number of characters which have been written into |
de2de319 | 189 | * the @buf not including the trailing '\0'. |
71ec92b6 SG |
190 | * |
191 | * If you're not already dealing with a va_list consider using scnprintf(). | |
192 | * | |
193 | * See the vsprintf() documentation for format string extensions over C99. | |
194 | */ | |
9785c905 | 195 | int vsprintf(char *buf, const char *fmt, va_list args); |
3bfb0f71 SG |
196 | |
197 | /** | |
198 | * simple_itoa() - convert an unsigned integer to a string | |
199 | * | |
200 | * This returns a static string containing the decimal representation of the | |
4a255ea3 | 201 | * given value. The returned value may be overwritten by other calls to other |
4e64cae0 | 202 | * simple... functions, so should be used immediately |
3bfb0f71 SG |
203 | * |
204 | * @val: Value to convert | |
185f812c | 205 | * Return: string containing the decimal representation of @val |
3bfb0f71 SG |
206 | */ |
207 | char *simple_itoa(ulong val); | |
9785c905 | 208 | |
4a255ea3 SG |
209 | /** |
210 | * simple_xtoa() - convert an unsigned integer to a hex string | |
211 | * | |
212 | * This returns a static string containing the hexadecimal representation of the | |
213 | * given value. The returned value may be overwritten by other calls to other | |
4e64cae0 | 214 | * simple... functions, so should be used immediately |
4a255ea3 | 215 | * |
4e64cae0 | 216 | * @num: Value to convert |
185f812c | 217 | * Return: string containing the hexecimal representation of @val |
4a255ea3 SG |
218 | */ |
219 | char *simple_xtoa(ulong num); | |
220 | ||
71ec92b6 SG |
221 | /** |
222 | * Format a string and place it in a buffer | |
223 | * | |
4e64cae0 SG |
224 | * @buf: The buffer to place the result into |
225 | * @size: The size of the buffer, including the trailing null space | |
226 | * @fmt: The format string to use | |
227 | * @...: Arguments for the format string | |
185f812c | 228 | * Return: the number of characters which would be |
71ec92b6 SG |
229 | * generated for the given input, excluding the trailing null, |
230 | * as per ISO C99. If the return is greater than or equal to | |
231 | * @size, the resulting string is truncated. | |
232 | * | |
233 | * See the vsprintf() documentation for format string extensions over C99. | |
234 | */ | |
046a37bd SR |
235 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
236 | __attribute__ ((format (__printf__, 3, 4))); | |
71ec92b6 SG |
237 | |
238 | /** | |
239 | * Format a string and place it in a buffer | |
240 | * | |
4e64cae0 SG |
241 | * @buf: The buffer to place the result into |
242 | * @size: The size of the buffer, including the trailing null space | |
243 | * @fmt: The format string to use | |
244 | * @...: Arguments for the format string | |
71ec92b6 SG |
245 | * |
246 | * The return value is the number of characters written into @buf not including | |
247 | * the trailing '\0'. If @size is == 0 the function returns 0. | |
248 | * | |
249 | * See the vsprintf() documentation for format string extensions over C99. | |
250 | */ | |
046a37bd SR |
251 | int scnprintf(char *buf, size_t size, const char *fmt, ...) |
252 | __attribute__ ((format (__printf__, 3, 4))); | |
71ec92b6 SG |
253 | |
254 | /** | |
255 | * Format a string and place it in a buffer (base function) | |
256 | * | |
4e64cae0 SG |
257 | * @buf: The buffer to place the result into |
258 | * @size: The size of the buffer, including the trailing null space | |
259 | * @fmt: The format string to use | |
260 | * @args: Arguments for the format string | |
185f812c | 261 | * Return: The number characters which would be generated for the given |
71ec92b6 SG |
262 | * input, excluding the trailing '\0', as per ISO C99. Note that fewer |
263 | * characters may be written if this number of characters is >= size. | |
264 | * | |
265 | * This function follows C99 vsnprintf, but has some extensions: | |
266 | * %pS output the name of a text symbol | |
267 | * %pF output the name of a function pointer | |
268 | * %pR output the address range in a struct resource | |
269 | * | |
270 | * The function returns the number of characters which would be | |
271 | * generated for the given input, excluding the trailing '\0', | |
272 | * as per ISO C99. | |
273 | * | |
274 | * Call this function if you are already dealing with a va_list. | |
275 | * You probably want snprintf() instead. | |
276 | */ | |
046a37bd | 277 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); |
71ec92b6 SG |
278 | |
279 | /** | |
280 | * Format a string and place it in a buffer (va_list version) | |
281 | * | |
4e64cae0 SG |
282 | * @buf: The buffer to place the result into |
283 | * @size: The size of the buffer, including the trailing null space | |
284 | * @fmt: The format string to use | |
285 | * @args: Arguments for the format string | |
185f812c | 286 | * Return: the number of characters which have been written into |
71ec92b6 SG |
287 | * the @buf not including the trailing '\0'. If @size is == 0 the function |
288 | * returns 0. | |
289 | * | |
290 | * If you're not already dealing with a va_list consider using scnprintf(). | |
291 | * | |
292 | * See the vsprintf() documentation for format string extensions over C99. | |
293 | */ | |
046a37bd | 294 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); |
046a37bd | 295 | |
b8bcaa3a SG |
296 | /** |
297 | * print_grouped_ull() - print a value with digits grouped by ',' | |
298 | * | |
299 | * This prints a value with grouped digits, like 12,345,678 to make it easier | |
300 | * to read. | |
301 | * | |
4e64cae0 SG |
302 | * @int_val: Value to print |
303 | * @digits: Number of digiits to print | |
b8bcaa3a SG |
304 | */ |
305 | void print_grouped_ull(unsigned long long int_val, int digits); | |
306 | ||
09c32807 HS |
307 | bool str2off(const char *p, loff_t *num); |
308 | bool str2long(const char *p, ulong *num); | |
2189d5f1 SG |
309 | |
310 | /** | |
311 | * strmhz() - Convert a value to a Hz string | |
312 | * | |
313 | * This creates a string indicating the number of MHz of a value. For example, | |
314 | * 2700000 produces "2.7". | |
315 | * @buf: Buffer to hold output string, which must be large enough | |
316 | * @hz: Value to convert | |
317 | */ | |
318 | char *strmhz(char *buf, unsigned long hz); | |
fdc79a6b SG |
319 | |
320 | /** | |
321 | * str_to_upper() - Convert a string to upper case | |
322 | * | |
323 | * This simply uses toupper() on each character of the string. | |
324 | * | |
325 | * @in: String to convert (must be large enough to hold the output string) | |
326 | * @out: Buffer to put converted string | |
327 | * @len: Number of bytes available in @out (SIZE_MAX for all) | |
328 | */ | |
329 | void str_to_upper(const char *in, char *out, size_t len); | |
330 | ||
3e96ed44 SG |
331 | /** |
332 | * str_to_list() - Convert a string to a list of string pointers | |
333 | * | |
334 | * Splits a string containing space-delimited substrings into a number of | |
335 | * separate strings, e.g. "this is" becomes {"this", "is", NULL}. If @instr is | |
336 | * empty then this returns just {NULL}. The string should have only a single | |
337 | * space between items, with no leading or trailing spaces. | |
338 | * | |
339 | * @instr: String to process (this is alloced by this function) | |
340 | * Returns: List of string pointers, terminated by NULL. Each entry points to | |
341 | * a string. If @instr is empty, the list consists just of a single NULL entry. | |
342 | * Note that the first entry points to the alloced string. | |
343 | * Returns NULL if out of memory | |
344 | */ | |
345 | const char **str_to_list(const char *instr); | |
346 | ||
347 | /** | |
348 | * str_free_list() - Free a string list | |
349 | * | |
350 | * @ptr: String list to free, as created by str_to_list(). This can also be | |
351 | * NULL, in which case the function does nothing | |
352 | */ | |
353 | void str_free_list(const char **ptr); | |
354 | ||
499f1844 SDR |
355 | /** |
356 | * vsscanf - Unformat a buffer into a list of arguments | |
4e64cae0 SG |
357 | * @inp: input buffer |
358 | * @fmt0: format of buffer | |
359 | * @ap: arguments | |
499f1844 SDR |
360 | */ |
361 | int vsscanf(const char *inp, char const *fmt0, va_list ap); | |
362 | ||
e87dfb05 AA |
363 | /** |
364 | * sscanf - Unformat a buffer into a list of arguments | |
365 | * @buf: input buffer | |
366 | * @fmt: formatting of buffer | |
367 | * @...: resulting arguments | |
368 | */ | |
369 | int sscanf(const char *buf, const char *fmt, ...); | |
370 | ||
9785c905 | 371 | #endif |