From 69513d62313efdb5150ef5061fb037b33a825c88 Mon Sep 17 00:00:00 2001 From: Richard Grenville Date: Thu, 27 Sep 2012 22:30:16 +0800 Subject: [PATCH] Bug fix: Issue #47: Locale cause problems with float arguments A locale using comma instead of dot in floating point values causes unexpected behavior when parsing floating point commandline arguments. This commits enforces LC_NUMERIC="C" during commandline argument parsing. --- src/compton.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/compton.c b/src/compton.c index 0b6f2a0..b9c3e96 100644 --- a/src/compton.c +++ b/src/compton.c @@ -3229,6 +3229,7 @@ get_cfg(int argc, char *const *argv) { Bool shadow_enable = False, fading_enable = False; int o, longopt_idx, i; char *config_file = NULL; + char *lc_numeric_old = mstrcpy(setlocale(LC_NUMERIC, NULL)); for (i = 0; i < NUM_WINTYPES; ++i) { opts.wintype_fade[i] = False; @@ -3251,6 +3252,11 @@ get_cfg(int argc, char *const *argv) { #endif // Parse commandline arguments. Range checking will be done later. + + // Enforce LC_NUMERIC locale "C" here to make sure dots are recognized + // instead of commas in atof(). + setlocale(LC_NUMERIC, "C"); + optind = 1; while (-1 != (o = getopt_long(argc, argv, shortopts, longopts, &longopt_idx))) { @@ -3363,6 +3369,10 @@ get_cfg(int argc, char *const *argv) { } } + // Restore LC_NUMERIC + setlocale(LC_NUMERIC, lc_numeric_old); + free(lc_numeric_old); + // Range checking and option assignments opts.fade_delta = max_i(opts.fade_delta, 1); opts.shadow_radius = max_i(opts.shadow_radius, 1);