Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | 9785c90 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000-2009 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Simon Glass | 9785c90 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef __VSPRINTF_H |
| 8 | #define __VSPRINTF_H |
| 9 | |
Maxime Ripard | f272f1f | 2016-07-05 10:26:36 +0200 | [diff] [blame] | 10 | #include <stdarg.h> |
Masahiro Yamada | f7d6b89 | 2017-09-16 14:10:43 +0900 | [diff] [blame] | 11 | #include <linux/types.h> |
Maxime Ripard | f272f1f | 2016-07-05 10:26:36 +0200 | [diff] [blame] | 12 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame^] | 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 |
| 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 | */ |
Simon Glass | 9785c90 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 25 | ulong simple_strtoul(const char *cp, char **endp, unsigned int base); |
Simon Glass | 71ec92b | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 26 | |
| 27 | /** |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame^] | 28 | * hex_strtoul - convert a string in hex to an unsigned long |
| 29 | * |
| 30 | * @param cp The string to be converted |
| 31 | * @param endp Updated to point to the first character not converted |
| 32 | * @return value decoded from string (0 if invalid) |
| 33 | * |
| 34 | * Converts a hex string to an unsigned long. If there are invalid characters at |
| 35 | * the end these are ignored. In the worst case, if all characters are invalid, |
| 36 | * 0 is returned |
| 37 | */ |
| 38 | unsigned long hextoul(const char *cp, char **endp); |
| 39 | |
| 40 | /** |
Simon Glass | 71ec92b | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 41 | * strict_strtoul - convert a string to an unsigned long strictly |
| 42 | * @param cp The string to be converted |
| 43 | * @param base The number base to use |
| 44 | * @param res The converted result value |
| 45 | * @return 0 if conversion is successful and *res is set to the converted |
| 46 | * value, otherwise it returns -EINVAL and *res is set to 0. |
| 47 | * |
| 48 | * strict_strtoul converts a string to an unsigned long only if the |
| 49 | * string is really an unsigned long string, any string containing |
| 50 | * any invalid char at the tail will be rejected and -EINVAL is returned, |
| 51 | * only a newline char at the tail is acceptible because people generally |
| 52 | * change a module parameter in the following way: |
| 53 | * |
| 54 | * echo 1024 > /sys/module/e1000/parameters/copybreak |
| 55 | * |
| 56 | * echo will append a newline to the tail. |
| 57 | * |
Simon Glass | 71ec92b | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 58 | * Copied this function from Linux 2.6.38 commit ID: |
| 59 | * 521cb40b0c44418a4fd36dc633f575813d59a43d |
| 60 | * |
| 61 | */ |
Simon Glass | 9785c90 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 62 | int strict_strtoul(const char *cp, unsigned int base, unsigned long *res); |
| 63 | unsigned long long simple_strtoull(const char *cp, char **endp, |
| 64 | unsigned int base); |
| 65 | long simple_strtol(const char *cp, char **endp, unsigned int base); |
Roland Gaudig | 0b01642 | 2021-07-23 12:29:18 +0000 | [diff] [blame] | 66 | long long simple_strtoll(const char *cp, char **endp, unsigned int base); |
Simon Glass | 6631237 | 2015-02-27 22:06:32 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
Simon Glass | c4af673 | 2015-06-23 15:39:08 -0600 | [diff] [blame] | 69 | * trailing_strtol() - extract a trailing integer from a string |
| 70 | * |
| 71 | * Given a string this finds a trailing number on the string and returns it. |
| 72 | * For example, "abc123" would return 123. |
| 73 | * |
| 74 | * @str: String to exxamine |
| 75 | * @return training number if found, else -1 |
| 76 | */ |
| 77 | long trailing_strtol(const char *str); |
| 78 | |
| 79 | /** |
| 80 | * trailing_strtoln() - extract a trailing integer from a fixed-length string |
| 81 | * |
| 82 | * Given a fixed-length string this finds a trailing number on the string |
| 83 | * and returns it. For example, "abc123" would return 123. Only the |
| 84 | * characters between @str and @end - 1 are examined. If @end is NULL, it is |
| 85 | * set to str + strlen(str). |
| 86 | * |
| 87 | * @str: String to exxamine |
| 88 | * @end: Pointer to end of string to examine, or NULL to use the |
| 89 | * whole string |
| 90 | * @return training number if found, else -1 |
| 91 | */ |
| 92 | long trailing_strtoln(const char *str, const char *end); |
| 93 | |
| 94 | /** |
Simon Glass | 6631237 | 2015-02-27 22:06:32 -0700 | [diff] [blame] | 95 | * panic() - Print a message and reset/hang |
| 96 | * |
| 97 | * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is |
Vagrant Cascadian | 3450a85 | 2016-10-23 20:45:19 -0700 | [diff] [blame] | 98 | * defined, then it will hang instead of resetting. |
Simon Glass | 6631237 | 2015-02-27 22:06:32 -0700 | [diff] [blame] | 99 | * |
| 100 | * @param fmt: printf() format string for message, which should not include |
| 101 | * \n, followed by arguments |
| 102 | */ |
Simon Glass | 9785c90 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 103 | void panic(const char *fmt, ...) |
| 104 | __attribute__ ((format (__printf__, 1, 2), noreturn)); |
Simon Glass | 71ec92b | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 105 | |
| 106 | /** |
Simon Glass | 6631237 | 2015-02-27 22:06:32 -0700 | [diff] [blame] | 107 | * panic_str() - Print a message and reset/hang |
| 108 | * |
| 109 | * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is |
Vagrant Cascadian | 3450a85 | 2016-10-23 20:45:19 -0700 | [diff] [blame] | 110 | * defined, then it will hang instead of resetting. |
Simon Glass | 6631237 | 2015-02-27 22:06:32 -0700 | [diff] [blame] | 111 | * |
| 112 | * This function can be used instead of panic() when your board does not |
| 113 | * already use printf(), * to keep code size small. |
| 114 | * |
| 115 | * @param fmt: string to display, which should not include \n |
| 116 | */ |
| 117 | void panic_str(const char *str) __attribute__ ((noreturn)); |
| 118 | |
| 119 | /** |
Simon Glass | 71ec92b | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 120 | * Format a string and place it in a buffer |
| 121 | * |
| 122 | * @param buf The buffer to place the result into |
| 123 | * @param fmt The format string to use |
| 124 | * @param ... Arguments for the format string |
| 125 | * |
| 126 | * The function returns the number of characters written |
| 127 | * into @buf. |
| 128 | * |
| 129 | * See the vsprintf() documentation for format string extensions over C99. |
| 130 | */ |
Simon Glass | 9785c90 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 131 | int sprintf(char *buf, const char *fmt, ...) |
| 132 | __attribute__ ((format (__printf__, 2, 3))); |
Simon Glass | 71ec92b | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * Format a string and place it in a buffer (va_list version) |
| 136 | * |
| 137 | * @param buf The buffer to place the result into |
Simon Glass | 71ec92b | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 138 | * @param fmt The format string to use |
| 139 | * @param args Arguments for the format string |
| 140 | * @return the number of characters which have been written into |
Heinrich Schuchardt | de2de31 | 2017-09-06 17:55:13 +0200 | [diff] [blame] | 141 | * the @buf not including the trailing '\0'. |
Simon Glass | 71ec92b | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 142 | * |
| 143 | * If you're not already dealing with a va_list consider using scnprintf(). |
| 144 | * |
| 145 | * See the vsprintf() documentation for format string extensions over C99. |
| 146 | */ |
Simon Glass | 9785c90 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 147 | int vsprintf(char *buf, const char *fmt, va_list args); |
| 148 | char *simple_itoa(ulong i); |
| 149 | |
Simon Glass | 71ec92b | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 150 | /** |
| 151 | * Format a string and place it in a buffer |
| 152 | * |
| 153 | * @param buf The buffer to place the result into |
| 154 | * @param size The size of the buffer, including the trailing null space |
| 155 | * @param fmt The format string to use |
| 156 | * @param ... Arguments for the format string |
| 157 | * @return the number of characters which would be |
| 158 | * generated for the given input, excluding the trailing null, |
| 159 | * as per ISO C99. If the return is greater than or equal to |
| 160 | * @size, the resulting string is truncated. |
| 161 | * |
| 162 | * See the vsprintf() documentation for format string extensions over C99. |
| 163 | */ |
Sonny Rao | 046a37b | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 164 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
| 165 | __attribute__ ((format (__printf__, 3, 4))); |
Simon Glass | 71ec92b | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 166 | |
| 167 | /** |
| 168 | * Format a string and place it in a buffer |
| 169 | * |
| 170 | * @param buf The buffer to place the result into |
| 171 | * @param size The size of the buffer, including the trailing null space |
| 172 | * @param fmt The format string to use |
| 173 | * @param ... Arguments for the format string |
| 174 | * |
| 175 | * The return value is the number of characters written into @buf not including |
| 176 | * the trailing '\0'. If @size is == 0 the function returns 0. |
| 177 | * |
| 178 | * See the vsprintf() documentation for format string extensions over C99. |
| 179 | */ |
Sonny Rao | 046a37b | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 180 | int scnprintf(char *buf, size_t size, const char *fmt, ...) |
| 181 | __attribute__ ((format (__printf__, 3, 4))); |
Simon Glass | 71ec92b | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * Format a string and place it in a buffer (base function) |
| 185 | * |
| 186 | * @param buf The buffer to place the result into |
| 187 | * @param size The size of the buffer, including the trailing null space |
| 188 | * @param fmt The format string to use |
| 189 | * @param args Arguments for the format string |
| 190 | * @return The number characters which would be generated for the given |
| 191 | * input, excluding the trailing '\0', as per ISO C99. Note that fewer |
| 192 | * characters may be written if this number of characters is >= size. |
| 193 | * |
| 194 | * This function follows C99 vsnprintf, but has some extensions: |
| 195 | * %pS output the name of a text symbol |
| 196 | * %pF output the name of a function pointer |
| 197 | * %pR output the address range in a struct resource |
| 198 | * |
| 199 | * The function returns the number of characters which would be |
| 200 | * generated for the given input, excluding the trailing '\0', |
| 201 | * as per ISO C99. |
| 202 | * |
| 203 | * Call this function if you are already dealing with a va_list. |
| 204 | * You probably want snprintf() instead. |
| 205 | */ |
Sonny Rao | 046a37b | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 206 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); |
Simon Glass | 71ec92b | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 207 | |
| 208 | /** |
| 209 | * Format a string and place it in a buffer (va_list version) |
| 210 | * |
| 211 | * @param buf The buffer to place the result into |
| 212 | * @param size The size of the buffer, including the trailing null space |
| 213 | * @param fmt The format string to use |
| 214 | * @param args Arguments for the format string |
| 215 | * @return the number of characters which have been written into |
| 216 | * the @buf not including the trailing '\0'. If @size is == 0 the function |
| 217 | * returns 0. |
| 218 | * |
| 219 | * If you're not already dealing with a va_list consider using scnprintf(). |
| 220 | * |
| 221 | * See the vsprintf() documentation for format string extensions over C99. |
| 222 | */ |
Sonny Rao | 046a37b | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 223 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); |
Sonny Rao | 046a37b | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 224 | |
Simon Glass | b8bcaa3 | 2013-06-11 11:14:38 -0700 | [diff] [blame] | 225 | /** |
| 226 | * print_grouped_ull() - print a value with digits grouped by ',' |
| 227 | * |
| 228 | * This prints a value with grouped digits, like 12,345,678 to make it easier |
| 229 | * to read. |
| 230 | * |
| 231 | * @val: Value to print |
| 232 | * @digits: Number of digiits to print |
| 233 | */ |
| 234 | void print_grouped_ull(unsigned long long int_val, int digits); |
| 235 | |
Heiko Schocher | 09c3280 | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 236 | bool str2off(const char *p, loff_t *num); |
| 237 | bool str2long(const char *p, ulong *num); |
Simon Glass | 2189d5f | 2019-11-14 12:57:20 -0700 | [diff] [blame] | 238 | |
| 239 | /** |
| 240 | * strmhz() - Convert a value to a Hz string |
| 241 | * |
| 242 | * This creates a string indicating the number of MHz of a value. For example, |
| 243 | * 2700000 produces "2.7". |
| 244 | * @buf: Buffer to hold output string, which must be large enough |
| 245 | * @hz: Value to convert |
| 246 | */ |
| 247 | char *strmhz(char *buf, unsigned long hz); |
Simon Glass | fdc79a6 | 2020-04-08 08:32:56 -0600 | [diff] [blame] | 248 | |
| 249 | /** |
| 250 | * str_to_upper() - Convert a string to upper case |
| 251 | * |
| 252 | * This simply uses toupper() on each character of the string. |
| 253 | * |
| 254 | * @in: String to convert (must be large enough to hold the output string) |
| 255 | * @out: Buffer to put converted string |
| 256 | * @len: Number of bytes available in @out (SIZE_MAX for all) |
| 257 | */ |
| 258 | void str_to_upper(const char *in, char *out, size_t len); |
| 259 | |
Andrii Anisov | e87dfb0 | 2020-08-06 12:42:52 +0300 | [diff] [blame] | 260 | /** |
| 261 | * sscanf - Unformat a buffer into a list of arguments |
| 262 | * @buf: input buffer |
| 263 | * @fmt: formatting of buffer |
| 264 | * @...: resulting arguments |
| 265 | */ |
| 266 | int sscanf(const char *buf, const char *fmt, ...); |
| 267 | |
Simon Glass | 9785c90 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 268 | #endif |