lib/strutils: add strappend()

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2021-08-05 15:42:15 +02:00
parent 8420463b6d
commit 2e03758dc5
6 changed files with 31 additions and 42 deletions

View file

@ -382,6 +382,9 @@ extern char *strnconcat(const char *s, const char *suffix, size_t b);
extern char *strconcat(const char *s, const char *suffix);
extern char *strfconcat(const char *s, const char *format, ...)
__attribute__ ((__format__ (__printf__, 2, 3)));
extern int strappend(char **a, const char *b);
extern const char *split(const char **state, size_t *l, const char *separator, int quoted);
extern int skip_fline(FILE *fp);

View file

@ -992,6 +992,31 @@ char *strfconcat(const char *s, const char *format, ...)
return res;
}
int strappend(char **a, const char *b)
{
size_t al, bl;
char *tmp;
if (!a)
return -EINVAL;
if (!b || !*b)
return 0;
if (!*a) {
*a = strdup(b);
return !*a ? -ENOMEM : 0;
}
al = strlen(*a);
bl = strlen(b);
tmp = realloc(*a, al + bl + 1);
if (!tmp)
return -ENOMEM;
*a = tmp;
memcpy((*a) + al, b, bl + 1);
return 0;
}
static size_t strcspn_escaped(const char *s, const char *reject)
{
int escaped = 0;

View file

@ -1369,7 +1369,7 @@ int mnt_fs_append_comment(struct libmnt_fs *fs, const char *comm)
if (!fs)
return -EINVAL;
return append_string(&fs->comment, comm);
return strappend(&fs->comment, comm);
}
/**

View file

@ -95,7 +95,6 @@ extern int mnt_run_test(struct libmnt_test *tests, int argc, char *argv[]);
/* utils.c */
extern int mnt_valid_tagname(const char *tagname);
extern int append_string(char **a, const char *b);
extern const char *mnt_statfs_get_fstype(struct statfs *vfs);
extern int is_file_empty(const char *name);

View file

@ -309,7 +309,7 @@ int mnt_table_append_intro_comment(struct libmnt_table *tb, const char *comm)
{
if (!tb)
return -EINVAL;
return append_string(&tb->comm_intro, comm);
return strappend(&tb->comm_intro, comm);
}
/**
@ -350,7 +350,7 @@ int mnt_table_append_trailing_comment(struct libmnt_table *tb, const char *comm)
{
if (!tb)
return -EINVAL;
return append_string(&tb->comm_tail, comm);
return strappend(&tb->comm_tail, comm);
}
/**

View file

@ -32,31 +32,6 @@
#include "statfs_magic.h"
#include "sysfs.h"
int append_string(char **a, const char *b)
{
size_t al, bl;
char *tmp;
assert(a);
if (!b || !*b)
return 0;
if (!*a) {
*a = strdup(b);
return !*a ? -ENOMEM : 0;
}
al = strlen(*a);
bl = strlen(b);
tmp = realloc(*a, al + bl + 1);
if (!tmp)
return -ENOMEM;
*a = tmp;
memcpy((*a) + al, b, bl + 1);
return 0;
}
/*
* Return 1 if the file is not accessible or empty
*/
@ -1205,18 +1180,6 @@ static int test_endswith(struct libmnt_test *ts, int argc, char *argv[])
return 0;
}
static int test_appendstr(struct libmnt_test *ts, int argc, char *argv[])
{
char *str = strdup(argv[1]);
const char *ap = argv[2];
append_string(&str, ap);
printf("new string: '%s'\n", str);
free(str);
return 0;
}
static int test_mountpoint(struct libmnt_test *ts, int argc, char *argv[])
{
char *path = canonicalize_path(argv[1]),
@ -1343,7 +1306,6 @@ int main(int argc, char *argv[])
{ "--filesystems", test_filesystems, "[<pattern>] list /{etc,proc}/filesystems" },
{ "--starts-with", test_startswith, "<string> <prefix>" },
{ "--ends-with", test_endswith, "<string> <prefix>" },
{ "--append-string", test_appendstr, "<string> <appendix>" },
{ "--mountpoint", test_mountpoint, "<path>" },
{ "--cd-parent", test_chdir, "<path>" },
{ "--kernel-cmdline",test_kernel_cmdline, "<option> | <option>=" },