xref: /openbmc/u-boot/include/vsprintf.h (revision 7336278e)
1 /*
2  * (C) Copyright 2000-2009
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23 
24 #ifndef __VSPRINTF_H
25 #define __VSPRINTF_H
26 
27 ulong simple_strtoul(const char *cp, char **endp, unsigned int base);
28 
29 /**
30  * strict_strtoul - convert a string to an unsigned long strictly
31  * @param cp	The string to be converted
32  * @param base	The number base to use
33  * @param res	The converted result value
34  * @return 0 if conversion is successful and *res is set to the converted
35  * value, otherwise it returns -EINVAL and *res is set to 0.
36  *
37  * strict_strtoul converts a string to an unsigned long only if the
38  * string is really an unsigned long string, any string containing
39  * any invalid char at the tail will be rejected and -EINVAL is returned,
40  * only a newline char at the tail is acceptible because people generally
41  * change a module parameter in the following way:
42  *
43  *      echo 1024 > /sys/module/e1000/parameters/copybreak
44  *
45  * echo will append a newline to the tail.
46  *
47  * simple_strtoul just ignores the successive invalid characters and
48  * return the converted value of prefix part of the string.
49  *
50  * Copied this function from Linux 2.6.38 commit ID:
51  * 521cb40b0c44418a4fd36dc633f575813d59a43d
52  *
53  */
54 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
55 unsigned long long simple_strtoull(const char *cp, char **endp,
56 					unsigned int base);
57 long simple_strtol(const char *cp, char **endp, unsigned int base);
58 void panic(const char *fmt, ...)
59 		__attribute__ ((format (__printf__, 1, 2), noreturn));
60 
61 /**
62  * Format a string and place it in a buffer
63  *
64  * @param buf	The buffer to place the result into
65  * @param fmt	The format string to use
66  * @param ...	Arguments for the format string
67  *
68  * The function returns the number of characters written
69  * into @buf.
70  *
71  * See the vsprintf() documentation for format string extensions over C99.
72  */
73 int sprintf(char *buf, const char *fmt, ...)
74 		__attribute__ ((format (__printf__, 2, 3)));
75 
76 /**
77  * Format a string and place it in a buffer (va_list version)
78  *
79  * @param buf	The buffer to place the result into
80  * @param size	The size of the buffer, including the trailing null space
81  * @param fmt	The format string to use
82  * @param args	Arguments for the format string
83  * @return the number of characters which have been written into
84  * the @buf not including the trailing '\0'. If @size is == 0 the function
85  * returns 0.
86  *
87  * If you're not already dealing with a va_list consider using scnprintf().
88  *
89  * See the vsprintf() documentation for format string extensions over C99.
90  */
91 int vsprintf(char *buf, const char *fmt, va_list args);
92 char *simple_itoa(ulong i);
93 
94 #ifdef CONFIG_SYS_VSNPRINTF
95 /**
96  * Format a string and place it in a buffer
97  *
98  * @param buf	The buffer to place the result into
99  * @param size	The size of the buffer, including the trailing null space
100  * @param fmt	The format string to use
101  * @param ...	Arguments for the format string
102  * @return the number of characters which would be
103  * generated for the given input, excluding the trailing null,
104  * as per ISO C99.  If the return is greater than or equal to
105  * @size, the resulting string is truncated.
106  *
107  * See the vsprintf() documentation for format string extensions over C99.
108  */
109 int snprintf(char *buf, size_t size, const char *fmt, ...)
110 		__attribute__ ((format (__printf__, 3, 4)));
111 
112 /**
113  * Format a string and place it in a buffer
114  *
115  * @param buf	The buffer to place the result into
116  * @param size	The size of the buffer, including the trailing null space
117  * @param fmt	The format string to use
118  * @param ...	Arguments for the format string
119  *
120  * The return value is the number of characters written into @buf not including
121  * the trailing '\0'. If @size is == 0 the function returns 0.
122  *
123  * See the vsprintf() documentation for format string extensions over C99.
124  */
125 int scnprintf(char *buf, size_t size, const char *fmt, ...)
126 		__attribute__ ((format (__printf__, 3, 4)));
127 
128 /**
129  * Format a string and place it in a buffer (base function)
130  *
131  * @param buf	The buffer to place the result into
132  * @param size	The size of the buffer, including the trailing null space
133  * @param fmt	The format string to use
134  * @param args	Arguments for the format string
135  * @return The number characters which would be generated for the given
136  * input, excluding the trailing '\0', as per ISO C99. Note that fewer
137  * characters may be written if this number of characters is >= size.
138  *
139  * This function follows C99 vsnprintf, but has some extensions:
140  * %pS output the name of a text symbol
141  * %pF output the name of a function pointer
142  * %pR output the address range in a struct resource
143  *
144  * The function returns the number of characters which would be
145  * generated for the given input, excluding the trailing '\0',
146  * as per ISO C99.
147  *
148  * Call this function if you are already dealing with a va_list.
149  * You probably want snprintf() instead.
150  */
151 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
152 
153 /**
154  * Format a string and place it in a buffer (va_list version)
155  *
156  * @param buf	The buffer to place the result into
157  * @param size	The size of the buffer, including the trailing null space
158  * @param fmt	The format string to use
159  * @param args	Arguments for the format string
160  * @return the number of characters which have been written into
161  * the @buf not including the trailing '\0'. If @size is == 0 the function
162  * returns 0.
163  *
164  * If you're not already dealing with a va_list consider using scnprintf().
165  *
166  * See the vsprintf() documentation for format string extensions over C99.
167  */
168 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
169 #else
170 /*
171  * Use macros to silently drop the size parameter. Note that the 'cn'
172  * versions are the same as the 'n' versions since the functions assume
173  * there is always enough buffer space when !CONFIG_SYS_VSNPRINTF
174  */
175 #define snprintf(buf, size, fmt, args...) sprintf(buf, fmt, ##args)
176 #define scnprintf(buf, size, fmt, args...) sprintf(buf, fmt, ##args)
177 #define vsnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args)
178 #define vscnprintf(buf, size, fmt, args...) vsprintf(buf, fmt, ##args)
179 #endif /* CONFIG_SYS_VSNPRINTF */
180 
181 #endif
182