19785c905SSimon Glass /* 29785c905SSimon Glass * (C) Copyright 2000-2009 39785c905SSimon Glass * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 49785c905SSimon Glass * 59785c905SSimon Glass * See file CREDITS for list of people who contributed to this 69785c905SSimon Glass * project. 79785c905SSimon Glass * 89785c905SSimon Glass * This program is free software; you can redistribute it and/or 99785c905SSimon Glass * modify it under the terms of the GNU General Public License as 109785c905SSimon Glass * published by the Free Software Foundation; either version 2 of 119785c905SSimon Glass * the License, or (at your option) any later version. 129785c905SSimon Glass * 139785c905SSimon Glass * This program is distributed in the hope that it will be useful, 149785c905SSimon Glass * but WITHOUT ANY WARRANTY; without even the implied warranty of 159785c905SSimon Glass * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 169785c905SSimon Glass * GNU General Public License for more details. 179785c905SSimon Glass * 189785c905SSimon Glass * You should have received a copy of the GNU General Public License 199785c905SSimon Glass * along with this program; if not, write to the Free Software 209785c905SSimon Glass * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 219785c905SSimon Glass * MA 02111-1307 USA 229785c905SSimon Glass */ 239785c905SSimon Glass 249785c905SSimon Glass #ifndef __VSPRINTF_H 259785c905SSimon Glass #define __VSPRINTF_H 269785c905SSimon Glass 279785c905SSimon Glass ulong simple_strtoul(const char *cp, char **endp, unsigned int base); 2871ec92b6SSimon Glass 2971ec92b6SSimon Glass /** 3071ec92b6SSimon Glass * strict_strtoul - convert a string to an unsigned long strictly 3171ec92b6SSimon Glass * @param cp The string to be converted 3271ec92b6SSimon Glass * @param base The number base to use 3371ec92b6SSimon Glass * @param res The converted result value 3471ec92b6SSimon Glass * @return 0 if conversion is successful and *res is set to the converted 3571ec92b6SSimon Glass * value, otherwise it returns -EINVAL and *res is set to 0. 3671ec92b6SSimon Glass * 3771ec92b6SSimon Glass * strict_strtoul converts a string to an unsigned long only if the 3871ec92b6SSimon Glass * string is really an unsigned long string, any string containing 3971ec92b6SSimon Glass * any invalid char at the tail will be rejected and -EINVAL is returned, 4071ec92b6SSimon Glass * only a newline char at the tail is acceptible because people generally 4171ec92b6SSimon Glass * change a module parameter in the following way: 4271ec92b6SSimon Glass * 4371ec92b6SSimon Glass * echo 1024 > /sys/module/e1000/parameters/copybreak 4471ec92b6SSimon Glass * 4571ec92b6SSimon Glass * echo will append a newline to the tail. 4671ec92b6SSimon Glass * 4771ec92b6SSimon Glass * simple_strtoul just ignores the successive invalid characters and 4871ec92b6SSimon Glass * return the converted value of prefix part of the string. 4971ec92b6SSimon Glass * 5071ec92b6SSimon Glass * Copied this function from Linux 2.6.38 commit ID: 5171ec92b6SSimon Glass * 521cb40b0c44418a4fd36dc633f575813d59a43d 5271ec92b6SSimon Glass * 5371ec92b6SSimon Glass */ 549785c905SSimon Glass int strict_strtoul(const char *cp, unsigned int base, unsigned long *res); 559785c905SSimon Glass unsigned long long simple_strtoull(const char *cp, char **endp, 569785c905SSimon Glass unsigned int base); 579785c905SSimon Glass long simple_strtol(const char *cp, char **endp, unsigned int base); 589785c905SSimon Glass void panic(const char *fmt, ...) 599785c905SSimon Glass __attribute__ ((format (__printf__, 1, 2), noreturn)); 6071ec92b6SSimon Glass 6171ec92b6SSimon Glass /** 6271ec92b6SSimon Glass * Format a string and place it in a buffer 6371ec92b6SSimon Glass * 6471ec92b6SSimon Glass * @param buf The buffer to place the result into 6571ec92b6SSimon Glass * @param fmt The format string to use 6671ec92b6SSimon Glass * @param ... Arguments for the format string 6771ec92b6SSimon Glass * 6871ec92b6SSimon Glass * The function returns the number of characters written 6971ec92b6SSimon Glass * into @buf. 7071ec92b6SSimon Glass * 7171ec92b6SSimon Glass * See the vsprintf() documentation for format string extensions over C99. 7271ec92b6SSimon Glass */ 739785c905SSimon Glass int sprintf(char *buf, const char *fmt, ...) 749785c905SSimon Glass __attribute__ ((format (__printf__, 2, 3))); 7571ec92b6SSimon Glass 7671ec92b6SSimon Glass /** 7771ec92b6SSimon Glass * Format a string and place it in a buffer (va_list version) 7871ec92b6SSimon Glass * 7971ec92b6SSimon Glass * @param buf The buffer to place the result into 8071ec92b6SSimon Glass * @param size The size of the buffer, including the trailing null space 8171ec92b6SSimon Glass * @param fmt The format string to use 8271ec92b6SSimon Glass * @param args Arguments for the format string 8371ec92b6SSimon Glass * @return the number of characters which have been written into 8471ec92b6SSimon Glass * the @buf not including the trailing '\0'. If @size is == 0 the function 8571ec92b6SSimon Glass * returns 0. 8671ec92b6SSimon Glass * 8771ec92b6SSimon Glass * If you're not already dealing with a va_list consider using scnprintf(). 8871ec92b6SSimon Glass * 8971ec92b6SSimon Glass * See the vsprintf() documentation for format string extensions over C99. 9071ec92b6SSimon Glass */ 919785c905SSimon Glass int vsprintf(char *buf, const char *fmt, va_list args); 929785c905SSimon Glass char *simple_itoa(ulong i); 939785c905SSimon Glass 94046a37bdSSonny Rao #ifdef CONFIG_SYS_VSNPRINTF 9571ec92b6SSimon Glass /** 9671ec92b6SSimon Glass * Format a string and place it in a buffer 9771ec92b6SSimon Glass * 9871ec92b6SSimon Glass * @param buf The buffer to place the result into 9971ec92b6SSimon Glass * @param size The size of the buffer, including the trailing null space 10071ec92b6SSimon Glass * @param fmt The format string to use 10171ec92b6SSimon Glass * @param ... Arguments for the format string 10271ec92b6SSimon Glass * @return the number of characters which would be 10371ec92b6SSimon Glass * generated for the given input, excluding the trailing null, 10471ec92b6SSimon Glass * as per ISO C99. If the return is greater than or equal to 10571ec92b6SSimon Glass * @size, the resulting string is truncated. 10671ec92b6SSimon Glass * 10771ec92b6SSimon Glass * See the vsprintf() documentation for format string extensions over C99. 10871ec92b6SSimon Glass */ 109046a37bdSSonny Rao int snprintf(char *buf, size_t size, const char *fmt, ...) 110046a37bdSSonny Rao __attribute__ ((format (__printf__, 3, 4))); 11171ec92b6SSimon Glass 11271ec92b6SSimon Glass /** 11371ec92b6SSimon Glass * Format a string and place it in a buffer 11471ec92b6SSimon Glass * 11571ec92b6SSimon Glass * @param buf The buffer to place the result into 11671ec92b6SSimon Glass * @param size The size of the buffer, including the trailing null space 11771ec92b6SSimon Glass * @param fmt The format string to use 11871ec92b6SSimon Glass * @param ... Arguments for the format string 11971ec92b6SSimon Glass * 12071ec92b6SSimon Glass * The return value is the number of characters written into @buf not including 12171ec92b6SSimon Glass * the trailing '\0'. If @size is == 0 the function returns 0. 12271ec92b6SSimon Glass * 12371ec92b6SSimon Glass * See the vsprintf() documentation for format string extensions over C99. 12471ec92b6SSimon Glass */ 125046a37bdSSonny Rao int scnprintf(char *buf, size_t size, const char *fmt, ...) 126046a37bdSSonny Rao __attribute__ ((format (__printf__, 3, 4))); 12771ec92b6SSimon Glass 12871ec92b6SSimon Glass /** 12971ec92b6SSimon Glass * Format a string and place it in a buffer (base function) 13071ec92b6SSimon Glass * 13171ec92b6SSimon Glass * @param buf The buffer to place the result into 13271ec92b6SSimon Glass * @param size The size of the buffer, including the trailing null space 13371ec92b6SSimon Glass * @param fmt The format string to use 13471ec92b6SSimon Glass * @param args Arguments for the format string 13571ec92b6SSimon Glass * @return The number characters which would be generated for the given 13671ec92b6SSimon Glass * input, excluding the trailing '\0', as per ISO C99. Note that fewer 13771ec92b6SSimon Glass * characters may be written if this number of characters is >= size. 13871ec92b6SSimon Glass * 13971ec92b6SSimon Glass * This function follows C99 vsnprintf, but has some extensions: 14071ec92b6SSimon Glass * %pS output the name of a text symbol 14171ec92b6SSimon Glass * %pF output the name of a function pointer 14271ec92b6SSimon Glass * %pR output the address range in a struct resource 14371ec92b6SSimon Glass * 14471ec92b6SSimon Glass * The function returns the number of characters which would be 14571ec92b6SSimon Glass * generated for the given input, excluding the trailing '\0', 14671ec92b6SSimon Glass * as per ISO C99. 14771ec92b6SSimon Glass * 14871ec92b6SSimon Glass * Call this function if you are already dealing with a va_list. 14971ec92b6SSimon Glass * You probably want snprintf() instead. 15071ec92b6SSimon Glass */ 151046a37bdSSonny Rao int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 15271ec92b6SSimon Glass 15371ec92b6SSimon Glass /** 15471ec92b6SSimon Glass * Format a string and place it in a buffer (va_list version) 15571ec92b6SSimon Glass * 15671ec92b6SSimon Glass * @param buf The buffer to place the result into 15771ec92b6SSimon Glass * @param size The size of the buffer, including the trailing null space 15871ec92b6SSimon Glass * @param fmt The format string to use 15971ec92b6SSimon Glass * @param args Arguments for the format string 16071ec92b6SSimon Glass * @return the number of characters which have been written into 16171ec92b6SSimon Glass * the @buf not including the trailing '\0'. If @size is == 0 the function 16271ec92b6SSimon Glass * returns 0. 16371ec92b6SSimon Glass * 16471ec92b6SSimon Glass * If you're not already dealing with a va_list consider using scnprintf(). 16571ec92b6SSimon Glass * 16671ec92b6SSimon Glass * See the vsprintf() documentation for format string extensions over C99. 16771ec92b6SSimon Glass */ 168046a37bdSSonny Rao int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); 169046a37bdSSonny Rao #else 170046a37bdSSonny Rao /* 171046a37bdSSonny Rao * Use macros to silently drop the size parameter. Note that the 'cn' 172046a37bdSSonny Rao * versions are the same as the 'n' versions since the functions assume 173046a37bdSSonny Rao * there is always enough buffer space when !CONFIG_SYS_VSNPRINTF 174046a37bdSSonny Rao */ 175046a37bdSSonny Rao #define snprintf(buf, size, fmt, args...) sprintf(buf, fmt, ##args) 176046a37bdSSonny Rao #define scnprintf(buf, size, fmt, args...) sprintf(buf, fmt, ##args) 177046a37bdSSonny Rao #define vsnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args) 178046a37bdSSonny Rao #define vscnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args) 179046a37bdSSonny Rao #endif /* CONFIG_SYS_VSNPRINTF */ 180046a37bdSSonny Rao 181*b8bcaa3aSSimon Glass /** 182*b8bcaa3aSSimon Glass * print_grouped_ull() - print a value with digits grouped by ',' 183*b8bcaa3aSSimon Glass * 184*b8bcaa3aSSimon Glass * This prints a value with grouped digits, like 12,345,678 to make it easier 185*b8bcaa3aSSimon Glass * to read. 186*b8bcaa3aSSimon Glass * 187*b8bcaa3aSSimon Glass * @val: Value to print 188*b8bcaa3aSSimon Glass * @digits: Number of digiits to print 189*b8bcaa3aSSimon Glass */ 190*b8bcaa3aSSimon Glass void print_grouped_ull(unsigned long long int_val, int digits); 191*b8bcaa3aSSimon Glass 1929785c905SSimon Glass #endif 193