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 * 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.
28 ulong simple_strtoul(const char *cp, char **endp, unsigned int base);
31 * hex_strtoul - convert a string in hex to an unsigned long
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)
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,
41 unsigned long hextoul(const char *cp, char **endp);
44 * dec_strtoul - convert a string in decimal to an unsigned long
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)
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
54 unsigned long dectoul(const char *cp, char **endp);
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.
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:
70 * echo 1024 > /sys/module/e1000/parameters/copybreak
72 * echo will append a newline to the tail.
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.
77 * Copied this function from Linux 2.6.38 commit ID:
78 * 521cb40b0c44418a4fd36dc633f575813d59a43d
81 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
82 unsigned long long simple_strtoull(const char *cp, char **endp,
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);
88 * trailing_strtol() - extract a trailing integer from a string
90 * Given a string this finds a trailing number on the string and returns it.
91 * For example, "abc123" would return 123.
93 * @str: String to exxamine
94 * @return training number if found, else -1
96 long trailing_strtol(const char *str);
99 * trailing_strtoln() - extract a trailing integer from a fixed-length string
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).
106 * @str: String to exxamine
107 * @end: Pointer to end of string to examine, or NULL to use the
109 * @return training number if found, else -1
111 long trailing_strtoln(const char *str, const char *end);
114 * panic() - Print a message and reset/hang
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.
119 * @param fmt: printf() format string for message, which should not include
120 * \n, followed by arguments
122 void panic(const char *fmt, ...)
123 __attribute__ ((format (__printf__, 1, 2), noreturn));
126 * panic_str() - Print a message and reset/hang
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.
131 * This function can be used instead of panic() when your board does not
132 * already use printf(), * to keep code size small.
134 * @param fmt: string to display, which should not include \n
136 void panic_str(const char *str) __attribute__ ((noreturn));
139 * Format a string and place it in a buffer
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
145 * The function returns the number of characters written
148 * See the vsprintf() documentation for format string extensions over C99.
150 int sprintf(char *buf, const char *fmt, ...)
151 __attribute__ ((format (__printf__, 2, 3)));
154 * Format a string and place it in a buffer (va_list version)
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'.
162 * If you're not already dealing with a va_list consider using scnprintf().
164 * See the vsprintf() documentation for format string extensions over C99.
166 int vsprintf(char *buf, const char *fmt, va_list args);
167 char *simple_itoa(ulong i);
170 * Format a string and place it in a buffer
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.
181 * See the vsprintf() documentation for format string extensions over C99.
183 int snprintf(char *buf, size_t size, const char *fmt, ...)
184 __attribute__ ((format (__printf__, 3, 4)));
187 * Format a string and place it in a buffer
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
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.
197 * See the vsprintf() documentation for format string extensions over C99.
199 int scnprintf(char *buf, size_t size, const char *fmt, ...)
200 __attribute__ ((format (__printf__, 3, 4)));
203 * Format a string and place it in a buffer (base function)
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.
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
218 * The function returns the number of characters which would be
219 * generated for the given input, excluding the trailing '\0',
222 * Call this function if you are already dealing with a va_list.
223 * You probably want snprintf() instead.
225 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
228 * Format a string and place it in a buffer (va_list version)
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
238 * If you're not already dealing with a va_list consider using scnprintf().
240 * See the vsprintf() documentation for format string extensions over C99.
242 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
245 * print_grouped_ull() - print a value with digits grouped by ','
247 * This prints a value with grouped digits, like 12,345,678 to make it easier
250 * @val: Value to print
251 * @digits: Number of digiits to print
253 void print_grouped_ull(unsigned long long int_val, int digits);
255 bool str2off(const char *p, loff_t *num);
256 bool str2long(const char *p, ulong *num);
259 * strmhz() - Convert a value to a Hz string
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
266 char *strmhz(char *buf, unsigned long hz);
269 * str_to_upper() - Convert a string to upper case
271 * This simply uses toupper() on each character of the string.
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)
277 void str_to_upper(const char *in, char *out, size_t len);
280 * sscanf - Unformat a buffer into a list of arguments
282 * @fmt: formatting of buffer
283 * @...: resulting arguments
285 int sscanf(const char *buf, const char *fmt, ...);