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

@@ -144,12 +144,14 @@ libshadow_la_SOURCES = \
spawn.c \
sssd.c \
sssd.h \
string/sprintf.c \
string/sprintf.h \
string/sprintf/snprintf.c \
string/sprintf/snprintf.h \
string/sprintf/stpeprintf.c \
string/sprintf/stpeprintf.h \
string/sprintf/xasprintf.c \
string/sprintf/xasprintf.h \
string/stpecpy.c \
string/stpecpy.h \
string/stpeprintf.c \
string/stpeprintf.h \
string/strftime.c \
string/strftime.h \
string/strncpy.h \

View File

@@ -33,7 +33,7 @@
#include "prototypes.h"
#include "commonio.h"
#include "shadowlog_internal.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
/* local function prototypes */

View File

@@ -36,7 +36,7 @@
#include <attr/libattr.h>
#endif /* WITH_ATTR */
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/sprintf/xasprintf.h"
static /*@null@*/const char *src_orig;

View File

@@ -20,7 +20,8 @@
#include "prototypes.h"
#include "defines.h"
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
#include "string/sprintf/xasprintf.h"
/*

View File

@@ -14,7 +14,7 @@
#include <fcntl.h>
#include "atoi/getnum.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
/*

View File

@@ -26,7 +26,7 @@
#include "atoi/str2i.h"
#include "getdef.h"
#include "shadowlog_internal.h"
#include "string/sprintf.h"
#include "string/sprintf/xasprintf.h"
/*

View File

@@ -18,7 +18,7 @@
#include "defines.h"
#include "prototypes.h"
#include "getdef.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
/*

View File

@@ -16,7 +16,7 @@
#include "alloc.h"
#include "atoi/str2i.h"
#include "prototypes.h"
#include "string/stpeprintf.h"
#include "string/sprintf/stpeprintf.h"
#include "idmapping.h"
#if HAVE_SYS_CAPABILITY_H
#include <sys/prctl.h>

View File

@@ -17,7 +17,7 @@
#include "alloc.h"
#include "getdef.h"
#include "string/sprintf.h"
#include "string/sprintf/xasprintf.h"
#ident "$Id$"

View File

@@ -14,7 +14,7 @@
#include "../libsubid/subid.h"
#include "shadowlog_internal.h"
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
#define NSSWITCH "/etc/nsswitch.conf"

View File

@@ -29,7 +29,7 @@
#endif /* ENABLE_SUBIDS */
#include "getdef.h"
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/sprintf/xasprintf.h"
static char *passwd_db_file = NULL;

View File

@@ -25,7 +25,7 @@
#include "prototypes.h"
#include "pwauth.h"
#include "getdef.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
#ifdef SKEY
#include <skey.h>

View File

@@ -27,7 +27,7 @@
#include <pwd.h>
#include "getdef.h"
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/sprintf/xasprintf.h"
#ifndef USE_PAM

View File

@@ -15,7 +15,7 @@
#include <errno.h>
#include "prototypes.h"
#include "defines.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
extern char **newenvp;

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

View File

@@ -20,7 +20,7 @@
#include "alloc.h"
#include "atoi/str2i.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
#define ID_SIZE 31

View File

@@ -25,7 +25,7 @@
#include "subordinateio.h"
#endif /* ENABLE_SUBIDS */
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
#ifdef __linux__

View File

@@ -34,7 +34,7 @@
#include "pwio.h"
#include "shadowio.h"
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
#include "string/strftime.h"
#include "string/strtcpy.h"
#include "time/day_to_str.h"

View File

@@ -32,7 +32,7 @@
/*@-exitarg@*/
#include "exitcodes.h"
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
#include "string/strtcpy.h"

View File

@@ -35,7 +35,7 @@
/*@-exitarg@*/
#include "exitcodes.h"
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
#include "string/strtcpy.h"

View File

@@ -39,8 +39,8 @@
#include "sgroupio.h"
#endif
#include "shadowlog.h"
#include "string/sprintf/stpeprintf.h"
#include "string/stpecpy.h"
#include "string/stpeprintf.h"
/*

View File

@@ -38,7 +38,7 @@
/*@-exitarg@*/
#include "exitcodes.h"
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
#include "string/strftime.h"
#include "string/strtcpy.h"

View File

@@ -25,7 +25,7 @@
/*@-exitarg@*/
#include "exitcodes.h"
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
/*

View File

@@ -52,7 +52,7 @@
#endif /* ENABLE_SUBIDS */
#include "chkname.h"
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
/*

View File

@@ -59,7 +59,7 @@
/*@-exitarg@*/
#include "exitcodes.h"
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
#include "string/strtcpy.h"

View File

@@ -65,7 +65,8 @@
#include "tcbfuncs.h"
#endif
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
#include "string/sprintf/xasprintf.h"
#ifndef SKEL_DIR

View File

@@ -52,7 +52,7 @@
#include "subordinateio.h"
#endif /* ENABLE_SUBIDS */
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/sprintf/xasprintf.h"
/*

View File

@@ -61,7 +61,7 @@
#include "tcbfuncs.h"
#endif
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/sprintf/xasprintf.h"
#include "time/day_to_str.h"

View File

@@ -44,7 +44,8 @@
#include "tcbfuncs.h"
#endif /* WITH_TCB */
#include "shadowlog.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
#include "string/sprintf/xasprintf.h"
#define MSG_WARN_EDIT_OTHER_FILE _( \

View File

@@ -7,7 +7,7 @@ check_PROGRAMS = \
test_adds \
test_atoi_strtoi \
test_chkname \
test_sprintf \
test_snprintf \
test_strncpy \
test_strtcpy \
test_typetraits \
@@ -78,16 +78,16 @@ test_logind_LDADD = \
$(LIBSYSTEMD) \
$(NULL)
test_sprintf_SOURCES = \
../../lib/string/sprintf.c \
test_sprintf.c \
test_snprintf_SOURCES = \
../../lib/string/sprintf/snprintf.c \
test_snprintf.c \
$(NULL)
test_sprintf_CFLAGS = \
test_snprintf_CFLAGS = \
$(AM_CFLAGS) \
$(NULL)
test_sprintf_LDFLAGS = \
test_snprintf_LDFLAGS = \
$(NULL)
test_sprintf_LDADD = \
test_snprintf_LDADD = \
$(CMOCKA_LIBS) \
$(NULL)
@@ -129,7 +129,7 @@ test_typetraits_LDADD = \
$(NULL)
test_xasprintf_SOURCES = \
../../lib/string/sprintf.c \
../../lib/string/sprintf/xasprintf.c \
test_xasprintf.c \
$(NULL)
test_xasprintf_CFLAGS = \

View File

@@ -14,7 +14,7 @@
#include <cmocka.h>
#include "sizeof.h"
#include "string/sprintf.h"
#include "string/sprintf/snprintf.h"
static void test_SNPRINTF_trunc(void **state);

View File

@@ -16,7 +16,7 @@
#include <stdint.h> // Required by <cmocka.h>
#include <cmocka.h>
#include "string/sprintf.h"
#include "string/sprintf/xasprintf.h"
#define assert_unreachable() assert_true(0)