1 /* Implementation of the bindtextdomain(3) function
2 Copyright (C) 1995, 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
23 #if defined STDC_HEADERS || defined _LIBC
33 #if defined HAVE_STRING_H || defined _LIBC
38 # define memcpy(Dst, Src, Num) bcopy (Src, Dst, Num)
45 # include "libgettext.h"
51 /* We have to handle multi-threaded applications. */
52 # include <bits/libc-lock.h>
54 /* Provide dummy implementation if this is outside glibc. */
55 # define __libc_rwlock_define(CLASS, NAME)
56 # define __libc_rwlock_wrlock(NAME)
57 # define __libc_rwlock_unlock(NAME)
60 /* @@ end of prolog @@ */
62 /* Contains the default location of the message catalogs. */
63 extern const char _nl_default_dirname[];
65 /* List with bindings of specific domains. */
66 extern struct binding *_nl_domain_bindings;
68 /* Lock variable to protect the global data in the gettext implementation. */
69 __libc_rwlock_define (extern, _nl_state_lock)
72 /* Names for the libintl functions are a problem. They must not clash
73 with existing names and they should follow ANSI C. But this source
74 code is also used in GNU C Library where the names have a __
75 prefix. So we have to make a difference here. */
77 # define BINDTEXTDOMAIN __bindtextdomain
79 # define strdup(str) __strdup (str)
82 # define BINDTEXTDOMAIN bindtextdomain__
85 /* Specify that the DOMAINNAME message catalog will be found
86 in DIRNAME rather than in the system locale data base. */
88 BINDTEXTDOMAIN (domainname, dirname)
89 const char *domainname;
92 struct binding *binding;
95 /* Some sanity checks. */
96 if (domainname == NULL || domainname[0] == '\0')
99 __libc_rwlock_wrlock (_nl_state_lock);
101 for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
103 int compare = strcmp (domainname, binding->domainname);
109 /* It is not in the list. */
116 /* The current binding has be to returned. */
117 result = binding == NULL ? (char *) _nl_default_dirname : binding->dirname;
118 else if (binding != NULL)
120 /* The domain is already bound. If the new value and the old
121 one are equal we simply do nothing. Otherwise replace the
123 result = binding->dirname;
124 if (strcmp (dirname, result) != 0)
126 if (strcmp (dirname, _nl_default_dirname) == 0)
127 result = (char *) _nl_default_dirname;
130 #if defined _LIBC || defined HAVE_STRDUP
131 result = strdup (dirname);
133 size_t len = strlen (dirname) + 1;
134 result = (char *) malloc (len);
136 memcpy (result, dirname, len);
142 if (binding->dirname != _nl_default_dirname)
143 free (binding->dirname);
145 binding->dirname = result;
151 /* We have to create a new binding. */
152 size_t len = strlen (domainname) + 1;
153 struct binding *new_binding =
154 (struct binding *) malloc (sizeof (*new_binding) + len);
156 if (new_binding == NULL)
160 memcpy (new_binding->domainname, domainname, len);
162 if (strcmp (dirname, _nl_default_dirname) == 0)
163 result = new_binding->dirname = (char *) _nl_default_dirname;
166 #if defined _LIBC || defined HAVE_STRDUP
167 result = new_binding->dirname = strdup (dirname);
169 len = strlen (dirname) + 1;
170 result = new_binding->dirname = (char *) malloc (len);
172 memcpy (new_binding->dirname, dirname, len);
179 /* Now enqueue it. */
180 if (_nl_domain_bindings == NULL
181 || strcmp (domainname, _nl_domain_bindings->domainname) < 0)
183 new_binding->next = _nl_domain_bindings;
184 _nl_domain_bindings = new_binding;
188 binding = _nl_domain_bindings;
189 while (binding->next != NULL
190 && strcmp (domainname, binding->next->domainname) > 0)
191 binding = binding->next;
193 new_binding->next = binding->next;
194 binding->next = new_binding;
197 else if (new_binding != NULL)
201 /* For a succesful call we flush the caches. */
205 __libc_rwlock_unlock (_nl_state_lock);
211 /* Alias for function name in GNU C Library. */
212 weak_alias (__bindtextdomain, bindtextdomain);