use snprintf to get identifier len

master
Tony Crisci 7 years ago
parent 9df4a2c7a8
commit 730af5e721

@ -34,13 +34,6 @@ static struct sway_seat *input_manager_get_seat(
return sway_seat_create(input, seat_name); 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) { static char *get_device_identifier(struct wlr_input_device *device) {
int vendor = device->vendor; int vendor = device->vendor;
int product = device->product; int product = device->product;
@ -54,19 +47,14 @@ static char *get_device_identifier(struct wlr_input_device *device) {
} }
} }
int len = const char *fmt = "%d:%d:%s";
(strlen(name) + int len = snprintf(NULL, 0, fmt, vendor, product, name) + 1;
strlen_num(device->vendor) +
strlen_num(device->product) +
3) * sizeof(char);
char *identifier = malloc(len); char *identifier = malloc(len);
if (!identifier) { if (!identifier) {
sway_log(L_ERROR, "Unable to allocate unique input device name"); sway_log(L_ERROR, "Unable to allocate unique input device name");
return NULL; return NULL;
} }
const char *fmt = "%d:%d:%s";
snprintf(identifier, len, fmt, vendor, product, name); snprintf(identifier, len, fmt, vendor, product, name);
free(name); free(name);
return identifier; return identifier;

Loading…
Cancel
Save