blob: 4016de6677a0352350584320a339dbfbeb735239 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glassf7ae68062011-11-02 09:52:07 +00002/*
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glassf7ae68062011-11-02 09:52:07 +00005 */
6
7#ifndef __VSPRINTF_H
8#define __VSPRINTF_H
9
Maxime Ripard9f6903f2016-07-05 10:26:36 +020010#include <stdarg.h>
Masahiro Yamada70ed9932017-09-16 14:10:43 +090011#include <linux/types.h>
Maxime Ripard9f6903f2016-07-05 10:26:36 +020012
Simon Glassf7ae68062011-11-02 09:52:07 +000013ulong simple_strtoul(const char *cp, char **endp, unsigned int base);
Simon Glass71d4f182011-11-02 09:52:09 +000014
15/**
16 * strict_strtoul - convert a string to an unsigned long strictly
17 * @param cp The string to be converted
18 * @param base The number base to use
19 * @param res The converted result value
20 * @return 0 if conversion is successful and *res is set to the converted
21 * value, otherwise it returns -EINVAL and *res is set to 0.
22 *
23 * strict_strtoul converts a string to an unsigned long only if the
24 * string is really an unsigned long string, any string containing
25 * any invalid char at the tail will be rejected and -EINVAL is returned,
26 * only a newline char at the tail is acceptible because people generally
27 * change a module parameter in the following way:
28 *
29 * echo 1024 > /sys/module/e1000/parameters/copybreak
30 *
31 * echo will append a newline to the tail.
32 *
33 * simple_strtoul just ignores the successive invalid characters and
34 * return the converted value of prefix part of the string.
35 *
36 * Copied this function from Linux 2.6.38 commit ID:
37 * 521cb40b0c44418a4fd36dc633f575813d59a43d
38 *
39 */
Simon Glassf7ae68062011-11-02 09:52:07 +000040int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
41unsigned long long simple_strtoull(const char *cp, char **endp,
42 unsigned int base);
43long simple_strtol(const char *cp, char **endp, unsigned int base);
Roland Gaudigfd8b0342021-07-23 12:29:18 +000044long long simple_strtoll(const char *cp, char **endp, unsigned int base);
Simon Glass509dbf92015-02-27 22:06:32 -070045
46/**
Simon Glass4aaa4602015-06-23 15:39:08 -060047 * trailing_strtol() - extract a trailing integer from a string
48 *
49 * Given a string this finds a trailing number on the string and returns it.
50 * For example, "abc123" would return 123.
51 *
52 * @str: String to exxamine
53 * @return training number if found, else -1
54 */
55long trailing_strtol(const char *str);
56
57/**
58 * trailing_strtoln() - extract a trailing integer from a fixed-length string
59 *
60 * Given a fixed-length string this finds a trailing number on the string
61 * and returns it. For example, "abc123" would return 123. Only the
62 * characters between @str and @end - 1 are examined. If @end is NULL, it is
63 * set to str + strlen(str).
64 *
65 * @str: String to exxamine
66 * @end: Pointer to end of string to examine, or NULL to use the
67 * whole string
68 * @return training number if found, else -1
69 */
70long trailing_strtoln(const char *str, const char *end);
71
72/**
Simon Glass509dbf92015-02-27 22:06:32 -070073 * panic() - Print a message and reset/hang
74 *
75 * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
Vagrant Cascadian259b1fb2016-10-23 20:45:19 -070076 * defined, then it will hang instead of resetting.
Simon Glass509dbf92015-02-27 22:06:32 -070077 *
78 * @param fmt: printf() format string for message, which should not include
79 * \n, followed by arguments
80 */
Simon Glassf7ae68062011-11-02 09:52:07 +000081void panic(const char *fmt, ...)
82 __attribute__ ((format (__printf__, 1, 2), noreturn));
Simon Glass71d4f182011-11-02 09:52:09 +000083
84/**
Simon Glass509dbf92015-02-27 22:06:32 -070085 * panic_str() - Print a message and reset/hang
86 *
87 * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
Vagrant Cascadian259b1fb2016-10-23 20:45:19 -070088 * defined, then it will hang instead of resetting.
Simon Glass509dbf92015-02-27 22:06:32 -070089 *
90 * This function can be used instead of panic() when your board does not
91 * already use printf(), * to keep code size small.
92 *
93 * @param fmt: string to display, which should not include \n
94 */
95void panic_str(const char *str) __attribute__ ((noreturn));
96
97/**
Simon Glass71d4f182011-11-02 09:52:09 +000098 * Format a string and place it in a buffer
99 *
100 * @param buf The buffer to place the result into
101 * @param fmt The format string to use
102 * @param ... Arguments for the format string
103 *
104 * The function returns the number of characters written
105 * into @buf.
106 *
107 * See the vsprintf() documentation for format string extensions over C99.
108 */
Simon Glassf7ae68062011-11-02 09:52:07 +0000109int sprintf(char *buf, const char *fmt, ...)
110 __attribute__ ((format (__printf__, 2, 3)));
Simon Glass71d4f182011-11-02 09:52:09 +0000111
112/**
113 * Format a string and place it in a buffer (va_list version)
114 *
115 * @param buf The buffer to place the result into
Simon Glass71d4f182011-11-02 09:52:09 +0000116 * @param fmt The format string to use
117 * @param args Arguments for the format string
118 * @return the number of characters which have been written into
Heinrich Schuchardt20662532017-09-06 17:55:13 +0200119 * the @buf not including the trailing '\0'.
Simon Glass71d4f182011-11-02 09:52:09 +0000120 *
121 * If you're not already dealing with a va_list consider using scnprintf().
122 *
123 * See the vsprintf() documentation for format string extensions over C99.
124 */
Simon Glassf7ae68062011-11-02 09:52:07 +0000125int vsprintf(char *buf, const char *fmt, va_list args);
126char *simple_itoa(ulong i);
127
Simon Glass71d4f182011-11-02 09:52:09 +0000128/**
129 * Format a string and place it in a buffer
130 *
131 * @param buf The buffer to place the result into
132 * @param size The size of the buffer, including the trailing null space
133 * @param fmt The format string to use
134 * @param ... Arguments for the format string
135 * @return the number of characters which would be
136 * generated for the given input, excluding the trailing null,
137 * as per ISO C99. If the return is greater than or equal to
138 * @size, the resulting string is truncated.
139 *
140 * See the vsprintf() documentation for format string extensions over C99.
141 */
Sonny Raocd55bde2011-11-02 09:52:08 +0000142int snprintf(char *buf, size_t size, const char *fmt, ...)
143 __attribute__ ((format (__printf__, 3, 4)));
Simon Glass71d4f182011-11-02 09:52:09 +0000144
145/**
146 * Format a string and place it in a buffer
147 *
148 * @param buf The buffer to place the result into
149 * @param size The size of the buffer, including the trailing null space
150 * @param fmt The format string to use
151 * @param ... Arguments for the format string
152 *
153 * The return value is the number of characters written into @buf not including
154 * the trailing '\0'. If @size is == 0 the function returns 0.
155 *
156 * See the vsprintf() documentation for format string extensions over C99.
157 */
Sonny Raocd55bde2011-11-02 09:52:08 +0000158int scnprintf(char *buf, size_t size, const char *fmt, ...)
159 __attribute__ ((format (__printf__, 3, 4)));
Simon Glass71d4f182011-11-02 09:52:09 +0000160
161/**
162 * Format a string and place it in a buffer (base function)
163 *
164 * @param buf The buffer to place the result into
165 * @param size The size of the buffer, including the trailing null space
166 * @param fmt The format string to use
167 * @param args Arguments for the format string
168 * @return The number characters which would be generated for the given
169 * input, excluding the trailing '\0', as per ISO C99. Note that fewer
170 * characters may be written if this number of characters is >= size.
171 *
172 * This function follows C99 vsnprintf, but has some extensions:
173 * %pS output the name of a text symbol
174 * %pF output the name of a function pointer
175 * %pR output the address range in a struct resource
176 *
177 * The function returns the number of characters which would be
178 * generated for the given input, excluding the trailing '\0',
179 * as per ISO C99.
180 *
181 * Call this function if you are already dealing with a va_list.
182 * You probably want snprintf() instead.
183 */
Sonny Raocd55bde2011-11-02 09:52:08 +0000184int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
Simon Glass71d4f182011-11-02 09:52:09 +0000185
186/**
187 * Format a string and place it in a buffer (va_list version)
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 args Arguments for the format string
193 * @return the number of characters which have been written into
194 * the @buf not including the trailing '\0'. If @size is == 0 the function
195 * returns 0.
196 *
197 * If you're not already dealing with a va_list consider using scnprintf().
198 *
199 * See the vsprintf() documentation for format string extensions over C99.
200 */
Sonny Raocd55bde2011-11-02 09:52:08 +0000201int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
Sonny Raocd55bde2011-11-02 09:52:08 +0000202
Simon Glassb09cfd82013-06-11 11:14:38 -0700203/**
204 * print_grouped_ull() - print a value with digits grouped by ','
205 *
206 * This prints a value with grouped digits, like 12,345,678 to make it easier
207 * to read.
208 *
209 * @val: Value to print
210 * @digits: Number of digiits to print
211 */
212void print_grouped_ull(unsigned long long int_val, int digits);
213
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200214bool str2off(const char *p, loff_t *num);
215bool str2long(const char *p, ulong *num);
Simon Glassf5c208d2019-11-14 12:57:20 -0700216
217/**
218 * strmhz() - Convert a value to a Hz string
219 *
220 * This creates a string indicating the number of MHz of a value. For example,
221 * 2700000 produces "2.7".
222 * @buf: Buffer to hold output string, which must be large enough
223 * @hz: Value to convert
224 */
225char *strmhz(char *buf, unsigned long hz);
Simon Glass811a2602020-04-08 08:32:56 -0600226
227/**
228 * str_to_upper() - Convert a string to upper case
229 *
230 * This simply uses toupper() on each character of the string.
231 *
232 * @in: String to convert (must be large enough to hold the output string)
233 * @out: Buffer to put converted string
234 * @len: Number of bytes available in @out (SIZE_MAX for all)
235 */
236void str_to_upper(const char *in, char *out, size_t len);
237
Andrii Anisov6e29ac42020-08-06 12:42:52 +0300238/**
239 * sscanf - Unformat a buffer into a list of arguments
240 * @buf: input buffer
241 * @fmt: formatting of buffer
242 * @...: resulting arguments
243 */
244int sscanf(const char *buf, const char *fmt, ...);
245
Simon Glassf7ae68062011-11-02 09:52:07 +0000246#endif