cutils: Provide strchrnul

strchrnul is a GNU extension and thus unavailable on a number of targets.
In the review for a commit removing strchrnul from 9p, I was asked to
create a qemu_strchrnul helper to factor out this functionality.
Do so, and use it in a number of other places in the code base that inlined
the replacement pattern in a place where strchrnul could be used.

Signed-off-by: Keno Fischer <keno@juliacomputing.com>
Acked-by: Greg Kurz <groug@kaod.org>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Greg Kurz <groug@kaod.org>
This commit is contained in:
Keno Fischer 2018-06-29 12:32:10 +02:00 committed by Greg Kurz
parent 609ef9f451
commit 5c99fa375d
8 changed files with 51 additions and 20 deletions

18
configure vendored
View File

@ -4793,6 +4793,21 @@ if compile_prog "" "" ; then
sem_timedwait=yes sem_timedwait=yes
fi fi
##########################################
# check if we have strchrnul
strchrnul=no
cat > $TMPC << EOF
#include <string.h>
int main(void);
// Use a haystack that the compiler shouldn't be able to constant fold
char *haystack = (char*)&main;
int main(void) { return strchrnul(haystack, 'x') != &haystack[6]; }
EOF
if compile_prog "" "" ; then
strchrnul=yes
fi
########################################## ##########################################
# check if trace backend exists # check if trace backend exists
@ -6276,6 +6291,9 @@ fi
if test "$sem_timedwait" = "yes" ; then if test "$sem_timedwait" = "yes" ; then
echo "CONFIG_SEM_TIMEDWAIT=y" >> $config_host_mak echo "CONFIG_SEM_TIMEDWAIT=y" >> $config_host_mak
fi fi
if test "$strchrnul" = "yes" ; then
echo "HAVE_STRCHRNUL=y" >> $config_host_mak
fi
if test "$byteswap_h" = "yes" ; then if test "$byteswap_h" = "yes" ; then
echo "CONFIG_BYTESWAP_H=y" >> $config_host_mak echo "CONFIG_BYTESWAP_H=y" >> $config_host_mak
fi fi

8
hmp.c
View File

@ -2140,12 +2140,12 @@ void hmp_sendkey(Monitor *mon, const QDict *qdict)
int has_hold_time = qdict_haskey(qdict, "hold-time"); int has_hold_time = qdict_haskey(qdict, "hold-time");
int hold_time = qdict_get_try_int(qdict, "hold-time", -1); int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
Error *err = NULL; Error *err = NULL;
char *separator; const char *separator;
int keyname_len; int keyname_len;
while (1) { while (1) {
separator = strchr(keys, '-'); separator = qemu_strchrnul(keys, '-');
keyname_len = separator ? separator - keys : strlen(keys); keyname_len = separator - keys;
/* Be compatible with old interface, convert user inputted "<" */ /* Be compatible with old interface, convert user inputted "<" */
if (keys[0] == '<' && keyname_len == 1) { if (keys[0] == '<' && keyname_len == 1) {
@ -2182,7 +2182,7 @@ void hmp_sendkey(Monitor *mon, const QDict *qdict)
keylist->value->u.qcode.data = idx; keylist->value->u.qcode.data = idx;
} }
if (!separator) { if (!*separator) {
break; break;
} }
keys = separator + 1; keys = separator + 1;

View File

@ -65,7 +65,7 @@ int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
assert(*path != '/'); assert(*path != '/');
head = g_strdup(path); head = g_strdup(path);
c = strchrnul(path, '/'); c = qemu_strchrnul(path, '/');
if (*c) { if (*c) {
/* Intermediate path element */ /* Intermediate path element */
head[c - path] = 0; head[c - path] = 0;

View File

@ -122,6 +122,14 @@ int qemu_strnlen(const char *s, int max_len);
* Returns: the pointer originally in @input. * Returns: the pointer originally in @input.
*/ */
char *qemu_strsep(char **input, const char *delim); char *qemu_strsep(char **input, const char *delim);
#ifdef HAVE_STRCHRNUL
static inline const char *qemu_strchrnul(const char *s, int c)
{
return strchrnul(s, c);
}
#else
const char *qemu_strchrnul(const char *s, int c);
#endif
time_t mktimegm(struct tm *tm); time_t mktimegm(struct tm *tm);
int qemu_fdatasync(int fd); int qemu_fdatasync(int fd);
int fcntl_setfl(int fd, int flag); int fcntl_setfl(int fd, int flag);

View File

@ -820,9 +820,7 @@ static int compare_cmd(const char *name, const char *list)
p = list; p = list;
for(;;) { for(;;) {
pstart = p; pstart = p;
p = strchr(p, '|'); p = qemu_strchrnul(p, '|');
if (!p)
p = pstart + strlen(pstart);
if ((p - pstart) == len && !memcmp(pstart, name, len)) if ((p - pstart) == len && !memcmp(pstart, name, len))
return 1; return 1;
if (*p == '\0') if (*p == '\0')
@ -3489,9 +3487,7 @@ static void cmd_completion(Monitor *mon, const char *name, const char *list)
p = list; p = list;
for(;;) { for(;;) {
pstart = p; pstart = p;
p = strchr(p, '|'); p = qemu_strchrnul(p, '|');
if (!p)
p = pstart + strlen(pstart);
len = p - pstart; len = p - pstart;
if (len > sizeof(cmd) - 2) if (len > sizeof(cmd) - 2)
len = sizeof(cmd) - 2; len = sizeof(cmd) - 2;

View File

@ -544,6 +544,21 @@ int qemu_strtou64(const char *nptr, const char **endptr, int base,
return check_strtox_error(nptr, ep, endptr, errno); return check_strtox_error(nptr, ep, endptr, errno);
} }
/**
* Searches for the first occurrence of 'c' in 's', and returns a pointer
* to the trailing null byte if none was found.
*/
#ifndef HAVE_STRCHRNUL
const char *qemu_strchrnul(const char *s, int c)
{
const char *e = strchr(s, c);
if (!e) {
e = s + strlen(s);
}
return e;
}
#endif
/** /**
* parse_uint: * parse_uint:
* *

View File

@ -77,11 +77,7 @@ const char *get_opt_value(const char *p, char **value)
*value = NULL; *value = NULL;
while (1) { while (1) {
offset = strchr(p, ','); offset = qemu_strchrnul(p, ',');
if (!offset) {
offset = p + strlen(p);
}
length = offset - p; length = offset - p;
if (*offset != '\0' && *(offset + 1) == ',') { if (*offset != '\0' && *(offset + 1) == ',') {
length++; length++;

View File

@ -52,6 +52,7 @@
*/ */
#include "qemu/osdep.h" #include "qemu/osdep.h"
#include "qemu/cutils.h"
#include "qemu/uri.h" #include "qemu/uri.h"
@ -2266,10 +2267,7 @@ struct QueryParams *query_params_parse(const char *query)
/* Find the next separator, or end of the string. */ /* Find the next separator, or end of the string. */
end = strchr(query, '&'); end = strchr(query, '&');
if (!end) { if (!end) {
end = strchr(query, ';'); end = qemu_strchrnul(query, ';');
}
if (!end) {
end = query + strlen(query);
} }
/* Find the first '=' character between here and end. */ /* Find the first '=' character between here and end. */