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.
This commit is contained in:
Richard Grenville 2012-09-27 22:30:16 +08:00
parent 75ebe94bb6
commit 69513d6231
1 changed files with 10 additions and 0 deletions

View File

@ -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);