diff options
Diffstat (limited to 'src/libnm-systemd-shared/src/basic/string-util.c')
-rw-r--r-- | src/libnm-systemd-shared/src/basic/string-util.c | 57 |
1 files changed, 44 insertions, 13 deletions
diff --git a/src/libnm-systemd-shared/src/basic/string-util.c b/src/libnm-systemd-shared/src/basic/string-util.c index 8bf548d81b..1afa49bba0 100644 --- a/src/libnm-systemd-shared/src/basic/string-util.c +++ b/src/libnm-systemd-shared/src/basic/string-util.c @@ -11,11 +11,13 @@ #include "alloc-util.h" #include "escape.h" #include "extract-word.h" +#include "fd-util.h" #include "fileio.h" #include "gunicode.h" #include "locale-util.h" #include "macro.h" #include "memory-util.h" +#include "memstream-util.h" #include "string-util.h" #include "strv.h" #include "terminal-util.h" @@ -610,8 +612,8 @@ char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) { STATE_CSI, STATE_CSO, } state = STATE_OTHER; - char *obuf = NULL; - size_t osz = 0, isz, shift[2] = {}, n_carriage_returns = 0; + _cleanup_(memstream_done) MemStream m = {}; + size_t isz, shift[2] = {}, n_carriage_returns = 0; FILE *f; assert(ibuf); @@ -635,7 +637,7 @@ char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) { /* Note we turn off internal locking on f for performance reasons. It's safe to do so since we * created f here and it doesn't leave our scope. */ - f = open_memstream_unlocked(&obuf, &osz); + f = memstream_init(&m); if (!f) return NULL; @@ -720,17 +722,12 @@ char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) { } } - if (fflush_and_check(f) < 0) { - fclose(f); - return mfree(obuf); - } - fclose(f); + char *obuf; + if (memstream_finalize(&m, &obuf, _isz) < 0) + return NULL; free_and_replace(*ibuf, obuf); - if (_isz) - *_isz = osz; - if (highlight) { highlight[0] += shift[0]; highlight[1] += shift[1]; @@ -963,8 +960,7 @@ int free_and_strdup(char **p, const char *s) { } else t = NULL; - free(*p); - *p = t; + free_and_replace(*p, t); return 1; } @@ -1269,4 +1265,39 @@ char *strdupcspn(const char *a, const char *reject) { return strndup(a, strcspn(a, reject)); } + +char *find_line_startswith(const char *haystack, const char *needle) { + char *p; + + assert(haystack); + assert(needle); + + /* Finds the first line in 'haystack' that starts with the specified string. Returns a pointer to the + * first character after it */ + + p = strstr(haystack, needle); + if (!p) + return NULL; + + if (p > haystack) + while (p[-1] != '\n') { + p = strstr(p + 1, needle); + if (!p) + return NULL; + } + + return p + strlen(needle); +} #endif /* NM_IGNORED */ + +char *startswith_strv(const char *string, char **strv) { + char *found = NULL; + + STRV_FOREACH(i, strv) { + found = startswith(string, *i); + if (found) + break; + } + + return found; +} |