xref: /openbmc/u-boot/include/vsprintf.h (revision f7d6b896)
19785c905SSimon Glass /*
29785c905SSimon Glass  * (C) Copyright 2000-2009
39785c905SSimon Glass  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
49785c905SSimon Glass  *
51a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
69785c905SSimon Glass  */
79785c905SSimon Glass 
89785c905SSimon Glass #ifndef __VSPRINTF_H
99785c905SSimon Glass #define __VSPRINTF_H
109785c905SSimon Glass 
11f272f1fcSMaxime Ripard #include <stdarg.h>
12*f7d6b896SMasahiro Yamada #include <linux/types.h>
13f272f1fcSMaxime Ripard 
149785c905SSimon Glass ulong simple_strtoul(const char *cp, char **endp, unsigned int base);
1571ec92b6SSimon Glass 
1671ec92b6SSimon Glass /**
1771ec92b6SSimon Glass  * strict_strtoul - convert a string to an unsigned long strictly
1871ec92b6SSimon Glass  * @param cp	The string to be converted
1971ec92b6SSimon Glass  * @param base	The number base to use
2071ec92b6SSimon Glass  * @param res	The converted result value
2171ec92b6SSimon Glass  * @return 0 if conversion is successful and *res is set to the converted
2271ec92b6SSimon Glass  * value, otherwise it returns -EINVAL and *res is set to 0.
2371ec92b6SSimon Glass  *
2471ec92b6SSimon Glass  * strict_strtoul converts a string to an unsigned long only if the
2571ec92b6SSimon Glass  * string is really an unsigned long string, any string containing
2671ec92b6SSimon Glass  * any invalid char at the tail will be rejected and -EINVAL is returned,
2771ec92b6SSimon Glass  * only a newline char at the tail is acceptible because people generally
2871ec92b6SSimon Glass  * change a module parameter in the following way:
2971ec92b6SSimon Glass  *
3071ec92b6SSimon Glass  *      echo 1024 > /sys/module/e1000/parameters/copybreak
3171ec92b6SSimon Glass  *
3271ec92b6SSimon Glass  * echo will append a newline to the tail.
3371ec92b6SSimon Glass  *
3471ec92b6SSimon Glass  * simple_strtoul just ignores the successive invalid characters and
3571ec92b6SSimon Glass  * return the converted value of prefix part of the string.
3671ec92b6SSimon Glass  *
3771ec92b6SSimon Glass  * Copied this function from Linux 2.6.38 commit ID:
3871ec92b6SSimon Glass  * 521cb40b0c44418a4fd36dc633f575813d59a43d
3971ec92b6SSimon Glass  *
4071ec92b6SSimon Glass  */
419785c905SSimon Glass int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
429785c905SSimon Glass unsigned long long simple_strtoull(const char *cp, char **endp,
439785c905SSimon Glass 					unsigned int base);
449785c905SSimon Glass long simple_strtol(const char *cp, char **endp, unsigned int base);
4566312374SSimon Glass 
4666312374SSimon Glass /**
47c4af6732SSimon Glass  * trailing_strtol() - extract a trailing integer from a string
48c4af6732SSimon Glass  *
49c4af6732SSimon Glass  * Given a string this finds a trailing number on the string and returns it.
50c4af6732SSimon Glass  * For example, "abc123" would return 123.
51c4af6732SSimon Glass  *
52c4af6732SSimon Glass  * @str:	String to exxamine
53c4af6732SSimon Glass  * @return training number if found, else -1
54c4af6732SSimon Glass  */
55c4af6732SSimon Glass long trailing_strtol(const char *str);
56c4af6732SSimon Glass 
57c4af6732SSimon Glass /**
58c4af6732SSimon Glass  * trailing_strtoln() - extract a trailing integer from a fixed-length string
59c4af6732SSimon Glass  *
60c4af6732SSimon Glass  * Given a fixed-length string this finds a trailing number on the string
61c4af6732SSimon Glass  * and returns it. For example, "abc123" would return 123. Only the
62c4af6732SSimon Glass  * characters between @str and @end - 1 are examined. If @end is NULL, it is
63c4af6732SSimon Glass  * set to str + strlen(str).
64c4af6732SSimon Glass  *
65c4af6732SSimon Glass  * @str:	String to exxamine
66c4af6732SSimon Glass  * @end:	Pointer to end of string to examine, or NULL to use the
67c4af6732SSimon Glass  *		whole string
68c4af6732SSimon Glass  * @return training number if found, else -1
69c4af6732SSimon Glass  */
70c4af6732SSimon Glass long trailing_strtoln(const char *str, const char *end);
71c4af6732SSimon Glass 
72c4af6732SSimon Glass /**
7366312374SSimon Glass  * panic() - Print a message and reset/hang
7466312374SSimon Glass  *
7566312374SSimon Glass  * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
763450a859SVagrant Cascadian  * defined, then it will hang instead of resetting.
7766312374SSimon Glass  *
7866312374SSimon Glass  * @param fmt:	printf() format string for message, which should not include
7966312374SSimon Glass  *		\n, followed by arguments
8066312374SSimon Glass  */
819785c905SSimon Glass void panic(const char *fmt, ...)
829785c905SSimon Glass 		__attribute__ ((format (__printf__, 1, 2), noreturn));
8371ec92b6SSimon Glass 
8471ec92b6SSimon Glass /**
8566312374SSimon Glass  * panic_str() - Print a message and reset/hang
8666312374SSimon Glass  *
8766312374SSimon Glass  * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
883450a859SVagrant Cascadian  * defined, then it will hang instead of resetting.
8966312374SSimon Glass  *
9066312374SSimon Glass  * This function can be used instead of panic() when your board does not
9166312374SSimon Glass  * already use printf(), * to keep code size small.
9266312374SSimon Glass  *
9366312374SSimon Glass  * @param fmt:	string to display, which should not include \n
9466312374SSimon Glass  */
9566312374SSimon Glass void panic_str(const char *str) __attribute__ ((noreturn));
9666312374SSimon Glass 
9766312374SSimon Glass /**
9871ec92b6SSimon Glass  * Format a string and place it in a buffer
9971ec92b6SSimon Glass  *
10071ec92b6SSimon Glass  * @param buf	The buffer to place the result into
10171ec92b6SSimon Glass  * @param fmt	The format string to use
10271ec92b6SSimon Glass  * @param ...	Arguments for the format string
10371ec92b6SSimon Glass  *
10471ec92b6SSimon Glass  * The function returns the number of characters written
10571ec92b6SSimon Glass  * into @buf.
10671ec92b6SSimon Glass  *
10771ec92b6SSimon Glass  * See the vsprintf() documentation for format string extensions over C99.
10871ec92b6SSimon Glass  */
1099785c905SSimon Glass int sprintf(char *buf, const char *fmt, ...)
1109785c905SSimon Glass 		__attribute__ ((format (__printf__, 2, 3)));
11171ec92b6SSimon Glass 
11271ec92b6SSimon Glass /**
11371ec92b6SSimon Glass  * Format a string and place it in a buffer (va_list version)
11471ec92b6SSimon Glass  *
11571ec92b6SSimon Glass  * @param buf	The buffer to place the result into
11671ec92b6SSimon Glass  * @param fmt	The format string to use
11771ec92b6SSimon Glass  * @param args	Arguments for the format string
11871ec92b6SSimon Glass  * @return the number of characters which have been written into
119de2de319SHeinrich Schuchardt  * the @buf not including the trailing '\0'.
12071ec92b6SSimon Glass  *
12171ec92b6SSimon Glass  * If you're not already dealing with a va_list consider using scnprintf().
12271ec92b6SSimon Glass  *
12371ec92b6SSimon Glass  * See the vsprintf() documentation for format string extensions over C99.
12471ec92b6SSimon Glass  */
1259785c905SSimon Glass int vsprintf(char *buf, const char *fmt, va_list args);
1269785c905SSimon Glass char *simple_itoa(ulong i);
1279785c905SSimon Glass 
12871ec92b6SSimon Glass /**
12971ec92b6SSimon Glass  * Format a string and place it in a buffer
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 ...	Arguments for the format string
13571ec92b6SSimon Glass  * @return the number of characters which would be
13671ec92b6SSimon Glass  * generated for the given input, excluding the trailing null,
13771ec92b6SSimon Glass  * as per ISO C99.  If the return is greater than or equal to
13871ec92b6SSimon Glass  * @size, the resulting string is truncated.
13971ec92b6SSimon Glass  *
14071ec92b6SSimon Glass  * See the vsprintf() documentation for format string extensions over C99.
14171ec92b6SSimon Glass  */
142046a37bdSSonny Rao int snprintf(char *buf, size_t size, const char *fmt, ...)
143046a37bdSSonny Rao 		__attribute__ ((format (__printf__, 3, 4)));
14471ec92b6SSimon Glass 
14571ec92b6SSimon Glass /**
14671ec92b6SSimon Glass  * Format a string and place it in a buffer
14771ec92b6SSimon Glass  *
14871ec92b6SSimon Glass  * @param buf	The buffer to place the result into
14971ec92b6SSimon Glass  * @param size	The size of the buffer, including the trailing null space
15071ec92b6SSimon Glass  * @param fmt	The format string to use
15171ec92b6SSimon Glass  * @param ...	Arguments for the format string
15271ec92b6SSimon Glass  *
15371ec92b6SSimon Glass  * The return value is the number of characters written into @buf not including
15471ec92b6SSimon Glass  * the trailing '\0'. If @size is == 0 the function returns 0.
15571ec92b6SSimon Glass  *
15671ec92b6SSimon Glass  * See the vsprintf() documentation for format string extensions over C99.
15771ec92b6SSimon Glass  */
158046a37bdSSonny Rao int scnprintf(char *buf, size_t size, const char *fmt, ...)
159046a37bdSSonny Rao 		__attribute__ ((format (__printf__, 3, 4)));
16071ec92b6SSimon Glass 
16171ec92b6SSimon Glass /**
16271ec92b6SSimon Glass  * Format a string and place it in a buffer (base function)
16371ec92b6SSimon Glass  *
16471ec92b6SSimon Glass  * @param buf	The buffer to place the result into
16571ec92b6SSimon Glass  * @param size	The size of the buffer, including the trailing null space
16671ec92b6SSimon Glass  * @param fmt	The format string to use
16771ec92b6SSimon Glass  * @param args	Arguments for the format string
16871ec92b6SSimon Glass  * @return The number characters which would be generated for the given
16971ec92b6SSimon Glass  * input, excluding the trailing '\0', as per ISO C99. Note that fewer
17071ec92b6SSimon Glass  * characters may be written if this number of characters is >= size.
17171ec92b6SSimon Glass  *
17271ec92b6SSimon Glass  * This function follows C99 vsnprintf, but has some extensions:
17371ec92b6SSimon Glass  * %pS output the name of a text symbol
17471ec92b6SSimon Glass  * %pF output the name of a function pointer
17571ec92b6SSimon Glass  * %pR output the address range in a struct resource
17671ec92b6SSimon Glass  *
17771ec92b6SSimon Glass  * The function returns the number of characters which would be
17871ec92b6SSimon Glass  * generated for the given input, excluding the trailing '\0',
17971ec92b6SSimon Glass  * as per ISO C99.
18071ec92b6SSimon Glass  *
18171ec92b6SSimon Glass  * Call this function if you are already dealing with a va_list.
18271ec92b6SSimon Glass  * You probably want snprintf() instead.
18371ec92b6SSimon Glass  */
184046a37bdSSonny Rao int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
18571ec92b6SSimon Glass 
18671ec92b6SSimon Glass /**
18771ec92b6SSimon Glass  * Format a string and place it in a buffer (va_list version)
18871ec92b6SSimon Glass  *
18971ec92b6SSimon Glass  * @param buf	The buffer to place the result into
19071ec92b6SSimon Glass  * @param size	The size of the buffer, including the trailing null space
19171ec92b6SSimon Glass  * @param fmt	The format string to use
19271ec92b6SSimon Glass  * @param args	Arguments for the format string
19371ec92b6SSimon Glass  * @return the number of characters which have been written into
19471ec92b6SSimon Glass  * the @buf not including the trailing '\0'. If @size is == 0 the function
19571ec92b6SSimon Glass  * returns 0.
19671ec92b6SSimon Glass  *
19771ec92b6SSimon Glass  * If you're not already dealing with a va_list consider using scnprintf().
19871ec92b6SSimon Glass  *
19971ec92b6SSimon Glass  * See the vsprintf() documentation for format string extensions over C99.
20071ec92b6SSimon Glass  */
201046a37bdSSonny Rao int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
202046a37bdSSonny Rao 
203b8bcaa3aSSimon Glass /**
204b8bcaa3aSSimon Glass  * print_grouped_ull() - print a value with digits grouped by ','
205b8bcaa3aSSimon Glass  *
206b8bcaa3aSSimon Glass  * This prints a value with grouped digits, like 12,345,678 to make it easier
207b8bcaa3aSSimon Glass  * to read.
208b8bcaa3aSSimon Glass  *
209b8bcaa3aSSimon Glass  * @val:	Value to print
210b8bcaa3aSSimon Glass  * @digits:	Number of digiits to print
211b8bcaa3aSSimon Glass  */
212b8bcaa3aSSimon Glass void print_grouped_ull(unsigned long long int_val, int digits);
213b8bcaa3aSSimon Glass 
21409c32807SHeiko Schocher bool str2off(const char *p, loff_t *num);
21509c32807SHeiko Schocher bool str2long(const char *p, ulong *num);
2169785c905SSimon Glass #endif
217