File: alloc_printf.c

package info (click to toggle)
libreswan 4.10-2%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 68,788 kB
  • sloc: ansic: 114,477; sh: 27,411; xml: 12,342; python: 10,400; makefile: 1,658; javascript: 1,358; perl: 584; sed: 534; yacc: 392; awk: 171
file content (51 lines) | stat: -rw-r--r-- 1,248 bytes parent folder | download | duplicates (3)
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
/* asprintf(), for libreswan
 *
 * Copyright (C) 2020  Andrew Cagney
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <https://www.gnu.org/licenses/gpl2.txt>.
 *
 * This program 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 General Public License
 * for more details.
 */

#include <stdio.h>
#include <stdarg.h>

#include "passert.h"

#include "lswalloc.h"

char *alloc_vprintf(const char *fmt, va_list master_ap /* must va_copy */)
{
	int length;
	{
		va_list ap;
		va_copy(ap, master_ap);
		length = vsnprintf(NULL, 0, fmt, ap);
		va_end(ap);
	}
	passert(length >= 0);
	length++; /* space for '\0' */
	char *buf = alloc_things(char, length, fmt);
	{
		va_list ap;
		va_copy(ap, master_ap);
		vsnprintf(buf, length, fmt, ap);
		va_end(ap);
	}
	return buf;
}

char *alloc_printf(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	char *d = alloc_vprintf(fmt, ap);
	va_end(ap);
	return d;
}