xref: /netbsd-src/lib/libintl/textdomain.c (revision 5aefcfdc06931dd97e76246d2fe0302f7b3fe094)
1 /*	$NetBSD: textdomain.c,v 1.4 2000/11/03 14:29:23 itojun Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 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 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: textdomain.c,v 1.4 2000/11/03 14:29:23 itojun Exp $");
32 #endif /* LIBC_SCCS and not lint */
33 
34 #include <sys/types.h>
35 #include <sys/param.h>
36 
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <libintl.h>
41 #include "libintl_local.h"
42 #include "pathnames.h"
43 
44 struct domainbinding __binding = { NULL, DEFAULT_DOMAINNAME, _PATH_TEXTDOMAIN };
45 
46 /*
47  * set the default domainname for dcngettext() and friends.
48  */
49 char *
50 textdomain(domainname)
51 	const char *domainname;
52 {
53 
54 	/* NULL pointer gives the current setting */
55 	if (!domainname)
56 		return __binding.domainname;
57 
58 	/* empty string sets the value back to the default */
59 	if (!*domainname) {
60 		strlcpy(__binding.domainname, DEFAULT_DOMAINNAME,
61 		    sizeof(__binding.domainname));
62 	} else {
63 		strlcpy(__binding.domainname, domainname,
64 		    sizeof(__binding.domainname));
65 	}
66 	return __binding.domainname;
67 }
68 
69 char *
70 bindtextdomain(domainname, dirname)
71 	const char *domainname;
72 	const char *dirname;
73 {
74 	struct domainbinding *p;
75 
76 	/* NULL pointer or empty string returns NULL with no operation */
77 	if (!domainname || !*domainname)
78 		return NULL;
79 
80 	if (strlen(dirname) + 1 > sizeof(p->path))
81 		return NULL;
82 	/* disallow relative path */
83 	if (dirname[0] != '/')
84 		return NULL;
85 
86 	if (strlen(domainname) + 1 > sizeof(p->domainname))
87 		return NULL;
88 
89 	/* lookup binding for the domainname */
90 	for (p = __binding.next; p; p = p->next)
91 		if (strcmp(p->domainname, domainname) == 0)
92 			break;
93 	if (!p) {
94 		p = (struct domainbinding *)malloc(sizeof(*p));
95 		if (!p)
96 			return NULL;
97 		memset(p, 0, sizeof(*p));
98 		p->next = __binding.next;
99 		__binding.next = p;
100 	}
101 
102 	strlcpy(p->path, dirname, sizeof(p->path));
103 	strlcpy(p->domainname, domainname, sizeof(p->domainname));
104 
105 	/* LINTED const cast */
106 	return (char *)domainname;
107 }
108 
109 char *
110 bind_textdomain_codeset(domainname, codeset)
111 	const char *domainname;
112 	const char *codeset;
113 {
114 
115 	/* yet to be done - always fail */
116 	return NULL;
117 }
118