Home | History | Annotate | Line # | Download | only in nls
catgets.c revision 1.17
      1  1.17  christos /*	$NetBSD: catgets.c,v 1.17 2005/11/29 03:12:00 christos 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.16     lukem #include <sys/cdefs.h>
     40  1.16     lukem #if defined(LIBC_SCCS) && !defined(lint)
     41  1.17  christos __RCSID("$NetBSD: catgets.c,v 1.17 2005/11/29 03:12:00 christos Exp $");
     42  1.16     lukem #endif /* LIBC_SCCS and not lint */
     43  1.16     lukem 
     44   1.8       jtc #define _NLS_PRIVATE
     45   1.1       jtc 
     46  1.11  christos #include "namespace.h"
     47   1.9       jtc #include <errno.h>
     48   1.8       jtc #include <stdlib.h>
     49   1.8       jtc #include <string.h>
     50   1.1       jtc #include <nl_types.h>
     51  1.15   mycroft 
     52  1.15   mycroft #ifdef __weak_alias
     53  1.15   mycroft __weak_alias(catgets, _catgets)
     54  1.15   mycroft #endif
     55   1.1       jtc 
     56   1.4       cgd char *
     57   1.8       jtc _catgets(catd, set_id, msg_id, s)
     58   1.1       jtc 	nl_catd catd;
     59   1.1       jtc 	int set_id;
     60   1.1       jtc 	int msg_id;
     61   1.8       jtc 	const char *s;
     62   1.1       jtc {
     63   1.8       jtc 	struct _nls_cat_hdr *cat_hdr;
     64   1.8       jtc 	struct _nls_set_hdr *set_hdr;
     65   1.8       jtc 	struct _nls_msg_hdr *msg_hdr;
     66   1.8       jtc 	int l, u, i, r;
     67   1.8       jtc 
     68   1.9       jtc 	if (catd == (nl_catd) -1) {
     69   1.9       jtc 		errno = EBADF;
     70  1.17  christos 		return __UNCONST(s);
     71   1.8       jtc 	}
     72   1.8       jtc 
     73  1.14  christos 	cat_hdr = (struct _nls_cat_hdr *)catd->__data;
     74  1.14  christos 	set_hdr = (struct _nls_set_hdr *)(void *)((char *)catd->__data
     75   1.8       jtc 		+ sizeof(struct _nls_cat_hdr));
     76   1.8       jtc 
     77   1.8       jtc 	/* binary search, see knuth algorithm b */
     78   1.8       jtc 	l = 0;
     79  1.14  christos 	u = ntohl((u_int32_t)cat_hdr->__nsets) - 1;
     80   1.8       jtc 	while (l <= u) {
     81   1.8       jtc 		i = (l + u) / 2;
     82  1.14  christos 		r = set_id - ntohl((u_int32_t)set_hdr[i].__setno);
     83   1.8       jtc 
     84   1.8       jtc 		if (r == 0) {
     85  1.14  christos 			msg_hdr = (struct _nls_msg_hdr *)
     86  1.14  christos 			    (void *)((char *)catd->__data +
     87  1.14  christos 			    sizeof(struct _nls_cat_hdr) +
     88  1.14  christos 			    ntohl((u_int32_t)cat_hdr->__msg_hdr_offset));
     89   1.8       jtc 
     90  1.14  christos 			l = ntohl((u_int32_t)set_hdr[i].__index);
     91  1.14  christos 			u = l + ntohl((u_int32_t)set_hdr[i].__nmsgs) - 1;
     92   1.8       jtc 			while (l <= u) {
     93   1.8       jtc 				i = (l + u) / 2;
     94  1.14  christos 				r = msg_id -
     95  1.14  christos 				    ntohl((u_int32_t)msg_hdr[i].__msgno);
     96   1.8       jtc 				if (r == 0) {
     97  1.14  christos 					return ((char *) catd->__data +
     98  1.14  christos 					    sizeof(struct _nls_cat_hdr) +
     99  1.14  christos 					    ntohl((u_int32_t)
    100  1.14  christos 					    cat_hdr->__msg_txt_offset) +
    101  1.14  christos 					    ntohl((u_int32_t)
    102  1.14  christos 					    msg_hdr[i].__offset));
    103   1.8       jtc 				} else if (r < 0) {
    104   1.8       jtc 					u = i - 1;
    105   1.8       jtc 				} else {
    106   1.8       jtc 					l = i + 1;
    107   1.8       jtc 				}
    108   1.8       jtc 			}
    109   1.8       jtc 
    110   1.8       jtc 			/* not found */
    111  1.13    kleink 			goto notfound;
    112   1.8       jtc 
    113   1.8       jtc 		} else if (r < 0) {
    114   1.8       jtc 			u = i - 1;
    115   1.8       jtc 		} else {
    116   1.8       jtc 			l = i + 1;
    117   1.8       jtc 		}
    118   1.8       jtc 	}
    119   1.8       jtc 
    120  1.13    kleink notfound:
    121   1.8       jtc 	/* not found */
    122  1.13    kleink 	errno = ENOMSG;
    123  1.17  christos 	return __UNCONST(s);
    124   1.1       jtc }
    125