Home | History | Annotate | Line # | Download | only in libintl
textdomain.c revision 1.9
      1 /*	$NetBSD: textdomain.c,v 1.9 2004/01/05 19:21:00 itojun Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000, 2001 Citrus Project,
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __RCSID("$NetBSD: textdomain.c,v 1.9 2004/01/05 19:21:00 itojun Exp $");
     31 
     32 #include <sys/param.h>
     33 
     34 #include <stdio.h>
     35 #include <string.h>
     36 #include <stdlib.h>
     37 #include <libintl.h>
     38 #include "libintl_local.h"
     39 #include "pathnames.h"
     40 
     41 static struct domainbinding __default_binding = {
     42 	NULL, DEFAULT_DOMAINNAME, _PATH_TEXTDOMAIN,
     43 };
     44 struct domainbinding *__bindings = &__default_binding;
     45 char __current_domainname[PATH_MAX] = DEFAULT_DOMAINNAME;
     46 
     47 /*
     48  * set the default domainname for dcngettext() and friends.
     49  */
     50 char *
     51 textdomain(domainname)
     52 	const char *domainname;
     53 {
     54 
     55 	/* NULL pointer gives the current setting */
     56 	if (!domainname)
     57 		return __current_domainname;
     58 
     59 	/* empty string sets the value back to the default */
     60 	if (!*domainname) {
     61 		strlcpy(__current_domainname, DEFAULT_DOMAINNAME,
     62 		    sizeof(__current_domainname));
     63 	} else {
     64 		strlcpy(__current_domainname, domainname,
     65 		    sizeof(__current_domainname));
     66 	}
     67 	return __current_domainname;
     68 }
     69 
     70 char *
     71 bindtextdomain(domainname, dirname)
     72 	const char *domainname;
     73 	const char *dirname;
     74 {
     75 	struct domainbinding *p;
     76 
     77 	/* NULL pointer or empty string returns NULL with no operation */
     78 	if (!domainname || !*domainname)
     79 		return NULL;
     80 
     81 	if (dirname && (strlen(dirname) + 1 > sizeof(p->path)))
     82 		return NULL;
     83 
     84 #if 0
     85 	/* disallow relative path */
     86 	if (dirname[0] != '/')
     87 		return NULL;
     88 #endif
     89 
     90 	if (strlen(domainname) + 1 > sizeof(p->domainname))
     91 		return NULL;
     92 
     93 	/* lookup binding for the domainname */
     94 	for (p = __bindings; p; p = p->next)
     95 		if (strcmp(p->domainname, domainname) == 0)
     96 			break;
     97 
     98 	if (!dirname) {
     99 		if (p)
    100 			return (p->path);
    101 		else
    102 			return _PATH_TEXTDOMAIN;
    103 	}
    104 
    105 	if (!p) {
    106 		p = (struct domainbinding *)malloc(sizeof(*p));
    107 		if (!p)
    108 			return NULL;
    109 		memset(p, 0, sizeof(*p));
    110 		p->next = __bindings;
    111 		strlcpy(p->domainname, domainname, sizeof(p->domainname));
    112 		__bindings = p;
    113 	}
    114 
    115 	strlcpy(p->path, dirname, sizeof(p->path));
    116 	p->mohandle.mo.mo_magic = 0; /* invalidate current mapping */
    117 
    118 	return (p->path);
    119 }
    120 
    121 /* ARGSUSED */
    122 char *
    123 bind_textdomain_codeset(domainname, codeset)
    124 	const char *domainname;
    125 	const char *codeset;
    126 {
    127 
    128 	/* yet to be done - always fail */
    129 	return NULL;
    130 }
    131