lib/string/sprintf/, lib/, src/, tests/: Move all sprintf(3)-like APIs to a subdirectory

And have a separate file for each pair of APIs.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar
2024-06-27 11:29:56 +02:00
committed by Serge Hallyn
parent 89e4be3957
commit 22272347b6
38 changed files with 152 additions and 132 deletions

View File

@@ -1,24 +0,0 @@
/*
* SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <config.h>
#ident "$Id$"
#include "string/sprintf.h"
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
extern inline int xasprintf(char **restrict s, const char *restrict fmt, ...);
extern inline int xvasprintf(char **restrict s, const char *restrict fmt,
va_list ap);
extern inline int snprintf_(char *restrict s, size_t size,
const char *restrict fmt, ...);
extern inline int vsnprintf_(char *restrict s, size_t size,
const char *restrict fmt, va_list ap);

View File

@@ -0,0 +1,16 @@
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <config.h>
#include "string/sprintf/snprintf.h"
#include <stdarg.h>
#include <stddef.h>
extern inline int snprintf_(char *restrict s, size_t size,
const char *restrict fmt, ...);
extern inline int vsnprintf_(char *restrict s, size_t size,
const char *restrict fmt, va_list ap);

View File

@@ -1,11 +1,9 @@
/*
* SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
* SPDX-License-Identifier: BSD-3-Clause
*/
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_SPRINTF_H_
#define SHADOW_INCLUDE_LIB_SPRINTF_H_
#ifndef SHADOW_INCLUDE_LIB_STRING_SPRINTF_SNPRINTF_H_
#define SHADOW_INCLUDE_LIB_STRING_SPRINTF_SNPRINTF_H_
#include <config.h>
@@ -15,19 +13,15 @@
#include <stdio.h>
#include "attr.h"
#include "defines.h"
#include "sizeof.h"
#define SNPRINTF(s, fmt, ...) \
snprintf_(s, NITEMS(s), fmt __VA_OPT__(,) __VA_ARGS__)
( \
snprintf_(s, NITEMS(s), fmt __VA_OPT__(,) __VA_ARGS__) \
)
format_attr(printf, 2, 3)
inline int xasprintf(char **restrict s, const char *restrict fmt, ...);
format_attr(printf, 2, 0)
inline int xvasprintf(char **restrict s, const char *restrict fmt, va_list ap);
format_attr(printf, 3, 4)
inline int snprintf_(char *restrict s, size_t size, const char *restrict fmt,
...);
@@ -36,35 +30,6 @@ inline int vsnprintf_(char *restrict s, size_t size, const char *restrict fmt,
va_list ap);
inline int
xasprintf(char **restrict s, const char *restrict fmt, ...)
{
int len;
va_list ap;
va_start(ap, fmt);
len = xvasprintf(s, fmt, ap);
va_end(ap);
return len;
}
inline int
xvasprintf(char **restrict s, const char *restrict fmt, va_list ap)
{
int len;
len = vasprintf(s, fmt, ap);
if (len == -1) {
perror("asprintf");
exit(EXIT_FAILURE);
}
return len;
}
inline int
snprintf_(char *restrict s, size_t size, const char *restrict fmt, ...)
{

View File

@@ -1,25 +1,17 @@
/*
* SPDX-FileCopyrightText: 2022 - 2023, Alejandro Colomar <alx@kernel.org>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
// SPDX-FileCopyrightText: 2022-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <config.h>
#if !defined(HAVE_STPEPRINTF)
#ident "$Id$"
#include "string/stpeprintf.h"
#include "string/sprintf/stpeprintf.h"
#include <stdarg.h>
#if !defined(HAVE_STPEPRINTF)
extern inline char *stpeprintf(char *dst, char *end, const char *restrict fmt,
...);
extern inline char *vstpeprintf(char *dst, char *end, const char *restrict fmt,
va_list ap);
#endif // !HAVE_STPEPRINTF
#endif

View File

@@ -1,32 +1,27 @@
/*
* SPDX-FileCopyrightText: 2022 - 2023, Alejandro Colomar <alx@kernel.org>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
// SPDX-FileCopyrightText: 2022-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STPEPRINTF_H_
#define SHADOW_INCLUDE_LIB_STPEPRINTF_H_
#ifndef SHADOW_INCLUDE_LIB_STRING_SPRINTF_STPEPRINTF_H_
#define SHADOW_INCLUDE_LIB_STRING_SPRINTF_STPEPRINTF_H_
#include <config.h>
#if !defined(HAVE_STPEPRINTF)
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include "attr.h"
#include "defines.h"
#if !defined(HAVE_STPEPRINTF)
format_attr(printf, 3, 4)
inline char *stpeprintf(char *dst, char *end, const char *restrict fmt, ...);
format_attr(printf, 3, 0)
inline char *vstpeprintf(char *dst, char *end, const char *restrict fmt,
va_list ap);
#endif
/*
@@ -79,6 +74,7 @@ inline char *vstpeprintf(char *dst, char *end, const char *restrict fmt,
*/
#if !defined(HAVE_STPEPRINTF)
inline char *
stpeprintf(char *dst, char *end, const char *restrict fmt, ...)
{
@@ -91,8 +87,10 @@ stpeprintf(char *dst, char *end, const char *restrict fmt, ...)
return p;
}
#endif
#if !defined(HAVE_STPEPRINTF)
inline char *
vstpeprintf(char *dst, char *end, const char *restrict fmt, va_list ap)
{
@@ -114,7 +112,7 @@ vstpeprintf(char *dst, char *end, const char *restrict fmt, va_list ap)
return dst + len;
}
#endif
#endif // !HAVE_STPEPRINTF
#endif // include guard

View File

@@ -0,0 +1,14 @@
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <config.h>
#include "string/sprintf/xasprintf.h"
#include <stdarg.h>
extern inline int xasprintf(char **restrict s, const char *restrict fmt, ...);
extern inline int xvasprintf(char **restrict s, const char *restrict fmt,
va_list ap);

View File

@@ -0,0 +1,54 @@
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef SHADOW_INCLUDE_LIB_STRING_SPRINTF_XASPRINTF_H_
#define SHADOW_INCLUDE_LIB_STRING_SPRINTF_XASPRINTF_H_
#include <config.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "attr.h"
format_attr(printf, 2, 3)
inline int xasprintf(char **restrict s, const char *restrict fmt, ...);
format_attr(printf, 2, 0)
inline int xvasprintf(char **restrict s, const char *restrict fmt, va_list ap);
inline int
xasprintf(char **restrict s, const char *restrict fmt, ...)
{
int len;
va_list ap;
va_start(ap, fmt);
len = xvasprintf(s, fmt, ap);
va_end(ap);
return len;
}
inline int
xvasprintf(char **restrict s, const char *restrict fmt, va_list ap)
{
int len;
len = vasprintf(s, fmt, ap);
if (len == -1) {
perror("asprintf");
exit(EXIT_FAILURE);
}
return len;
}
#endif // include guard