Skip to content

Commit

Permalink
wrap: use nl_langinfo to get locale codeset
Browse files Browse the repository at this point in the history
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/%[email protected]%3E
Signed-off-by: Matěj Cepl <[email protected]>
Acked-by: Robin Jarry <[email protected]>
  • Loading branch information
mcepl authored and rjarry committed May 28, 2024
1 parent 3a97f68 commit d3288e6
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions filters/wrap.c
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit d3288e6

Please sign in to comment.