]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/strutils: add ul_optstr_get_value()
authorKarel Zak <kzak@redhat.com>
Tue, 15 Jul 2025 13:36:22 +0000 (15:36 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 15 Jul 2025 13:36:22 +0000 (15:36 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/strutils.h
lib/strutils.c

index 2ee7c3e531e32eb7b6c7dedddc09bc7e11dbbeb4..add042d8f2c32108c690c204167350cec3036895 100644 (file)
@@ -476,5 +476,6 @@ extern int ul_stralnumcmp(const char *p1, const char *p2);
 
 extern int ul_optstr_next(char **optstr, char **name, size_t *namesz, char **value, size_t *valsz);
 extern int ul_optstr_is_valid(const char *optstr);
+extern char *ul_optstr_get_value(const char *optstr, const char *key);
 
 #endif
index c27d645c21b8200914e5f22a105fa876af97aa0c..1ecc0c398191bbdc759f68cac0827b4187fc301f 100644 (file)
@@ -1290,6 +1290,25 @@ int ul_optstr_is_valid(const char *optstr)
        return rc < 0 ? 0 : 1;
 }
 
+char *ul_optstr_get_value(const char *optstr, const char *key)
+{
+       size_t sz, namesz = 0, valsz = 0;
+       char *name = NULL, *value = NULL;
+       char *p = (char *) optstr;
+
+       if (!optstr || !key || !*key)
+               return NULL;
+
+       sz = strlen(key);
+       while (ul_optstr_next(&p, &name, &namesz, &value, &valsz) == 0) {
+               if (namesz != sz || !valsz)
+                       continue;
+               if (strncmp(name, key, namesz) == 0)
+                       return strndup(value, valsz);
+       }
+       return NULL;
+}
+
 #ifdef TEST_PROGRAM_STRUTILS
 
 #include "cctype.h"