[BACK]Return to textdomain.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libintl

Annotation of src/lib/libintl/textdomain.c, Revision 1.12

1.12    ! junyoung    1: /*     $NetBSD: textdomain.c,v 1.11 2004/09/23 16:44:26 tshiozak Exp $ */
1.1       itojun      2:
                      3: /*-
1.5       minoura     4:  * Copyright (c) 2000, 2001 Citrus Project,
1.1       itojun      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>
1.12    ! junyoung   30: __RCSID("$NetBSD: textdomain.c,v 1.11 2004/09/23 16:44:26 tshiozak Exp $");
1.1       itojun     31:
                     32: #include <sys/param.h>
                     33:
                     34: #include <stdio.h>
                     35: #include <string.h>
1.4       itojun     36: #include <stdlib.h>
1.1       itojun     37: #include <libintl.h>
                     38: #include "libintl_local.h"
                     39: #include "pathnames.h"
                     40:
1.5       minoura    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;
1.1       itojun     46:
1.12    ! junyoung   47: static struct domainbinding *domainbinding_lookup(const char *, int);
1.10      yamt       48:
1.1       itojun     49: /*
                     50:  * set the default domainname for dcngettext() and friends.
                     51:  */
                     52: char *
1.12    ! junyoung   53: textdomain(const char *domainname)
1.1       itojun     54: {
                     55:
1.2       itojun     56:        /* NULL pointer gives the current setting */
1.4       itojun     57:        if (!domainname)
1.5       minoura    58:                return __current_domainname;
1.2       itojun     59:
                     60:        /* empty string sets the value back to the default */
1.4       itojun     61:        if (!*domainname) {
1.5       minoura    62:                strlcpy(__current_domainname, DEFAULT_DOMAINNAME,
                     63:                    sizeof(__current_domainname));
1.4       itojun     64:        } else {
1.5       minoura    65:                strlcpy(__current_domainname, domainname,
                     66:                    sizeof(__current_domainname));
1.4       itojun     67:        }
1.5       minoura    68:        return __current_domainname;
1.1       itojun     69: }
                     70:
                     71: char *
1.12    ! junyoung   72: bindtextdomain(const char *domainname, const char *dirname)
1.1       itojun     73: {
1.4       itojun     74:        struct domainbinding *p;
1.2       itojun     75:
                     76:        /* NULL pointer or empty string returns NULL with no operation */
                     77:        if (!domainname || !*domainname)
                     78:                return NULL;
1.1       itojun     79:
1.8       drochner   80:        if (dirname && (strlen(dirname) + 1 > sizeof(p->path)))
1.1       itojun     81:                return NULL;
1.6       yamt       82:
                     83: #if 0
1.1       itojun     84:        /* disallow relative path */
                     85:        if (dirname[0] != '/')
                     86:                return NULL;
1.6       yamt       87: #endif
1.1       itojun     88:
1.4       itojun     89:        if (strlen(domainname) + 1 > sizeof(p->domainname))
1.1       itojun     90:                return NULL;
                     91:
1.10      yamt       92:        p = domainbinding_lookup(domainname, (dirname != NULL));
1.8       drochner   93:
                     94:        if (!dirname) {
                     95:                if (p)
                     96:                        return (p->path);
                     97:                else
1.11      tshiozak   98:                        return (char *)__UNCONST(_PATH_TEXTDOMAIN);
1.8       drochner   99:        }
                    100:
1.4       itojun    101:        strlcpy(p->path, dirname, sizeof(p->path));
1.5       minoura   102:        p->mohandle.mo.mo_magic = 0; /* invalidate current mapping */
1.1       itojun    103:
1.8       drochner  104:        return (p->path);
1.1       itojun    105: }
                    106:
                    107: char *
1.12    ! junyoung  108: bind_textdomain_codeset(const char *domainname, const char *codeset)
1.1       itojun    109: {
1.10      yamt      110:        struct domainbinding *p;
                    111:
                    112:        p = domainbinding_lookup(domainname, (codeset != NULL));
                    113:        if (p == NULL)
                    114:                return NULL;
                    115:
                    116:        if (codeset) {
                    117:                if (p->codeset)
                    118:                        free(p->codeset);
                    119:                p->codeset = strdup(codeset);
                    120:        }
                    121:
                    122:        return p->codeset;
                    123: }
                    124:
                    125: /*
                    126:  * lookup binding for the domainname
                    127:  */
                    128: static struct domainbinding *
1.12    ! junyoung  129: domainbinding_lookup(const char *domainname, int alloc)
1.10      yamt      130: {
                    131:        struct domainbinding *p;
                    132:
                    133:        for (p = __bindings; p; p = p->next)
                    134:                if (strcmp(p->domainname, domainname) == 0)
                    135:                        break;
                    136:
                    137:        if (!p && alloc) {
                    138:                p = (struct domainbinding *)malloc(sizeof(*p));
                    139:                if (!p)
                    140:                        return NULL;
                    141:                memset(p, 0, sizeof(*p));
                    142:                p->next = __bindings;
                    143:                strlcpy(p->domainname, domainname, sizeof(p->domainname));
                    144:                __bindings = p;
                    145:        }
1.1       itojun    146:
1.10      yamt      147:        return p;
1.1       itojun    148: }

CVSweb <webmaster@jp.NetBSD.org>