diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2024-05-28 14:51:31 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2024-05-28 23:52:33 +0200 |
commit | d3288e6359744ad0383d08270aea74f4497c974d (patch) | |
tree | 4a4a168c1f0a95e39dac43f04ac186e678c14071 /filters | |
parent | 3a97f68a31320bf0bac0d2d703fdc140692861c3 (diff) | |
download | aerc-d3288e6359744ad0383d08270aea74f4497c974d.tar.gz |
wrap: use nl_langinfo to get locale codeset
Using more and more complicated constructs comparing locale strings
doesn't lead to anywhere. Even with the current two steps long
comparison the program is wrong on systems which use "utf8" in their
locale names (e.g., my openSUSE/Tumbleweed). It is better to use proper
locale functions to get canonical name of the current codeset.
Link: https://lists.sr.ht/~rjarry/aerc-devel/%3CD1L7746XIJ3Z.3SN11SOVO175Q@cepl.eu%3E
Signed-off-by: Matěj Cepl <mcepl@cepl.eu>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'filters')
-rw-r--r-- | filters/wrap.c | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/filters/wrap.c b/filters/wrap.c index 59c44522..4ae7c72d 100644 --- a/filters/wrap.c +++ b/filters/wrap.c @@ -1,9 +1,10 @@ /* SPDX-License-Identifier: MIT */ /* Copyright (c) 2023 Robin Jarry */ -#define _XOPEN_SOURCE +#define _XOPEN_SOURCE 700 #include <errno.h> #include <getopt.h> +#include <langinfo.h> #include <locale.h> #include <regex.h> #include <stdbool.h> @@ -446,7 +447,10 @@ static int set_stdio_encoding(void) } /* aerc will always send UTF-8 text, ensure that we read that properly */ - if (!strstr(locale, "UTF-8") && !strstr(locale, "utf-8")) { + locale_t loc = newlocale(LC_ALL_MASK, locale, NULL); + char *codeset = nl_langinfo_l(CODESET, loc); + freelocale(loc); + if (!strstr(codeset, "UTF-8")) { fprintf(stderr, "error: locale '%s' is not UTF-8\n", locale); return 1; } |