Simon Glass | f7ae6806 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2009 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | bd8ec7e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | f7ae6806 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef __VSPRINTF_H |
| 9 | #define __VSPRINTF_H |
| 10 | |
| 11 | ulong simple_strtoul(const char *cp, char **endp, unsigned int base); |
Simon Glass | 71d4f18 | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 12 | |
| 13 | /** |
| 14 | * strict_strtoul - convert a string to an unsigned long strictly |
| 15 | * @param cp The string to be converted |
| 16 | * @param base The number base to use |
| 17 | * @param res The converted result value |
| 18 | * @return 0 if conversion is successful and *res is set to the converted |
| 19 | * value, otherwise it returns -EINVAL and *res is set to 0. |
| 20 | * |
| 21 | * strict_strtoul converts a string to an unsigned long only if the |
| 22 | * string is really an unsigned long string, any string containing |
| 23 | * any invalid char at the tail will be rejected and -EINVAL is returned, |
| 24 | * only a newline char at the tail is acceptible because people generally |
| 25 | * change a module parameter in the following way: |
| 26 | * |
| 27 | * echo 1024 > /sys/module/e1000/parameters/copybreak |
| 28 | * |
| 29 | * echo will append a newline to the tail. |
| 30 | * |
| 31 | * simple_strtoul just ignores the successive invalid characters and |
| 32 | * return the converted value of prefix part of the string. |
| 33 | * |
| 34 | * Copied this function from Linux 2.6.38 commit ID: |
| 35 | * 521cb40b0c44418a4fd36dc633f575813d59a43d |
| 36 | * |
| 37 | */ |
Simon Glass | f7ae6806 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 38 | int strict_strtoul(const char *cp, unsigned int base, unsigned long *res); |
| 39 | unsigned long long simple_strtoull(const char *cp, char **endp, |
| 40 | unsigned int base); |
| 41 | long simple_strtol(const char *cp, char **endp, unsigned int base); |
Simon Glass | 509dbf9 | 2015-02-27 22:06:32 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
Simon Glass | 4aaa460 | 2015-06-23 15:39:08 -0600 | [diff] [blame] | 44 | * trailing_strtol() - extract a trailing integer from a string |
| 45 | * |
| 46 | * Given a string this finds a trailing number on the string and returns it. |
| 47 | * For example, "abc123" would return 123. |
| 48 | * |
| 49 | * @str: String to exxamine |
| 50 | * @return training number if found, else -1 |
| 51 | */ |
| 52 | long trailing_strtol(const char *str); |
| 53 | |
| 54 | /** |
| 55 | * trailing_strtoln() - extract a trailing integer from a fixed-length string |
| 56 | * |
| 57 | * Given a fixed-length string this finds a trailing number on the string |
| 58 | * and returns it. For example, "abc123" would return 123. Only the |
| 59 | * characters between @str and @end - 1 are examined. If @end is NULL, it is |
| 60 | * set to str + strlen(str). |
| 61 | * |
| 62 | * @str: String to exxamine |
| 63 | * @end: Pointer to end of string to examine, or NULL to use the |
| 64 | * whole string |
| 65 | * @return training number if found, else -1 |
| 66 | */ |
| 67 | long trailing_strtoln(const char *str, const char *end); |
| 68 | |
| 69 | /** |
Simon Glass | 509dbf9 | 2015-02-27 22:06:32 -0700 | [diff] [blame] | 70 | * panic() - Print a message and reset/hang |
| 71 | * |
| 72 | * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is |
| 73 | * defined, then it will hang instead of reseting. |
| 74 | * |
| 75 | * @param fmt: printf() format string for message, which should not include |
| 76 | * \n, followed by arguments |
| 77 | */ |
Simon Glass | f7ae6806 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 78 | void panic(const char *fmt, ...) |
| 79 | __attribute__ ((format (__printf__, 1, 2), noreturn)); |
Simon Glass | 71d4f18 | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 80 | |
| 81 | /** |
Simon Glass | 509dbf9 | 2015-02-27 22:06:32 -0700 | [diff] [blame] | 82 | * panic_str() - Print a message and reset/hang |
| 83 | * |
| 84 | * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is |
| 85 | * defined, then it will hang instead of reseting. |
| 86 | * |
| 87 | * This function can be used instead of panic() when your board does not |
| 88 | * already use printf(), * to keep code size small. |
| 89 | * |
| 90 | * @param fmt: string to display, which should not include \n |
| 91 | */ |
| 92 | void panic_str(const char *str) __attribute__ ((noreturn)); |
| 93 | |
| 94 | /** |
Simon Glass | 71d4f18 | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 95 | * Format a string and place it in a buffer |
| 96 | * |
| 97 | * @param buf The buffer to place the result into |
| 98 | * @param fmt The format string to use |
| 99 | * @param ... Arguments for the format string |
| 100 | * |
| 101 | * The function returns the number of characters written |
| 102 | * into @buf. |
| 103 | * |
| 104 | * See the vsprintf() documentation for format string extensions over C99. |
| 105 | */ |
Simon Glass | f7ae6806 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 106 | int sprintf(char *buf, const char *fmt, ...) |
| 107 | __attribute__ ((format (__printf__, 2, 3))); |
Simon Glass | 71d4f18 | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Format a string and place it in a buffer (va_list version) |
| 111 | * |
| 112 | * @param buf The buffer to place the result into |
| 113 | * @param size The size of the buffer, including the trailing null space |
| 114 | * @param fmt The format string to use |
| 115 | * @param args Arguments for the format string |
| 116 | * @return the number of characters which have been written into |
| 117 | * the @buf not including the trailing '\0'. If @size is == 0 the function |
| 118 | * returns 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 | */ |
Simon Glass | f7ae6806 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 124 | int vsprintf(char *buf, const char *fmt, va_list args); |
| 125 | char *simple_itoa(ulong i); |
| 126 | |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 127 | #ifdef CONFIG_SYS_VSNPRINTF |
Simon Glass | 71d4f18 | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 128 | /** |
| 129 | * Format a string and place it in a buffer |
| 130 | * |
| 131 | * @param buf The buffer to place the result into |
| 132 | * @param size The size of the buffer, including the trailing null space |
| 133 | * @param fmt The format string to use |
| 134 | * @param ... Arguments for the format string |
| 135 | * @return the number of characters which would be |
| 136 | * generated for the given input, excluding the trailing null, |
| 137 | * as per ISO C99. If the return is greater than or equal to |
| 138 | * @size, the resulting string is truncated. |
| 139 | * |
| 140 | * See the vsprintf() documentation for format string extensions over C99. |
| 141 | */ |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 142 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
| 143 | __attribute__ ((format (__printf__, 3, 4))); |
Simon Glass | 71d4f18 | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * Format a string and place it in a buffer |
| 147 | * |
| 148 | * @param buf The buffer to place the result into |
| 149 | * @param size The size of the buffer, including the trailing null space |
| 150 | * @param fmt The format string to use |
| 151 | * @param ... Arguments for the format string |
| 152 | * |
| 153 | * The return value is the number of characters written into @buf not including |
| 154 | * the trailing '\0'. If @size is == 0 the function returns 0. |
| 155 | * |
| 156 | * See the vsprintf() documentation for format string extensions over C99. |
| 157 | */ |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 158 | int scnprintf(char *buf, size_t size, const char *fmt, ...) |
| 159 | __attribute__ ((format (__printf__, 3, 4))); |
Simon Glass | 71d4f18 | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * Format a string and place it in a buffer (base function) |
| 163 | * |
| 164 | * @param buf The buffer to place the result into |
| 165 | * @param size The size of the buffer, including the trailing null space |
| 166 | * @param fmt The format string to use |
| 167 | * @param args Arguments for the format string |
| 168 | * @return The number characters which would be generated for the given |
| 169 | * input, excluding the trailing '\0', as per ISO C99. Note that fewer |
| 170 | * characters may be written if this number of characters is >= size. |
| 171 | * |
| 172 | * This function follows C99 vsnprintf, but has some extensions: |
| 173 | * %pS output the name of a text symbol |
| 174 | * %pF output the name of a function pointer |
| 175 | * %pR output the address range in a struct resource |
| 176 | * |
| 177 | * The function returns the number of characters which would be |
| 178 | * generated for the given input, excluding the trailing '\0', |
| 179 | * as per ISO C99. |
| 180 | * |
| 181 | * Call this function if you are already dealing with a va_list. |
| 182 | * You probably want snprintf() instead. |
| 183 | */ |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 184 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); |
Simon Glass | 71d4f18 | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 185 | |
| 186 | /** |
| 187 | * Format a string and place it in a buffer (va_list version) |
| 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 args Arguments for the format string |
| 193 | * @return the number of characters which have been written into |
| 194 | * the @buf not including the trailing '\0'. If @size is == 0 the function |
| 195 | * returns 0. |
| 196 | * |
| 197 | * If you're not already dealing with a va_list consider using scnprintf(). |
| 198 | * |
| 199 | * See the vsprintf() documentation for format string extensions over C99. |
| 200 | */ |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 201 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); |
| 202 | #else |
| 203 | /* |
| 204 | * Use macros to silently drop the size parameter. Note that the 'cn' |
| 205 | * versions are the same as the 'n' versions since the functions assume |
| 206 | * there is always enough buffer space when !CONFIG_SYS_VSNPRINTF |
| 207 | */ |
| 208 | #define snprintf(buf, size, fmt, args...) sprintf(buf, fmt, ##args) |
| 209 | #define scnprintf(buf, size, fmt, args...) sprintf(buf, fmt, ##args) |
| 210 | #define vsnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args) |
| 211 | #define vscnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args) |
| 212 | #endif /* CONFIG_SYS_VSNPRINTF */ |
| 213 | |
Simon Glass | b09cfd8 | 2013-06-11 11:14:38 -0700 | [diff] [blame] | 214 | /** |
| 215 | * print_grouped_ull() - print a value with digits grouped by ',' |
| 216 | * |
| 217 | * This prints a value with grouped digits, like 12,345,678 to make it easier |
| 218 | * to read. |
| 219 | * |
| 220 | * @val: Value to print |
| 221 | * @digits: Number of digiits to print |
| 222 | */ |
| 223 | void print_grouped_ull(unsigned long long int_val, int digits); |
| 224 | |
Heiko Schocher | 7e5a16c | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 225 | bool str2off(const char *p, loff_t *num); |
| 226 | bool str2long(const char *p, ulong *num); |
Simon Glass | f7ae6806 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 227 | #endif |