tools include: Adopt strstarts() from the kernel
authorArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 20 Jul 2017 18:27:39 +0000 (15:27 -0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 20 Jul 2017 18:46:10 +0000 (15:46 -0300)
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>
15 files changed:
tools/include/linux/string.h
tools/lib/string.c
tools/lib/subcmd/help.c
tools/lib/subcmd/parse-options.c
tools/perf/builtin-config.c
tools/perf/builtin-ftrace.c
tools/perf/builtin-help.c
tools/perf/perf.c
tools/perf/ui/browser.c
tools/perf/ui/browsers/annotate.c
tools/perf/ui/stdio/hist.c
tools/perf/util/bpf-loader.c
tools/perf/util/callchain.c
tools/perf/util/config.c
tools/perf/util/llvm-utils.c

index d62b56cf8c12eedcdda935bc1180c4b4b70270b9..a30fad536f52080a8d6351f258858a89fa61f927 100644 (file)
@@ -1,8 +1,8 @@
 #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_ */
index 8e678af1c6ee479eae333cf5766598db5dce5208..bd239bc1d557dbde447e8128ac7cd3163dc11a04 100644 (file)
@@ -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;
-}
index ba970a73d053f3c019a318dbb7fa14b9dbab9b40..0310520f918e450c06babea8042d13b5dd390d10 100644 (file)
@@ -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);
index 359bfa77f39cdebac7df82876b227d1a63975c39..2bd6fd0c1d40d40d56000a34c2b2dd99bed0ef1c 100644 (file)
@@ -368,7 +368,7 @@ retry:
                        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 @@ retry:
                                        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 @@ is_abbreviated:
                                continue;
                        }
                        /* negated and abbreviated very much? */
-                       if (!prefixcmp("no-", arg)) {
+                       if (strstarts("no-", arg)) {
                                flags |= OPT_UNSET;
                                goto is_abbreviated;
                        }
@@ -416,7 +416,7 @@ is_abbreviated:
                        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 @@ opt:
                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);
        }
 
index ece45582a48d04fcd64a05424d9128c12ba166a6..3ddcc6e2abebfd15a39095eef9be5ce61e4ca26c 100644 (file)
@@ -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) {
index dd26c62c9893dcaebdbaf4b3193bb0684016d5e5..25a42acabee18ff5782ba4f3f7962ef5b914a3d0 100644 (file)
@@ -381,7 +381,7 @@ static int perf_ftrace_config(const char *var, const char *value, void *cb)
 {
        struct perf_ftrace *ftrace = cb;
 
-       if (prefixcmp(var, "ftrace."))
+       if (!strstarts(var, "ftrace."))
                return 0;
 
        if (strcmp(var, "ftrace.tracer"))
index 530a7f2fa0f3e5326e9d7ea5886f97411046417f..dbe4e4153bcfb6dc6e4a348cfd0b9ded44188f99 100644 (file)
@@ -90,7 +90,7 @@ static int check_emacsclient_version(void)
         */
        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 @@ static int perf_help_config(const char *var, const char *value, void *cb)
                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 @@ static const char *cmd_to_page(const char *perf_cmd)
 
        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;
index 628a5e412cb14b00d9ead40bdcbb13db4c82bd75..e0279babe0c03936f44126d7c4703e2c70eda951 100644 (file)
@@ -89,7 +89,7 @@ struct pager_config {
 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 check_pager_config(const char *cmd)
 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 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
                /*
                 * 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 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
                                *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 @@ int main(int argc, const char **argv)
         * 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 @@ int main(int argc, const char **argv)
        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 */
index f73f3f13e01de3d6ebd3df661e0cac704a2346b0..d0c2007c307bbaf266a8a84d7f054c1d41994c72 100644 (file)
@@ -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 @@ static int ui_browser__color_config(const char *var, const char *value,
        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) {
index 8d3f6f53c122d7fff9a62573f7adfdcb3e06c9a5..6794a8bec40484573ae1d87323fc4cd44d3925ad 100644 (file)
@@ -13,6 +13,7 @@
 #include <inttypes.h>
 #include <pthread.h>
 #include <linux/kernel.h>
+#include <linux/string.h>
 #include <sys/ttydefaults.h>
 
 struct disasm_line_samples {
@@ -1198,7 +1199,7 @@ static int annotate__config(const char *var, const char *value,
        struct annotate_config *cfg;
        const char *name;
 
-       if (prefixcmp(var, "annotate.") != 0)
+       if (!strstarts(var, "annotate."))
                return 0;
 
        name = var + 9;
index 2df8eb1ed3c01c55dc84f2bc19285507223e7764..5c95b8301c670f96f72e1dcbaf4b7ef1142328d9 100644 (file)
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <linux/string.h>
 
 #include "../../util/util.h"
 #include "../../util/hist.h"
@@ -292,7 +293,7 @@ static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
                         * displayed twice.
                         */
                        if (!i++ && field_order == NULL &&
-                           sort_order && !prefixcmp(sort_order, "sym"))
+                           sort_order && strstarts(sort_order, "sym"))
                                continue;
 
                        if (!printed) {
index 4bd2d1d882af1d6e9da7561e80d572a82258b5cd..4a1264c66101bda56833f0151af25e2a41fd9de6 100644 (file)
@@ -1246,7 +1246,7 @@ int bpf__config_obj(struct bpf_object *obj,
        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;
index 22d413ae60256d68adc25bbc886f53e44235f997..02130e2e72c760aa2277f7c5d4eeebd46f88e65a 100644 (file)
@@ -304,7 +304,7 @@ int perf_callchain_config(const char *var, const char *value)
 {
        char *endptr;
 
-       if (prefixcmp(var, "call-graph."))
+       if (!strstarts(var, "call-graph."))
                return 0;
        var += sizeof("call-graph.") - 1;
 
index 31a7dea248d03323ce238281dd54d5554eb62520..bc75596f9e79ed1b5d0b747df40e933e52deb4a0 100644 (file)
@@ -19,6 +19,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <linux/string.h>
 
 #include "sane_ctype.h"
 
@@ -433,22 +434,22 @@ static int perf_ui_config(const char *var, const char *value)
 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. */
index c6a15f204c03827bec7fa4cfdf879d8d9d5b89fc..209b0c82eff4660ad0612be724786fb73a0881e2 100644 (file)
@@ -33,7 +33,7 @@ struct llvm_param llvm_param = {
 
 int perf_llvm_config(const char *var, const char *value)
 {
-       if (prefixcmp(var, "llvm."))
+       if (!strstarts(var, "llvm."))
                return 0;
        var += sizeof("llvm.") - 1;