Skip to content

Commit

Permalink
conver strncmp to g_str_has_prefix
Browse files Browse the repository at this point in the history
Avoids implicit bool conversions.

Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed Jul 8, 2024
1 parent fa7d07f commit 3eb6f9b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 35 deletions.
2 changes: 1 addition & 1 deletion cputree.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ static void do_one_cpu(char *path)
entry = readdir(dir);
if (!entry)
break;
if (strncmp(entry->d_name, "node", 4) == 0) {
if (g_str_has_prefix(entry->d_name, "node")) {
char *end;
int num;
num = strtol(entry->d_name + 4, &end, 10);
Expand Down
21 changes: 9 additions & 12 deletions irqbalance.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ static void parse_command_line(int argc, char **argv)
add_cl_banned_module(optarg);
break;
case 'p':
if (!strncmp(optarg, "off", strlen(optarg)))
if (g_str_has_prefix(optarg, "off"))
power_thresh = ULONG_MAX;
else {
power_thresh = strtoull(optarg, &endptr, 10);
Expand Down Expand Up @@ -443,15 +443,14 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri
goto out_close;
}

if (!strncmp(buff, "stats", strlen("stats"))) {
if (g_str_has_prefix(buff, "stats")) {
char *stats = NULL;
for_each_object(numa_nodes, get_object_stat, &stats);
send(sock, stats, strlen(stats), 0);
free(stats);
}
if (!strncmp(buff, "settings ", strlen("settings "))) {
if (!(strncmp(buff + strlen("settings "), "sleep ",
strlen("sleep ")))) {
if (g_str_has_prefix(buff, "settings ")) {
if (g_str_has_prefix(buff + strlen("settings "), "sleep ")) {
char *sleep_string = malloc(
sizeof(char) * (recv_size - strlen("settings sleep ") + 1));

Expand All @@ -465,8 +464,7 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri
sleep_interval = new_iterval;
}
free(sleep_string);
} else if (!(strncmp(buff + strlen("settings "), "ban irqs ",
strlen("ban irqs ")))) {
} else if (g_str_has_prefix(buff + strlen("settings "), "ban irqs ")) {
char *end;
char *irq_string = malloc(
sizeof(char) * (recv_size - strlen("settings ban irqs ") + 1));
Expand All @@ -479,7 +477,7 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri
g_list_free_full(cl_banned_irqs, free);
cl_banned_irqs = NULL;
need_rescan = 1;
if (!strncmp(irq_string, "NONE", strlen("NONE"))) {
if (g_str_has_prefix(irq_string, "NONE")) {
free(irq_string);
goto out_close;
}
Expand All @@ -488,8 +486,7 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri
add_cl_banned_irq(irq);
} while((irq = strtoul(end, &end, 10)));
free(irq_string);
} else if (!(strncmp(buff + strlen("settings "), "cpus ",
strlen("cpus")))) {
} else if (g_str_has_prefix(buff + strlen("settings "), "cpus ")) {
banned_cpumask_from_ui = NULL;
free(cpu_ban_string);
cpu_ban_string = NULL;
Expand All @@ -503,15 +500,15 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri
recv_size - strlen("settings cpus "));
cpu_ban_string[recv_size - strlen("settings cpus ")] = '\0';
banned_cpumask_from_ui = strtok(cpu_ban_string, " ");
if (banned_cpumask_from_ui && !strncmp(banned_cpumask_from_ui, "NULL", strlen("NULL"))) {
if (banned_cpumask_from_ui && g_str_has_prefix(banned_cpumask_from_ui, "NULL")) {
banned_cpumask_from_ui = NULL;
free(cpu_ban_string);
cpu_ban_string = NULL;
}
need_rescan = 1;
}
}
if (!strncmp(buff, "setup", strlen("setup"))) {
if (g_str_has_prefix(buff, "setup")) {
char banned[512];
char *setup = calloc(strlen("SLEEP ") + 11 + 1, 1);
char *newptr = NULL;
Expand Down
2 changes: 1 addition & 1 deletion numa.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void build_numa_node_list(void)
if (!entry)
break;
if ((entry->d_type == DT_DIR) &&
(strncmp(entry->d_name, "node", 4) == 0) &&
g_str_has_prefix(entry->d_name, "node") &&
isdigit(entry->d_name[4])) {
add_one_node(strtoul(&entry->d_name[4], NULL, 10));
}
Expand Down
6 changes: 3 additions & 3 deletions procinterrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static int check_platform_device(char *name, struct irq_info *info)

log(TO_ALL, LOG_DEBUG, "Checking entry %s\n", ent->d_name);
for (i = 0; pdev_irq_info[i].d_name != NULL; i++) {
if (!strncmp(ent->d_name, pdev_irq_info[i].d_name, strlen(pdev_irq_info[i].d_name))) {
if (g_str_has_prefix(ent->d_name, pdev_irq_info[i].d_name)) {
info->type = pdev_irq_info[i].type;
info->class = pdev_irq_info[i].class;
rc = 0;
Expand Down Expand Up @@ -171,8 +171,8 @@ void init_irq_class_and_type(char *savedline, struct irq_info *info, int irq)
* /proc/interrupts format defined, after of interrupt type
* the reset string is mark the irq desc name.
*/
if (strncmp(irq_name, "Level", strlen("Level")) == 0 ||
strncmp(irq_name, "Edge", strlen("Edge")) == 0)
if (!g_str_has_prefix(irq_name, "Level") ||
!g_str_has_prefix(irq_name, "Edge")
break;
#endif
}
Expand Down
30 changes: 15 additions & 15 deletions ui/irqbalance-ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,21 +156,21 @@ void parse_setup(char *setup_data)
setup.banned_irqs = NULL;
setup.banned_cpus = NULL;
token = strtok_r(copy, " ", &ptr);
if(strncmp(token, "SLEEP", strlen("SLEEP")) != 0) goto out;
if(!g_str_has_prefix(token, "SLEEP")) goto out;
setup.sleep = strtol(strtok_r(NULL, " ", &ptr), NULL, 10);
token = strtok_r(NULL, " ", &ptr);
/* Parse banned IRQ data */
while(!strncmp(token, "IRQ", strlen("IRQ"))) {
while(g_str_has_prefix(token, "IRQ")) {
new_irq = g_malloc(sizeof(irq_t));
new_irq->vector = strtol(strtok_r(NULL, " ", &ptr), NULL, 10);
token = strtok_r(NULL, " ", &ptr);
if(strncmp(token, "LOAD", strlen("LOAD")) != 0) goto out;
if(!g_str_has_prefix(token, "LOAD")) goto out;
new_irq->load = strtol(strtok_r(NULL, " ", &ptr), NULL, 10);
token = strtok_r(NULL, " ", &ptr);
if(strncmp(token, "DIFF", strlen("DIFF")) != 0) goto out;
if(!g_str_has_prefix(token, "DIFF")) goto out;
new_irq->diff = strtol(strtok_r(NULL, " ", &ptr), NULL, 10);
token = strtok_r(ptr, " ", &ptr);
if(strncmp(token, "CLASS", strlen("CLASS")) != 0) goto out;
if(!g_str_has_prefix(token, "CLASS")) goto out;
new_irq->class = strtol(strtok_r(NULL, " ", &ptr), NULL, 10);
new_irq->is_banned = 1;
new_irq->assigned_to = NULL;
Expand All @@ -179,7 +179,7 @@ void parse_setup(char *setup_data)
new_irq = NULL;
}

if(strncmp(token, "BANNED", strlen("BANNED")) != 0) goto out;
if(!g_str_has_prefix(token, "BANNED")) goto out;
token = strtok_r(NULL, " ", &ptr);
for(i = strlen(token) - 1; i >= 0; i--) {
if (token[i] == ',')
Expand Down Expand Up @@ -284,7 +284,7 @@ void parse_into_tree(char *data)
token = strtok_r(copy, " ", &ptr);
while(token != NULL) {
/* Parse node data */
if(strncmp(token, "TYPE", strlen("TYPE")) != 0) {
if(!g_str_has_prefix(token, "TYPE")) {
g_free(copy);
goto out;
}
Expand All @@ -296,36 +296,36 @@ void parse_into_tree(char *data)
parent = parent->parent;
}
token = strtok_r(NULL, " ", &ptr);
if(strncmp(token, "NUMBER", strlen("NUMBER")) != 0) goto out;
if(!g_str_has_prefix(token, "NUMBER")) goto out;
new->number = strtol(strtok_r(NULL, " ", &ptr), NULL, 10);
token = strtok_r(NULL, " ", &ptr);
if(strncmp(token, "LOAD", strlen("LOAD")) != 0) goto out;
if(!g_str_has_prefix(token, "LOAD")) goto out;
new->load = strtol(strtok_r(NULL, " ", &ptr), NULL, 10);
token = strtok_r(NULL, " ", &ptr);
if(strncmp(token, "SAVE_MODE", strlen("SAVE_MODE")) != 0) goto out;
if(!g_str_has_prefix(token, "SAVE_MODE")) goto out;
new->is_powersave = strtol(strtok_r(NULL, " ", &ptr), NULL, 10);
token = strtok_r(NULL, " ", &ptr);

/* Parse assigned IRQ data */
while((token != NULL) && (!strncmp(token, "IRQ", strlen("IRQ")))) {
while(token && g_str_has_prefix(token, "IRQ")) {
new_irq = g_malloc(sizeof(irq_t));
new_irq->vector = strtol(strtok_r(NULL, " ", &ptr), NULL, 10);
token = strtok_r(NULL, " ", &ptr);
if(strncmp(token, "LOAD", strlen("LOAD")) != 0) goto out;
if(!g_str_has_prefix(token, "LOAD")) goto out;
new_irq->load = strtol(strtok_r(NULL, " ", &ptr), NULL, 10);
token = strtok_r(NULL, " ", &ptr);
if(strncmp(token, "DIFF", strlen("DIFF")) != 0) goto out;
if(!g_str_has_prefix(token, "DIFF")) goto out;
new_irq->diff = strtol(strtok_r(NULL, " ", &ptr), NULL, 10);
token = strtok_r(NULL, " ", &ptr);
if(strncmp(token, "CLASS", strlen("CLASS")) != 0) goto out;
if(!g_str_has_prefix(token, "CLASS")) goto out;
new_irq->class = strtol(strtok_r(NULL, " ", &ptr), NULL, 10);
new_irq->is_banned = 0;
new->irqs = g_list_append(new->irqs, new_irq);
token = strtok_r(NULL, " ", &ptr);
new_irq = NULL;
}

if((token == NULL) || (strncmp(token, "IRQ", strlen("IRQ")) != 0)) {
if(!token || !g_str_has_prefix(token, "IRQ")) {
new->parent = parent;
if(parent == NULL) {
tree = g_list_append(tree, new);
Expand Down
6 changes: 3 additions & 3 deletions ui/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,9 @@ void handle_cpu_banning(void)
show_frame();
show_footer();
refresh();
char settings_string[1024] = "settings cpus \0";
char settings_string[1024] = "settings cpus ";
for_each_cpu(all_cpus, get_new_cpu_ban_values, settings_string);
if(!strcmp("settings cpus \0", settings_string)) {
if(g_str_has_prefix(settings_string, "settings cpus ")) {
strncpy(settings_string + strlen(settings_string),
"NULL", 1024 - strlen(settings_string));
}
Expand Down Expand Up @@ -654,7 +654,7 @@ void handle_irq_banning(void)
refresh();
char settings_string[1024] = BAN_IRQS;
for_each_irq(all_irqs, get_new_irq_ban_values, settings_string);
if(!strcmp(BAN_IRQS, settings_string)) {
if(g_str_has_prefix(settings_string, BAN_IRQS)) {
strncpy(settings_string + strlen(settings_string),
" NONE", 1024 - strlen(settings_string));
}
Expand Down

0 comments on commit 3eb6f9b

Please sign in to comment.