debf977401aff5e60384b9098ee91de850a23e12
[platform/kernel/u-boot.git] / include / vsprintf.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * (C) Copyright 2000-2009
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6
7 #ifndef __VSPRINTF_H
8 #define __VSPRINTF_H
9
10 #include <stdarg.h>
11 #include <linux/types.h>
12
13 /**
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 (0 for the default)
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  *
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.
27  */
28 ulong simple_strtoul(const char *cp, char **endp, unsigned int base);
29
30 /**
31  * 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  */
41 unsigned long hextoul(const char *cp, char **endp);
42
43 /**
44  * 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  */
54 unsigned long dectoul(const char *cp, char **endp);
55
56 /**
57  * strict_strtoul - convert a string to an unsigned long strictly
58  * @param cp    The string to be converted
59  * @param base  The number base to use (0 for the default)
60  * @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  *
74  * 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  *
77  * Copied this function from Linux 2.6.38 commit ID:
78  * 521cb40b0c44418a4fd36dc633f575813d59a43d
79  *
80  */
81 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
82 unsigned long long simple_strtoull(const char *cp, char **endp,
83                                         unsigned int base);
84 long simple_strtol(const char *cp, char **endp, unsigned int base);
85 long long simple_strtoll(const char *cp, char **endp, unsigned int base);
86
87 /**
88  * 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  */
96 long 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  */
111 long trailing_strtoln(const char *str, const char *end);
112
113 /**
114  * panic() - Print a message and reset/hang
115  *
116  * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
117  * defined, then it will hang instead of resetting.
118  *
119  * @param fmt:  printf() format string for message, which should not include
120  *              \n, followed by arguments
121  */
122 void panic(const char *fmt, ...)
123                 __attribute__ ((format (__printf__, 1, 2), noreturn));
124
125 /**
126  * 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
129  * defined, then it will hang instead of resetting.
130  *
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  */
136 void panic_str(const char *str) __attribute__ ((noreturn));
137
138 /**
139  * 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  */
150 int sprintf(char *buf, const char *fmt, ...)
151                 __attribute__ ((format (__printf__, 2, 3)));
152
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
157  * @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
160  * the @buf not including the trailing '\0'.
161  *
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  */
166 int vsprintf(char *buf, const char *fmt, va_list args);
167 char *simple_itoa(ulong i);
168
169 /**
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  */
183 int snprintf(char *buf, size_t size, const char *fmt, ...)
184                 __attribute__ ((format (__printf__, 3, 4)));
185
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  */
199 int scnprintf(char *buf, size_t size, const char *fmt, ...)
200                 __attribute__ ((format (__printf__, 3, 4)));
201
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  */
225 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
226
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  */
242 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
243
244 /**
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  */
253 void print_grouped_ull(unsigned long long int_val, int digits);
254
255 bool str2off(const char *p, loff_t *num);
256 bool str2long(const char *p, ulong *num);
257
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  */
266 char *strmhz(char *buf, unsigned long hz);
267
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  */
277 void str_to_upper(const char *in, char *out, size_t len);
278
279 /**
280  * sscanf - Unformat a buffer into a list of arguments
281  * @buf:        input buffer
282  * @fmt:        formatting of buffer
283  * @...:        resulting arguments
284  */
285 int sscanf(const char *buf, const char *fmt, ...);
286
287 #endif