Unify use of string comparisons
Patch status: merged
Patch by Mats
Long description:
* strncmp(s1, s2, strlen(s2)) → BEGINS_WITH(s1, s2) * strncmp(s1, s2, strlen(s1)) → strcmp(s1, s2) * Prefer case-insensitive comparison for options
To apply this patch, use:
curl http://cr.i3wm.org/patch/464/raw.patch | git am
b/src/print_battery_info.c
| 26 |
@@ -153,11 +153,11 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char |
| 27 |
seconds -= (minutes * 60); |
| 28 |
|
| 29 |
if (status == CS_DISCHARGING && low_threshold > 0) {
|
| 30 |
- if (strncmp(threshold_type, "percentage", strlen(threshold_type)) == 0 |
| 31 |
+ if (strcasecmp(threshold_type, "percentage") == 0 |
| 32 |
&& percentage_remaining < low_threshold) {
|
| 33 |
START_COLOR("color_bad");
|
| 34 |
colorful_output = true; |
| 35 |
- } else if (strncmp(threshold_type, "time", strlen(threshold_type)) == 0 |
| 36 |
+ } else if (strcasecmp(threshold_type, "time") == 0 |
| 37 |
&& seconds_remaining < 60 * low_threshold) {
|
| 38 |
START_COLOR("color_bad");
|
| 39 |
colorful_output = true; |
| 40 |
@@ -191,7 +191,7 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char |
| 41 |
* we colorize the output if threshold_type is set to percentage |
| 42 |
* (since we don't have any information on remaining time). */ |
| 43 |
if (status == CS_DISCHARGING && low_threshold > 0) {
|
| 44 |
- if (strncmp(threshold_type, "percentage", strlen(threshold_type)) == 0 |
| 45 |
+ if (strcasecmp(threshold_type, "percentage") == 0 |
| 46 |
&& percentage_remaining < low_threshold) {
|
| 47 |
START_COLOR("color_bad");
|
| 48 |
colorful_output = true; |
| 49 |
@@ -242,11 +242,11 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char |
| 50 |
minutes -= (hours * 60); |
| 51 |
(void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d", |
| 52 |
max(hours, 0), max(minutes, 0)); |
| 53 |
- if (strncmp(threshold_type, "percentage", strlen(threshold_type)) == 0 |
| 54 |
+ if (strcasecmp(threshold_type, "percentage") == 0 |
| 55 |
&& present_rate < low_threshold) {
|
| 56 |
START_COLOR("color_bad");
|
| 57 |
colorful_output = true; |
| 58 |
- } else if (strncmp(threshold_type, "time", strlen(threshold_type)) == 0 |
| 59 |
+ } else if (strcasecmp(threshold_type, "time") == 0 |
| 60 |
&& remaining < (u_int) low_threshold) {
|
| 61 |
START_COLOR("color_bad");
|
| 62 |
colorful_output = true; |
| 63 |
@@ -295,11 +295,11 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char |
| 64 |
(void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%", apm_info.battery_life); |
| 65 |
|
| 66 |
if (status == CS_DISCHARGING && low_threshold > 0) {
|
| 67 |
- if (strncmp(threshold_type, "percentage", strlen(threshold_type)) == 0 |
| 68 |
+ if (strcasecmp(threshold_type, "percentage") == 0 |
| 69 |
&& apm_info.battery_life < low_threshold) {
|
| 70 |
START_COLOR("color_bad");
|
| 71 |
colorful_output = true; |
| 72 |
- } else if (strncmp(threshold_type, "time", strlen(threshold_type)) == 0 |
| 73 |
+ } else if (strcasecmp(threshold_type, "time") == 0 |
| 74 |
&& apm_info.minutes_left < (u_int) low_threshold) {
|
| 75 |
START_COLOR("color_bad");
|
| 76 |
colorful_output = true; |
| 77 |
@@ -333,21 +333,21 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char |
| 78 |
continue; |
| 79 |
} |
| 80 |
|
| 81 |
- if (strncmp(walk+1, "status", strlen("status")) == 0) {
|
| 82 |
+ if (BEGINS_WITH(walk+1, "status")) {
|
| 83 |
outwalk += sprintf(outwalk, "%s", statusbuf); |
| 84 |
walk += strlen("status");
|
| 85 |
- } else if (strncmp(walk+1, "percentage", strlen("percentage")) == 0) {
|
| 86 |
+ } else if (BEGINS_WITH(walk+1, "percentage")) {
|
| 87 |
outwalk += sprintf(outwalk, "%s", percentagebuf); |
| 88 |
walk += strlen("percentage");
|
| 89 |
- } else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
|
| 90 |
+ } else if (BEGINS_WITH(walk+1, "remaining")) {
|
| 91 |
outwalk += sprintf(outwalk, "%s", remainingbuf); |
| 92 |
walk += strlen("remaining");
|
| 93 |
EAT_SPACE_FROM_OUTPUT_IF_EMPTY(remainingbuf); |
| 94 |
- } else if (strncmp(walk+1, "emptytime", strlen("emptytime")) == 0) {
|
| 95 |
+ } else if (BEGINS_WITH(walk+1, "emptytime")) {
|
| 96 |
outwalk += sprintf(outwalk, "%s", emptytimebuf); |
| 97 |
walk += strlen("emptytime");
|
| 98 |
EAT_SPACE_FROM_OUTPUT_IF_EMPTY(emptytimebuf); |
| 99 |
- } else if (strncmp(walk+1, "consumption", strlen("consumption")) == 0) {
|
| 100 |
+ } else if (BEGINS_WITH(walk+1, "consumption")) {
|
| 101 |
outwalk += sprintf(outwalk, "%s", consumptionbuf); |
| 102 |
walk += strlen("consumption");
|
| 103 |
EAT_SPACE_FROM_OUTPUT_IF_EMPTY(consumptionbuf); |
b/src/print_cpu_temperature.c
| 108 |
@@ -117,7 +117,7 @@ void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const |
| 109 |
goto error; |
| 110 |
} |
| 111 |
/* 'path' is the node within the full path (defaults to acpitz0). */ |
| 112 |
- if (strncmp(sensordev.xname, thermal_zone, strlen(thermal_zone)) == 0) {
|
| 113 |
+ if (BEGINS_WITH(sensordev.xname, thermal_zone)) {
|
| 114 |
mib[3] = SENSOR_TEMP; |
| 115 |
/* Limit to temo0, but should retrieve from a full path... */ |
| 116 |
for (numt = 0; numt < 1 /*sensordev.maxnumt[SENSOR_TEMP]*/; numt++) {
|
b/src/print_cpu_usage.c
| 121 |
@@ -97,7 +97,7 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format) {
|
| 122 |
continue; |
| 123 |
} |
| 124 |
|
| 125 |
- if (strncmp(walk+1, "usage", strlen("usage")) == 0) {
|
| 126 |
+ if (BEGINS_WITH(walk+1, "usage")) {
|
| 127 |
outwalk += sprintf(outwalk, "%02d%%", diff_usage); |
| 128 |
walk += strlen("usage");
|
| 129 |
} |
b/src/print_disk_info.c
| 134 |
@@ -43,9 +43,9 @@ static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char |
| 135 |
* |
| 136 |
*/ |
| 137 |
static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_type) {
|
| 138 |
- if (strncmp(prefix_type, "decimal", strlen(prefix_type)) == 0) {
|
| 139 |
+ if (strcasecmp(prefix_type, "decimal") == 0) {
|
| 140 |
return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols); |
| 141 |
- } else if (strncmp(prefix_type, "custom", strlen(prefix_type)) == 0) {
|
| 142 |
+ } else if (strcasecmp(prefix_type, "custom") == 0) {
|
| 143 |
return format_bytes(outwalk, bytes, BINARY_BASE, custom_symbols); |
| 144 |
} else {
|
| 145 |
return format_bytes(outwalk, bytes, BINARY_BASE, iec_symbols); |
b/src/print_eth_info.c
| 150 |
@@ -90,8 +90,8 @@ static int print_eth_speed(char *outwalk, const char *interface) {
|
| 151 |
* Skip these non-informative values and go right ahead to the |
| 152 |
* actual speeds. |
| 153 |
*/ |
| 154 |
- if (strncmp(desc->ifmt_string, "autoselect", strlen("autoselect")) == 0 ||
|
| 155 |
- strncmp(desc->ifmt_string, "auto", strlen("auto")) == 0)
|
| 156 |
+ if (BEGINS_WITH(desc->ifmt_string, "autoselect") || |
| 157 |
+ BEGINS_WITH(desc->ifmt_string, "auto")) |
| 158 |
continue; |
| 159 |
|
| 160 |
if (IFM_TYPE_MATCH(desc->ifmt_word, ifmr.ifm_active) && |
| 161 |
@@ -131,10 +131,10 @@ void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, cons |
| 162 |
continue; |
| 163 |
} |
| 164 |
|
| 165 |
- if (strncmp(walk+1, "ip", strlen("ip")) == 0) {
|
| 166 |
+ if (BEGINS_WITH(walk+1, "ip")) {
|
| 167 |
outwalk += sprintf(outwalk, "%s", ip_address); |
| 168 |
walk += strlen("ip");
|
| 169 |
- } else if (strncmp(walk+1, "speed", strlen("speed")) == 0) {
|
| 170 |
+ } else if (BEGINS_WITH(walk+1, "speed")) {
|
| 171 |
outwalk += print_eth_speed(outwalk, interface); |
| 172 |
walk += strlen("speed");
|
| 173 |
} |
b/src/print_ipv6_addr.c
| 178 |
@@ -136,7 +136,7 @@ void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, con |
| 179 |
continue; |
| 180 |
} |
| 181 |
|
| 182 |
- if (strncmp(walk+1, "ip", strlen("ip")) == 0) {
|
| 183 |
+ if (BEGINS_WITH(walk+1, "ip")) {
|
| 184 |
outwalk += sprintf(outwalk, "%s", addr_string); |
| 185 |
walk += strlen("ip");
|
| 186 |
} |
b/src/print_path_exists.c
| 191 |
@@ -21,10 +21,10 @@ void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const |
| 192 |
continue; |
| 193 |
} |
| 194 |
|
| 195 |
- if (strncmp(walk+1, "title", strlen("title")) == 0) {
|
| 196 |
+ if (BEGINS_WITH(walk+1, "title")) {
|
| 197 |
outwalk += sprintf(outwalk, "%s", title); |
| 198 |
walk += strlen("title");
|
| 199 |
- } else if (strncmp(walk+1, "status", strlen("status")) == 0) {
|
| 200 |
+ } else if (BEGINS_WITH(walk+1, "status")) {
|
| 201 |
outwalk += sprintf(outwalk, "%s", (exists ? "yes" : "no")); |
| 202 |
walk += strlen("status");
|
| 203 |
} |
b/src/print_run_watch.c
| 208 |
@@ -19,10 +19,10 @@ void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const c |
| 209 |
continue; |
| 210 |
} |
| 211 |
|
| 212 |
- if (strncmp(walk+1, "title", strlen("title")) == 0) {
|
| 213 |
+ if (BEGINS_WITH(walk+1, "title")) {
|
| 214 |
outwalk += sprintf(outwalk, "%s", title); |
| 215 |
walk += strlen("title");
|
| 216 |
- } else if (strncmp(walk+1, "status", strlen("status")) == 0) {
|
| 217 |
+ } else if (BEGINS_WITH(walk+1, "status")) {
|
| 218 |
outwalk += sprintf(outwalk, "%s", (running ? "yes" : "no")); |
| 219 |
walk += strlen("status");
|
| 220 |
} |