From 6e037d6bfbbb93b349c8ca331ebde03a837f76bf Mon Sep 17 00:00:00 2001 From: Jiang Junyu Date: Fri, 21 Feb 2020 13:53:38 -0800 Subject: Refactor string comparisons Clean up use of strcmp/strncmp/strncasecmp for command line arguments. Never use anything but `strcmp()` unless absolutely neccessary. Partialy resolves ipmitool/ipmitool#104 --- lib/ipmi_mc.c | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'lib/ipmi_mc.c') diff --git a/lib/ipmi_mc.c b/lib/ipmi_mc.c index 681fe41..2f68a99 100644 --- a/lib/ipmi_mc.c +++ b/lib/ipmi_mc.c @@ -323,7 +323,7 @@ ipmi_mc_set_enables(struct ipmi_intf * intf, int argc, char ** argv) printf_mc_usage(); return (-1); } - else if (strncmp(argv[0], "help", 4) == 0) { + else if (strcmp(argv[0], "help") == 0) { printf_mc_usage(); return 0; } @@ -348,13 +348,13 @@ ipmi_mc_set_enables(struct ipmi_intf * intf, int argc, char ** argv) for (i = 0; i < argc; i++) { for (bf = mc_enables_bf; bf->name; bf++) { int nl = strlen(bf->name); - if (strncmp(argv[i], bf->name, nl) != 0) + if (strcmp(argv[i], bf->name) != 0) continue; - if (strncmp(argv[i]+nl+1, "off", 3) == 0) { + if (strcmp(argv[i]+nl+1, "off") == 0) { printf("Disabling %s\n", bf->desc); en &= ~bf->mask; } - else if (strncmp(argv[i]+nl+1, "on", 2) == 0) { + else if (strcmp(argv[i]+nl+1, "on") == 0) { printf("Enabling %s\n", bf->desc); en |= bf->mask; } @@ -1284,24 +1284,24 @@ ipmi_mc_main(struct ipmi_intf * intf, int argc, char ** argv) printf_mc_usage(); rc = (-1); } - else if (strncmp(argv[0], "help", 4) == 0) { + else if (strcmp(argv[0], "help") == 0) { printf_mc_usage(); rc = 0; } - else if (strncmp(argv[0], "reset", 5) == 0) { + else if (strcmp(argv[0], "reset") == 0) { if (argc < 2) { lprintf(LOG_ERR, "Not enough parameters given."); printf_mc_reset_usage(); rc = (-1); } - else if (strncmp(argv[1], "help", 4) == 0) { + else if (strcmp(argv[1], "help") == 0) { printf_mc_reset_usage(); rc = 0; } - else if (strncmp(argv[1], "cold", 4) == 0) { + else if (strcmp(argv[1], "cold") == 0) { rc = ipmi_mc_reset(intf, BMC_COLD_RESET); } - else if (strncmp(argv[1], "warm", 4) == 0) { + else if (strcmp(argv[1], "warm") == 0) { rc = ipmi_mc_reset(intf, BMC_WARM_RESET); } else { @@ -1310,15 +1310,15 @@ ipmi_mc_main(struct ipmi_intf * intf, int argc, char ** argv) rc = (-1); } } - else if (strncmp(argv[0], "info", 4) == 0) { + else if (strcmp(argv[0], "info") == 0) { rc = ipmi_mc_get_deviceid(intf); } - else if (strncmp(argv[0], "guid", 4) == 0) { + else if (strcmp(argv[0], "guid") == 0) { ipmi_guid_mode_t guid_mode = GUID_AUTO; /* Allow for 'rfc' and 'rfc4122' */ if (argc > 1) { - if (!strncmp(argv[1], "rfc", 3)) { + if (!strcmp(argv[1], "rfc")) { guid_mode = GUID_RFC4122; } else if (!strcmp(argv[1], "smbios")) { @@ -1336,26 +1336,26 @@ ipmi_mc_main(struct ipmi_intf * intf, int argc, char ** argv) } rc = ipmi_mc_print_guid(intf, guid_mode); } - else if (strncmp(argv[0], "getenables", 10) == 0) { + else if (strcmp(argv[0], "getenables") == 0) { rc = ipmi_mc_get_enables(intf); } - else if (strncmp(argv[0], "setenables", 10) == 0) { + else if (strcmp(argv[0], "setenables") == 0) { rc = ipmi_mc_set_enables(intf, argc-1, &(argv[1])); } - else if (!strncmp(argv[0], "selftest", 8)) { + else if (!strcmp(argv[0], "selftest")) { rc = ipmi_mc_get_selftest(intf); } - else if (!strncmp(argv[0], "watchdog", 8)) { + else if (!strcmp(argv[0], "watchdog")) { if (argc < 2) { lprintf(LOG_ERR, "Not enough parameters given."); print_watchdog_usage(); rc = (-1); } - else if (strncmp(argv[1], "help", 4) == 0) { + else if (strcmp(argv[1], "help") == 0) { print_watchdog_usage(); rc = 0; } - else if (strncmp(argv[1], "set", 3) == 0) { + else if (strcmp(argv[1], "set") == 0) { if (argc < 3) { /* Requires options */ lprintf(LOG_ERR, "Not enough parameters given."); print_watchdog_usage(); @@ -1365,13 +1365,13 @@ ipmi_mc_main(struct ipmi_intf * intf, int argc, char ** argv) rc = ipmi_mc_set_watchdog(intf, argc - 2, &(argv[2])); } } - else if (strncmp(argv[1], "get", 3) == 0) { + else if (strcmp(argv[1], "get") == 0) { rc = ipmi_mc_get_watchdog(intf); } - else if(strncmp(argv[1], "off", 3) == 0) { + else if(strcmp(argv[1], "off") == 0) { rc = ipmi_mc_shutoff_watchdog(intf); } - else if(strncmp(argv[1], "reset", 5) == 0) { + else if(strcmp(argv[1], "reset") == 0) { rc = ipmi_mc_rst_watchdog(intf); } else { @@ -1380,10 +1380,10 @@ ipmi_mc_main(struct ipmi_intf * intf, int argc, char ** argv) rc = (-1); } } - else if (strncmp(argv[0], "getsysinfo", 10) == 0) { + else if (strcmp(argv[0], "getsysinfo") == 0) { rc = ipmi_sysinfo_main(intf, argc, argv, 0); } - else if (strncmp(argv[0], "setsysinfo", 10) == 0) { + else if (strcmp(argv[0], "setsysinfo") == 0) { rc = ipmi_sysinfo_main(intf, argc, argv, 1); } else { -- cgit v1.2.1