dch

dch / wat-ldns

Last active 2 hours ago

Like 0
Expires 4 months from now

locale-dependent output

Revision e8902c25047cad7255dbc992fda133f993ecfe42

wat.c Raw
1/*
2 * ldns_rdf2str() output depends on the caller's LC_CTYPE.
3 *
4 * cc -o wat wat.c $(ldns-config --cflags --libs) \
5 * -I$(brew --prefix openssl@3)/include
6 *
7 * LC_ALL=C ./wat
8 * LC_ALL=en_US.UTF-8 ./wat
9 */
10#include <ldns/ldns.h>
11#include <ctype.h>
12#include <locale.h>
13#include <stdio.h>
14
15int
16main(void)
17{
18 ldns_rr *rr = NULL;
19 const char *zone = "example.net. 300 IN TXT \"a\\255b\"";
20
21 /* the Erlang emulator does this on the way up; so do many others */
22 printf("LC_CTYPE=%-12s isprint(255)=%d\n", setlocale(LC_CTYPE, ""),
23 isprint(255));
24
25 if (ldns_rr_new_frm_str(&rr, zone, 0, NULL, NULL) != LDNS_STATUS_OK)
26 return 1;
27
28 char *str = ldns_rdf2str(ldns_rr_rdf(rr, 0));
29 printf("in %s\nout ", zone);
30 for (unsigned char *p = (unsigned char *)str; *p; p++)
31 printf("%02x ", *p);
32
33 for (unsigned char *p = (unsigned char *)str; *p; p++)
34 if (*p >= 0x80) {
35 printf("<- raw 0x%02x, expected \\%03u\n", *p, *p);
36 return 1;
37 }
38 printf("<- ASCII, ok\n");
39 return 0;
40}
41
wat.md Raw
$ uname -a
Darwin tessier.local 25.5.0 Darwin Kernel Version 25.5.0: Tue Jun  9 22:27:52 PDT 2026; root:xnu-12377.121.10~1/RELEASE_ARM64_T8112 arm64

$ sh
sh-3.2$ cc -o wat wat.c $(ldns-config --cflags --libs) -I$(brew --prefix openssl@3)/include
sh-3.2$ LC_ALL=C ./wat
LC_CTYPE=C            isprint(255)=0
in   example.net. 300 IN TXT "a\255b"
out  22 61 5c 32 35 35 62 22 <- ASCII, ok
sh-3.2$
LC_ALL=e
sh-3.2$ LC_ALL=en_US.UTF-8 ./wat
LC_CTYPE=en_US.UTF-8  isprint(255)=1
in   example.net. 300 IN TXT "a\255b"
out  22 61 ff 62 22 <- raw 0xff, expected \255