-
Notifications
You must be signed in to change notification settings - Fork 894
/
Copy pathprintf.h
140 lines (129 loc) · 5.07 KB
/
printf.h
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2005 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2004-2007 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
/** @file
*
* Buffer safe printf functions for portability to archaic platforms.
*/
#ifndef OPAL_PRINTF_H
#define OPAL_PRINTF_H
#include "opal_config.h"
#include <stdarg.h>
#include <stdlib.h>
BEGIN_C_DECLS
/**
* Writes to a string under the control of a format string
* that specifies how subsequent arguments are converted for output.
*
* @param str Output string buffer
* @param size Size of string buffer
* @param fmt Output format
* @return Length of output string
*
* At most size-1 characters are printed into the output string (the
* size'th character then gets the terminating `\0'); if the return
* value is greater than or equal to the size argument, the string was
* too short and some of the printed characters were discarded. The
* output is always null-terminated.
*
* Returns the number of characters that would have been printed if
* the size were unlimited (again, not including the final `\0').
*
* THIS IS A PORTABILITY FEATURE: USE snprintf() in CODE.
*/
OPAL_DECLSPEC int opal_snprintf(char *str, size_t size, const char *fmt, ...)
__opal_attribute_format__(__printf__, 3, 4);
/**
* Writes to a string under the control of a format string that
* specifies how arguments accessed via the variable-length argument
* facilities of stdarg(3) are converted for output.
*
* @param str Output string buffer
* @param size Size of string buffer
* @param fmt Output format
* @param ap Variable argument list pointer
* @return Length of output string
*
* At most size-1 characters are printed into the output string (the
* size'th character then gets the terminating `\0'); if the return
* value is greater than or equal to the size argument, the string was
* too short and some of the printed characters were discarded. The
* output is always null-terminated.
*
* Returns the number of characters that would have been printed if
* the size were unlimited (again, not including the final `\0').
*
* THIS IS A PORTABILITY FEATURE: USE vsnprintf() in CODE.
*/
OPAL_DECLSPEC int opal_vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
__opal_attribute_format__(__printf__, 3, 0);
/**
* Allocates and writes to a string under the control of a format
* string that specifies how subsequent arguments are converted for
* output.
*
* @param *ptr Pointer to output string buffer
* @param fmt Output format
* @return Length of output string
*
* Sets *ptr to be a pointer to a buffer sufficiently large to hold
* the formatted string. This pointer should be passed to free(3) to
* release the allocated storage when it is no longer needed. If
* sufficient space cannot be allocated, asprintf() and vasprintf()
* will return -1 and set ret to be a NULL pointer.
*
* Returns the number of characters printed.
*
* Unlike opal_snprintf and opal_vsnprintf, opal_asprintf() is always
* available and guarantees that *ptr is NULL when the underlying
* asprintf fails. The standard does not require *ptr be set to NULL
* on error and some implementations (modern Linux) do not guarantee
* such behavior.
*
*/
OPAL_DECLSPEC int opal_asprintf(char **ptr, const char *fmt, ...)
__opal_attribute_format__(__printf__, 2, 3);
/**
* Allocates and writes to a string under the control of a format
* string that specifies how arguments accessed via the
* variable-length argument facilities of stdarg(3) are converted for
* output.
*
* @param *ptr Pointer to output string buffer
* @param fmt Output format
* @param ap Variable argument list pointer
* @return Length of output string
*
* Sets *ptr to be a pointer to a buffer sufficiently large to hold
* the formatted string. This pointer should be passed to free(3) to
* release the allocated storage when it is no longer needed. If
* sufficient space cannot be allocated, asprintf() and vasprintf()
* will return -1 and set ret to be a NULL pointer.
*
* Returns the number of characters printed.
*
* Unlike opal_snprintf and opal_vsnprintf, opal_vasprintf() is always
* available and guarantees that *ptr is NULL when the underlying
* asprintf fails. The standard does not require *ptr be set to NULL
* on error and some implementations (modern Linux) do not guarantee
* such behavior.
*
*/
OPAL_DECLSPEC int opal_vasprintf(char **ptr, const char *fmt, va_list ap)
__opal_attribute_format__(__printf__, 2, 0);
END_C_DECLS
#endif /* OPAL_PRINTF_H */