Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8e99b6d4 authored by Arnaldo Carvalho de Melo's avatar Arnaldo Carvalho de Melo
Browse files

tools include: Adopt strstarts() from the kernel

Replacing prefixcmp(), same purpose, inverted result, so standardize on
the kernel variant, to reduce silly differences among tools/ and the
kernel sources, making it easier for people to work in both codebases.

And then doing:

	if (strstarts(option, "no-"))

Looks clearer than doing:

	if (!prefixcmp(option, "no-"))

To figure out if option starts witn "no-".

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Rusty Russell <rusty@rustcorp.com.au>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-kaei42gi7lpa8subwtv7eug8@git.kernel.org


Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 082ab9a1
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
#ifndef _TOOLS_LINUX_STRING_H_
#define _TOOLS_LINUX_STRING_H_


#include <linux/types.h>	/* for size_t */
#include <string.h>

void *memdup(const void *src, size_t len);

@@ -18,6 +18,14 @@ extern size_t strlcpy(char *dest, const char *src, size_t size);

char *str_error_r(int errnum, char *buf, size_t buflen);

int prefixcmp(const char *str, const char *prefix);
/**
 * strstarts - does @str start with @prefix?
 * @str: string to examine
 * @prefix: prefix to look for.
 */
static inline bool strstarts(const char *str, const char *prefix)
{
	return strncmp(str, prefix, strlen(prefix)) == 0;
}

#endif /* _LINUX_STRING_H_ */
+0 −9
Original line number Diff line number Diff line
@@ -87,12 +87,3 @@ size_t __weak strlcpy(char *dest, const char *src, size_t size)
	}
	return ret;
}

int prefixcmp(const char *str, const char *prefix)
{
	for (; ; str++, prefix++)
		if (!*prefix)
			return 0;
		else if (*str != *prefix)
			return (unsigned char)*prefix - (unsigned char)*str;
}
+1 −1
Original line number Diff line number Diff line
@@ -171,7 +171,7 @@ static void list_commands_in_dir(struct cmdnames *cmds,
	while ((de = readdir(dir)) != NULL) {
		int entlen;

		if (prefixcmp(de->d_name, prefix))
		if (!strstarts(de->d_name, prefix))
			continue;

		astrcat(&buf, de->d_name);
+9 −9
Original line number Diff line number Diff line
@@ -368,7 +368,7 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
			return 0;
		}
		if (!rest) {
			if (!prefixcmp(options->long_name, "no-")) {
			if (strstarts(options->long_name, "no-")) {
				/*
				 * The long name itself starts with "no-", so
				 * accept the option without "no-" so that users
@@ -381,7 +381,7 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
					goto match;
				}
				/* Abbreviated case */
				if (!prefixcmp(options->long_name + 3, arg)) {
				if (strstarts(options->long_name + 3, arg)) {
					flags |= OPT_UNSET;
					goto is_abbreviated;
				}
@@ -406,7 +406,7 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
				continue;
			}
			/* negated and abbreviated very much? */
			if (!prefixcmp("no-", arg)) {
			if (strstarts("no-", arg)) {
				flags |= OPT_UNSET;
				goto is_abbreviated;
			}
@@ -416,7 +416,7 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
			flags |= OPT_UNSET;
			rest = skip_prefix(arg + 3, options->long_name);
			/* abbreviated and negated? */
			if (!rest && !prefixcmp(options->long_name, arg + 3))
			if (!rest && strstarts(options->long_name, arg + 3))
				goto is_abbreviated;
			if (!rest)
				continue;
@@ -456,7 +456,7 @@ static void check_typos(const char *arg, const struct option *options)
	if (strlen(arg) < 3)
		return;

	if (!prefixcmp(arg, "no-")) {
	if (strstarts(arg, "no-")) {
		fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)", arg);
		exit(129);
	}
@@ -464,7 +464,7 @@ static void check_typos(const char *arg, const struct option *options)
	for (; options->type != OPTION_END; options++) {
		if (!options->long_name)
			continue;
		if (!prefixcmp(options->long_name, arg)) {
		if (strstarts(options->long_name, arg)) {
			fprintf(stderr, " Error: did you mean `--%s` (with two dashes ?)", arg);
			exit(129);
		}
@@ -933,10 +933,10 @@ int parse_options_usage(const char * const *usagestr,
		if (opts->long_name == NULL)
			continue;

		if (!prefixcmp(opts->long_name, optstr))
		if (strstarts(opts->long_name, optstr))
			print_option_help(opts, 0);
		if (!prefixcmp("no-", optstr) &&
		    !prefixcmp(opts->long_name, optstr + 3))
		if (strstarts("no-", optstr) &&
		    strstarts(opts->long_name, optstr + 3))
			print_option_help(opts, 0);
	}

+2 −1
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@
#include "util/util.h"
#include "util/debug.h"
#include "util/config.h"
#include <linux/string.h>

static bool use_system_config, use_user_config;

@@ -79,7 +80,7 @@ static int show_spec_config(struct perf_config_set *set, const char *var)
		return -1;

	perf_config_items__for_each_entry(&set->sections, section) {
		if (prefixcmp(var, section->name) != 0)
		if (!strstarts(var, section->name))
			continue;

		perf_config_items__for_each_entry(&section->items, item) {
Loading