catgets.c revision 1.19 1 1.19 abs /* $NetBSD: catgets.c,v 1.19 2012/06/25 22:32:45 abs Exp $ */
2 1.6 cgd
3 1.8 jtc /*-
4 1.8 jtc * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 1.8 jtc * All rights reserved.
6 1.8 jtc *
7 1.8 jtc * This code is derived from software contributed to The NetBSD Foundation
8 1.8 jtc * by J.T. Conklin.
9 1.8 jtc *
10 1.8 jtc * Redistribution and use in source and binary forms, with or without
11 1.8 jtc * modification, are permitted provided that the following conditions
12 1.8 jtc * are met:
13 1.8 jtc * 1. Redistributions of source code must retain the above copyright
14 1.8 jtc * notice, this list of conditions and the following disclaimer.
15 1.8 jtc * 2. Redistributions in binary form must reproduce the above copyright
16 1.8 jtc * notice, this list of conditions and the following disclaimer in the
17 1.8 jtc * documentation and/or other materials provided with the distribution.
18 1.8 jtc *
19 1.8 jtc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.8 jtc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.8 jtc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.12 jtc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.12 jtc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.8 jtc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.8 jtc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.8 jtc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.8 jtc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.8 jtc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.8 jtc * POSSIBILITY OF SUCH DAMAGE.
30 1.1 jtc */
31 1.1 jtc
32 1.16 lukem #include <sys/cdefs.h>
33 1.16 lukem #if defined(LIBC_SCCS) && !defined(lint)
34 1.19 abs __RCSID("$NetBSD: catgets.c,v 1.19 2012/06/25 22:32:45 abs Exp $");
35 1.16 lukem #endif /* LIBC_SCCS and not lint */
36 1.16 lukem
37 1.8 jtc #define _NLS_PRIVATE
38 1.1 jtc
39 1.11 christos #include "namespace.h"
40 1.9 jtc #include <errno.h>
41 1.8 jtc #include <stdlib.h>
42 1.8 jtc #include <string.h>
43 1.1 jtc #include <nl_types.h>
44 1.15 mycroft
45 1.15 mycroft #ifdef __weak_alias
46 1.15 mycroft __weak_alias(catgets, _catgets)
47 1.15 mycroft #endif
48 1.1 jtc
49 1.4 cgd char *
50 1.19 abs _catgets(nl_catd catd, int set_id, int msg_id, const char *s)
51 1.1 jtc {
52 1.8 jtc struct _nls_cat_hdr *cat_hdr;
53 1.8 jtc struct _nls_set_hdr *set_hdr;
54 1.8 jtc struct _nls_msg_hdr *msg_hdr;
55 1.8 jtc int l, u, i, r;
56 1.8 jtc
57 1.9 jtc if (catd == (nl_catd) -1) {
58 1.9 jtc errno = EBADF;
59 1.17 christos return __UNCONST(s);
60 1.8 jtc }
61 1.8 jtc
62 1.14 christos cat_hdr = (struct _nls_cat_hdr *)catd->__data;
63 1.14 christos set_hdr = (struct _nls_set_hdr *)(void *)((char *)catd->__data
64 1.8 jtc + sizeof(struct _nls_cat_hdr));
65 1.8 jtc
66 1.8 jtc /* binary search, see knuth algorithm b */
67 1.8 jtc l = 0;
68 1.14 christos u = ntohl((u_int32_t)cat_hdr->__nsets) - 1;
69 1.8 jtc while (l <= u) {
70 1.8 jtc i = (l + u) / 2;
71 1.14 christos r = set_id - ntohl((u_int32_t)set_hdr[i].__setno);
72 1.8 jtc
73 1.8 jtc if (r == 0) {
74 1.14 christos msg_hdr = (struct _nls_msg_hdr *)
75 1.14 christos (void *)((char *)catd->__data +
76 1.14 christos sizeof(struct _nls_cat_hdr) +
77 1.14 christos ntohl((u_int32_t)cat_hdr->__msg_hdr_offset));
78 1.8 jtc
79 1.14 christos l = ntohl((u_int32_t)set_hdr[i].__index);
80 1.14 christos u = l + ntohl((u_int32_t)set_hdr[i].__nmsgs) - 1;
81 1.8 jtc while (l <= u) {
82 1.8 jtc i = (l + u) / 2;
83 1.14 christos r = msg_id -
84 1.14 christos ntohl((u_int32_t)msg_hdr[i].__msgno);
85 1.8 jtc if (r == 0) {
86 1.14 christos return ((char *) catd->__data +
87 1.14 christos sizeof(struct _nls_cat_hdr) +
88 1.14 christos ntohl((u_int32_t)
89 1.14 christos cat_hdr->__msg_txt_offset) +
90 1.14 christos ntohl((u_int32_t)
91 1.14 christos msg_hdr[i].__offset));
92 1.8 jtc } else if (r < 0) {
93 1.8 jtc u = i - 1;
94 1.8 jtc } else {
95 1.8 jtc l = i + 1;
96 1.8 jtc }
97 1.8 jtc }
98 1.8 jtc
99 1.8 jtc /* not found */
100 1.13 kleink goto notfound;
101 1.8 jtc
102 1.8 jtc } else if (r < 0) {
103 1.8 jtc u = i - 1;
104 1.8 jtc } else {
105 1.8 jtc l = i + 1;
106 1.8 jtc }
107 1.8 jtc }
108 1.8 jtc
109 1.13 kleink notfound:
110 1.8 jtc /* not found */
111 1.13 kleink errno = ENOMSG;
112 1.17 christos return __UNCONST(s);
113 1.1 jtc }
114