]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme-print: check locale to use temperatures in degrees fahrenheit
authorTokunori Ikegami <ikegami.t@gmail.com>
Thu, 4 Jul 2024 16:55:45 +0000 (01:55 +0900)
committerDaniel Wagner <wagi@monom.org>
Tue, 9 Jul 2024 10:04:14 +0000 (12:04 +0200)
Delete the fahrenheit option separately.

Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
nvme-print-stdout.c
nvme-print.c
nvme-print.h

index 4204a1d58573cc1f042d122629633dc08ad2c77c..a14239b98691517308ed301bc1ce9712c2b26a90 100644 (file)
@@ -3883,7 +3883,6 @@ static void stdout_smart_log(struct nvme_smart_log *smart, unsigned int nsid, co
        __u16 temperature = smart->temperature[1] << 8 | smart->temperature[0];
        int i;
        bool human = stdout_print_ops.flags & VERBOSE;
-       bool fahrenheit = !!(stdout_print_ops.flags & FAHRENHEIT);
 
        printf("Smart Log for NVME device:%s namespace-id:%x\n", devname, nsid);
        printf("critical_warning                        : %#x\n", smart->critical_warning);
@@ -3904,7 +3903,7 @@ static void stdout_smart_log(struct nvme_smart_log *smart, unsigned int nsid, co
        }
 
        printf("temperature                             : %s (%u K)\n",
-              nvme_degrees_string(temperature, fahrenheit), temperature);
+              nvme_degrees_string(temperature), temperature);
        printf("available_spare                         : %u%%\n", smart->avail_spare);
        printf("available_spare_threshold               : %u%%\n", smart->spare_thresh);
        printf("percentage_used                         : %u%%\n", smart->percent_used);
@@ -3941,7 +3940,7 @@ static void stdout_smart_log(struct nvme_smart_log *smart, unsigned int nsid, co
                if (!temperature)
                        continue;
                printf("Temperature Sensor %d                   : %s (%u K)\n", i + 1,
-                      nvme_degrees_string(temperature, fahrenheit), temperature);
+                      nvme_degrees_string(temperature), temperature);
        }
 
        printf("Thermal Management T1 Trans Count       : %u\n",
index a3b8ddf34c1682d18e5f5d20177aa9ae8d1ca166..f2b9988a85d7e01ec127a9c8b04b114508b77f84 100644 (file)
@@ -6,6 +6,7 @@
 #include <stdlib.h>
 #include <time.h>
 #include <sys/stat.h>
+#include <locale.h>
 
 #include "nvme.h"
 #include "libnvme.h"
@@ -767,10 +768,47 @@ void nvme_show_endurance_log(struct nvme_endurance_group_log *endurance_log,
        nvme_print(endurance_log, flags, endurance_log, group_id, devname);
 }
 
-const char *nvme_degrees_string(long t, bool fahrenheit)
+static bool is_fahrenheit_country(const char *country)
+{
+       static const char * const countries[] = {
+               "AQ", "AS", "BS", "BZ", "CY", "FM", "GU", "KN", "KY", "LR",
+               "MH", "MP", "MS", "PR", "PW", "TC", "US", "VG", "VI"
+       };
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(countries); i++) {
+               if (!strcmp(country, countries[i]))
+                       return true;
+       }
+
+       return false;
+}
+
+static bool is_temperature_fahrenheit(void)
+{
+       const char *locale, *underscore;
+       char country[3] = { 0 };
+
+       setlocale(LC_ALL, "");
+       locale = setlocale(LC_ALL, NULL);
+
+       if (!locale || strlen(locale) < 2)
+               return false;
+
+       underscore = strchr(locale, '_');
+       if (underscore && strlen(underscore) >= 3)
+               locale = underscore + 1;
+
+       memcpy(country, locale, 2);
+
+       return is_fahrenheit_country(country);
+}
+
+const char *nvme_degrees_string(long t)
 {
        static char str[STR_LEN];
        long val = kelvin_to_celsius(t);
+       bool fahrenheit = is_temperature_fahrenheit();
 
        if (fahrenheit)
                val = kelvin_to_fahrenheit(t);
index 11928af60e7e40c98acc06d35025b0e3b345a7a3..a3943ccd98bbc472e5912538097ad7c179385f49 100644 (file)
@@ -322,5 +322,5 @@ void nvme_show_finish(void);
 bool nvme_is_fabrics_reg(int offset);
 bool nvme_registers_cmbloc_support(__u32 cmbsz);
 bool nvme_registers_pmrctl_ready(__u32 pmrctl);
-const char *nvme_degrees_string(long t, bool fahrenheit);
+const char *nvme_degrees_string(long t);
 #endif /* NVME_PRINT_H */