[PATCH libiio] iio-monitor: Set locale before parsing double values from sysfs

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

 



strtod() uses the current locale when parsing numbers, while all values
in sysfs use the "C" locale.  Hence the former may expect a comma as the
radix character, while the latter always uses a decimal point, leading
to the display of incorrect values.

Temporarily switch to the "C" locale before parsing double numbers to
fix this.

Signed-off-by: Geert Uytterhoeven <geert@xxxxxxxxxxxxxx>
---
 examples/iio-monitor.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/examples/iio-monitor.c b/examples/iio-monitor.c
index 8b632187829869ae..558c0f33a36ef7b9 100644
--- a/examples/iio-monitor.c
+++ b/examples/iio-monitor.c
@@ -21,6 +21,7 @@
 #define _DEFAULT_SOURCE
 
 #include <cdk/cdk.h>
+#include <locale.h>
 #include <pthread.h>
 #include <stdbool.h>
 #include <unistd.h>
@@ -61,9 +62,13 @@ static bool is_valid_channel(struct iio_channel *chn)
 
 static double get_channel_value(struct iio_channel *chn)
 {
+	char *old_locale;
 	char buf[1024];
 	double val;
 
+	old_locale = strdup(setlocale(LC_NUMERIC, NULL));
+	setlocale(LC_NUMERIC, "C");
+
 	if (channel_has_attr(chn, "input")) {
 		iio_channel_attr_read(chn, "input", buf, sizeof(buf));
 		val = strtod(buf, NULL);
@@ -82,6 +87,9 @@ static double get_channel_value(struct iio_channel *chn)
 		}
 	}
 
+	setlocale(LC_NUMERIC, old_locale);
+	free(old_locale);
+
 	return val / 1000.0;
 }
 
-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Input]     [Linux Kernel]     [Linux SCSI]     [X.org]

  Powered by Linux