From 8e99b6d4533cf3f49dcd813155a513a5b572baef Mon Sep 17 00:00:00 2001
From: Arnaldo Carvalho de Melo <acme@redhat.com>
Date: Thu, 20 Jul 2017 15:27:39 -0300
Subject: [PATCH] tools include: Adopt strstarts() from the kernel

Patch-mainline: v4.14-rc1
Git-commit: 8e99b6d4533cf3f49dcd813155a513a5b572baef

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: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Michal Suchanek <msuchanek@suse.de>
---
 tools/include/linux/string.h      | 12 ++++++++++--
 tools/lib/string.c                |  9 ---------
 tools/lib/subcmd/help.c           |  2 +-
 tools/lib/subcmd/parse-options.c  | 18 +++++++++---------
 tools/perf/builtin-config.c       |  3 ++-
 tools/perf/builtin-ftrace.c       |  2 +-
 tools/perf/builtin-help.c         |  6 +++---
 tools/perf/perf.c                 | 16 ++++++++--------
 tools/perf/ui/browser.c           |  3 ++-
 tools/perf/ui/browsers/annotate.c |  3 ++-
 tools/perf/ui/stdio/hist.c        |  3 ++-
 tools/perf/util/bpf-loader.c      |  2 +-
 tools/perf/util/callchain.c       |  2 +-
 tools/perf/util/config.c          | 13 +++++++------
 tools/perf/util/llvm-utils.c      |  2 +-
 15 files changed, 50 insertions(+), 46 deletions(-)

diff -ur tools/perf/builtin-config.c tools/perf/builtin-config.c
--- a/tools/perf/builtin-config.c	2019-04-29 10:45:33.000000000 +0200
+++ b/tools/perf/builtin-config.c	2019-04-30 16:52:11.140042397 +0200
@@ -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 @@
 		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) {
diff -ur tools/perf/builtin-ftrace.c tools/perf/builtin-ftrace.c
--- a/tools/perf/builtin-ftrace.c	2019-04-30 16:41:58.029225093 +0200
+++ b/tools/perf/builtin-ftrace.c	2019-04-30 16:52:11.140042397 +0200
@@ -291,7 +291,7 @@
 {
 	struct perf_ftrace *ftrace = cb;
 
-	if (prefixcmp(var, "ftrace."))
+	if (!strstarts(var, "ftrace."))
 		return 0;
 
 	if (strcmp(var, "ftrace.tracer"))
diff -ur tools/perf/builtin-help.c tools/perf/builtin-help.c
--- a/tools/perf/builtin-help.c	2019-04-29 10:45:33.000000000 +0200
+++ b/tools/perf/builtin-help.c	2019-04-30 16:52:11.140042397 +0200
@@ -90,7 +90,7 @@
 	 */
 	finish_command(&ec_process);
 
-	if (prefixcmp(buffer.buf, "emacsclient")) {
+	if (!strstarts(buffer.buf, "emacsclient")) {
 		fprintf(stderr, "Failed to parse emacsclient version.\n");
 		goto out;
 	}
@@ -283,7 +283,7 @@
 		add_man_viewer(value);
 		return 0;
 	}
-	if (!prefixcmp(var, "man."))
+	if (!strstarts(var, "man."))
 		return add_man_viewer_info(var, value);
 
 	return 0;
@@ -313,7 +313,7 @@
 
 	if (!perf_cmd)
 		return "perf";
-	else if (!prefixcmp(perf_cmd, "perf"))
+	else if (!strstarts(perf_cmd, "perf"))
 		return perf_cmd;
 
 	return asprintf(&s, "perf-%s", perf_cmd) < 0 ? NULL : s;
diff -ur tools/perf/perf.c tools/perf/perf.c
--- a/tools/perf/perf.c	2019-04-29 10:45:33.000000000 +0200
+++ b/tools/perf/perf.c	2019-04-30 16:52:11.144042467 +0200
@@ -89,7 +89,7 @@
 static int pager_command_config(const char *var, const char *value, void *data)
 {
 	struct pager_config *c = data;
-	if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd))
+	if (strstarts(var, "pager.") && !strcmp(var + 6, c->cmd))
 		c->val = perf_config_bool(var, value);
 	return 0;
 }
@@ -108,9 +108,9 @@
 static int browser_command_config(const char *var, const char *value, void *data)
 {
 	struct pager_config *c = data;
-	if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
+	if (strstarts(var, "tui.") && !strcmp(var + 4, c->cmd))
 		c->val = perf_config_bool(var, value);
-	if (!prefixcmp(var, "gtk.") && !strcmp(var + 4, c->cmd))
+	if (strstarts(var, "gtk.") && !strcmp(var + 4, c->cmd))
 		c->val = perf_config_bool(var, value) ? 2 : 0;
 	return 0;
 }
@@ -192,7 +192,7 @@
 		/*
 		 * Check remaining flags.
 		 */
-		if (!prefixcmp(cmd, CMD_EXEC_PATH)) {
+		if (strstarts(cmd, CMD_EXEC_PATH)) {
 			cmd += strlen(CMD_EXEC_PATH);
 			if (*cmd == '=')
 				set_argv_exec_path(cmd + 1);
@@ -229,7 +229,7 @@
 				*envchanged = 1;
 			(*argv)++;
 			(*argc)--;
-		} else if (!prefixcmp(cmd, CMD_DEBUGFS_DIR)) {
+		} else if (strstarts(cmd, CMD_DEBUGFS_DIR)) {
 			tracing_path_set(cmd + strlen(CMD_DEBUGFS_DIR));
 			fprintf(stderr, "dir: %s\n", tracing_path);
 			if (envchanged)
@@ -470,14 +470,14 @@
 	 * So we just directly call the internal command handler, and
 	 * die if that one cannot handle it.
 	 */
-	if (!prefixcmp(cmd, "perf-")) {
+	if (strstarts(cmd, "perf-")) {
 		cmd += 5;
 		argv[0] = cmd;
 		handle_internal_command(argc, argv);
 		fprintf(stderr, "cannot handle %s internally", cmd);
 		goto out;
 	}
-	if (!prefixcmp(cmd, "trace")) {
+	if (strstarts(cmd, "trace")) {
 #ifdef HAVE_LIBAUDIT_SUPPORT
 		setup_path();
 		argv[0] = "trace";
@@ -495,7 +495,7 @@
 	commit_pager_choice();
 
 	if (argc > 0) {
-		if (!prefixcmp(argv[0], "--"))
+		if (strstarts(argv[0], "--"))
 			argv[0] += 2;
 	} else {
 		/* The user didn't specify a command; give them help */
diff -ur tools/perf/ui/browser.c tools/perf/ui/browser.c
--- a/tools/perf/ui/browser.c	2019-04-29 10:45:33.000000000 +0200
+++ b/tools/perf/ui/browser.c	2019-04-30 16:52:11.144042467 +0200
@@ -8,6 +8,7 @@
 #include <linux/compiler.h>
 #include <linux/list.h>
 #include <linux/rbtree.h>
+#include <linux/string.h>
 #include <stdlib.h>
 #include <sys/ttydefaults.h>
 #include "browser.h"
@@ -563,7 +564,7 @@
 	int i;
 
 	/* same dir for all commands */
-	if (prefixcmp(var, "colors.") != 0)
+	if (!strstarts(var, "colors.") != 0)
 		return 0;
 
 	for (i = 0; ui_browser__colorsets[i].name != NULL; ++i) {
diff -ur tools/perf/ui/browsers/annotate.c tools/perf/ui/browsers/annotate.c
--- a/tools/perf/ui/browsers/annotate.c	2019-04-30 16:41:58.065225729 +0200
+++ b/tools/perf/ui/browsers/annotate.c	2019-04-30 16:52:11.144042467 +0200
@@ -12,6 +12,7 @@
 #include <inttypes.h>
 #include <pthread.h>
 #include <linux/kernel.h>
+#include <linux/string.h>
 #include <sys/ttydefaults.h>
 
 struct disasm_line_samples {
@@ -1170,7 +1171,7 @@
 	struct annotate_config *cfg;
 	const char *name;
 
-	if (prefixcmp(var, "annotate.") != 0)
+	if (!strstarts(var, "annotate."))
 		return 0;
 
 	name = var + 9;
diff -ur tools/perf/ui/stdio/hist.c tools/perf/ui/stdio/hist.c
--- a/tools/perf/ui/stdio/hist.c	2019-04-29 10:45:33.000000000 +0200
+++ b/tools/perf/ui/stdio/hist.c	2019-04-30 16:52:11.144042467 +0200
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <linux/string.h>
 
 #include "../../util/util.h"
 #include "../../util/hist.h"
@@ -295,7 +296,7 @@
 			 * displayed twice.
 			 */
 			if (!i++ && field_order == NULL &&
-			    sort_order && !prefixcmp(sort_order, "sym"))
+			    sort_order && strstarts(sort_order, "sym"))
 				continue;
 
 			if (!printed) {
diff -ur tools/perf/util/bpf-loader.c tools/perf/util/bpf-loader.c
--- a/tools/perf/util/bpf-loader.c	2019-04-29 10:45:33.000000000 +0200
+++ b/tools/perf/util/bpf-loader.c	2019-04-30 16:52:11.144042467 +0200
@@ -1246,7 +1246,7 @@
 	if (!obj || !term || !term->config)
 		return -EINVAL;
 
-	if (!prefixcmp(term->config, "map:")) {
+	if (strstarts(term->config, "map:")) {
 		key_scan_pos = sizeof("map:") - 1;
 		err = bpf__obj_config_map(obj, term, evlist, &key_scan_pos);
 		goto out;
diff -ur tools/perf/util/callchain.c tools/perf/util/callchain.c
--- a/tools/perf/util/callchain.c	2019-04-29 10:45:33.000000000 +0200
+++ b/tools/perf/util/callchain.c	2019-04-30 16:52:11.144042467 +0200
@@ -303,7 +303,7 @@
 {
 	char *endptr;
 
-	if (prefixcmp(var, "call-graph."))
+	if (!strstarts(var, "call-graph."))
 		return 0;
 	var += sizeof("call-graph.") - 1;
 
diff -ur tools/perf/util/config.c tools/perf/util/config.c
--- a/tools/perf/util/config.c	2019-04-29 10:45:33.000000000 +0200
+++ b/tools/perf/util/config.c	2019-04-30 16:52:11.144042467 +0200
@@ -19,6 +19,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <linux/string.h>
 
 #include "sane_ctype.h"
 
@@ -423,22 +424,22 @@
 int perf_default_config(const char *var, const char *value,
 			void *dummy __maybe_unused)
 {
-	if (!prefixcmp(var, "core."))
+	if (strstarts(var, "core."))
 		return perf_default_core_config(var, value);
 
-	if (!prefixcmp(var, "hist."))
+	if (strstarts(var, "hist."))
 		return perf_hist_config(var, value);
 
-	if (!prefixcmp(var, "ui."))
+	if (strstarts(var, "ui."))
 		return perf_ui_config(var, value);
 
-	if (!prefixcmp(var, "call-graph."))
+	if (strstarts(var, "call-graph."))
 		return perf_callchain_config(var, value);
 
-	if (!prefixcmp(var, "llvm."))
+	if (strstarts(var, "llvm."))
 		return perf_llvm_config(var, value);
 
-	if (!prefixcmp(var, "buildid."))
+	if (strstarts(var, "buildid."))
 		return perf_buildid_config(var, value);
 
 	/* Add other config variables here. */
diff -ur tools/perf/util/llvm-utils.c tools/perf/util/llvm-utils.c
--- a/tools/perf/util/llvm-utils.c	2019-04-29 10:45:33.000000000 +0200
+++ b/tools/perf/util/llvm-utils.c	2019-04-30 16:52:11.144042467 +0200
@@ -33,7 +33,7 @@
 
 int perf_llvm_config(const char *var, const char *value)
 {
-	if (prefixcmp(var, "llvm."))
+	if (!strstarts(var, "llvm."))
 		return 0;
 	var += sizeof("llvm.") - 1;
 
