From 730af5e721f04cf12b613341e00b44a93f3397c6 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Tue, 19 Dec 2017 04:57:42 -0500 Subject: use snprintf to get identifier len --- sway/input/input-manager.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) (limited to 'sway/input/input-manager.c') diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index 52da8f5e..b20b7b7e 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -34,13 +34,6 @@ static struct sway_seat *input_manager_get_seat( return sway_seat_create(input, seat_name); } -static inline int strlen_num(int num) { - if (num == 0) { - return 2; - } - return (int)((ceil(log10(abs(num)))+2)); -} - static char *get_device_identifier(struct wlr_input_device *device) { int vendor = device->vendor; int product = device->product; @@ -54,19 +47,14 @@ static char *get_device_identifier(struct wlr_input_device *device) { } } - int len = - (strlen(name) + - strlen_num(device->vendor) + - strlen_num(device->product) + - 3) * sizeof(char); - + const char *fmt = "%d:%d:%s"; + int len = snprintf(NULL, 0, fmt, vendor, product, name) + 1; char *identifier = malloc(len); if (!identifier) { sway_log(L_ERROR, "Unable to allocate unique input device name"); return NULL; } - const char *fmt = "%d:%d:%s"; snprintf(identifier, len, fmt, vendor, product, name); free(name); return identifier; -- cgit v1.2.3