From 81d0ebc3e8c94495d91d6be9fe19d7e6f85a64ba Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Tue, 28 Jul 2020 17:57:51 -0400 Subject: [PATCH] remove unnecessary strcmp wrapper, use typecasting instead --- Makefile | 1 - libifupdown/compar.c | 26 -------------------------- libifupdown/compar.h | 21 --------------------- libifupdown/dict.c | 2 +- libifupdown/dict.h | 4 +++- libifupdown/interface-file.c | 4 ++-- 6 files changed, 6 insertions(+), 52 deletions(-) delete mode 100644 libifupdown/compar.c delete mode 100644 libifupdown/compar.h diff --git a/Makefile b/Makefile index d0eca05..ccd016e 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,6 @@ CPPFLAGS += -DEXECUTOR_PATH=\"${EXECUTOR_PATH}\" LIBIFUPDOWN_SRC = \ - libifupdown/compar.c \ libifupdown/list.c \ libifupdown/dict.c \ libifupdown/interface.c \ diff --git a/libifupdown/compar.c b/libifupdown/compar.c deleted file mode 100644 index 8705404..0000000 --- a/libifupdown/compar.c +++ /dev/null @@ -1,26 +0,0 @@ -/* - * libifupdown/compar.c - * Purpose: comparators - * - * Copyright (c) 2020 Maximilian Wilhelm - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * This software is provided 'as is' and without any warranty, express or - * implied. In no event shall the authors be liable for any damages arising - * from the use of this software. - */ - -#include -#include "libifupdown/compar.h" - -int -compar_str (const void *a, const void *b) -{ - const char *str_a = (const char *)a; - const char *str_b = (const char *)b; - - return strcmp (str_a, str_b); -} diff --git a/libifupdown/compar.h b/libifupdown/compar.h deleted file mode 100644 index 8652a26..0000000 --- a/libifupdown/compar.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * libifupdown/compar.h - * Purpose: Comparators - * - * Copyright (c) 2020 Maximilian Wilhelm - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * This software is provided 'as is' and without any warranty, express or - * implied. In no event shall the authors be liable for any damages arising - * from the use of this software. - */ - -#ifndef LIBIFUPDOWN_COMPAR_H__GUARD -#define LIBIFUPDOWN_COMPAR_H__GUARD - -int compar_str (const void *a, const void *b); - -#endif diff --git a/libifupdown/dict.c b/libifupdown/dict.c index 3f5b1e1..4906f26 100644 --- a/libifupdown/dict.c +++ b/libifupdown/dict.c @@ -53,7 +53,7 @@ lif_dict_add(struct lif_dict *dict, const char *key, void *data) struct lif_dict_entry * lif_dict_add_once(struct lif_dict *dict, const char *key, void *data, - int (*compar)(const void *, const void *)) + lif_dict_cmp_t compar) { struct lif_list *existing = lif_dict_find_all(dict, key); if (existing != NULL) diff --git a/libifupdown/dict.h b/libifupdown/dict.h index 2332c05..9a48ad2 100644 --- a/libifupdown/dict.h +++ b/libifupdown/dict.h @@ -35,10 +35,12 @@ struct lif_dict_entry { #define LIF_DICT_FOREACH_SAFE(iter, iter_next, dict) \ LIF_LIST_FOREACH_SAFE((iter), (iter_next), (dict)->list.head) +typedef int (*lif_dict_cmp_t)(const void *, const void *); + extern void lif_dict_init(struct lif_dict *dict); extern void lif_dict_fini(struct lif_dict *dict); extern struct lif_dict_entry *lif_dict_add(struct lif_dict *dict, const char *key, void *data); -extern struct lif_dict_entry *lif_dict_add_once(struct lif_dict *dict, const char *key, void *data, int (*compar)(const void *, const void *)); +extern struct lif_dict_entry *lif_dict_add_once(struct lif_dict *dict, const char *key, void *data, lif_dict_cmp_t compar); extern struct lif_dict_entry *lif_dict_find(struct lif_dict *dict, const char *key); extern struct lif_list *lif_dict_find_all(struct lif_dict *dict, const char *key); extern void lif_dict_delete(struct lif_dict *dict, const char *key); diff --git a/libifupdown/interface-file.c b/libifupdown/interface-file.c index 205c920..c451d47 100644 --- a/libifupdown/interface-file.c +++ b/libifupdown/interface-file.c @@ -19,7 +19,6 @@ #include "libifupdown/interface-file.h" #include "libifupdown/fgetline.h" #include "libifupdown/tokenize.h" -#include "libifupdown/compar.h" bool lif_interface_file_parse(struct lif_dict *collection, const char *filename) @@ -146,7 +145,8 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename) { /* Copy word1 to not mangle *token */ char *addon = strndup(token, word_end - token); - lif_dict_add_once(&cur_iface->vars, "use", addon, compar_str); + if (lif_dict_add_once(&cur_iface->vars, "use", addon, (lif_dict_cmp_t) strcmp) == NULL) + free(addon); /* pass requires as compatibility env vars to appropriate executors (bridge, bond) */ if (!strcmp(addon, "dhcp"))