xref: /openbmc/u-boot/include/vsprintf.h (revision 71ec92b6)
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);
28*71ec92b6SSimon Glass 
29*71ec92b6SSimon Glass /**
30*71ec92b6SSimon Glass  * strict_strtoul - convert a string to an unsigned long strictly
31*71ec92b6SSimon Glass  * @param cp	The string to be converted
32*71ec92b6SSimon Glass  * @param base	The number base to use
33*71ec92b6SSimon Glass  * @param res	The converted result value
34*71ec92b6SSimon Glass  * @return 0 if conversion is successful and *res is set to the converted
35*71ec92b6SSimon Glass  * value, otherwise it returns -EINVAL and *res is set to 0.
36*71ec92b6SSimon Glass  *
37*71ec92b6SSimon Glass  * strict_strtoul converts a string to an unsigned long only if the
38*71ec92b6SSimon Glass  * string is really an unsigned long string, any string containing
39*71ec92b6SSimon Glass  * any invalid char at the tail will be rejected and -EINVAL is returned,
40*71ec92b6SSimon Glass  * only a newline char at the tail is acceptible because people generally
41*71ec92b6SSimon Glass  * change a module parameter in the following way:
42*71ec92b6SSimon Glass  *
43*71ec92b6SSimon Glass  *      echo 1024 > /sys/module/e1000/parameters/copybreak
44*71ec92b6SSimon Glass  *
45*71ec92b6SSimon Glass  * echo will append a newline to the tail.
46*71ec92b6SSimon Glass  *
47*71ec92b6SSimon Glass  * simple_strtoul just ignores the successive invalid characters and
48*71ec92b6SSimon Glass  * return the converted value of prefix part of the string.
49*71ec92b6SSimon Glass  *
50*71ec92b6SSimon Glass  * Copied this function from Linux 2.6.38 commit ID:
51*71ec92b6SSimon Glass  * 521cb40b0c44418a4fd36dc633f575813d59a43d
52*71ec92b6SSimon Glass  *
53*71ec92b6SSimon 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));
60*71ec92b6SSimon Glass 
61*71ec92b6SSimon Glass /**
62*71ec92b6SSimon Glass  * Format a string and place it in a buffer
63*71ec92b6SSimon Glass  *
64*71ec92b6SSimon Glass  * @param buf	The buffer to place the result into
65*71ec92b6SSimon Glass  * @param fmt	The format string to use
66*71ec92b6SSimon Glass  * @param ...	Arguments for the format string
67*71ec92b6SSimon Glass  *
68*71ec92b6SSimon Glass  * The function returns the number of characters written
69*71ec92b6SSimon Glass  * into @buf.
70*71ec92b6SSimon Glass  *
71*71ec92b6SSimon Glass  * See the vsprintf() documentation for format string extensions over C99.
72*71ec92b6SSimon Glass  */
739785c905SSimon Glass int sprintf(char *buf, const char *fmt, ...)
749785c905SSimon Glass 		__attribute__ ((format (__printf__, 2, 3)));
75*71ec92b6SSimon Glass 
76*71ec92b6SSimon Glass /**
77*71ec92b6SSimon Glass  * Format a string and place it in a buffer (va_list version)
78*71ec92b6SSimon Glass  *
79*71ec92b6SSimon Glass  * @param buf	The buffer to place the result into
80*71ec92b6SSimon Glass  * @param size	The size of the buffer, including the trailing null space
81*71ec92b6SSimon Glass  * @param fmt	The format string to use
82*71ec92b6SSimon Glass  * @param args	Arguments for the format string
83*71ec92b6SSimon Glass  * @return the number of characters which have been written into
84*71ec92b6SSimon Glass  * the @buf not including the trailing '\0'. If @size is == 0 the function
85*71ec92b6SSimon Glass  * returns 0.
86*71ec92b6SSimon Glass  *
87*71ec92b6SSimon Glass  * If you're not already dealing with a va_list consider using scnprintf().
88*71ec92b6SSimon Glass  *
89*71ec92b6SSimon Glass  * See the vsprintf() documentation for format string extensions over C99.
90*71ec92b6SSimon 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
95*71ec92b6SSimon Glass /**
96*71ec92b6SSimon Glass  * Format a string and place it in a buffer
97*71ec92b6SSimon Glass  *
98*71ec92b6SSimon Glass  * @param buf	The buffer to place the result into
99*71ec92b6SSimon Glass  * @param size	The size of the buffer, including the trailing null space
100*71ec92b6SSimon Glass  * @param fmt	The format string to use
101*71ec92b6SSimon Glass  * @param ...	Arguments for the format string
102*71ec92b6SSimon Glass  * @return the number of characters which would be
103*71ec92b6SSimon Glass  * generated for the given input, excluding the trailing null,
104*71ec92b6SSimon Glass  * as per ISO C99.  If the return is greater than or equal to
105*71ec92b6SSimon Glass  * @size, the resulting string is truncated.
106*71ec92b6SSimon Glass  *
107*71ec92b6SSimon Glass  * See the vsprintf() documentation for format string extensions over C99.
108*71ec92b6SSimon Glass  */
109046a37bdSSonny Rao int snprintf(char *buf, size_t size, const char *fmt, ...)
110046a37bdSSonny Rao 		__attribute__ ((format (__printf__, 3, 4)));
111*71ec92b6SSimon Glass 
112*71ec92b6SSimon Glass /**
113*71ec92b6SSimon Glass  * Format a string and place it in a buffer
114*71ec92b6SSimon Glass  *
115*71ec92b6SSimon Glass  * @param buf	The buffer to place the result into
116*71ec92b6SSimon Glass  * @param size	The size of the buffer, including the trailing null space
117*71ec92b6SSimon Glass  * @param fmt	The format string to use
118*71ec92b6SSimon Glass  * @param ...	Arguments for the format string
119*71ec92b6SSimon Glass  *
120*71ec92b6SSimon Glass  * The return value is the number of characters written into @buf not including
121*71ec92b6SSimon Glass  * the trailing '\0'. If @size is == 0 the function returns 0.
122*71ec92b6SSimon Glass  *
123*71ec92b6SSimon Glass  * See the vsprintf() documentation for format string extensions over C99.
124*71ec92b6SSimon Glass  */
125046a37bdSSonny Rao int scnprintf(char *buf, size_t size, const char *fmt, ...)
126046a37bdSSonny Rao 		__attribute__ ((format (__printf__, 3, 4)));
127*71ec92b6SSimon Glass 
128*71ec92b6SSimon Glass /**
129*71ec92b6SSimon Glass  * Format a string and place it in a buffer (base function)
130*71ec92b6SSimon Glass  *
131*71ec92b6SSimon Glass  * @param buf	The buffer to place the result into
132*71ec92b6SSimon Glass  * @param size	The size of the buffer, including the trailing null space
133*71ec92b6SSimon Glass  * @param fmt	The format string to use
134*71ec92b6SSimon Glass  * @param args	Arguments for the format string
135*71ec92b6SSimon Glass  * @return The number characters which would be generated for the given
136*71ec92b6SSimon Glass  * input, excluding the trailing '\0', as per ISO C99. Note that fewer
137*71ec92b6SSimon Glass  * characters may be written if this number of characters is >= size.
138*71ec92b6SSimon Glass  *
139*71ec92b6SSimon Glass  * This function follows C99 vsnprintf, but has some extensions:
140*71ec92b6SSimon Glass  * %pS output the name of a text symbol
141*71ec92b6SSimon Glass  * %pF output the name of a function pointer
142*71ec92b6SSimon Glass  * %pR output the address range in a struct resource
143*71ec92b6SSimon Glass  *
144*71ec92b6SSimon Glass  * The function returns the number of characters which would be
145*71ec92b6SSimon Glass  * generated for the given input, excluding the trailing '\0',
146*71ec92b6SSimon Glass  * as per ISO C99.
147*71ec92b6SSimon Glass  *
148*71ec92b6SSimon Glass  * Call this function if you are already dealing with a va_list.
149*71ec92b6SSimon Glass  * You probably want snprintf() instead.
150*71ec92b6SSimon Glass  */
151046a37bdSSonny Rao int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
152*71ec92b6SSimon Glass 
153*71ec92b6SSimon Glass /**
154*71ec92b6SSimon Glass  * Format a string and place it in a buffer (va_list version)
155*71ec92b6SSimon Glass  *
156*71ec92b6SSimon Glass  * @param buf	The buffer to place the result into
157*71ec92b6SSimon Glass  * @param size	The size of the buffer, including the trailing null space
158*71ec92b6SSimon Glass  * @param fmt	The format string to use
159*71ec92b6SSimon Glass  * @param args	Arguments for the format string
160*71ec92b6SSimon Glass  * @return the number of characters which have been written into
161*71ec92b6SSimon Glass  * the @buf not including the trailing '\0'. If @size is == 0 the function
162*71ec92b6SSimon Glass  * returns 0.
163*71ec92b6SSimon Glass  *
164*71ec92b6SSimon Glass  * If you're not already dealing with a va_list consider using scnprintf().
165*71ec92b6SSimon Glass  *
166*71ec92b6SSimon Glass  * See the vsprintf() documentation for format string extensions over C99.
167*71ec92b6SSimon 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 
1819785c905SSimon Glass #endif
182