File: aprintf.h

package info (click to toggle)
shadow 1%3A4.19.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 66,824 kB
  • sloc: sh: 44,185; ansic: 34,143; xml: 12,350; exp: 3,691; makefile: 1,655; python: 1,409; perl: 120; sed: 16
file content (62 lines) | stat: -rw-r--r-- 1,127 bytes parent folder | download
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
// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_STRING_SPRINTF_APRINTF_H_
#define SHADOW_INCLUDE_LIB_STRING_SPRINTF_APRINTF_H_


#include "config.h"

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

#include "attr.h"
#include "exit_if_null.h"


// exit-on-error allocate print formatted
#define xaprintf(...)  exit_if_null(aprintf(__VA_ARGS__))


ATTR_MALLOC(free)
format_attr(printf, 1, 2)
inline char *aprintf(const char *restrict fmt, ...);

ATTR_MALLOC(free)
format_attr(printf, 1, 0)
inline char *vaprintf(const char *restrict fmt, va_list ap);


// allocate print formatted
// Like asprintf(3), but simpler; omit the length.
inline char *
aprintf(const char *restrict fmt, ...)
{
	char     *p;
	va_list  ap;

	va_start(ap, fmt);
	p = vaprintf(fmt, ap);
	va_end(ap);

	return p;
}


// Like vasprintf(3), but simpler; omit the length.
inline char *
vaprintf(const char *restrict fmt, va_list ap)
{
	char  *p;

	if (vasprintf(&p, fmt, ap) == -1)
		return NULL;

	return p;
}


#endif  // include guard