mirror of
https://github.com/swaywm/sway.git
synced 2025-01-21 00:06:40 +00:00
Use has_prefix() instead of strncmp() throughout
This is safer than hardcoded string lengths.
This commit is contained in:
parent
c55dff95bc
commit
0c60d1581f
|
@ -23,7 +23,7 @@ struct cmd_results *cmd_assign(int argc, char **argv) {
|
|||
|
||||
--argc; ++argv;
|
||||
|
||||
if (strncmp(*argv, "→", strlen("→")) == 0) {
|
||||
if (has_prefix(*argv, "→")) {
|
||||
if (argc < 2) {
|
||||
free(criteria);
|
||||
return cmd_results_new(CMD_INVALID, "Missing workspace");
|
||||
|
|
|
@ -11,7 +11,7 @@ struct cmd_results *bar_cmd_font(int argc, char **argv) {
|
|||
char *font = join_args(argv, argc);
|
||||
free(config->current_bar->font);
|
||||
|
||||
if (strncmp(font, "pango:", 6) == 0) {
|
||||
if (has_prefix(font, "pango:")) {
|
||||
if (config->current_bar->pango_markup == PANGO_MARKUP_DEFAULT) {
|
||||
config->current_bar->pango_markup = true;
|
||||
}
|
||||
|
|
|
@ -367,8 +367,7 @@ static struct cmd_results *cmd_bindsym_or_bindcode(int argc, char **argv,
|
|||
}
|
||||
} else if (strcmp("--exclude-titlebar", argv[0]) == 0) {
|
||||
exclude_titlebar = true;
|
||||
} else if (strncmp("--input-device=", argv[0],
|
||||
strlen("--input-device=")) == 0) {
|
||||
} else if (has_prefix("--input-device=", argv[0])) {
|
||||
free(binding->input);
|
||||
binding->input = strdup(argv[0] + strlen("--input-device="));
|
||||
strip_quotes(binding->input);
|
||||
|
@ -399,7 +398,7 @@ static struct cmd_results *cmd_bindsym_or_bindcode(int argc, char **argv,
|
|||
list_t *split = split_string(argv[0], "+");
|
||||
for (int i = 0; i < split->length; ++i) {
|
||||
// Check for group
|
||||
if (strncmp(split->items[i], "Group", strlen("Group")) == 0) {
|
||||
if (has_prefix(split->items[i], "Group") == 0) {
|
||||
if (binding->group != XKB_LAYOUT_INVALID) {
|
||||
free_sway_binding(binding);
|
||||
list_free_items_and_destroy(split);
|
||||
|
|
|
@ -13,9 +13,9 @@ struct cmd_results *cmd_font(int argc, char **argv) {
|
|||
char *font = join_args(argv, argc);
|
||||
free(config->font);
|
||||
|
||||
if (strncmp(font, "pango:", 6) == 0) {
|
||||
if (has_prefix(font, "pango:")) {
|
||||
config->pango_markup = true;
|
||||
config->font = strdup(font + 6);
|
||||
config->font = strdup(font + strlen("pango:"));
|
||||
free(font);
|
||||
} else {
|
||||
config->pango_markup = false;
|
||||
|
|
|
@ -121,8 +121,7 @@ static struct cmd_results *cmd_bind_or_unbind_gesture(int argc, char **argv, boo
|
|||
binding->flags |= BINDING_EXACT;
|
||||
} else if (strcmp("--no-warn", argv[0]) == 0) {
|
||||
warn = false;
|
||||
} else if (strncmp("--input-device=", argv[0],
|
||||
strlen("--input-device=")) == 0) {
|
||||
} else if (has_prefix("--input-device=", argv[0])) {
|
||||
free(binding->input);
|
||||
binding->input = strdup(argv[0] + strlen("--input-device="));
|
||||
} else {
|
||||
|
|
|
@ -86,7 +86,7 @@ static void toggle_select_send_events_for_device(struct input_config *ic,
|
|||
static void toggle_send_events(int argc, char **argv) {
|
||||
struct input_config *ic = config->handler_context.input_config;
|
||||
bool wildcard = strcmp(ic->identifier, "*") == 0;
|
||||
const char *type = strncmp(ic->identifier, "type:", strlen("type:")) == 0
|
||||
const char *type = has_prefix(ic->identifier, "type:")
|
||||
? ic->identifier + strlen("type:") : NULL;
|
||||
struct sway_input_device *device = NULL;
|
||||
wl_list_for_each(device, &server.input->devices, link) {
|
||||
|
@ -146,8 +146,7 @@ struct cmd_results *input_cmd_events(int argc, char **argv) {
|
|||
|
||||
toggle_send_events(argc - 1, argv + 1);
|
||||
|
||||
if (strcmp(ic->identifier, "*") == 0 ||
|
||||
strncmp(ic->identifier, "type:", strlen("type:")) == 0) {
|
||||
if (strcmp(ic->identifier, "*") == 0 || has_prefix(ic->identifier, "type:")) {
|
||||
// Update the device input configs and then reset the type/wildcard
|
||||
// config send events mode so that is does not override the device
|
||||
// ones. The device ones will be applied when attempting to apply
|
||||
|
|
|
@ -23,7 +23,7 @@ struct cmd_results *cmd_mark(int argc, char **argv) {
|
|||
}
|
||||
|
||||
bool add = false, toggle = false;
|
||||
while (argc > 0 && strncmp(*argv, "--", 2) == 0) {
|
||||
while (argc > 0 && has_prefix(*argv, "--") == 0) {
|
||||
if (strcmp(*argv, "--add") == 0) {
|
||||
add = true;
|
||||
} else if (strcmp(*argv, "--replace") == 0) {
|
||||
|
|
|
@ -925,8 +925,8 @@ char *do_var_replacement(char *str) {
|
|||
// Find matching variable
|
||||
for (i = 0; i < config->symbols->length; ++i) {
|
||||
struct sway_variable *var = config->symbols->items[i];
|
||||
int vnlen = strlen(var->name);
|
||||
if (strncmp(find, var->name, vnlen) == 0) {
|
||||
if (has_prefix(find, var->name)) {
|
||||
int vnlen = strlen(var->name);
|
||||
int vvlen = strlen(var->value);
|
||||
char *newstr = malloc(strlen(str) - vnlen + vvlen + 1);
|
||||
if (!newstr) {
|
||||
|
|
|
@ -300,7 +300,7 @@ struct input_config *store_input_config(struct input_config *ic,
|
|||
return NULL;
|
||||
}
|
||||
|
||||
bool type = strncmp(ic->identifier, "type:", strlen("type:")) == 0;
|
||||
bool type = has_prefix(ic->identifier, "type:");
|
||||
if (type && error && !validate_type_on_existing(ic, error)) {
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
@ -1212,7 +1212,7 @@ uint32_t get_mouse_bindsym(const char *name, char **error) {
|
|||
SWAY_SCROLL_UP, SWAY_SCROLL_DOWN, SWAY_SCROLL_LEFT,
|
||||
SWAY_SCROLL_RIGHT, BTN_SIDE, BTN_EXTRA};
|
||||
return buttons[number - 1];
|
||||
} else if (strncmp(name, "BTN_", strlen("BTN_")) == 0) {
|
||||
} else if (has_prefix(name, "BTN_")) {
|
||||
// Get event code from name
|
||||
int code = libevdev_event_code_from_name(EV_KEY, name);
|
||||
if (code == -1) {
|
||||
|
@ -1237,7 +1237,7 @@ uint32_t get_mouse_bindcode(const char *name, char **error) {
|
|||
return 0;
|
||||
}
|
||||
const char *event = libevdev_event_code_get_name(EV_KEY, code);
|
||||
if (!event || strncmp(event, "BTN_", strlen("BTN_")) != 0) {
|
||||
if (!event || !has_prefix(event, "BTN_")) {
|
||||
*error = format_str("Event code %d (%s) is not a button",
|
||||
code, event ? event : "(null)");
|
||||
return 0;
|
||||
|
|
|
@ -577,7 +577,7 @@ void input_manager_configure_all_input_mappings(void) {
|
|||
void input_manager_apply_input_config(struct input_config *input_config) {
|
||||
struct sway_input_device *input_device = NULL;
|
||||
bool wildcard = strcmp(input_config->identifier, "*") == 0;
|
||||
bool type_wildcard = strncmp(input_config->identifier, "type:", 5) == 0;
|
||||
bool type_wildcard = has_prefix(input_config->identifier, "type:");
|
||||
wl_list_for_each(input_device, &server.input->devices, link) {
|
||||
bool type_matches = type_wildcard &&
|
||||
strcmp(input_device_get_type(input_device), input_config->identifier + 5) == 0;
|
||||
|
|
|
@ -417,13 +417,9 @@ bool sway_libinput_device_is_builtin(struct sway_input_device *sway_device) {
|
|||
return false;
|
||||
}
|
||||
|
||||
const char prefix_platform[] = "platform-";
|
||||
if (strncmp(id_path, prefix_platform, strlen(prefix_platform)) == 0) {
|
||||
if (has_prefix(id_path, "platform-")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const char prefix_pci[] = "pci-";
|
||||
const char infix_platform[] = "-platform-";
|
||||
return (strncmp(id_path, prefix_pci, strlen(prefix_pci)) == 0) &&
|
||||
strstr(id_path, infix_platform);
|
||||
return has_prefix(id_path, "pci-") && strstr(id_path, "-platform-");
|
||||
}
|
||||
|
|
|
@ -159,8 +159,8 @@ void enable_debug_flag(const char *flag) {
|
|||
debug.txn_wait = true;
|
||||
} else if (strcmp(flag, "txn-timings") == 0) {
|
||||
debug.txn_timings = true;
|
||||
} else if (strncmp(flag, "txn-timeout=", 12) == 0) {
|
||||
server.txn_timeout_ms = atoi(&flag[12]);
|
||||
} else if (has_prefix(flag, "txn-timeout=")) {
|
||||
server.txn_timeout_ms = atoi(&flag[strlen("txn-timeout=")]);
|
||||
} else if (strcmp(flag, "legacy-wl-drm") == 0) {
|
||||
debug.legacy_wl_drm = true;
|
||||
} else {
|
||||
|
|
|
@ -696,26 +696,26 @@ size_t parse_title_format(struct sway_container *container, char *buffer) {
|
|||
len += next - format;
|
||||
format = next;
|
||||
|
||||
if (strncmp(next, "%title", 6) == 0) {
|
||||
if (has_prefix(next, "%title")) {
|
||||
if (container->view) {
|
||||
len += append_prop(buffer, view_get_title(container->view));
|
||||
} else {
|
||||
len += container_build_representation(container->pending.layout, container->pending.children, buffer);
|
||||
}
|
||||
format += 6;
|
||||
format += strlen("%title");
|
||||
} else if (container->view) {
|
||||
if (strncmp(next, "%app_id", 7) == 0) {
|
||||
if (has_prefix(next, "%app_id")) {
|
||||
len += append_prop(buffer, view_get_app_id(container->view));
|
||||
format += 7;
|
||||
} else if (strncmp(next, "%class", 6) == 0) {
|
||||
format += strlen("%app_id");
|
||||
} else if (has_prefix(next, "%class")) {
|
||||
len += append_prop(buffer, view_get_class(container->view));
|
||||
format += 6;
|
||||
} else if (strncmp(next, "%instance", 9) == 0) {
|
||||
format += strlen("%class");
|
||||
} else if (has_prefix(next, "%instance")) {
|
||||
len += append_prop(buffer, view_get_instance(container->view));
|
||||
format += 9;
|
||||
} else if (strncmp(next, "%shell", 6) == 0) {
|
||||
format += strlen("%instance");
|
||||
} else if (has_prefix(next, "%shell")) {
|
||||
len += append_prop(buffer, view_get_shell(container->view));
|
||||
format += 6;
|
||||
format += strlen("%shell");
|
||||
} else {
|
||||
lenient_strcat(buffer, "%");
|
||||
++format;
|
||||
|
@ -778,7 +778,7 @@ size_t container_build_representation(enum sway_container_layout layout,
|
|||
len += strlen(identifier);
|
||||
lenient_strcat(buffer, identifier);
|
||||
} else {
|
||||
len += 6;
|
||||
len += strlen("(null)");
|
||||
lenient_strcat(buffer, "(null)");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -246,7 +246,7 @@ static void workspace_name_from_binding(const struct sway_binding * binding,
|
|||
}
|
||||
|
||||
// If the command is workspace number <name>, isolate the name
|
||||
if (strncmp(_target, "number ", strlen("number ")) == 0) {
|
||||
if (has_prefix(_target, "number ")) {
|
||||
size_t length = strlen(_target) - strlen("number ") + 1;
|
||||
char *temp = malloc(length);
|
||||
strncpy(temp, _target + strlen("number "), length - 1);
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#include "list.h"
|
||||
#include "log.h"
|
||||
#include "loop.h"
|
||||
#include "stringop.h"
|
||||
#include "util.h"
|
||||
|
||||
void ipc_send_workspace_command(struct swaybar *bar, const char *ws) {
|
||||
|
@ -45,8 +46,8 @@ void ipc_send_workspace_command(struct swaybar *bar, const char *ws) {
|
|||
|
||||
char *parse_font(const char *font) {
|
||||
char *new_font = NULL;
|
||||
if (strncmp("pango:", font, 6) == 0) {
|
||||
font += 6;
|
||||
if (has_prefix("pango:", font)) {
|
||||
font += strlen("pango:");
|
||||
}
|
||||
new_font = strdup(font);
|
||||
return new_font;
|
||||
|
|
|
@ -293,11 +293,11 @@ static uint32_t render_status_block(struct render_context *ctx,
|
|||
}
|
||||
|
||||
double offset = 0;
|
||||
if (strncmp(block->align, "left", 4) == 0) {
|
||||
if (has_prefix(block->align, "left")) {
|
||||
offset = x_pos;
|
||||
} else if (strncmp(block->align, "right", 5) == 0) {
|
||||
} else if (has_prefix(block->align, "right")) {
|
||||
offset = x_pos + width - text_width;
|
||||
} else if (strncmp(block->align, "center", 6) == 0) {
|
||||
} else if (has_prefix(block->align, "center")) {
|
||||
offset = x_pos + (width - text_width) / 2;
|
||||
}
|
||||
double text_y = height / 2.0 - text_height / 2.0;
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#include "cairo_util.h"
|
||||
#include "list.h"
|
||||
#include "log.h"
|
||||
#include "stringop.h"
|
||||
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
|
||||
|
||||
// TODO menu
|
||||
|
@ -161,7 +162,7 @@ static int get_property_callback(sd_bus_message *msg, void *data,
|
|||
}
|
||||
|
||||
if (strcmp(prop, "Status") == 0 || (sni->status && (sni->status[0] == 'N' ?
|
||||
prop[0] == 'A' : strncmp(prop, "Icon", 4) == 0))) {
|
||||
prop[0] == 'A' : has_prefix(prop, "Icon")))) {
|
||||
set_sni_dirty(sni);
|
||||
}
|
||||
cleanup:
|
||||
|
@ -364,7 +365,7 @@ static void handle_click(struct swaybar_sni *sni, int x, int y,
|
|||
method = "ContextMenu";
|
||||
}
|
||||
|
||||
if (strncmp(method, "Scroll", strlen("Scroll")) == 0) {
|
||||
if (has_prefix(method, "Scroll")) {
|
||||
char dir = method[strlen("Scroll")];
|
||||
char *orientation = (dir == 'U' || dir == 'D') ? "vertical" : "horizontal";
|
||||
int sign = (dir == 'U' || dir == 'L') ? -1 : 1;
|
||||
|
|
|
@ -31,9 +31,9 @@ static int handle_lost_service(sd_bus_message *msg,
|
|||
struct swaybar_watcher *watcher = data;
|
||||
for (int idx = 0; idx < watcher->items->length; ++idx) {
|
||||
char *id = watcher->items->items[idx];
|
||||
int cmp_res = using_standard_protocol(watcher) ?
|
||||
cmp_id(id, service) : strncmp(id, service, strlen(service));
|
||||
if (cmp_res == 0) {
|
||||
bool cmp_res = using_standard_protocol(watcher) ?
|
||||
cmp_id(id, service) == 0 : has_prefix(id, service);
|
||||
if (cmp_res) {
|
||||
sway_log(SWAY_DEBUG, "Unregistering Status Notifier Item '%s'", id);
|
||||
list_del(watcher->items, idx--);
|
||||
sd_bus_emit_signal(watcher->bus, obj_path, watcher->interface,
|
||||
|
|
Loading…
Reference in a new issue