]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/strutils: add ul_next_string()
authorKarel Zak <kzak@redhat.com>
Wed, 11 Oct 2023 10:37:40 +0000 (12:37 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 23 Oct 2023 19:54:00 +0000 (21:54 +0200)
The function jumps to the next string in buffer where strings are
separated by \0.

Signed-off-by: Karel Zak <kzak@redhat.com>
include/strutils.h
lib/strutils.c

index 7ae8511ab838991b61509c52087868ddb43babe8..7b44b4e00bf34876973fe39afc246d15d2053ffc 100644 (file)
@@ -388,6 +388,23 @@ static inline void strrem(char *s, int rem)
        *p = '\0';
 }
 
+/* returns next string after \0 if before @end */
+static inline char *ul_next_string(char *p, char *end)
+{
+       char *last;
+
+       if (!p || !end || p >= end)
+               return NULL;
+
+       for (last = p; p < end; p++) {
+               if (*last == '\0' && p != last)
+                       return p;
+               last = p;
+       }
+
+       return NULL;
+}
+
 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, ...)
index 2bfc8ac6cfcb6667ed60748a153bd1bf30b9aa80..ccf71b987f33ae840a06e10d56a0c2d457bdaecc 100644 (file)
@@ -1414,6 +1414,15 @@ int main(int argc, char *argv[])
                printf("\"%s\" --> \"%s\"\n", argv[2], ul_strchr_escaped(argv[2], *argv[3]));
                return EXIT_SUCCESS;
 
+       } else if (argc == 2 && strcmp(argv[1], "--next-string") == 0) {
+               char *buf = "abc\0Y\0\0xyz\0X";
+               char *end = buf + 12;
+               char *p = buf;
+
+               do {
+                       printf("str: '%s'\n", p);
+               } while ((p = ul_next_string(p, end)));
+
        } else {
                fprintf(stderr, "usage: %1$s --size <number>[suffix]\n"
                                "       %1$s --cmp-paths <path> <path>\n"