tree-wide: replace string functions with fundamental functions

This commit is contained in:
Rose
2024-01-11 13:36:25 +09:00
committed by Yu Watanabe
parent 658169e6d3
commit aa9ff6c28d
7 changed files with 30 additions and 38 deletions
+2 -2
View File
@@ -244,9 +244,9 @@ static bool env_match(const char *t, const char *pattern) {
return true;
if (!strchr(pattern, '=')) {
size_t l = strlen(pattern);
t = startswith(t, pattern);
return strneq(t, pattern, l) && t[l] == '=';
return t && *t == '=';
}
return false;
+5 -4
View File
@@ -1024,7 +1024,7 @@ int getenv_for_pid(pid_t pid, const char *field, char **ret) {
_cleanup_fclose_ FILE *f = NULL;
char *value = NULL;
const char *path;
size_t l, sum = 0;
size_t sum = 0;
int r;
assert(pid >= 0);
@@ -1059,9 +1059,9 @@ int getenv_for_pid(pid_t pid, const char *field, char **ret) {
if (r < 0)
return r;
l = strlen(field);
for (;;) {
_cleanup_free_ char *line = NULL;
const char *match;
if (sum > ENVIRONMENT_BLOCK_MAX) /* Give up searching eventually */
return -ENOBUFS;
@@ -1074,8 +1074,9 @@ int getenv_for_pid(pid_t pid, const char *field, char **ret) {
sum += r;
if (strneq(line, field, l) && line[l] == '=') {
value = strdup(line + l + 1);
match = startswith(line, field);
if (match && *match == '=') {
value = strdup(match + 1);
if (!value)
return -ENOMEM;
+7 -11
View File
@@ -1509,24 +1509,20 @@ ssize_t strlevenshtein(const char *x, const char *y) {
}
char *strrstr(const char *haystack, const char *needle) {
const char *f = NULL;
size_t l;
/* Like strstr() but returns the last rather than the first occurrence of "needle" in "haystack". */
if (!haystack || !needle)
return NULL;
l = strlen(needle);
/* Special case: for the empty string we return the very last possible occurrence, i.e. *after* the
* last char, not before. */
if (l == 0)
if (*needle == 0)
return strchr(haystack, 0);
for (const char *p = haystack; *p; p++)
if (strneq(p, needle, l))
f = p;
return (char*) f;
for (const char *p = strstr(haystack, needle), *q; p; p = q) {
q = strstr(p + 1, needle);
if (!q)
return (char *) p;
}
return NULL;
}
+8 -11
View File
@@ -640,7 +640,7 @@ int bus_start_running(sd_bus *bus) {
static int parse_address_key(const char **p, const char *key, char **value) {
_cleanup_free_ char *r = NULL;
size_t l, n = 0;
size_t n = 0;
const char *a;
assert(p);
@@ -648,17 +648,14 @@ static int parse_address_key(const char **p, const char *key, char **value) {
assert(value);
if (key) {
l = strlen(key);
if (strncmp(*p, key, l) != 0)
return 0;
if ((*p)[l] != '=')
a = startswith(*p, key);
if (!a || *a != '=')
return 0;
if (*value)
return -EINVAL;
a = *p + l + 1;
a++;
} else
a = *p;
@@ -4121,13 +4118,13 @@ _public_ int sd_bus_path_decode_many(const char *path, const char *path_template
for (template_pos = path_template; *template_pos; ) {
const char *sep;
size_t length;
size_t length, path_length;
char *label;
/* verify everything until the next '%' matches verbatim */
sep = strchrnul(template_pos, '%');
length = sep - template_pos;
if (strncmp(path_pos, template_pos, length))
if (!strneq(path_pos, template_pos, length))
return 0;
path_pos += length;
@@ -4148,8 +4145,8 @@ _public_ int sd_bus_path_decode_many(const char *path, const char *path_template
/* verify the suffixes match */
sep = strchrnul(path_pos, '/');
if (sep - path_pos < (ssize_t)length ||
strncmp(sep - length, template_pos, length))
path_length = sep - path_pos;
if (length > path_length || !strneq(sep - length, template_pos, length))
return 0;
template_pos += length; /* skip over matched label */
+2 -2
View File
@@ -453,13 +453,13 @@ int efi_get_boot_options(uint16_t **ret_options) {
FOREACH_DIRENT(de, dir, return -errno) {
int id;
if (strncmp(de->d_name, "Boot", 4) != 0)
if (!startswith(de->d_name, "Boot"))
continue;
if (strlen(de->d_name) != 45)
continue;
if (strcmp(de->d_name + 8, EFI_GLOBAL_VARIABLE_STR("")) != 0) /* generate variable suffix using macro */
if (!streq(de->d_name + 8, EFI_GLOBAL_VARIABLE_STR(""))) /* generate variable suffix using macro */
continue;
id = boot_id_hex(de->d_name + 4);
+5 -7
View File
@@ -232,23 +232,21 @@ int fstab_filter_options(
}
NULSTR_FOREACH(name, names) {
if (end < word + strlen(name))
continue;
if (!strneq(word, name, strlen(name)))
x = startswith(word, name);
if (!x || x > end)
continue;
/* We know that the string is NUL terminated, so *x is valid */
x = word + strlen(name);
if (IN_SET(*x, '\0', '=', ',')) {
namefound = name;
break;
}
}
if (*end)
word = end + 1;
else
if (*end == '\0')
break;
word = end + 1;
}
answer:
+1 -1
View File
@@ -186,7 +186,7 @@ static sd_device *handle_scsi_sas(sd_device *parent, char **path) {
return NULL;
/* Check if we are simple disk */
if (strncmp(phy_count, "1", 2) != 0)
if (!streq(phy_count, "1"))
return handle_scsi_sas_wide_port(parent, path);
/* Get connected phy */