summaryrefslogtreecommitdiff
path: root/shlibs/mount/src/optstr.c
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2010-09-14 10:42:44 +0200
committerKarel Zak <kzak@redhat.com>2011-01-03 12:28:42 +0100
commite87c03f47ee2f0dac56cd03b08fe601ee97a2466 (patch)
treee09bb674c07c40ccd965b3edcd2a0fcba6a2b236 /shlibs/mount/src/optstr.c
parent496f189e776d726ef264afb793e114bb0a193898 (diff)
downloadutil-linux-old-e87c03f47ee2f0dac56cd03b08fe601ee97a2466.tar.gz
libmount: add mnt_optstr_prepend_option()
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'shlibs/mount/src/optstr.c')
-rw-r--r--shlibs/mount/src/optstr.c55
1 files changed, 53 insertions, 2 deletions
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)
{
@@ -210,6 +211,35 @@ int mnt_optstr_append_option(char **optstr, const char *name, const char *value)
}
/**
+ * 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
* @name: requested option name
@@ -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, "<optstr> <name> [<value>] append value to optstr" },
+ { "--prepend",test_prepend,"<optstr> <name> [<value>] prepend value to optstr" },
{ "--set", test_set, "<optstr> <name> [<value>] (un)set value" },
{ "--get", test_get, "<optstr> <name> search name in optstr" },
{ "--remove", test_remove, "<optstr> <name> remove name in optstr" },