Skip to content

Commit b35405b

Browse files
committed
nvme-print: fix to use nl_langinfo() instead of setlocale()
Remove the hard coded implementation to check the fahrenheit print. Signed-off-by: Tokunori Ikegami <[email protected]>
1 parent 717a4cd commit b35405b

3 files changed

Lines changed: 14 additions & 42 deletions

File tree

nvme-print.c

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <stdlib.h>
77
#include <time.h>
88
#include <sys/stat.h>
9-
#include <locale.h>
109

1110
#include "nvme.h"
1211
#include "libnvme.h"
@@ -815,46 +814,6 @@ void nvme_show_endurance_log(struct nvme_endurance_group_log *endurance_log,
815814
nvme_print(endurance_log, flags, endurance_log, group_id, devname);
816815
}
817816

818-
static bool is_fahrenheit_country(const char *country)
819-
{
820-
static const char * const countries[] = {
821-
"AQ", "AS", "BS", "BZ", "CY", "FM", "GU", "KN", "KY", "LR",
822-
"MH", "MP", "MS", "PR", "PW", "TC", "US", "VG", "VI"
823-
};
824-
int i;
825-
826-
for (i = 0; i < ARRAY_SIZE(countries); i++) {
827-
if (!strcmp(country, countries[i]))
828-
return true;
829-
}
830-
831-
return false;
832-
}
833-
834-
#ifndef LC_MEASUREMENT
835-
#define LC_MEASUREMENT LC_ALL
836-
#endif
837-
838-
static bool is_temperature_fahrenheit(void)
839-
{
840-
const char *locale, *underscore;
841-
char country[3] = { 0 };
842-
843-
setlocale(LC_MEASUREMENT, "");
844-
locale = setlocale(LC_MEASUREMENT, NULL);
845-
846-
if (!locale || strlen(locale) < 2)
847-
return false;
848-
849-
underscore = strchr(locale, '_');
850-
if (underscore && strlen(underscore) >= 3)
851-
locale = underscore + 1;
852-
853-
memcpy(country, locale, 2);
854-
855-
return is_fahrenheit_country(country);
856-
}
857-
858817
const char *nvme_degrees_string(long t)
859818
{
860819
static char str[STR_LEN];
@@ -873,7 +832,7 @@ const char *nvme_degrees_fahrenheit_string(long t)
873832
static char str[STR_LEN];
874833
long val = kelvin_to_fahrenheit(t);
875834

876-
if (!is_temperature_fahrenheit())
835+
if (!nvme_cfg.fahrenheit)
877836
return NULL;
878837

879838
if (nvme_is_output_format_json())

nvme.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <dirent.h>
4343
#include <libgen.h>
4444
#include <signal.h>
45+
#include <langinfo.h>
4546

4647
#include <linux/fs.h>
4748

@@ -11125,6 +11126,16 @@ void register_extension(struct plugin *plugin)
1112511126
nvme.extensions->tail = plugin;
1112611127
}
1112711128

11129+
static bool is_temperature_fahrenheit(void)
11130+
{
11131+
const char *m = nl_langinfo(_NL_MEASUREMENT_MEASUREMENT);
11132+
11133+
if (m && m[0] == 2)
11134+
return true;
11135+
11136+
return false;
11137+
}
11138+
1112811139
int main(int argc, char **argv)
1112911140
{
1113011141
int err;
@@ -11135,6 +11146,7 @@ int main(int argc, char **argv)
1113511146
return 0;
1113611147
}
1113711148
setlocale(LC_ALL, "");
11149+
nvme_cfg.fahrenheit = is_temperature_fahrenheit();
1113811150

1113911151
err = nvme_install_sigint_handler();
1114011152
if (err)

nvme.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ struct nvme_config {
8383
bool dry_run;
8484
bool no_retries;
8585
unsigned int output_format_ver;
86+
bool fahrenheit;
8687
};
8788

8889
/*

0 commit comments

Comments
 (0)