From e87c03f47ee2f0dac56cd03b08fe601ee97a2466 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 14 Sep 2010 10:42:44 +0200 Subject: [PATCH] libmount: add mnt_optstr_prepend_option() Signed-off-by: Karel Zak --- shlibs/mount/src/mount.h.in | 2 ++ shlibs/mount/src/mount.sym | 1 + shlibs/mount/src/optstr.c | 55 +++++++++++++++++++++++++++++++++++-- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/shlibs/mount/src/mount.h.in b/shlibs/mount/src/mount.h.in index a4170b47..559f59a4 100644 --- a/shlibs/mount/src/mount.h.in +++ b/shlibs/mount/src/mount.h.in @@ -150,6 +150,8 @@ extern int mnt_optstr_next_option(char **optstr, char **name, size_t *namesz, char **value, size_t *valuesz); extern int mnt_optstr_append_option(char **optstr, const char *name, const char *value); +extern int mnt_optstr_prepend_option(char **optstr, const char *name, + const char *value); extern int mnt_optstr_get_option(char *optstr, const char *name, char **value, size_t *valsz); extern int mnt_optstr_set_option(char **optstr, const char *name, diff --git a/shlibs/mount/src/mount.sym b/shlibs/mount/src/mount.sym index 56e20ddd..af0cc88f 100644 --- a/shlibs/mount/src/mount.sym +++ b/shlibs/mount/src/mount.sym @@ -106,6 +106,7 @@ global: mnt_optls_remove_option_by_flags; mnt_optls_remove_option_by_iflags; mnt_optstr_append_option; + mnt_optstr_prepend_option; mnt_optstr_get_mountflags; mnt_optstr_get_option; mnt_optstr_next_option; diff --git a/shlibs/mount/src/optstr.c b/shlibs/mount/src/optstr.c index caa46eae..6d4d3505 100644 --- a/shlibs/mount/src/optstr.c +++ b/shlibs/mount/src/optstr.c @@ -190,11 +190,12 @@ static int __mnt_optstr_append_option(char **optstr, /** * mnt_optstr_append_option: - * @optstr: option string or NULL + * @optstr: option string or NULL, returns reallocated string * @name: value name * @value: value * - * Returns: reallocated (or newly allocated) @optstr with ,name=value + * Returns: 0 on success or -1 in case of error. After error the @optstr should + * be unmodified. */ int mnt_optstr_append_option(char **optstr, const char *name, const char *value) { @@ -209,6 +210,35 @@ int mnt_optstr_append_option(char **optstr, const char *name, const char *value) return __mnt_optstr_append_option(optstr, name, nsz, value, vsz); } +/** + * mnt_optstr_prepend_option: + * @optstr: option string or NULL, returns reallocated string + * @name: value name + * @value: value + * + * Returns: 0 on success or -1 in case of error. After error the @optstr should + * be unmodified. + */ +int mnt_optstr_prepend_option(char **optstr, const char *name, const char *value) +{ + int rc = 0; + char *tmp = *optstr; + + *optstr = NULL; + + rc = mnt_optstr_append_option(optstr, name, value); + if (!rc) + rc = mnt_optstr_append_option(optstr, tmp, NULL); + if (!rc) { + free(tmp); + return 0; + } + + free(*optstr); + *optstr = tmp; + return rc; +} + /** * mnt_optstr_get_option: * @optstr: string with comma separated list of options @@ -479,6 +509,26 @@ int test_append(struct mtest *ts, int argc, char *argv[]) return rc; } +int test_prepend(struct mtest *ts, int argc, char *argv[]) +{ + const char *value = NULL, *name; + char *optstr; + int rc; + + if (argc < 3) + return -EINVAL; + optstr = strdup(argv[1]); + name = argv[2]; + + if (argc == 4) + value = argv[3]; + + rc = mnt_optstr_prepend_option(&optstr, name, value); + if (!rc) + printf("result: >%s<\n", optstr); + return rc; +} + int test_split(struct mtest *ts, int argc, char *argv[]) { char *optstr, *user = NULL, *fs = NULL, *vfs = NULL; @@ -574,6 +624,7 @@ int main(int argc, char *argv[]) { struct mtest tss[] = { { "--append", test_append, " [] append value to optstr" }, + { "--prepend",test_prepend," [] prepend value to optstr" }, { "--set", test_set, " [] (un)set value" }, { "--get", test_get, " search name in optstr" }, { "--remove", test_remove, " remove name in optstr" }, -- 2.39.5