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 ulong simple_strtoul(const char *cp, char **endp, unsigned int base);
14 
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  */
40 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
41 unsigned long long simple_strtoull(const char *cp, char **endp,
42 					unsigned int base);
43 long simple_strtol(const char *cp, char **endp, unsigned int base);
44 
45 /**
46  * trailing_strtol() - extract a trailing integer from a string
47  *
48  * Given a string this finds a trailing number on the string and returns it.
49  * For example, "abc123" would return 123.
50  *
51  * @str:	String to exxamine
52  * @return training number if found, else -1
53  */
54 long trailing_strtol(const char *str);
55 
56 /**
57  * trailing_strtoln() - extract a trailing integer from a fixed-length string
58  *
59  * Given a fixed-length string this finds a trailing number on the string
60  * and returns it. For example, "abc123" would return 123. Only the
61  * characters between @str and @end - 1 are examined. If @end is NULL, it is
62  * set to str + strlen(str).
63  *
64  * @str:	String to exxamine
65  * @end:	Pointer to end of string to examine, or NULL to use the
66  *		whole string
67  * @return training number if found, else -1
68  */
69 long trailing_strtoln(const char *str, const char *end);
70 
71 /**
72  * panic() - Print a message and reset/hang
73  *
74  * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
75  * defined, then it will hang instead of resetting.
76  *
77  * @param fmt:	printf() format string for message, which should not include
78  *		\n, followed by arguments
79  */
80 void panic(const char *fmt, ...)
81 		__attribute__ ((format (__printf__, 1, 2), noreturn));
82 
83 /**
84  * panic_str() - Print a message and reset/hang
85  *
86  * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
87  * defined, then it will hang instead of resetting.
88  *
89  * This function can be used instead of panic() when your board does not
90  * already use printf(), * to keep code size small.
91  *
92  * @param fmt:	string to display, which should not include \n
93  */
94 void panic_str(const char *str) __attribute__ ((noreturn));
95 
96 /**
97  * Format a string and place it in a buffer
98  *
99  * @param buf	The buffer to place the result into
100  * @param fmt	The format string to use
101  * @param ...	Arguments for the format string
102  *
103  * The function returns the number of characters written
104  * into @buf.
105  *
106  * See the vsprintf() documentation for format string extensions over C99.
107  */
108 int sprintf(char *buf, const char *fmt, ...)
109 		__attribute__ ((format (__printf__, 2, 3)));
110 
111 /**
112  * Format a string and place it in a buffer (va_list version)
113  *
114  * @param buf	The buffer to place the result into
115  * @param fmt	The format string to use
116  * @param args	Arguments for the format string
117  * @return the number of characters which have been written into
118  * the @buf not including the trailing '\0'.
119  *
120  * If you're not already dealing with a va_list consider using scnprintf().
121  *
122  * See the vsprintf() documentation for format string extensions over C99.
123  */
124 int vsprintf(char *buf, const char *fmt, va_list args);
125 char *simple_itoa(ulong i);
126 
127 /**
128  * Format a string and place it in a buffer
129  *
130  * @param buf	The buffer to place the result into
131  * @param size	The size of the buffer, including the trailing null space
132  * @param fmt	The format string to use
133  * @param ...	Arguments for the format string
134  * @return the number of characters which would be
135  * generated for the given input, excluding the trailing null,
136  * as per ISO C99.  If the return is greater than or equal to
137  * @size, the resulting string is truncated.
138  *
139  * See the vsprintf() documentation for format string extensions over C99.
140  */
141 int snprintf(char *buf, size_t size, const char *fmt, ...)
142 		__attribute__ ((format (__printf__, 3, 4)));
143 
144 /**
145  * Format a string and place it in a buffer
146  *
147  * @param buf	The buffer to place the result into
148  * @param size	The size of the buffer, including the trailing null space
149  * @param fmt	The format string to use
150  * @param ...	Arguments for the format string
151  *
152  * The return value is the number of characters written into @buf not including
153  * the trailing '\0'. If @size is == 0 the function returns 0.
154  *
155  * See the vsprintf() documentation for format string extensions over C99.
156  */
157 int scnprintf(char *buf, size_t size, const char *fmt, ...)
158 		__attribute__ ((format (__printf__, 3, 4)));
159 
160 /**
161  * Format a string and place it in a buffer (base function)
162  *
163  * @param buf	The buffer to place the result into
164  * @param size	The size of the buffer, including the trailing null space
165  * @param fmt	The format string to use
166  * @param args	Arguments for the format string
167  * @return The number characters which would be generated for the given
168  * input, excluding the trailing '\0', as per ISO C99. Note that fewer
169  * characters may be written if this number of characters is >= size.
170  *
171  * This function follows C99 vsnprintf, but has some extensions:
172  * %pS output the name of a text symbol
173  * %pF output the name of a function pointer
174  * %pR output the address range in a struct resource
175  *
176  * The function returns the number of characters which would be
177  * generated for the given input, excluding the trailing '\0',
178  * as per ISO C99.
179  *
180  * Call this function if you are already dealing with a va_list.
181  * You probably want snprintf() instead.
182  */
183 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
184 
185 /**
186  * Format a string and place it in a buffer (va_list version)
187  *
188  * @param buf	The buffer to place the result into
189  * @param size	The size of the buffer, including the trailing null space
190  * @param fmt	The format string to use
191  * @param args	Arguments for the format string
192  * @return the number of characters which have been written into
193  * the @buf not including the trailing '\0'. If @size is == 0 the function
194  * returns 0.
195  *
196  * If you're not already dealing with a va_list consider using scnprintf().
197  *
198  * See the vsprintf() documentation for format string extensions over C99.
199  */
200 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
201 
202 /**
203  * print_grouped_ull() - print a value with digits grouped by ','
204  *
205  * This prints a value with grouped digits, like 12,345,678 to make it easier
206  * to read.
207  *
208  * @val:	Value to print
209  * @digits:	Number of digiits to print
210  */
211 void print_grouped_ull(unsigned long long int_val, int digits);
212 
213 bool str2off(const char *p, loff_t *num);
214 bool str2long(const char *p, ulong *num);
215 
216 /**
217  * strmhz() - Convert a value to a Hz string
218  *
219  * This creates a string indicating the number of MHz of a value. For example,
220  * 2700000 produces "2.7".
221  * @buf: Buffer to hold output string, which must be large enough
222  * @hz: Value to convert
223  */
224 char *strmhz(char *buf, unsigned long hz);
225 
226 /**
227  * str_to_upper() - Convert a string to upper case
228  *
229  * This simply uses toupper() on each character of the string.
230  *
231  * @in: String to convert (must be large enough to hold the output string)
232  * @out: Buffer to put converted string
233  * @len: Number of bytes available in @out (SIZE_MAX for all)
234  */
235 void str_to_upper(const char *in, char *out, size_t len);
236 
237 /**
238  * sscanf - Unformat a buffer into a list of arguments
239  * @buf:	input buffer
240  * @fmt:	formatting of buffer
241  * @...:	resulting arguments
242  */
243 int sscanf(const char *buf, const char *fmt, ...);
244 
245 #endif
246