1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
11 #include <linux/types.h>
14 * simple_strtoul - convert a string to an unsigned long
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)
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,
25 * A hex prefix is supported (e.g. 0x123) regardless of the value of @base.
26 * If found, the base is set to hex (16).
29 * - an octal '0' prefix (e.g. 0777) sets the base to octal (8).
30 * - otherwise the base defaults to decimal (10).
32 ulong simple_strtoul(const char *cp, char **endp, unsigned int base);
35 * hex_strtoul - convert a string in hex to an unsigned long
37 * @param cp The string to be converted
38 * @param endp Updated to point to the first character not converted
39 * @return value decoded from string (0 if invalid)
41 * Converts a hex string to an unsigned long. If there are invalid characters at
42 * the end these are ignored. In the worst case, if all characters are invalid,
45 unsigned long hextoul(const char *cp, char **endp);
48 * dec_strtoul - convert a string in decimal to an unsigned long
50 * @param cp The string to be converted
51 * @param endp Updated to point to the first character not converted
52 * @return value decoded from string (0 if invalid)
54 * Converts a decimal string to an unsigned long. If there are invalid
55 * characters at the end these are ignored. In the worst case, if all characters
56 * are invalid, 0 is returned
58 unsigned long dectoul(const char *cp, char **endp);
61 * strict_strtoul - convert a string to an unsigned long strictly
62 * @param cp The string to be converted
63 * @param base The number base to use (0 for the default)
64 * @param res The converted result value
65 * @return 0 if conversion is successful and *res is set to the converted
66 * value, otherwise it returns -EINVAL and *res is set to 0.
68 * strict_strtoul converts a string to an unsigned long only if the
69 * string is really an unsigned long string, any string containing
70 * any invalid char at the tail will be rejected and -EINVAL is returned,
71 * only a newline char at the tail is acceptible because people generally
72 * change a module parameter in the following way:
74 * echo 1024 > /sys/module/e1000/parameters/copybreak
76 * echo will append a newline to the tail.
78 * A hex prefix is supported (e.g. 0x123) regardless of the value of @base.
79 * If found, the base is set to hex (16).
82 * - an octal '0' prefix (e.g. 0777) sets the base to octal (8).
83 * - otherwise the base defaults to decimal (10).
85 * Copied this function from Linux 2.6.38 commit ID:
86 * 521cb40b0c44418a4fd36dc633f575813d59a43d
89 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
90 unsigned long long simple_strtoull(const char *cp, char **endp,
92 long simple_strtol(const char *cp, char **endp, unsigned int base);
93 long long simple_strtoll(const char *cp, char **endp, unsigned int base);
96 * trailing_strtol() - extract a trailing integer from a string
98 * Given a string this finds a trailing number on the string and returns it.
99 * For example, "abc123" would return 123.
101 * @str: String to exxamine
102 * @return training number if found, else -1
104 long trailing_strtol(const char *str);
107 * trailing_strtoln() - extract a trailing integer from a fixed-length string
109 * Given a fixed-length string this finds a trailing number on the string
110 * and returns it. For example, "abc123" would return 123. Only the
111 * characters between @str and @end - 1 are examined. If @end is NULL, it is
112 * set to str + strlen(str).
114 * @str: String to exxamine
115 * @end: Pointer to end of string to examine, or NULL to use the
117 * @return training number if found, else -1
119 long trailing_strtoln(const char *str, const char *end);
122 * panic() - Print a message and reset/hang
124 * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
125 * defined, then it will hang instead of resetting.
127 * @param fmt: printf() format string for message, which should not include
128 * \n, followed by arguments
130 void panic(const char *fmt, ...)
131 __attribute__ ((format (__printf__, 1, 2), noreturn));
134 * panic_str() - Print a message and reset/hang
136 * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
137 * defined, then it will hang instead of resetting.
139 * This function can be used instead of panic() when your board does not
140 * already use printf(), * to keep code size small.
142 * @param fmt: string to display, which should not include \n
144 void panic_str(const char *str) __attribute__ ((noreturn));
147 * Format a string and place it in a buffer
149 * @param buf The buffer to place the result into
150 * @param fmt The format string to use
151 * @param ... Arguments for the format string
153 * The function returns the number of characters written
156 * See the vsprintf() documentation for format string extensions over C99.
158 int sprintf(char *buf, const char *fmt, ...)
159 __attribute__ ((format (__printf__, 2, 3)));
162 * Format a string and place it in a buffer (va_list version)
164 * @param buf The buffer to place the result into
165 * @param fmt The format string to use
166 * @param args Arguments for the format string
167 * @return the number of characters which have been written into
168 * the @buf not including the trailing '\0'.
170 * If you're not already dealing with a va_list consider using scnprintf().
172 * See the vsprintf() documentation for format string extensions over C99.
174 int vsprintf(char *buf, const char *fmt, va_list args);
175 char *simple_itoa(ulong i);
178 * Format a string and place it in a buffer
180 * @param buf The buffer to place the result into
181 * @param size The size of the buffer, including the trailing null space
182 * @param fmt The format string to use
183 * @param ... Arguments for the format string
184 * @return the number of characters which would be
185 * generated for the given input, excluding the trailing null,
186 * as per ISO C99. If the return is greater than or equal to
187 * @size, the resulting string is truncated.
189 * See the vsprintf() documentation for format string extensions over C99.
191 int snprintf(char *buf, size_t size, const char *fmt, ...)
192 __attribute__ ((format (__printf__, 3, 4)));
195 * Format a string and place it in a buffer
197 * @param buf The buffer to place the result into
198 * @param size The size of the buffer, including the trailing null space
199 * @param fmt The format string to use
200 * @param ... Arguments for the format string
202 * The return value is the number of characters written into @buf not including
203 * the trailing '\0'. If @size is == 0 the function returns 0.
205 * See the vsprintf() documentation for format string extensions over C99.
207 int scnprintf(char *buf, size_t size, const char *fmt, ...)
208 __attribute__ ((format (__printf__, 3, 4)));
211 * Format a string and place it in a buffer (base function)
213 * @param buf The buffer to place the result into
214 * @param size The size of the buffer, including the trailing null space
215 * @param fmt The format string to use
216 * @param args Arguments for the format string
217 * @return The number characters which would be generated for the given
218 * input, excluding the trailing '\0', as per ISO C99. Note that fewer
219 * characters may be written if this number of characters is >= size.
221 * This function follows C99 vsnprintf, but has some extensions:
222 * %pS output the name of a text symbol
223 * %pF output the name of a function pointer
224 * %pR output the address range in a struct resource
226 * The function returns the number of characters which would be
227 * generated for the given input, excluding the trailing '\0',
230 * Call this function if you are already dealing with a va_list.
231 * You probably want snprintf() instead.
233 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
236 * Format a string and place it in a buffer (va_list version)
238 * @param buf The buffer to place the result into
239 * @param size The size of the buffer, including the trailing null space
240 * @param fmt The format string to use
241 * @param args Arguments for the format string
242 * @return the number of characters which have been written into
243 * the @buf not including the trailing '\0'. If @size is == 0 the function
246 * If you're not already dealing with a va_list consider using scnprintf().
248 * See the vsprintf() documentation for format string extensions over C99.
250 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
253 * print_grouped_ull() - print a value with digits grouped by ','
255 * This prints a value with grouped digits, like 12,345,678 to make it easier
258 * @val: Value to print
259 * @digits: Number of digiits to print
261 void print_grouped_ull(unsigned long long int_val, int digits);
263 bool str2off(const char *p, loff_t *num);
264 bool str2long(const char *p, ulong *num);
267 * strmhz() - Convert a value to a Hz string
269 * This creates a string indicating the number of MHz of a value. For example,
270 * 2700000 produces "2.7".
271 * @buf: Buffer to hold output string, which must be large enough
272 * @hz: Value to convert
274 char *strmhz(char *buf, unsigned long hz);
277 * str_to_upper() - Convert a string to upper case
279 * This simply uses toupper() on each character of the string.
281 * @in: String to convert (must be large enough to hold the output string)
282 * @out: Buffer to put converted string
283 * @len: Number of bytes available in @out (SIZE_MAX for all)
285 void str_to_upper(const char *in, char *out, size_t len);
288 * sscanf - Unformat a buffer into a list of arguments
290 * @fmt: formatting of buffer
291 * @...: resulting arguments
293 int sscanf(const char *buf, const char *fmt, ...);