comapre: add generic implementation for comapring with verscmp+fnmatch
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <fnmatch.h>
|
||||
|
||||
#include "compare-operator.h"
|
||||
#include "string-util.h"
|
||||
|
||||
@@ -63,3 +65,24 @@ int test_order(int k, CompareOperator op) {
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
int version_or_fnmatch_compare(
|
||||
CompareOperator op,
|
||||
const char *a,
|
||||
const char *b) {
|
||||
|
||||
switch (op) {
|
||||
|
||||
case COMPARE_FNMATCH_EQUAL:
|
||||
return fnmatch(b, a, FNM_EXTMATCH) != FNM_NOMATCH;
|
||||
|
||||
case COMPARE_FNMATCH_UNEQUAL:
|
||||
return fnmatch(b, a, FNM_EXTMATCH) == FNM_NOMATCH;
|
||||
|
||||
case _COMPARE_OPERATOR_ORDER_FIRST..._COMPARE_OPERATOR_ORDER_LAST:
|
||||
return test_order(strverscmp_improved(a, b), op);
|
||||
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,3 +39,5 @@ static inline bool COMPARE_OPERATOR_IS_ORDER(CompareOperator c) {
|
||||
CompareOperator parse_compare_operator(const char **s, bool allow_fnmatch);
|
||||
|
||||
int test_order(int k, CompareOperator op);
|
||||
|
||||
int version_or_fnmatch_compare(CompareOperator op, const char *a, const char *b);
|
||||
|
||||
@@ -562,11 +562,7 @@ static int condition_test_firmware_smbios_field(const char *expression) {
|
||||
delete_trailing_chars(actual_value, WHITESPACE);
|
||||
|
||||
/* Finally compare actual and expected value */
|
||||
if (operator == COMPARE_FNMATCH_EQUAL)
|
||||
return fnmatch(expected_value, actual_value, FNM_EXTMATCH) != FNM_NOMATCH;
|
||||
if (operator == COMPARE_FNMATCH_UNEQUAL)
|
||||
return fnmatch(expected_value, actual_value, FNM_EXTMATCH) == FNM_NOMATCH;
|
||||
return test_order(strverscmp_improved(actual_value, expected_value), operator);
|
||||
return version_or_fnmatch_compare(operator, actual_value, expected_value);
|
||||
}
|
||||
|
||||
static int condition_test_firmware(Condition *c, char **env) {
|
||||
|
||||
Reference in New Issue
Block a user