diff --git a/common/util.c b/common/util.c index 7602216cf..243f90a83 100644 --- a/common/util.c +++ b/common/util.c @@ -4,6 +4,16 @@ int wrap(int i, int max) { return ((i % max) + max) % max; } +int numlen(int n) { + if (n >= 1000000) return 7; + if (n >= 100000) return 6; + if (n >= 10000) return 5; + if (n >= 1000) return 4; + if (n >= 100) return 3; + if (n >= 10) return 2; + return 1; +} + static struct modifier_key { char *name; uint32_t mod; diff --git a/include/util.h b/include/util.h index 7f4b3ace6..dc47e3430 100644 --- a/include/util.h +++ b/include/util.h @@ -10,6 +10,11 @@ */ int wrap(int i, int max); +/** + * Count number of digits in int + */ +int numlen(int n); + /** * Get modifier mask from modifier name. * diff --git a/sway/commands.c b/sway/commands.c index 58fc4aa50..9f6e50327 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -14,7 +14,6 @@ #include #include #include -#include #include "stringop.h" #include "layout.h" #include "focus.h" @@ -1581,7 +1580,7 @@ static struct cmd_results *cmd_bar(int argc, char **argv) { int i; for (i = 0; i < config->bars->length; ++i) { if (bar == config->bars->items[i]) { - const int len = 5 + log10(i) + 1; // "bar-" + i + \0 + const int len = 5 + numlen(i); // "bar-" + i + \0 bar->id = malloc(len * sizeof(char)); snprintf(bar->id, len, "bar-%d", i); break; diff --git a/swaygrab/CMakeLists.txt b/swaygrab/CMakeLists.txt index 9bd06c086..9035ac8bf 100644 --- a/swaygrab/CMakeLists.txt +++ b/swaygrab/CMakeLists.txt @@ -10,7 +10,6 @@ target_link_libraries(swaygrab sway-common ${JSONC_LIBRARIES} rt - m ) install( diff --git a/swaygrab/main.c b/swaygrab/main.c index b944222cd..82d623e7a 100644 --- a/swaygrab/main.c +++ b/swaygrab/main.c @@ -39,7 +39,7 @@ void grab_and_apply_magick(const char *file, const char *output, const char *fmt = "convert -depth 8 -size %dx%d+0 rgba:- -flip %s"; char *cmd = malloc(strlen(fmt) - 6 /*args*/ - + log10(width) + 1 + log10(height) + 1 + strlen(file) + 1); + + numlen(width) + numlen(height) + strlen(file) + 1); sprintf(cmd, fmt, width, height, file); FILE *f = popen(cmd, "w"); @@ -72,7 +72,7 @@ void grab_and_apply_movie_magic(const char *file, const char *output, "-video_size %dx%d -pixel_format argb " "-i pipe:0 -r %d -vf vflip %s"; char *cmd = malloc(strlen(fmt) - 8 /*args*/ - + log10(width) + 1 + log10(height) + 1 + log10(framerate) + 1 * 2 + + numlen(width) + numlen(height) + numlen(framerate) * 2 + strlen(file) + 1); sprintf(cmd, fmt, framerate, width, height, framerate, file);