if (mid != mount_id)
continue;
- e = strstr(line, " - ");
+ e = strstrafter(line, " - ");
if (!e)
continue;
/* accept any name that starts with the currently expected type */
- if (startswith(e + 3, "devtmpfs"))
+ if (startswith(e, "devtmpfs"))
return true;
}
return strstr(haystack, needle);
}
+static inline char *strstrafter(const char *haystack, const char *needle) {
+ char *p;
+
+ /* Returns NULL if not found, or pointer to first character after needle if found */
+
+ p = strstr_ptr(haystack, needle);
+ if (!p)
+ return NULL;
+
+ return p + strlen(needle);
+}
+
static inline const char* strnull(const char *s) {
return s ?: "(null)";
}
* kernel makes this guarantee when creating those devices, and hence we should too when
* enumerating them. */
- sound_a = strstr(devpath_a, "/sound/card");
+ sound_a = strstrafter(devpath_a, "/sound/card");
if (!sound_a)
return 0;
- sound_a += STRLEN("/sound/card");
sound_a = strchr(devpath_a, '/');
if (!sound_a)
return 0;
s = is_soft ? strndupa_safe(property, is_soft - property) : property;
/* Skip over any prefix, such as "Default" */
- assert_se(p = strstr(s, "Limit"));
+ assert_se(p = strstrafter(s, "Limit"));
- z = rlimit_from_string(p + 5);
+ z = rlimit_from_string(p);
assert(z >= 0);
(void) getrlimit(z, &buf);
assert(e->context->marker_end);
- contents_start = strstr(old_contents, e->context->marker_start);
- if (contents_start)
- contents_start += strlen(e->context->marker_start);
- else
+ contents_start = strstrafter(old_contents, e->context->marker_start);
+ if (!contents_start)
contents_start = old_contents;
contents_end = strstr(contents_start, e->context->marker_end);
if (startswith(b, "!--")) {
/* A comment */
- e = strstr(b + 3, "-->");
+ e = strstrafter(b + 3, "-->");
if (!e)
return -EINVAL;
- inc_lines(line, b, e + 3 - b);
+ inc_lines(line, b, e - b);
- c = e + 3;
+ c = e;
continue;
}
if (*b == '?') {
/* Processing instruction */
- e = strstr(b + 1, "?>");
+ e = strstrafter(b + 1, "?>");
if (!e)
return -EINVAL;
- inc_lines(line, b, e + 2 - b);
+ inc_lines(line, b, e - b);
- c = e + 2;
+ c = e;
continue;
}
assert_se(!find_line_startswith(emptystring, "x"));
}
+TEST(strstrafter) {
+ static const char buffer[] = "abcdefghijklmnopqrstuvwxyz";
+
+ assert_se(!strstrafter(NULL, NULL));
+ assert_se(!strstrafter("", NULL));
+ assert_se(!strstrafter(NULL, ""));
+ assert_se(streq_ptr(strstrafter("", ""), ""));
+
+ assert_se(strstrafter(buffer, "a") == buffer + 1);
+ assert_se(strstrafter(buffer, "") == buffer);
+ assert_se(strstrafter(buffer, "ab") == buffer + 2);
+ assert_se(strstrafter(buffer, "cde") == buffer + 5);
+ assert_se(strstrafter(buffer, "xyz") == strchr(buffer, 0));
+ assert_se(strstrafter(buffer, buffer) == strchr(buffer, 0));
+ assert_se(!strstrafter(buffer, "-"));
+}
+
DEFINE_TEST_MAIN(LOG_DEBUG);