File: printf.c

package info (click to toggle)
wget2 2.2.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,468 kB
  • sloc: ansic: 121,166; sh: 11,559; makefile: 878; xml: 182; sed: 16
file content (266 lines) | stat: -rw-r--r-- 6,642 bytes parent folder | download | duplicates (2)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * Copyright (c) 2016-2024 Free Software Foundation, Inc.
 *
 * This file is part of libwget.
 *
 * Libwget is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Libwget is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libwget.  If not, see <https://www.gnu.org/licenses/>.
 *
 *
 * printf-style routines using buffer functions
 *
 * Changelog
 * 13.01.2016  Tim Ruehsen  created
 *
 */

#include <config.h>

#include <wget.h>
#include "private.h"

/**
 * \file
 * \brief Libwget printf functions
 * \defgroup libwget-printf Libwget printf functions
 * @{
 *
 * This is a collection of printf-style functions that are used with libwget and/or Wget2 code.
 * They may be useful to other developers that is why they are exported.
 */

/**
 * \param[in] strp Pointer to hold the string output
 * \param[in] fmt Printf-like format specifier
 * \param[in] args va_list of arguments
 * \return Length of the string returned via \p strp or `(size_t) -1` on error
 *
 * Prints arguments to allocated memory and 0-terminates it. The string is returned via the first argument.
 * It has to be free()'d by the caller when it is no longer needed.
 */
size_t wget_vasprintf(char **strp, const char *fmt, va_list args)
{
	wget_buffer buf;

	wget_buffer_init(&buf, NULL, 128);

	size_t len = wget_buffer_vprintf(&buf, fmt, args);

	if (unlikely(buf.error)) {
		xfree(buf.data);
		return (size_t) -1;
	}

	if (strp) {
		// shrink memory to real usage
		*strp = wget_realloc(buf.data, len + 1);
	} else {
		// behave like C99/POSIX snprintf - just return the length
		xfree(buf.data);
	}

	return len;
}

/**
 * \param[in] strp Pointer to hold the string output
 * \param[in] fmt Printf-like format specifier
 * \param[in] ... List of arguments
 * \return Length of the string returned via \p strp
 *
 * Prints arguments to allocated memory and 0-terminates it. The string is returned via the first argument.
 * It has to be free()'d by the caller when it is no longer needed.
 */
size_t wget_asprintf(char **strp, const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	size_t len = wget_vasprintf(strp, fmt, args);
	va_end(args);

	return len;
}

/**
 * \param[in] fmt Printf-like format specifier
 * \param[in] args va_list of arguments
 * \return Pointer to 0-terminated string in memory
 *
 * Prints arguments to memory and returns a pointer to allocated and 0-terminated chunk of memory.
 * The return value has to be free()'d by the caller when it is no longer needed.
 */
char *wget_vaprintf(const char *fmt, va_list args)
{
	char *s = NULL;

	wget_vasprintf(&s, fmt, args);

	return s;
}

/**
 * \param[in] fmt Printf-like format specifier
 * \param[in] ... List of arguments
 * \return Pointer to 0-terminated string in memory
 *
 * Prints arguments to memory and returns a pointer to allocated and 0-terminated chunk of memory.
 * The return value has to be free()'d by the caller when it is no longer needed.
 */
char *wget_aprintf(const char *fmt, ...)
{
	va_list args;
	char *s = NULL;

	va_start(args, fmt);
	wget_vasprintf(&s, fmt, args);
	va_end(args);

	return s;
}

/**
 * \param[in] fp FILE pointer
 * \param[in] fmt Printf-like format specifier
 * \param[in] args List of arguments
 * \return Number of bytes written or -1 on error
 *
 * Prints arguments to stream \p fp and returns number of bytes written.
 */
size_t wget_vfprintf(FILE *fp, const char *fmt, va_list args)
{
	wget_buffer buf;
	char sbuf[1024];
	size_t rc;

	wget_buffer_init(&buf, sbuf, sizeof(sbuf));

	size_t len = wget_buffer_vprintf(&buf, fmt, args);

	if (unlikely(buf.error)) {
		wget_buffer_deinit(&buf);
		return (size_t) -1;
	}

	if (len > 0)
		rc = fwrite(buf.data, 1, len, fp);
	else
		rc = 0;

	wget_buffer_deinit(&buf);

	return rc;
}

/**
 * \param[in] fp FILE pointer
 * \param[in] fmt Printf-like format specifier
 * \param[in] ... List of arguments
 * \return Number of bytes written or -1 on error
 *
 * Prints arguments to stream \p fp and returns number of bytes written.
 */
size_t wget_fprintf(FILE *fp, const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	size_t rc = wget_vfprintf(fp, fmt, args);
	va_end(args);

	return rc;
}

/**
 * \param[in] fmt Printf-like format specifier
 * \param[in] ... List of arguments
 * \return Number of bytes written or -1 on error
 *
 * Prints arguments to `stdout` and returns number of bytes written.
 */
size_t wget_printf(const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	size_t rc = wget_vfprintf(stdout, fmt, args);
	va_end(args);

	return rc;
}

/**
 * \param[in] str Destination buffer
 * \param[in] size Size of \p str
 * \param[in] fmt Printf-like format specifier
 * \param[in] args List of arguments
 * \return Number of bytes written, or, on truncation, that would have been written
 *
 * Prints arguments to buffer \p str and returns number of bytes written,
 * or on truncation: that would have been written.
 *
 * If \p str is %NULL the return value are the number of bytes that would have been written.
 */
size_t wget_vsnprintf(char *str, size_t size, const char *fmt, va_list args)
{
	wget_buffer buf;

	wget_buffer_init(&buf, str, size);

	size_t len = wget_buffer_vprintf(&buf, fmt, args);

	if (unlikely(buf.error)) {
		wget_buffer_deinit(&buf);
		return (size_t) -1;
	}

	if (str) {
		if (buf.data == str) {
			buf.data = NULL;
		} else if (len < size) {
			memcpy(str, buf.data, len + 1);
		} else {
			memcpy(str, buf.data, size - 1);
			str[size - 1] = 0;
		}
	}

	wget_buffer_deinit(&buf);

	return len;
}

/**
 * \param[in] str Destination buffer
 * \param[in] size Size of \p str
 * \param[in] fmt Printf-like format specifier
 * \param[in] ... List of arguments
 * \return Number of bytes written, or, on truncation, that would have been written
 *
 * Prints arguments to buffer \p str and returns number of bytes written,
 * or on truncation: that would have been written.
 *
 * If \p str is %NULL the return value are the number of bytes that would have been written.
 */
size_t wget_snprintf(char *str, size_t size, const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	size_t len = wget_vsnprintf(str, size, fmt, args);
	va_end(args);

	return len;
}

/**@}*/