Home | History | Annotate | Line # | Download | only in nls
catgets.c revision 1.10
      1  1.10  mikel /*	$NetBSD: catgets.c,v 1.10 1997/06/19 07:41:32 mikel 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.8    jtc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
     30   1.8    jtc  * 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.9    jtc #include <errno.h>
     42   1.8    jtc #include <stdlib.h>
     43   1.8    jtc #include <string.h>
     44   1.1    jtc #include <nl_types.h>
     45   1.1    jtc 
     46   1.4    cgd char *
     47   1.8    jtc _catgets(catd, set_id, msg_id, s)
     48   1.1    jtc 	nl_catd catd;
     49   1.1    jtc 	int set_id;
     50   1.1    jtc 	int msg_id;
     51   1.8    jtc 	const char *s;
     52   1.1    jtc {
     53   1.8    jtc 	struct _nls_cat_hdr *cat_hdr;
     54   1.8    jtc 	struct _nls_set_hdr *set_hdr;
     55   1.8    jtc 	struct _nls_msg_hdr *msg_hdr;
     56   1.8    jtc 	int l, u, i, r;
     57   1.8    jtc 
     58   1.9    jtc 	if (catd == (nl_catd) -1) {
     59   1.9    jtc 		errno = EBADF;
     60   1.8    jtc 		return (char *) s;
     61   1.8    jtc 	}
     62   1.8    jtc 
     63   1.8    jtc 	cat_hdr = (struct _nls_cat_hdr *) catd->__data;
     64  1.10  mikel 	set_hdr = (struct _nls_set_hdr *) ((char *) catd->__data
     65   1.8    jtc 		+ sizeof(struct _nls_cat_hdr));
     66   1.8    jtc 
     67   1.8    jtc 	/* binary search, see knuth algorithm b */
     68   1.8    jtc 	l = 0;
     69   1.8    jtc 	u = ntohl(cat_hdr->__nsets) - 1;
     70   1.8    jtc 	while (l <= u) {
     71   1.8    jtc 		i = (l + u) / 2;
     72   1.8    jtc 		r = set_id - ntohl(set_hdr[i].__setno);
     73   1.8    jtc 
     74   1.8    jtc 		if (r == 0) {
     75  1.10  mikel 			msg_hdr = (struct _nls_msg_hdr *) ((char *)catd->__data
     76   1.8    jtc 				+ sizeof(struct _nls_cat_hdr)
     77   1.8    jtc 				+ ntohl(cat_hdr->__msg_hdr_offset));
     78   1.8    jtc 
     79   1.8    jtc 			l = ntohl(set_hdr[i].__index);
     80   1.8    jtc 			u = l + ntohl(set_hdr[i].__nmsgs) - 1;
     81   1.8    jtc 			while (l <= u) {
     82   1.8    jtc 				i = (l + u) / 2;
     83   1.8    jtc 				r = msg_id - ntohl(msg_hdr[i].__msgno);
     84   1.8    jtc 				if (r == 0) {
     85  1.10  mikel 					return ((char *) catd->__data
     86   1.8    jtc 					    + sizeof(struct _nls_cat_hdr)
     87   1.8    jtc 					    + ntohl(cat_hdr->__msg_txt_offset)
     88   1.8    jtc 					    + ntohl(msg_hdr[i].__offset));
     89   1.8    jtc 				} else if (r < 0) {
     90   1.8    jtc 					u = i - 1;
     91   1.8    jtc 				} else {
     92   1.8    jtc 					l = i + 1;
     93   1.8    jtc 				}
     94   1.8    jtc 			}
     95   1.8    jtc 
     96   1.8    jtc 			/* not found */
     97   1.8    jtc 			return (char *) s;
     98   1.8    jtc 
     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.8    jtc 	return (char *) s;
    108   1.1    jtc }
    109