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