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