sys/stat.h: fix typo in statx member name stx_dio_offset_align
[musl.git] / src / locale / catgets.c
blob71c31c1d6d0105a074fa894fd00434e39094fc57
1 #define _BSD_SOURCE
2 #include <nl_types.h>
3 #include <endian.h>
4 #include <stdlib.h>
5 #include <stdint.h>
6 #include <errno.h>
8 #define V(p) be32toh(*(uint32_t *)(p))
10 static int cmp(const void *a, const void *b)
12 uint32_t x = V(a), y = V(b);
13 return x<y ? -1 : x>y ? 1 : 0;
16 char *catgets (nl_catd catd, int set_id, int msg_id, const char *s)
18 const char *map = (const char *)catd;
19 uint32_t nsets = V(map+4);
20 const char *sets = map+20;
21 const char *msgs = map+20+V(map+12);
22 const char *strings = map+20+V(map+16);
23 uint32_t set_id_be = htobe32(set_id);
24 uint32_t msg_id_be = htobe32(msg_id);
25 const char *set = bsearch(&set_id_be, sets, nsets, 12, cmp);
26 if (!set) {
27 errno = ENOMSG;
28 return (char *)s;
30 uint32_t nmsgs = V(set+4);
31 msgs += 12*V(set+8);
32 const char *msg = bsearch(&msg_id_be, msgs, nmsgs, 12, cmp);
33 if (!msg) {
34 errno = ENOMSG;
35 return (char *)s;
37 return (char *)(strings + V(msg+8));