blob: 4479df0af3f93bdc86397d9f1736b7af2d977311 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass9785c902011-11-02 09:52:07 +00002/*
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glass9785c902011-11-02 09:52:07 +00005 */
6
7#ifndef __VSPRINTF_H
8#define __VSPRINTF_H
9
Maxime Ripardf272f1f2016-07-05 10:26:36 +020010#include <stdarg.h>
Masahiro Yamadaf7d6b892017-09-16 14:10:43 +090011#include <linux/types.h>
Maxime Ripardf272f1f2016-07-05 10:26:36 +020012
Simon Glass7e5f4602021-07-24 09:03:29 -060013/**
14 * simple_strtoul - convert a string to an unsigned long
15 *
16 * @param cp The string to be converted
17 * @param endp Updated to point to the first character not converted
Simon Glass18546f22021-07-24 09:03:31 -060018 * @param base The number base to use (0 for the default)
Simon Glass7e5f4602021-07-24 09:03:29 -060019 * @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
Simon Glass18546f22021-07-24 09:03:31 -060024 *
Simon Glasse6951132021-07-24 09:03:38 -060025 * 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).
Simon Glass7e5f4602021-07-24 09:03:29 -060031 */
Simon Glass9785c902011-11-02 09:52:07 +000032ulong simple_strtoul(const char *cp, char **endp, unsigned int base);
Simon Glass71ec92b2011-11-02 09:52:09 +000033
34/**
Simon Glass7e5f4602021-07-24 09:03:29 -060035 * hex_strtoul - convert a string in hex to an unsigned long
36 *
37 * @param cp The string to be converted
38 * @param 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 */
45unsigned long hextoul(const char *cp, char **endp);
46
47/**
Simon Glass0b1284e2021-07-24 09:03:30 -060048 * dec_strtoul - convert a string in decimal to an unsigned long
49 *
50 * @param cp The string to be converted
51 * @param 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 */
58unsigned long dectoul(const char *cp, char **endp);
59
60/**
Simon Glass71ec92b2011-11-02 09:52:09 +000061 * strict_strtoul - convert a string to an unsigned long strictly
62 * @param cp The string to be converted
Simon Glass18546f22021-07-24 09:03:31 -060063 * @param base The number base to use (0 for the default)
Simon Glass71ec92b2011-11-02 09:52:09 +000064 * @param 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 *
Simon Glasse6951132021-07-24 09:03:38 -060078 * 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).
Simon Glass18546f22021-07-24 09:03:31 -060084 *
Simon Glass71ec92b2011-11-02 09:52:09 +000085 * Copied this function from Linux 2.6.38 commit ID:
86 * 521cb40b0c44418a4fd36dc633f575813d59a43d
87 *
88 */
Simon Glass9785c902011-11-02 09:52:07 +000089int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
90unsigned long long simple_strtoull(const char *cp, char **endp,
91 unsigned int base);
92long simple_strtol(const char *cp, char **endp, unsigned int base);
Roland Gaudig0b016422021-07-23 12:29:18 +000093long long simple_strtoll(const char *cp, char **endp, unsigned int base);
Simon Glass66312372015-02-27 22:06:32 -070094
95/**
Simon Glassc4af6732015-06-23 15:39:08 -060096 * 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 * @str: String to exxamine
102 * @return training number if found, else -1
103 */
104long trailing_strtol(const char *str);
105
106/**
107 * trailing_strtoln() - extract a trailing integer from a fixed-length string
108 *
109 * Given a fixed-length string this finds a trailing number on the string
110 * and returns it. For example, "abc123" would return 123. Only the
111 * characters between @str and @end - 1 are examined. If @end is NULL, it is
112 * set to str + strlen(str).
113 *
114 * @str: String to exxamine
115 * @end: Pointer to end of string to examine, or NULL to use the
116 * whole string
117 * @return training number if found, else -1
118 */
119long trailing_strtoln(const char *str, const char *end);
120
121/**
Simon Glass66312372015-02-27 22:06:32 -0700122 * panic() - Print a message and reset/hang
123 *
124 * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
Vagrant Cascadian3450a852016-10-23 20:45:19 -0700125 * defined, then it will hang instead of resetting.
Simon Glass66312372015-02-27 22:06:32 -0700126 *
127 * @param fmt: printf() format string for message, which should not include
128 * \n, followed by arguments
129 */
Simon Glass9785c902011-11-02 09:52:07 +0000130void panic(const char *fmt, ...)
131 __attribute__ ((format (__printf__, 1, 2), noreturn));
Simon Glass71ec92b2011-11-02 09:52:09 +0000132
133/**
Simon Glass66312372015-02-27 22:06:32 -0700134 * panic_str() - Print a message and reset/hang
135 *
136 * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
Vagrant Cascadian3450a852016-10-23 20:45:19 -0700137 * defined, then it will hang instead of resetting.
Simon Glass66312372015-02-27 22:06:32 -0700138 *
139 * This function can be used instead of panic() when your board does not
140 * already use printf(), * to keep code size small.
141 *
142 * @param fmt: string to display, which should not include \n
143 */
144void panic_str(const char *str) __attribute__ ((noreturn));
145
146/**
Simon Glass71ec92b2011-11-02 09:52:09 +0000147 * Format a string and place it in a buffer
148 *
149 * @param buf The buffer to place the result into
150 * @param fmt The format string to use
151 * @param ... Arguments for the format string
152 *
153 * The function returns the number of characters written
154 * into @buf.
155 *
156 * See the vsprintf() documentation for format string extensions over C99.
157 */
Simon Glass9785c902011-11-02 09:52:07 +0000158int sprintf(char *buf, const char *fmt, ...)
159 __attribute__ ((format (__printf__, 2, 3)));
Simon Glass71ec92b2011-11-02 09:52:09 +0000160
161/**
162 * Format a string and place it in a buffer (va_list version)
163 *
164 * @param buf The buffer to place the result into
Simon Glass71ec92b2011-11-02 09:52:09 +0000165 * @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
Heinrich Schuchardtde2de312017-09-06 17:55:13 +0200168 * the @buf not including the trailing '\0'.
Simon Glass71ec92b2011-11-02 09:52:09 +0000169 *
170 * If you're not already dealing with a va_list consider using scnprintf().
171 *
172 * See the vsprintf() documentation for format string extensions over C99.
173 */
Simon Glass9785c902011-11-02 09:52:07 +0000174int vsprintf(char *buf, const char *fmt, va_list args);
Simon Glass3bfb0f72021-10-14 12:48:06 -0600175
176/**
177 * simple_itoa() - convert an unsigned integer to a string
178 *
179 * This returns a static string containing the decimal representation of the
180 * given value. The returned value may be overwritten by other calls to the
181 * same function, so should be used immediately
182 *
183 * @val: Value to convert
184 * @return string containing the decimal representation of @val
185 */
186char *simple_itoa(ulong val);
Simon Glass9785c902011-11-02 09:52:07 +0000187
Simon Glass71ec92b2011-11-02 09:52:09 +0000188/**
189 * Format a string and place it in a buffer
190 *
191 * @param buf The buffer to place the result into
192 * @param size The size of the buffer, including the trailing null space
193 * @param fmt The format string to use
194 * @param ... Arguments for the format string
195 * @return the number of characters which would be
196 * generated for the given input, excluding the trailing null,
197 * as per ISO C99. If the return is greater than or equal to
198 * @size, the resulting string is truncated.
199 *
200 * See the vsprintf() documentation for format string extensions over C99.
201 */
Sonny Rao046a37b2011-11-02 09:52:08 +0000202int snprintf(char *buf, size_t size, const char *fmt, ...)
203 __attribute__ ((format (__printf__, 3, 4)));
Simon Glass71ec92b2011-11-02 09:52:09 +0000204
205/**
206 * Format a string and place it in a buffer
207 *
208 * @param buf The buffer to place the result into
209 * @param size The size of the buffer, including the trailing null space
210 * @param fmt The format string to use
211 * @param ... Arguments for the format string
212 *
213 * The return value is the number of characters written into @buf not including
214 * the trailing '\0'. If @size is == 0 the function returns 0.
215 *
216 * See the vsprintf() documentation for format string extensions over C99.
217 */
Sonny Rao046a37b2011-11-02 09:52:08 +0000218int scnprintf(char *buf, size_t size, const char *fmt, ...)
219 __attribute__ ((format (__printf__, 3, 4)));
Simon Glass71ec92b2011-11-02 09:52:09 +0000220
221/**
222 * Format a string and place it in a buffer (base function)
223 *
224 * @param buf The buffer to place the result into
225 * @param size The size of the buffer, including the trailing null space
226 * @param fmt The format string to use
227 * @param args Arguments for the format string
228 * @return The number characters which would be generated for the given
229 * input, excluding the trailing '\0', as per ISO C99. Note that fewer
230 * characters may be written if this number of characters is >= size.
231 *
232 * This function follows C99 vsnprintf, but has some extensions:
233 * %pS output the name of a text symbol
234 * %pF output the name of a function pointer
235 * %pR output the address range in a struct resource
236 *
237 * The function returns the number of characters which would be
238 * generated for the given input, excluding the trailing '\0',
239 * as per ISO C99.
240 *
241 * Call this function if you are already dealing with a va_list.
242 * You probably want snprintf() instead.
243 */
Sonny Rao046a37b2011-11-02 09:52:08 +0000244int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
Simon Glass71ec92b2011-11-02 09:52:09 +0000245
246/**
247 * Format a string and place it in a buffer (va_list version)
248 *
249 * @param buf The buffer to place the result into
250 * @param size The size of the buffer, including the trailing null space
251 * @param fmt The format string to use
252 * @param args Arguments for the format string
253 * @return the number of characters which have been written into
254 * the @buf not including the trailing '\0'. If @size is == 0 the function
255 * returns 0.
256 *
257 * If you're not already dealing with a va_list consider using scnprintf().
258 *
259 * See the vsprintf() documentation for format string extensions over C99.
260 */
Sonny Rao046a37b2011-11-02 09:52:08 +0000261int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
Sonny Rao046a37b2011-11-02 09:52:08 +0000262
Simon Glassb8bcaa32013-06-11 11:14:38 -0700263/**
264 * print_grouped_ull() - print a value with digits grouped by ','
265 *
266 * This prints a value with grouped digits, like 12,345,678 to make it easier
267 * to read.
268 *
269 * @val: Value to print
270 * @digits: Number of digiits to print
271 */
272void print_grouped_ull(unsigned long long int_val, int digits);
273
Heiko Schocher09c32802015-04-27 07:42:05 +0200274bool str2off(const char *p, loff_t *num);
275bool str2long(const char *p, ulong *num);
Simon Glass2189d5f2019-11-14 12:57:20 -0700276
277/**
278 * strmhz() - Convert a value to a Hz string
279 *
280 * This creates a string indicating the number of MHz of a value. For example,
281 * 2700000 produces "2.7".
282 * @buf: Buffer to hold output string, which must be large enough
283 * @hz: Value to convert
284 */
285char *strmhz(char *buf, unsigned long hz);
Simon Glassfdc79a62020-04-08 08:32:56 -0600286
287/**
288 * str_to_upper() - Convert a string to upper case
289 *
290 * This simply uses toupper() on each character of the string.
291 *
292 * @in: String to convert (must be large enough to hold the output string)
293 * @out: Buffer to put converted string
294 * @len: Number of bytes available in @out (SIZE_MAX for all)
295 */
296void str_to_upper(const char *in, char *out, size_t len);
297
Andrii Anisove87dfb02020-08-06 12:42:52 +0300298/**
299 * sscanf - Unformat a buffer into a list of arguments
300 * @buf: input buffer
301 * @fmt: formatting of buffer
302 * @...: resulting arguments
303 */
304int sscanf(const char *buf, const char *fmt, ...);
305
Simon Glass9785c902011-11-02 09:52:07 +0000306#endif