summaryrefslogtreecommitdiffstats
path: root/src/include/vsprintf.h
blob: 99d6683e197650896a1ce2de6dc232fa6a39f273 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef VSPRINTF_H
#define VSPRINTF_H

/** @file
 *
 * printf() and friends
 *
 * Etherboot's printf() functions understand the following subset of
 * the standard C printf()'s format specifiers:
 *
 *	- Flag characters
 *		- '#'		- Alternate form (i.e. "0x" prefix)
 *		- '0'		- Zero-pad
 *	- Field widths
 *	- Length modifiers
 *		- 'hh'		- Signed / unsigned char
 *		- 'h'		- Signed / unsigned short
 *		- 'l'		- Signed / unsigned long
 *		- 'll'		- Signed / unsigned long long
 *		- 'z'		- Signed / unsigned size_t
 *	- Conversion specifiers
 *		- 'd'		- Signed decimal
 *		- 'x','X'	- Unsigned hexadecimal
 *		- 'c'		- Character
 *		- 's'		- String
 *		- 'p'		- Pointer
 *
 * Hexadecimal numbers are always zero-padded to the specified field
 * width (if any); decimal numbers are always space-padded.  Decimal
 * long longs are not supported.
 *
 */

#include <stdarg.h>

#define PRINTF_NO_LENGTH ( ( size_t ) -1 )

extern int vsnprintf ( char *buf, size_t size, const char *fmt, va_list args );
extern int vprintf ( const char *fmt, va_list args );

extern int __attribute__ (( format ( printf, 3, 4 ) ))
snprintf ( char *buf, size_t size, const char *fmt, ... );

extern int __attribute__ (( format ( printf, 1, 2 ) ))
printf ( const char *fmt, ... );

/**
 * Write a formatted string to a buffer
 *
 * @v buf		Buffer into which to write the string
 * @v fmt		Format string
 * @v ...		Arguments corresponding to the format string
 * @ret len		Length of formatted string
 */
#define sprintf( buf, fmt, ... ) \
	snprintf ( (buf), PRINTF_NO_LENGTH, (fmt), ## __VA_ARGS__ )

/**
 * Write a formatted string to a buffer
 *
 * @v buf		Buffer into which to write the string
 * @v fmt		Format string
 * @v args		Arguments corresponding to the format string
 * @ret len		Length of formatted string
 */
static inline int vsprintf ( char *buf, const char *fmt, va_list args ) {
	return vsnprintf ( buf, PRINTF_NO_LENGTH, fmt, args );
}

#endif /* VSPRINTF_H */