[PATCH] Parse the configuration file in C locale

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The configuration file is currently parsed in the locale set by the main
program using the library. This means that if the decimal separator
(defined in LC_NUMERIC) is not '.' the values in the compute lines are
truncated. This happens for example with sensors-applet and a French
locale.

The patch below is an attempt to fix that. There may be more clever ways
to do that, but currently I fail to find them.


Index: lib/init.c
===================================================================
--- lib/init.c	(r?vision 5168)
+++ lib/init.c	(copie de travail)
@@ -19,6 +19,7 @@
     MA 02110-1301 USA.
 */
 
+#include <locale.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <errno.h>
@@ -37,6 +38,7 @@
 int sensors_init(FILE *input)
 {
 	int res;
+	char *locale = NULL;
 
 	if (!sensors_init_sysfs())
 		return -SENSORS_ERR_KERNEL;
@@ -44,6 +46,13 @@
 	    (res = sensors_read_sysfs_chips()))
 		goto exit_cleanup;
 
+	/* Read the current locale */
+	locale = setlocale(LC_NUMERIC, NULL);
+	if (locale)
+		locale = strdup(locale);
+	/* Set the locale to C */
+	setlocale(LC_NUMERIC, "C");
+
 	res = -SENSORS_ERR_PARSE;
 	if (input) {
 		if (sensors_scanner_init(input) ||
@@ -64,11 +73,21 @@
 		}
 	}
 
+	/* Restore the old locale */
+	if (locale) {
+		setlocale(LC_NUMERIC, locale);
+		free(locale);
+	}
 	if ((res = sensors_substitute_busses()))
 		goto exit_cleanup;
 	return 0;
 
 exit_cleanup:
+	/* Restore the old locale */
+	if (locale) {
+		setlocale(LC_NUMERIC, locale);
+		free(locale);
+	}
 	sensors_cleanup();
 	return res;
 }

-- 
  .''`.  Aurelien Jarno	            | GPG: 1024D/F1BCDB73
 : :' :  Debian developer           | Electrical Engineer
 `. `'   aurel32 at debian.org         | aurelien at aurel32.net
   `-    people.debian.org/~aurel32 | www.aurel32.net




[Index of Archives]     [Linux Kernel]     [Linux Hardware Monitoring]     [Linux USB Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]

  Powered by Linux