blob: debf977401aff5e60384b9098ee91de850a23e12 [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 *
25 * If @base is 0, octal or hex prefixes are supported (e.g. 0777, 0x123) to
26 * select a particular base. By default decimal is used.
Simon Glass7e5f4602021-07-24 09:03:29 -060027 */
Simon Glass9785c902011-11-02 09:52:07 +000028ulong simple_strtoul(const char *cp, char **endp, unsigned int base);
Simon Glass71ec92b2011-11-02 09:52:09 +000029
30/**
Simon Glass7e5f4602021-07-24 09:03:29 -060031 * hex_strtoul - convert a string in hex to an unsigned long
32 *
33 * @param cp The string to be converted
34 * @param endp Updated to point to the first character not converted
35 * @return value decoded from string (0 if invalid)
36 *
37 * Converts a hex string to an unsigned long. If there are invalid characters at
38 * the end these are ignored. In the worst case, if all characters are invalid,
39 * 0 is returned
40 */
41unsigned long hextoul(const char *cp, char **endp);
42
43/**
Simon Glass0b1284e2021-07-24 09:03:30 -060044 * dec_strtoul - convert a string in decimal to an unsigned long
45 *
46 * @param cp The string to be converted
47 * @param endp Updated to point to the first character not converted
48 * @return value decoded from string (0 if invalid)
49 *
50 * Converts a decimal string to an unsigned long. If there are invalid
51 * characters at the end these are ignored. In the worst case, if all characters
52 * are invalid, 0 is returned
53 */
54unsigned long dectoul(const char *cp, char **endp);
55
56/**
Simon Glass71ec92b2011-11-02 09:52:09 +000057 * strict_strtoul - convert a string to an unsigned long strictly
58 * @param cp The string to be converted
Simon Glass18546f22021-07-24 09:03:31 -060059 * @param base The number base to use (0 for the default)
Simon Glass71ec92b2011-11-02 09:52:09 +000060 * @param res The converted result value
61 * @return 0 if conversion is successful and *res is set to the converted
62 * value, otherwise it returns -EINVAL and *res is set to 0.
63 *
64 * strict_strtoul converts a string to an unsigned long only if the
65 * string is really an unsigned long string, any string containing
66 * any invalid char at the tail will be rejected and -EINVAL is returned,
67 * only a newline char at the tail is acceptible because people generally
68 * change a module parameter in the following way:
69 *
70 * echo 1024 > /sys/module/e1000/parameters/copybreak
71 *
72 * echo will append a newline to the tail.
73 *
Simon Glass18546f22021-07-24 09:03:31 -060074 * If @base is 0, octal or hex prefixes are supported (e.g. 0777, 0x123) to
75 * select a particular base. By default decimal is used.
76 *
Simon Glass71ec92b2011-11-02 09:52:09 +000077 * Copied this function from Linux 2.6.38 commit ID:
78 * 521cb40b0c44418a4fd36dc633f575813d59a43d
79 *
80 */
Simon Glass9785c902011-11-02 09:52:07 +000081int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
82unsigned long long simple_strtoull(const char *cp, char **endp,
83 unsigned int base);
84long simple_strtol(const char *cp, char **endp, unsigned int base);
Roland Gaudig0b016422021-07-23 12:29:18 +000085long long simple_strtoll(const char *cp, char **endp, unsigned int base);
Simon Glass66312372015-02-27 22:06:32 -070086
87/**
Simon Glassc4af6732015-06-23 15:39:08 -060088 * trailing_strtol() - extract a trailing integer from a string
89 *
90 * Given a string this finds a trailing number on the string and returns it.
91 * For example, "abc123" would return 123.
92 *
93 * @str: String to exxamine
94 * @return training number if found, else -1
95 */
96long trailing_strtol(const char *str);
97
98/**
99 * trailing_strtoln() - extract a trailing integer from a fixed-length string
100 *
101 * Given a fixed-length string this finds a trailing number on the string
102 * and returns it. For example, "abc123" would return 123. Only the
103 * characters between @str and @end - 1 are examined. If @end is NULL, it is
104 * set to str + strlen(str).
105 *
106 * @str: String to exxamine
107 * @end: Pointer to end of string to examine, or NULL to use the
108 * whole string
109 * @return training number if found, else -1
110 */
111long trailing_strtoln(const char *str, const char *end);
112
113/**
Simon Glass66312372015-02-27 22:06:32 -0700114 * panic() - Print a message and reset/hang
115 *
116 * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
Vagrant Cascadian3450a852016-10-23 20:45:19 -0700117 * defined, then it will hang instead of resetting.
Simon Glass66312372015-02-27 22:06:32 -0700118 *
119 * @param fmt: printf() format string for message, which should not include
120 * \n, followed by arguments
121 */
Simon Glass9785c902011-11-02 09:52:07 +0000122void panic(const char *fmt, ...)
123 __attribute__ ((format (__printf__, 1, 2), noreturn));
Simon Glass71ec92b2011-11-02 09:52:09 +0000124
125/**
Simon Glass66312372015-02-27 22:06:32 -0700126 * panic_str() - Print a message and reset/hang
127 *
128 * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
Vagrant Cascadian3450a852016-10-23 20:45:19 -0700129 * defined, then it will hang instead of resetting.
Simon Glass66312372015-02-27 22:06:32 -0700130 *
131 * This function can be used instead of panic() when your board does not
132 * already use printf(), * to keep code size small.
133 *
134 * @param fmt: string to display, which should not include \n
135 */
136void panic_str(const char *str) __attribute__ ((noreturn));
137
138/**
Simon Glass71ec92b2011-11-02 09:52:09 +0000139 * Format a string and place it in a buffer
140 *
141 * @param buf The buffer to place the result into
142 * @param fmt The format string to use
143 * @param ... Arguments for the format string
144 *
145 * The function returns the number of characters written
146 * into @buf.
147 *
148 * See the vsprintf() documentation for format string extensions over C99.
149 */
Simon Glass9785c902011-11-02 09:52:07 +0000150int sprintf(char *buf, const char *fmt, ...)
151 __attribute__ ((format (__printf__, 2, 3)));
Simon Glass71ec92b2011-11-02 09:52:09 +0000152
153/**
154 * Format a string and place it in a buffer (va_list version)
155 *
156 * @param buf The buffer to place the result into
Simon Glass71ec92b2011-11-02 09:52:09 +0000157 * @param fmt The format string to use
158 * @param args Arguments for the format string
159 * @return the number of characters which have been written into
Heinrich Schuchardtde2de312017-09-06 17:55:13 +0200160 * the @buf not including the trailing '\0'.
Simon Glass71ec92b2011-11-02 09:52:09 +0000161 *
162 * If you're not already dealing with a va_list consider using scnprintf().
163 *
164 * See the vsprintf() documentation for format string extensions over C99.
165 */
Simon Glass9785c902011-11-02 09:52:07 +0000166int vsprintf(char *buf, const char *fmt, va_list args);
167char *simple_itoa(ulong i);
168
Simon Glass71ec92b2011-11-02 09:52:09 +0000169/**
170 * Format a string and place it in a buffer
171 *
172 * @param buf The buffer to place the result into
173 * @param size The size of the buffer, including the trailing null space
174 * @param fmt The format string to use
175 * @param ... Arguments for the format string
176 * @return the number of characters which would be
177 * generated for the given input, excluding the trailing null,
178 * as per ISO C99. If the return is greater than or equal to
179 * @size, the resulting string is truncated.
180 *
181 * See the vsprintf() documentation for format string extensions over C99.
182 */
Sonny Rao046a37b2011-11-02 09:52:08 +0000183int snprintf(char *buf, size_t size, const char *fmt, ...)
184 __attribute__ ((format (__printf__, 3, 4)));
Simon Glass71ec92b2011-11-02 09:52:09 +0000185
186/**
187 * Format a string and place it in a buffer
188 *
189 * @param buf The buffer to place the result into
190 * @param size The size of the buffer, including the trailing null space
191 * @param fmt The format string to use
192 * @param ... Arguments for the format string
193 *
194 * The return value is the number of characters written into @buf not including
195 * the trailing '\0'. If @size is == 0 the function returns 0.
196 *
197 * See the vsprintf() documentation for format string extensions over C99.
198 */
Sonny Rao046a37b2011-11-02 09:52:08 +0000199int scnprintf(char *buf, size_t size, const char *fmt, ...)
200 __attribute__ ((format (__printf__, 3, 4)));
Simon Glass71ec92b2011-11-02 09:52:09 +0000201
202/**
203 * Format a string and place it in a buffer (base function)
204 *
205 * @param buf The buffer to place the result into
206 * @param size The size of the buffer, including the trailing null space
207 * @param fmt The format string to use
208 * @param args Arguments for the format string
209 * @return The number characters which would be generated for the given
210 * input, excluding the trailing '\0', as per ISO C99. Note that fewer
211 * characters may be written if this number of characters is >= size.
212 *
213 * This function follows C99 vsnprintf, but has some extensions:
214 * %pS output the name of a text symbol
215 * %pF output the name of a function pointer
216 * %pR output the address range in a struct resource
217 *
218 * The function returns the number of characters which would be
219 * generated for the given input, excluding the trailing '\0',
220 * as per ISO C99.
221 *
222 * Call this function if you are already dealing with a va_list.
223 * You probably want snprintf() instead.
224 */
Sonny Rao046a37b2011-11-02 09:52:08 +0000225int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
Simon Glass71ec92b2011-11-02 09:52:09 +0000226
227/**
228 * Format a string and place it in a buffer (va_list version)
229 *
230 * @param buf The buffer to place the result into
231 * @param size The size of the buffer, including the trailing null space
232 * @param fmt The format string to use
233 * @param args Arguments for the format string
234 * @return the number of characters which have been written into
235 * the @buf not including the trailing '\0'. If @size is == 0 the function
236 * returns 0.
237 *
238 * If you're not already dealing with a va_list consider using scnprintf().
239 *
240 * See the vsprintf() documentation for format string extensions over C99.
241 */
Sonny Rao046a37b2011-11-02 09:52:08 +0000242int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
Sonny Rao046a37b2011-11-02 09:52:08 +0000243
Simon Glassb8bcaa32013-06-11 11:14:38 -0700244/**
245 * print_grouped_ull() - print a value with digits grouped by ','
246 *
247 * This prints a value with grouped digits, like 12,345,678 to make it easier
248 * to read.
249 *
250 * @val: Value to print
251 * @digits: Number of digiits to print
252 */
253void print_grouped_ull(unsigned long long int_val, int digits);
254
Heiko Schocher09c32802015-04-27 07:42:05 +0200255bool str2off(const char *p, loff_t *num);
256bool str2long(const char *p, ulong *num);
Simon Glass2189d5f2019-11-14 12:57:20 -0700257
258/**
259 * strmhz() - Convert a value to a Hz string
260 *
261 * This creates a string indicating the number of MHz of a value. For example,
262 * 2700000 produces "2.7".
263 * @buf: Buffer to hold output string, which must be large enough
264 * @hz: Value to convert
265 */
266char *strmhz(char *buf, unsigned long hz);
Simon Glassfdc79a62020-04-08 08:32:56 -0600267
268/**
269 * str_to_upper() - Convert a string to upper case
270 *
271 * This simply uses toupper() on each character of the string.
272 *
273 * @in: String to convert (must be large enough to hold the output string)
274 * @out: Buffer to put converted string
275 * @len: Number of bytes available in @out (SIZE_MAX for all)
276 */
277void str_to_upper(const char *in, char *out, size_t len);
278
Andrii Anisove87dfb02020-08-06 12:42:52 +0300279/**
280 * sscanf - Unformat a buffer into a list of arguments
281 * @buf: input buffer
282 * @fmt: formatting of buffer
283 * @...: resulting arguments
284 */
285int sscanf(const char *buf, const char *fmt, ...);
286
Simon Glass9785c902011-11-02 09:52:07 +0000287#endif