catgets.c revision 1.15 1 1.15 mycroft /* $NetBSD: catgets.c,v 1.15 1999/08/17 04:00:51 mycroft 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 * 3. All advertising materials mentioning features or use of this software
19 1.8 jtc * must display the following acknowledgement:
20 1.8 jtc * This product includes software developed by the NetBSD
21 1.8 jtc * Foundation, Inc. and its contributors.
22 1.8 jtc * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.8 jtc * contributors may be used to endorse or promote products derived
24 1.8 jtc * from this software without specific prior written permission.
25 1.8 jtc *
26 1.8 jtc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.8 jtc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.8 jtc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.12 jtc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.12 jtc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.8 jtc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.8 jtc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.8 jtc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.8 jtc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.8 jtc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.8 jtc * POSSIBILITY OF SUCH DAMAGE.
37 1.1 jtc */
38 1.1 jtc
39 1.8 jtc #define _NLS_PRIVATE
40 1.1 jtc
41 1.11 christos #include "namespace.h"
42 1.9 jtc #include <errno.h>
43 1.8 jtc #include <stdlib.h>
44 1.8 jtc #include <string.h>
45 1.1 jtc #include <nl_types.h>
46 1.15 mycroft
47 1.15 mycroft #ifdef __weak_alias
48 1.15 mycroft __weak_alias(catgets, _catgets)
49 1.15 mycroft #endif
50 1.1 jtc
51 1.4 cgd char *
52 1.8 jtc _catgets(catd, set_id, msg_id, s)
53 1.1 jtc nl_catd catd;
54 1.1 jtc int set_id;
55 1.1 jtc int msg_id;
56 1.8 jtc const char *s;
57 1.1 jtc {
58 1.8 jtc struct _nls_cat_hdr *cat_hdr;
59 1.8 jtc struct _nls_set_hdr *set_hdr;
60 1.8 jtc struct _nls_msg_hdr *msg_hdr;
61 1.8 jtc int l, u, i, r;
62 1.8 jtc
63 1.9 jtc if (catd == (nl_catd) -1) {
64 1.9 jtc errno = EBADF;
65 1.14 christos /* LINTED interface problem */
66 1.8 jtc return (char *) s;
67 1.8 jtc }
68 1.8 jtc
69 1.14 christos cat_hdr = (struct _nls_cat_hdr *)catd->__data;
70 1.14 christos set_hdr = (struct _nls_set_hdr *)(void *)((char *)catd->__data
71 1.8 jtc + sizeof(struct _nls_cat_hdr));
72 1.8 jtc
73 1.8 jtc /* binary search, see knuth algorithm b */
74 1.8 jtc l = 0;
75 1.14 christos u = ntohl((u_int32_t)cat_hdr->__nsets) - 1;
76 1.8 jtc while (l <= u) {
77 1.8 jtc i = (l + u) / 2;
78 1.14 christos r = set_id - ntohl((u_int32_t)set_hdr[i].__setno);
79 1.8 jtc
80 1.8 jtc if (r == 0) {
81 1.14 christos msg_hdr = (struct _nls_msg_hdr *)
82 1.14 christos (void *)((char *)catd->__data +
83 1.14 christos sizeof(struct _nls_cat_hdr) +
84 1.14 christos ntohl((u_int32_t)cat_hdr->__msg_hdr_offset));
85 1.8 jtc
86 1.14 christos l = ntohl((u_int32_t)set_hdr[i].__index);
87 1.14 christos u = l + ntohl((u_int32_t)set_hdr[i].__nmsgs) - 1;
88 1.8 jtc while (l <= u) {
89 1.8 jtc i = (l + u) / 2;
90 1.14 christos r = msg_id -
91 1.14 christos ntohl((u_int32_t)msg_hdr[i].__msgno);
92 1.8 jtc if (r == 0) {
93 1.14 christos return ((char *) catd->__data +
94 1.14 christos sizeof(struct _nls_cat_hdr) +
95 1.14 christos ntohl((u_int32_t)
96 1.14 christos cat_hdr->__msg_txt_offset) +
97 1.14 christos ntohl((u_int32_t)
98 1.14 christos msg_hdr[i].__offset));
99 1.8 jtc } else if (r < 0) {
100 1.8 jtc u = i - 1;
101 1.8 jtc } else {
102 1.8 jtc l = i + 1;
103 1.8 jtc }
104 1.8 jtc }
105 1.8 jtc
106 1.8 jtc /* not found */
107 1.13 kleink goto notfound;
108 1.8 jtc
109 1.8 jtc } else if (r < 0) {
110 1.8 jtc u = i - 1;
111 1.8 jtc } else {
112 1.8 jtc l = i + 1;
113 1.8 jtc }
114 1.8 jtc }
115 1.8 jtc
116 1.13 kleink notfound:
117 1.8 jtc /* not found */
118 1.13 kleink errno = ENOMSG;
119 1.14 christos /* LINTED interface problem */
120 1.8 jtc return (char *) s;
121 1.1 jtc }
122