blob: c30e91fd6f746058ab87b125b6ace40ce3ac7314 [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
18 * @param base The number base to use
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 */
Simon Glass9785c902011-11-02 09:52:07 +000025ulong simple_strtoul(const char *cp, char **endp, unsigned int base);
Simon Glass71ec92b2011-11-02 09:52:09 +000026
27/**
Simon Glass7e5f4602021-07-24 09:03:29 -060028 * hex_strtoul - convert a string in hex to an unsigned long
29 *
30 * @param cp The string to be converted
31 * @param endp Updated to point to the first character not converted
32 * @return value decoded from string (0 if invalid)
33 *
34 * Converts a hex string to an unsigned long. If there are invalid characters at
35 * the end these are ignored. In the worst case, if all characters are invalid,
36 * 0 is returned
37 */
38unsigned long hextoul(const char *cp, char **endp);
39
40/**
Simon Glass0b1284e2021-07-24 09:03:30 -060041 * dec_strtoul - convert a string in decimal to an unsigned long
42 *
43 * @param cp The string to be converted
44 * @param endp Updated to point to the first character not converted
45 * @return value decoded from string (0 if invalid)
46 *
47 * Converts a decimal string to an unsigned long. If there are invalid
48 * characters at the end these are ignored. In the worst case, if all characters
49 * are invalid, 0 is returned
50 */
51unsigned long dectoul(const char *cp, char **endp);
52
53/**
Simon Glass71ec92b2011-11-02 09:52:09 +000054 * strict_strtoul - convert a string to an unsigned long strictly
55 * @param cp The string to be converted
56 * @param base The number base to use
57 * @param res The converted result value
58 * @return 0 if conversion is successful and *res is set to the converted
59 * value, otherwise it returns -EINVAL and *res is set to 0.
60 *
61 * strict_strtoul converts a string to an unsigned long only if the
62 * string is really an unsigned long string, any string containing
63 * any invalid char at the tail will be rejected and -EINVAL is returned,
64 * only a newline char at the tail is acceptible because people generally
65 * change a module parameter in the following way:
66 *
67 * echo 1024 > /sys/module/e1000/parameters/copybreak
68 *
69 * echo will append a newline to the tail.
70 *
Simon Glass71ec92b2011-11-02 09:52:09 +000071 * Copied this function from Linux 2.6.38 commit ID:
72 * 521cb40b0c44418a4fd36dc633f575813d59a43d
73 *
74 */
Simon Glass9785c902011-11-02 09:52:07 +000075int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
76unsigned long long simple_strtoull(const char *cp, char **endp,
77 unsigned int base);
78long simple_strtol(const char *cp, char **endp, unsigned int base);
Roland Gaudig0b016422021-07-23 12:29:18 +000079long long simple_strtoll(const char *cp, char **endp, unsigned int base);
Simon Glass66312372015-02-27 22:06:32 -070080
81/**
Simon Glassc4af6732015-06-23 15:39:08 -060082 * trailing_strtol() - extract a trailing integer from a string
83 *
84 * Given a string this finds a trailing number on the string and returns it.
85 * For example, "abc123" would return 123.
86 *
87 * @str: String to exxamine
88 * @return training number if found, else -1
89 */
90long trailing_strtol(const char *str);
91
92/**
93 * trailing_strtoln() - extract a trailing integer from a fixed-length string
94 *
95 * Given a fixed-length string this finds a trailing number on the string
96 * and returns it. For example, "abc123" would return 123. Only the
97 * characters between @str and @end - 1 are examined. If @end is NULL, it is
98 * set to str + strlen(str).
99 *
100 * @str: String to exxamine
101 * @end: Pointer to end of string to examine, or NULL to use the
102 * whole string
103 * @return training number if found, else -1
104 */
105long trailing_strtoln(const char *str, const char *end);
106
107/**
Simon Glass66312372015-02-27 22:06:32 -0700108 * panic() - Print a message and reset/hang
109 *
110 * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
Vagrant Cascadian3450a852016-10-23 20:45:19 -0700111 * defined, then it will hang instead of resetting.
Simon Glass66312372015-02-27 22:06:32 -0700112 *
113 * @param fmt: printf() format string for message, which should not include
114 * \n, followed by arguments
115 */
Simon Glass9785c902011-11-02 09:52:07 +0000116void panic(const char *fmt, ...)
117 __attribute__ ((format (__printf__, 1, 2), noreturn));
Simon Glass71ec92b2011-11-02 09:52:09 +0000118
119/**
Simon Glass66312372015-02-27 22:06:32 -0700120 * panic_str() - Print a message and reset/hang
121 *
122 * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
Vagrant Cascadian3450a852016-10-23 20:45:19 -0700123 * defined, then it will hang instead of resetting.
Simon Glass66312372015-02-27 22:06:32 -0700124 *
125 * This function can be used instead of panic() when your board does not
126 * already use printf(), * to keep code size small.
127 *
128 * @param fmt: string to display, which should not include \n
129 */
130void panic_str(const char *str) __attribute__ ((noreturn));
131
132/**
Simon Glass71ec92b2011-11-02 09:52:09 +0000133 * Format a string and place it in a buffer
134 *
135 * @param buf The buffer to place the result into
136 * @param fmt The format string to use
137 * @param ... Arguments for the format string
138 *
139 * The function returns the number of characters written
140 * into @buf.
141 *
142 * See the vsprintf() documentation for format string extensions over C99.
143 */
Simon Glass9785c902011-11-02 09:52:07 +0000144int sprintf(char *buf, const char *fmt, ...)
145 __attribute__ ((format (__printf__, 2, 3)));
Simon Glass71ec92b2011-11-02 09:52:09 +0000146
147/**
148 * Format a string and place it in a buffer (va_list version)
149 *
150 * @param buf The buffer to place the result into
Simon Glass71ec92b2011-11-02 09:52:09 +0000151 * @param fmt The format string to use
152 * @param args Arguments for the format string
153 * @return the number of characters which have been written into
Heinrich Schuchardtde2de312017-09-06 17:55:13 +0200154 * the @buf not including the trailing '\0'.
Simon Glass71ec92b2011-11-02 09:52:09 +0000155 *
156 * If you're not already dealing with a va_list consider using scnprintf().
157 *
158 * See the vsprintf() documentation for format string extensions over C99.
159 */
Simon Glass9785c902011-11-02 09:52:07 +0000160int vsprintf(char *buf, const char *fmt, va_list args);
161char *simple_itoa(ulong i);
162
Simon Glass71ec92b2011-11-02 09:52:09 +0000163/**
164 * Format a string and place it in a buffer
165 *
166 * @param buf The buffer to place the result into
167 * @param size The size of the buffer, including the trailing null space
168 * @param fmt The format string to use
169 * @param ... Arguments for the format string
170 * @return the number of characters which would be
171 * generated for the given input, excluding the trailing null,
172 * as per ISO C99. If the return is greater than or equal to
173 * @size, the resulting string is truncated.
174 *
175 * See the vsprintf() documentation for format string extensions over C99.
176 */
Sonny Rao046a37b2011-11-02 09:52:08 +0000177int snprintf(char *buf, size_t size, const char *fmt, ...)
178 __attribute__ ((format (__printf__, 3, 4)));
Simon Glass71ec92b2011-11-02 09:52:09 +0000179
180/**
181 * Format a string and place it in a buffer
182 *
183 * @param buf The buffer to place the result into
184 * @param size The size of the buffer, including the trailing null space
185 * @param fmt The format string to use
186 * @param ... Arguments for the format string
187 *
188 * The return value is the number of characters written into @buf not including
189 * the trailing '\0'. If @size is == 0 the function returns 0.
190 *
191 * See the vsprintf() documentation for format string extensions over C99.
192 */
Sonny Rao046a37b2011-11-02 09:52:08 +0000193int scnprintf(char *buf, size_t size, const char *fmt, ...)
194 __attribute__ ((format (__printf__, 3, 4)));
Simon Glass71ec92b2011-11-02 09:52:09 +0000195
196/**
197 * Format a string and place it in a buffer (base function)
198 *
199 * @param buf The buffer to place the result into
200 * @param size The size of the buffer, including the trailing null space
201 * @param fmt The format string to use
202 * @param args Arguments for the format string
203 * @return The number characters which would be generated for the given
204 * input, excluding the trailing '\0', as per ISO C99. Note that fewer
205 * characters may be written if this number of characters is >= size.
206 *
207 * This function follows C99 vsnprintf, but has some extensions:
208 * %pS output the name of a text symbol
209 * %pF output the name of a function pointer
210 * %pR output the address range in a struct resource
211 *
212 * The function returns the number of characters which would be
213 * generated for the given input, excluding the trailing '\0',
214 * as per ISO C99.
215 *
216 * Call this function if you are already dealing with a va_list.
217 * You probably want snprintf() instead.
218 */
Sonny Rao046a37b2011-11-02 09:52:08 +0000219int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
Simon Glass71ec92b2011-11-02 09:52:09 +0000220
221/**
222 * Format a string and place it in a buffer (va_list version)
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 of characters which have been written into
229 * the @buf not including the trailing '\0'. If @size is == 0 the function
230 * returns 0.
231 *
232 * If you're not already dealing with a va_list consider using scnprintf().
233 *
234 * See the vsprintf() documentation for format string extensions over C99.
235 */
Sonny Rao046a37b2011-11-02 09:52:08 +0000236int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
Sonny Rao046a37b2011-11-02 09:52:08 +0000237
Simon Glassb8bcaa32013-06-11 11:14:38 -0700238/**
239 * print_grouped_ull() - print a value with digits grouped by ','
240 *
241 * This prints a value with grouped digits, like 12,345,678 to make it easier
242 * to read.
243 *
244 * @val: Value to print
245 * @digits: Number of digiits to print
246 */
247void print_grouped_ull(unsigned long long int_val, int digits);
248
Heiko Schocher09c32802015-04-27 07:42:05 +0200249bool str2off(const char *p, loff_t *num);
250bool str2long(const char *p, ulong *num);
Simon Glass2189d5f2019-11-14 12:57:20 -0700251
252/**
253 * strmhz() - Convert a value to a Hz string
254 *
255 * This creates a string indicating the number of MHz of a value. For example,
256 * 2700000 produces "2.7".
257 * @buf: Buffer to hold output string, which must be large enough
258 * @hz: Value to convert
259 */
260char *strmhz(char *buf, unsigned long hz);
Simon Glassfdc79a62020-04-08 08:32:56 -0600261
262/**
263 * str_to_upper() - Convert a string to upper case
264 *
265 * This simply uses toupper() on each character of the string.
266 *
267 * @in: String to convert (must be large enough to hold the output string)
268 * @out: Buffer to put converted string
269 * @len: Number of bytes available in @out (SIZE_MAX for all)
270 */
271void str_to_upper(const char *in, char *out, size_t len);
272
Andrii Anisove87dfb02020-08-06 12:42:52 +0300273/**
274 * sscanf - Unformat a buffer into a list of arguments
275 * @buf: input buffer
276 * @fmt: formatting of buffer
277 * @...: resulting arguments
278 */
279int sscanf(const char *buf, const char *fmt, ...);
280
Simon Glass9785c902011-11-02 09:52:07 +0000281#endif