From bdd28aba63b6cebb92af221bcf41d2b5512fec43 Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Tue, 21 Nov 2023 22:19:29 +0900 Subject: [PATCH] xalloc.h: add new functions: xstrappend, xstrputc, xstrvfappend, and xstrfappend Signed-off-by: Masatake YAMATO --- include/xalloc.h | 38 ++++++++++++++++++++++++++++++++++++++ misc-utils/lsfd.h | 38 -------------------------------------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/include/xalloc.h b/include/xalloc.h index da9245fdba..f7d42c99f5 100644 --- a/include/xalloc.h +++ b/include/xalloc.h @@ -17,6 +17,7 @@ #include #include "c.h" +#include "strutils.h" #ifndef XALLOC_EXIT_CODE # define XALLOC_EXIT_CODE EXIT_FAILURE @@ -125,6 +126,43 @@ int xvasprintf(char **strp, const char *fmt, va_list ap) return ret; } +static inline void xstrappend(char **a, const char *b) +{ + if (strappend(a, b) < 0) + err(XALLOC_EXIT_CODE, "cannot allocate string"); +} + +static inline void xstrputc(char **a, char c) +{ + char b[] = {c, '\0'}; + xstrappend(a, b); +} + +static inline +__attribute__((__format__(printf, 2, 0))) +int xstrvfappend(char **a, const char *format, va_list ap) +{ + int ret = strvfappend(a, format, ap); + + if (ret < 0) + err(XALLOC_EXIT_CODE, "cannot allocate string"); + return ret; + +} + +static inline +__attribute__ ((__format__ (__printf__, 2, 3))) +int xstrfappend(char **a, const char *format, ...) +{ + va_list ap; + int ret; + + va_start(ap, format); + ret = xstrvfappend(a, format, ap); + va_end(ap); + + return ret; +} static inline __attribute__((warn_unused_result)) diff --git a/misc-utils/lsfd.h b/misc-utils/lsfd.h index a819e65096..1859dc746f 100644 --- a/misc-utils/lsfd.h +++ b/misc-utils/lsfd.h @@ -275,44 +275,6 @@ const char *get_miscdev(unsigned long minor); const char *get_nodev_filesystem(unsigned long minor); void add_nodev(unsigned long minor, const char *filesystem); -static inline void xstrappend(char **a, const char *b) -{ - if (strappend(a, b) < 0) - err(XALLOC_EXIT_CODE, _("failed to allocate memory for string")); -} - -static inline void xstrputc(char **a, char c) -{ - char b[] = {c, '\0'}; - xstrappend(a, b); -} - -static inline -__attribute__((__format__(printf, 2, 0))) -int xstrvfappend(char **a, const char *format, va_list ap) -{ - int ret = strvfappend(a, format, ap); - - if (ret < 0) - err(XALLOC_EXIT_CODE, "cannot allocate string"); - return ret; - -} - -static inline -__attribute__ ((__format__ (__printf__, 2, 3))) -int xstrfappend(char **a, const char *format, ...) -{ - va_list ap; - int ret; - - va_start(ap, format); - ret = xstrvfappend(a, format, ap); - va_end(ap); - - return ret; -} - /* * Net namespace */ -- 2.47.2