xref: /netbsd-src/lib/libintl/textdomain.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: textdomain.c,v 1.8 2003/04/09 14:11:33 drochner 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.8 2003/04/09 14:11:33 drochner Exp $");
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <libintl.h>
39 #include "libintl_local.h"
40 #include "pathnames.h"
41 
42 static struct domainbinding __default_binding = {
43 	NULL, DEFAULT_DOMAINNAME, _PATH_TEXTDOMAIN,
44 };
45 struct domainbinding *__bindings = &__default_binding;
46 char __current_domainname[PATH_MAX] = DEFAULT_DOMAINNAME;
47 
48 /*
49  * set the default domainname for dcngettext() and friends.
50  */
51 char *
52 textdomain(domainname)
53 	const char *domainname;
54 {
55 
56 	/* NULL pointer gives the current setting */
57 	if (!domainname)
58 		return __current_domainname;
59 
60 	/* empty string sets the value back to the default */
61 	if (!*domainname) {
62 		strlcpy(__current_domainname, DEFAULT_DOMAINNAME,
63 		    sizeof(__current_domainname));
64 	} else {
65 		strlcpy(__current_domainname, domainname,
66 		    sizeof(__current_domainname));
67 	}
68 	return __current_domainname;
69 }
70 
71 char *
72 bindtextdomain(domainname, dirname)
73 	const char *domainname;
74 	const char *dirname;
75 {
76 	struct domainbinding *p;
77 
78 	/* NULL pointer or empty string returns NULL with no operation */
79 	if (!domainname || !*domainname)
80 		return NULL;
81 
82 	if (dirname && (strlen(dirname) + 1 > sizeof(p->path)))
83 		return NULL;
84 
85 #if 0
86 	/* disallow relative path */
87 	if (dirname[0] != '/')
88 		return NULL;
89 #endif
90 
91 	if (strlen(domainname) + 1 > sizeof(p->domainname))
92 		return NULL;
93 
94 	/* lookup binding for the domainname */
95 	for (p = __bindings; p; p = p->next)
96 		if (strcmp(p->domainname, domainname) == 0)
97 			break;
98 
99 	if (!dirname) {
100 		if (p)
101 			return (p->path);
102 		else
103 			return _PATH_TEXTDOMAIN;
104 	}
105 
106 	if (!p) {
107 		p = (struct domainbinding *)malloc(sizeof(*p));
108 		if (!p)
109 			return NULL;
110 		memset(p, 0, sizeof(*p));
111 		p->next = __bindings;
112 		strlcpy(p->domainname, domainname, sizeof(p->domainname));
113 		__bindings = p;
114 	}
115 
116 	strlcpy(p->path, dirname, sizeof(p->path));
117 	p->mohandle.mo.mo_magic = 0; /* invalidate current mapping */
118 
119 	return (p->path);
120 }
121 
122 /* ARGSUSED */
123 char *
124 bind_textdomain_codeset(domainname, codeset)
125 	const char *domainname;
126 	const char *codeset;
127 {
128 
129 	/* yet to be done - always fail */
130 	return NULL;
131 }
132