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 | |
Maxime Ripard | 9f6903f | 2016-07-05 10:26:36 +0200 | [diff] [blame] | 11 | #include <stdarg.h> |
| 12 | |
Simon Glass | f7ae6806 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 13 | ulong simple_strtoul(const char *cp, char **endp, unsigned int base); |
Simon Glass | 71d4f18 | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 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 | */ |
Simon Glass | f7ae6806 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 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); |
Simon Glass | 509dbf9 | 2015-02-27 22:06:32 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
Simon Glass | 4aaa460 | 2015-06-23 15:39:08 -0600 | [diff] [blame] | 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 | /** |
Simon Glass | 509dbf9 | 2015-02-27 22:06:32 -0700 | [diff] [blame] | 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 reseting. |
| 76 | * |
| 77 | * @param fmt: printf() format string for message, which should not include |
| 78 | * \n, followed by arguments |
| 79 | */ |
Simon Glass | f7ae6806 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 80 | void panic(const char *fmt, ...) |
| 81 | __attribute__ ((format (__printf__, 1, 2), noreturn)); |
Simon Glass | 71d4f18 | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 82 | |
| 83 | /** |
Simon Glass | 509dbf9 | 2015-02-27 22:06:32 -0700 | [diff] [blame] | 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 reseting. |
| 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 | /** |
Simon Glass | 71d4f18 | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 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 | */ |
Simon Glass | f7ae6806 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 108 | int sprintf(char *buf, const char *fmt, ...) |
| 109 | __attribute__ ((format (__printf__, 2, 3))); |
Simon Glass | 71d4f18 | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 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 size The size of the buffer, including the trailing null space |
| 116 | * @param fmt The format string to use |
| 117 | * @param args Arguments for the format string |
| 118 | * @return the number of characters which have been written into |
| 119 | * the @buf not including the trailing '\0'. If @size is == 0 the function |
| 120 | * returns 0. |
| 121 | * |
| 122 | * If you're not already dealing with a va_list consider using scnprintf(). |
| 123 | * |
| 124 | * See the vsprintf() documentation for format string extensions over C99. |
| 125 | */ |
Simon Glass | f7ae6806 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 126 | int vsprintf(char *buf, const char *fmt, va_list args); |
| 127 | char *simple_itoa(ulong i); |
| 128 | |
Simon Glass | 71d4f18 | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 129 | /** |
| 130 | * Format a string and place it in a buffer |
| 131 | * |
| 132 | * @param buf The buffer to place the result into |
| 133 | * @param size The size of the buffer, including the trailing null space |
| 134 | * @param fmt The format string to use |
| 135 | * @param ... Arguments for the format string |
| 136 | * @return the number of characters which would be |
| 137 | * generated for the given input, excluding the trailing null, |
| 138 | * as per ISO C99. If the return is greater than or equal to |
| 139 | * @size, the resulting string is truncated. |
| 140 | * |
| 141 | * See the vsprintf() documentation for format string extensions over C99. |
| 142 | */ |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 143 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
| 144 | __attribute__ ((format (__printf__, 3, 4))); |
Simon Glass | 71d4f18 | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * Format a string and place it in a buffer |
| 148 | * |
| 149 | * @param buf The buffer to place the result into |
| 150 | * @param size The size of the buffer, including the trailing null space |
| 151 | * @param fmt The format string to use |
| 152 | * @param ... Arguments for the format string |
| 153 | * |
| 154 | * The return value is the number of characters written into @buf not including |
| 155 | * the trailing '\0'. If @size is == 0 the function returns 0. |
| 156 | * |
| 157 | * See the vsprintf() documentation for format string extensions over C99. |
| 158 | */ |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 159 | int scnprintf(char *buf, size_t size, const char *fmt, ...) |
| 160 | __attribute__ ((format (__printf__, 3, 4))); |
Simon Glass | 71d4f18 | 2011-11-02 09:52:09 +0000 | [diff] [blame] | 161 | |
| 162 | /** |
| 163 | * Format a string and place it in a buffer (base function) |
| 164 | * |
| 165 | * @param buf The buffer to place the result into |
| 166 | * @param size The size of the buffer, including the trailing null space |
| 167 | * @param fmt The format string to use |
| 168 | * @param args Arguments for the format string |
| 169 | * @return The number characters which would be generated for the given |
| 170 | * input, excluding the trailing '\0', as per ISO C99. Note that fewer |
| 171 | * characters may be written if this number of characters is >= size. |
| 172 | * |
| 173 | * This function follows C99 vsnprintf, but has some extensions: |
| 174 | * %pS output the name of a text symbol |
| 175 | * %pF output the name of a function pointer |
| 176 | * %pR output the address range in a struct resource |
| 177 | * |
| 178 | * The function returns the number of characters which would be |
| 179 | * generated for the given input, excluding the trailing '\0', |
| 180 | * as per ISO C99. |
| 181 | * |
| 182 | * Call this function if you are already dealing with a va_list. |
| 183 | * You probably want snprintf() instead. |
| 184 | */ |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 185 | 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] | 186 | |
| 187 | /** |
| 188 | * Format a string and place it in a buffer (va_list version) |
| 189 | * |
| 190 | * @param buf The buffer to place the result into |
| 191 | * @param size The size of the buffer, including the trailing null space |
| 192 | * @param fmt The format string to use |
| 193 | * @param args Arguments for the format string |
| 194 | * @return the number of characters which have been written into |
| 195 | * the @buf not including the trailing '\0'. If @size is == 0 the function |
| 196 | * returns 0. |
| 197 | * |
| 198 | * If you're not already dealing with a va_list consider using scnprintf(). |
| 199 | * |
| 200 | * See the vsprintf() documentation for format string extensions over C99. |
| 201 | */ |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 202 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 203 | |
Simon Glass | b09cfd8 | 2013-06-11 11:14:38 -0700 | [diff] [blame] | 204 | /** |
| 205 | * print_grouped_ull() - print a value with digits grouped by ',' |
| 206 | * |
| 207 | * This prints a value with grouped digits, like 12,345,678 to make it easier |
| 208 | * to read. |
| 209 | * |
| 210 | * @val: Value to print |
| 211 | * @digits: Number of digiits to print |
| 212 | */ |
| 213 | void print_grouped_ull(unsigned long long int_val, int digits); |
| 214 | |
Heiko Schocher | 7e5a16c | 2015-04-27 07:42:05 +0200 | [diff] [blame] | 215 | bool str2off(const char *p, loff_t *num); |
| 216 | bool str2long(const char *p, ulong *num); |
Simon Glass | f7ae6806 | 2011-11-02 09:52:07 +0000 | [diff] [blame] | 217 | #endif |