]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: add a generic append_string() function
authorKarel Zak <kzak@redhat.com>
Tue, 2 Jul 2013 09:58:58 +0000 (11:58 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 2 Jul 2013 09:58:58 +0000 (11:58 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/src/mountP.h
libmount/src/utils.c

index 7b0848f922040dbe594c597a607fb6ba19336c70..c6b70e29f3b02323ea9eccf5da6ecb537a2da994 100644 (file)
@@ -137,6 +137,7 @@ extern int startswith(const char *s, const char *sx)
 extern char *stripoff_last_component(char *path);
 
 extern int mnt_valid_tagname(const char *tagname);
+extern int append_string(char **a, const char *b);
 
 extern int is_file_empty(const char *name);
 
index 6c5171eca13bfc10869f59503e184ecaffbed8c2..1655764648537dd6336672b3b6dfb24b5a2e6d2b 100644 (file)
@@ -54,6 +54,31 @@ int startswith(const char *s, const char *sx)
         return !strncmp(s, sx, off);
 }
 
+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 = b ? strlen(b) : 0;
+
+       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 does not accessible of empty
  */
@@ -1082,6 +1107,18 @@ int test_endswith(struct libmnt_test *ts, int argc, char *argv[])
        return 0;
 }
 
+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;
+}
+
 int test_mountpoint(struct libmnt_test *ts, int argc, char *argv[])
 {
        char *path = canonicalize_path(argv[1]),
@@ -1177,6 +1214,7 @@ 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>" },
        { "--fs-root",       test_fsroot,          "<path>" },
        { "--cd-parent",     test_chdir,           "<path>" },