xref: /minix/lib/libc/nls/catgets.c (revision f14fb602)
1*f14fb602SLionel Sambuc /*	$NetBSD: catgets.c,v 1.19 2012/06/25 22:32:45 abs Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras  * Copyright (c) 1996 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras  * All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * This code is derived from software contributed to The NetBSD Foundation
82fe8fb19SBen Gras  * by J.T. Conklin.
92fe8fb19SBen Gras  *
102fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras  * are met:
132fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras  *
192fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
202fe8fb19SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
212fe8fb19SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
222fe8fb19SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
232fe8fb19SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242fe8fb19SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
252fe8fb19SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
262fe8fb19SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
272fe8fb19SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
282fe8fb19SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292fe8fb19SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
302fe8fb19SBen Gras  */
312fe8fb19SBen Gras 
322fe8fb19SBen Gras #include <sys/cdefs.h>
332fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
34*f14fb602SLionel Sambuc __RCSID("$NetBSD: catgets.c,v 1.19 2012/06/25 22:32:45 abs Exp $");
352fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
362fe8fb19SBen Gras 
372fe8fb19SBen Gras #define _NLS_PRIVATE
382fe8fb19SBen Gras 
392fe8fb19SBen Gras #include "namespace.h"
402fe8fb19SBen Gras #include <errno.h>
412fe8fb19SBen Gras #include <stdlib.h>
422fe8fb19SBen Gras #include <string.h>
432fe8fb19SBen Gras #include <nl_types.h>
442fe8fb19SBen Gras 
452fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(catgets,_catgets)462fe8fb19SBen Gras __weak_alias(catgets, _catgets)
472fe8fb19SBen Gras #endif
482fe8fb19SBen Gras 
492fe8fb19SBen Gras char *
50*f14fb602SLionel Sambuc _catgets(nl_catd catd, int set_id, int msg_id, const char *s)
512fe8fb19SBen Gras {
522fe8fb19SBen Gras 	struct _nls_cat_hdr *cat_hdr;
532fe8fb19SBen Gras 	struct _nls_set_hdr *set_hdr;
542fe8fb19SBen Gras 	struct _nls_msg_hdr *msg_hdr;
552fe8fb19SBen Gras 	int l, u, i, r;
562fe8fb19SBen Gras 
572fe8fb19SBen Gras 	if (catd == (nl_catd) -1) {
582fe8fb19SBen Gras 		errno = EBADF;
592fe8fb19SBen Gras 		return __UNCONST(s);
602fe8fb19SBen Gras 	}
612fe8fb19SBen Gras 
622fe8fb19SBen Gras 	cat_hdr = (struct _nls_cat_hdr *)catd->__data;
632fe8fb19SBen Gras 	set_hdr = (struct _nls_set_hdr *)(void *)((char *)catd->__data
642fe8fb19SBen Gras 		+ sizeof(struct _nls_cat_hdr));
652fe8fb19SBen Gras 
662fe8fb19SBen Gras 	/* binary search, see knuth algorithm b */
672fe8fb19SBen Gras 	l = 0;
682fe8fb19SBen Gras 	u = ntohl((u_int32_t)cat_hdr->__nsets) - 1;
692fe8fb19SBen Gras 	while (l <= u) {
702fe8fb19SBen Gras 		i = (l + u) / 2;
712fe8fb19SBen Gras 		r = set_id - ntohl((u_int32_t)set_hdr[i].__setno);
722fe8fb19SBen Gras 
732fe8fb19SBen Gras 		if (r == 0) {
742fe8fb19SBen Gras 			msg_hdr = (struct _nls_msg_hdr *)
752fe8fb19SBen Gras 			    (void *)((char *)catd->__data +
762fe8fb19SBen Gras 			    sizeof(struct _nls_cat_hdr) +
772fe8fb19SBen Gras 			    ntohl((u_int32_t)cat_hdr->__msg_hdr_offset));
782fe8fb19SBen Gras 
792fe8fb19SBen Gras 			l = ntohl((u_int32_t)set_hdr[i].__index);
802fe8fb19SBen Gras 			u = l + ntohl((u_int32_t)set_hdr[i].__nmsgs) - 1;
812fe8fb19SBen Gras 			while (l <= u) {
822fe8fb19SBen Gras 				i = (l + u) / 2;
832fe8fb19SBen Gras 				r = msg_id -
842fe8fb19SBen Gras 				    ntohl((u_int32_t)msg_hdr[i].__msgno);
852fe8fb19SBen Gras 				if (r == 0) {
862fe8fb19SBen Gras 					return ((char *) catd->__data +
872fe8fb19SBen Gras 					    sizeof(struct _nls_cat_hdr) +
882fe8fb19SBen Gras 					    ntohl((u_int32_t)
892fe8fb19SBen Gras 					    cat_hdr->__msg_txt_offset) +
902fe8fb19SBen Gras 					    ntohl((u_int32_t)
912fe8fb19SBen Gras 					    msg_hdr[i].__offset));
922fe8fb19SBen Gras 				} else if (r < 0) {
932fe8fb19SBen Gras 					u = i - 1;
942fe8fb19SBen Gras 				} else {
952fe8fb19SBen Gras 					l = i + 1;
962fe8fb19SBen Gras 				}
972fe8fb19SBen Gras 			}
982fe8fb19SBen Gras 
992fe8fb19SBen Gras 			/* not found */
1002fe8fb19SBen Gras 			goto notfound;
1012fe8fb19SBen Gras 
1022fe8fb19SBen Gras 		} else if (r < 0) {
1032fe8fb19SBen Gras 			u = i - 1;
1042fe8fb19SBen Gras 		} else {
1052fe8fb19SBen Gras 			l = i + 1;
1062fe8fb19SBen Gras 		}
1072fe8fb19SBen Gras 	}
1082fe8fb19SBen Gras 
1092fe8fb19SBen Gras notfound:
1102fe8fb19SBen Gras 	/* not found */
1112fe8fb19SBen Gras 	errno = ENOMSG;
1122fe8fb19SBen Gras 	return __UNCONST(s);
1132fe8fb19SBen Gras }
114