mirror of
https://github.com/ipmitool/ipmitool.git
synced 2025-07-10 06:33:37 +00:00
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
This commit is contained in:
committed by
Alexander Amelkin
parent
9d5ea21df7
commit
6e037d6bfb
@ -192,7 +192,7 @@ ipmi_event_intf_load(char * name)
|
||||
intf++)
|
||||
{
|
||||
i = *intf;
|
||||
if (strncmp(name, i->name, strlen(name)) == 0) {
|
||||
if (strcmp(name, i->name) == 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
@ -705,32 +705,32 @@ ipmievd_main(struct ipmi_event_intf * eintf, int argc, char ** argv)
|
||||
sprintf(pidfile, "%s%d", DEFAULT_PIDFILE, eintf->intf->devnum);
|
||||
|
||||
for (i = 0; i < argc; i++) {
|
||||
if (strncasecmp(argv[i], "help", 4) == 0) {
|
||||
if (strcasecmp(argv[i], "help") == 0) {
|
||||
ipmievd_usage();
|
||||
return 0;
|
||||
}
|
||||
if (strncasecmp(argv[i], "daemon", 6) == 0) {
|
||||
if (strcasecmp(argv[i], "daemon") == 0) {
|
||||
daemon = 1;
|
||||
}
|
||||
else if (strncasecmp(argv[i], "nodaemon", 8) == 0) {
|
||||
else if (strcasecmp(argv[i], "nodaemon") == 0) {
|
||||
daemon = 0;
|
||||
}
|
||||
else if (strncasecmp(argv[i], "daemon=", 7) == 0) {
|
||||
if (strncasecmp(argv[i]+7, "on", 2) == 0 ||
|
||||
strncasecmp(argv[i]+7, "yes", 3) == 0)
|
||||
else if (strcasecmp(argv[i], "daemon=") == 0) {
|
||||
if (strcasecmp(argv[i]+7, "on") == 0 ||
|
||||
strcasecmp(argv[i]+7, "yes") == 0)
|
||||
daemon = 1;
|
||||
else if (strncasecmp(argv[i]+7, "off", 3) == 0 ||
|
||||
strncasecmp(argv[i]+7, "no", 2) == 0)
|
||||
else if (strcasecmp(argv[i]+7, "off") == 0 ||
|
||||
strcasecmp(argv[i]+7, "no") == 0)
|
||||
daemon = 0;
|
||||
}
|
||||
else if (strncasecmp(argv[i], "timeout=", 8) == 0) {
|
||||
else if (strcasecmp(argv[i], "timeout=") == 0) {
|
||||
if ( (str2int(argv[i]+8, &selwatch_timeout) != 0) ||
|
||||
selwatch_timeout < 0) {
|
||||
lprintf(LOG_ERR, "Invalid input given or out of range for time-out.");
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
else if (strncasecmp(argv[i], "pidfile=", 8) == 0) {
|
||||
else if (strcasecmp(argv[i], "pidfile=") == 0) {
|
||||
memset(pidfile, 0, 64);
|
||||
strncpy(pidfile, argv[i]+8,
|
||||
__min(strlen((const char *)(argv[i]+8)), 63));
|
||||
@ -845,7 +845,7 @@ ipmievd_open_main(struct ipmi_intf * intf, int argc, char ** argv)
|
||||
struct ipmi_event_intf * eintf;
|
||||
|
||||
/* only one interface works for this */
|
||||
if (strncmp(intf->name, "open", 4) != 0) {
|
||||
if (strcmp(intf->name, "open") != 0) {
|
||||
lprintf(LOG_ERR, "Invalid Interface for OpenIPMI Event Handler: %s", intf->name);
|
||||
return -1;
|
||||
}
|
||||
|
@ -125,14 +125,14 @@ int ipmi_shell_main(struct ipmi_intf *intf, int argc, char **argv)
|
||||
pbuf = NULL;
|
||||
continue;
|
||||
}
|
||||
if (strncmp(pbuf, "quit", 4) == 0 ||
|
||||
strncmp(pbuf, "exit", 4) == 0) {
|
||||
if (strcmp(pbuf, "quit") == 0 ||
|
||||
strcmp(pbuf, "exit") == 0) {
|
||||
free(pbuf);
|
||||
pbuf = NULL;
|
||||
return 0;
|
||||
}
|
||||
if (strncmp(pbuf, "help", 4) == 0 ||
|
||||
strncmp(pbuf, "?", 1) == 0) {
|
||||
if (strcmp(pbuf, "help") == 0 ||
|
||||
strcmp(pbuf, "?") == 0) {
|
||||
ipmi_cmd_print(intf->cmdlist);
|
||||
free(pbuf);
|
||||
pbuf = NULL;
|
||||
@ -258,13 +258,13 @@ ipmi_set_usage(void)
|
||||
|
||||
int ipmi_set_main(struct ipmi_intf * intf, int argc, char ** argv)
|
||||
{
|
||||
if (argc == 0 || strncmp(argv[0], "help", 4) == 0) {
|
||||
if (argc == 0 || strcmp(argv[0], "help") == 0) {
|
||||
ipmi_set_usage();
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* these options can have no arguments */
|
||||
if (strncmp(argv[0], "verbose", 7) == 0) {
|
||||
if (strcmp(argv[0], "verbose") == 0) {
|
||||
if (argc > 1) {
|
||||
if (str2int(argv[1], &verbose) != 0) {
|
||||
lprintf(LOG_ERR,
|
||||
@ -277,7 +277,7 @@ int ipmi_set_main(struct ipmi_intf * intf, int argc, char ** argv)
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (strncmp(argv[0], "csv", 3) == 0) {
|
||||
if (strcmp(argv[0], "csv") == 0) {
|
||||
if (argc > 1) {
|
||||
if (str2int(argv[1], &csv_output) != 0) {
|
||||
lprintf(LOG_ERR,
|
||||
@ -297,8 +297,8 @@ int ipmi_set_main(struct ipmi_intf * intf, int argc, char ** argv)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (strncmp(argv[0], "host", 4) == 0 ||
|
||||
strncmp(argv[0], "hostname", 8) == 0) {
|
||||
if (strcmp(argv[0], "host") == 0 ||
|
||||
strcmp(argv[0], "hostname") == 0) {
|
||||
ipmi_intf_session_set_hostname(intf, argv[1]);
|
||||
if (!intf->session) {
|
||||
lprintf(LOG_ERR, "Failed to set session hostname.");
|
||||
@ -307,8 +307,8 @@ int ipmi_set_main(struct ipmi_intf * intf, int argc, char ** argv)
|
||||
printf("Set session hostname to %s\n",
|
||||
intf->ssn_params.hostname);
|
||||
}
|
||||
else if (strncmp(argv[0], "user", 4) == 0 ||
|
||||
strncmp(argv[0], "username", 8) == 0) {
|
||||
else if (strcmp(argv[0], "user") == 0 ||
|
||||
strcmp(argv[0], "username") == 0) {
|
||||
ipmi_intf_session_set_username(intf, argv[1]);
|
||||
if (!intf->session) {
|
||||
lprintf(LOG_ERR, "Failed to set session username.");
|
||||
@ -317,8 +317,8 @@ int ipmi_set_main(struct ipmi_intf * intf, int argc, char ** argv)
|
||||
printf("Set session username to %s\n",
|
||||
intf->ssn_params.username);
|
||||
}
|
||||
else if (strncmp(argv[0], "pass", 4) == 0 ||
|
||||
strncmp(argv[0], "password", 8) == 0) {
|
||||
else if (strcmp(argv[0], "pass") == 0 ||
|
||||
strcmp(argv[0], "password") == 0) {
|
||||
ipmi_intf_session_set_password(intf, argv[1]);
|
||||
if (!intf->session) {
|
||||
lprintf(LOG_ERR, "Failed to set session password.");
|
||||
@ -326,7 +326,7 @@ int ipmi_set_main(struct ipmi_intf * intf, int argc, char ** argv)
|
||||
}
|
||||
printf("Set session password\n");
|
||||
}
|
||||
else if (strncmp(argv[0], "authtype", 8) == 0) {
|
||||
else if (strcmp(argv[0], "authtype") == 0) {
|
||||
int authtype;
|
||||
authtype = str2val(argv[1], ipmi_authtype_session_vals);
|
||||
if (authtype == 0xFF) {
|
||||
@ -343,7 +343,7 @@ int ipmi_set_main(struct ipmi_intf * intf, int argc, char ** argv)
|
||||
val2str(intf->ssn_params.authtype_set,
|
||||
ipmi_authtype_session_vals));
|
||||
}
|
||||
else if (strncmp(argv[0], "privlvl", 7) == 0) {
|
||||
else if (strcmp(argv[0], "privlvl") == 0) {
|
||||
int privlvl;
|
||||
privlvl = str2val(argv[1], ipmi_privlvl_vals);
|
||||
if (privlvl == 0xFF) {
|
||||
@ -361,7 +361,7 @@ int ipmi_set_main(struct ipmi_intf * intf, int argc, char ** argv)
|
||||
val2str(intf->ssn_params.privlvl,
|
||||
ipmi_privlvl_vals));
|
||||
}
|
||||
else if (strncmp(argv[0], "port", 4) == 0) {
|
||||
else if (strcmp(argv[0], "port") == 0) {
|
||||
int port = 0;
|
||||
if (str2int(argv[1], &port) != 0 || port > MAX_PORT) {
|
||||
lprintf(LOG_ERR, "Given port '%s' is invalid.",
|
||||
@ -375,7 +375,7 @@ int ipmi_set_main(struct ipmi_intf * intf, int argc, char ** argv)
|
||||
}
|
||||
printf("Set session port to %d\n", intf->ssn_params.port);
|
||||
}
|
||||
else if (strncmp(argv[0], "localaddr", 9) == 0) {
|
||||
else if (strcmp(argv[0], "localaddr") == 0) {
|
||||
uint8_t my_addr = 0;
|
||||
if (str2uchar(argv[1], &my_addr) != 0) {
|
||||
lprintf(LOG_ERR, "Given localaddr '%s' is invalid.",
|
||||
@ -385,7 +385,7 @@ int ipmi_set_main(struct ipmi_intf * intf, int argc, char ** argv)
|
||||
intf->my_addr = my_addr;
|
||||
printf("Set local IPMB address to 0x%02x\n", intf->my_addr);
|
||||
}
|
||||
else if (strncmp(argv[0], "targetaddr", 10) == 0) {
|
||||
else if (strcmp(argv[0], "targetaddr") == 0) {
|
||||
uint8_t target_addr = 0;
|
||||
if (str2uchar(argv[1], &target_addr) != 0) {
|
||||
lprintf(LOG_ERR, "Given targetaddr '%s' is invalid.",
|
||||
|
@ -167,8 +167,8 @@ void ipmi_intf_print(struct ipmi_intf_support * intflist)
|
||||
if (intflist) {
|
||||
found = 0;
|
||||
for (sup=intflist; sup->name; sup++) {
|
||||
if (strncmp(sup->name, (*intf)->name, strlen(sup->name)) == 0 &&
|
||||
strncmp(sup->name, (*intf)->name, strlen((*intf)->name)) == 0 &&
|
||||
if (strcmp(sup->name, (*intf)->name) == 0 &&
|
||||
strcmp(sup->name, (*intf)->name) == 0 &&
|
||||
sup->supported == 1)
|
||||
found = 1;
|
||||
}
|
||||
@ -211,7 +211,7 @@ struct ipmi_intf * ipmi_intf_load(char * name)
|
||||
intf++)
|
||||
{
|
||||
i = *intf;
|
||||
if (strncmp(name, i->name, strlen(name)) == 0) {
|
||||
if (strcmp(name, i->name) == 0) {
|
||||
if (i->setup && (i->setup(i) < 0)) {
|
||||
lprintf(LOG_ERR, "Unable to setup "
|
||||
"interface %s", name);
|
||||
|
@ -394,7 +394,7 @@ recv_response(struct ipmi_intf * intf, unsigned char *data, int len)
|
||||
*pp = 0;
|
||||
|
||||
/* was it an error? */
|
||||
if (strncmp(p, "ERR ", 4) == 0) {
|
||||
if (strcmp(p, "ERR ") == 0) {
|
||||
serial_write_line(intf, "\r\r\r\r");
|
||||
sleep(1);
|
||||
serial_flush(intf);
|
||||
|
@ -133,7 +133,7 @@ scsiProbeNew(int *num_ami_devices, int *sg_nos)
|
||||
}
|
||||
|
||||
if (sscanf(linebuf, "%s", vendor) == 1) {
|
||||
if (strncmp(vendor, "AMI", strlen("AMI")) == 0) {
|
||||
if (strcmp(vendor, "AMI") == 0) {
|
||||
numdevfound++;
|
||||
sg_nos[numdevfound - 1] = lineno;
|
||||
if (numdevfound == inplen) {
|
||||
@ -249,7 +249,7 @@ IsG2Drive(int cd_desc)
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (strncmp(szSignature, "$$$AMI$$$", strlen("$$$AMI$$$")) != 0) {
|
||||
if (strcmp(szSignature, "$$$AMI$$$") != 0) {
|
||||
lprintf(LOG_ERR,
|
||||
"IsG2Drive:Signature mismatch when ID command sent");
|
||||
return 1;
|
||||
|
Reference in New Issue
Block a user