1 /* Implementation of the bindtextdomain(3) function
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #if defined STDC_HEADERS || defined _LIBC
32 #if defined HAVE_STRING_H || defined _LIBC
37 # define memcpy(Dst, Src, Num) bcopy (Src, Dst, Num)
44 # include "libgettext.h"
49 /* @@ end of prolog @@ */
51 /* Contains the default location of the message catalogs. */
52 extern const char _nl_default_dirname[];
54 /* List with bindings of specific domains. */
55 extern struct binding *_nl_domain_bindings;
58 /* Names for the libintl functions are a problem. They must not clash
59 with existing names and they should follow ANSI C. But this source
60 code is also used in GNU C Library where the names have a __
61 prefix. So we have to make a difference here. */
63 # define BINDTEXTDOMAIN __bindtextdomain
65 # define BINDTEXTDOMAIN bindtextdomain__
68 /* Specify that the DOMAINNAME message catalog will be found
69 in DIRNAME rather than in the system locale data base. */
71 BINDTEXTDOMAIN (domainname, dirname)
72 const char *domainname;
75 struct binding *binding;
77 /* Some sanity checks. */
78 if (domainname == NULL || domainname[0] == '\0')
81 for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
83 int compare = strcmp (domainname, binding->domainname);
89 /* It is not in the list. */
96 /* The current binding has be to returned. */
97 return binding == NULL ? (char *) _nl_default_dirname : binding->dirname;
101 /* The domain is already bound. Replace the old binding. */
104 if (strcmp (dirname, _nl_default_dirname) == 0)
105 new_dirname = (char *) _nl_default_dirname;
108 size_t len = strlen (dirname) + 1;
109 new_dirname = (char *) malloc (len);
110 if (new_dirname == NULL)
113 memcpy (new_dirname, dirname, len);
116 if (strcmp (binding->dirname, _nl_default_dirname) != 0)
117 free (binding->dirname);
119 binding->dirname = new_dirname;
123 /* We have to create a new binding. */
125 struct binding *new_binding =
126 (struct binding *) malloc (sizeof (*new_binding));
128 if (new_binding == NULL)
131 len = strlen (domainname) + 1;
132 new_binding->domainname = (char *) malloc (len);
133 if (new_binding->domainname == NULL)
135 memcpy (new_binding->domainname, domainname, len);
137 if (strcmp (dirname, _nl_default_dirname) == 0)
138 new_binding->dirname = (char *) _nl_default_dirname;
141 len = strlen (dirname) + 1;
142 new_binding->dirname = (char *) malloc (len);
143 if (new_binding->dirname == NULL)
145 memcpy (new_binding->dirname, dirname, len);
148 /* Now enqueue it. */
149 if (_nl_domain_bindings == NULL
150 || strcmp (domainname, _nl_domain_bindings->domainname) < 0)
152 new_binding->next = _nl_domain_bindings;
153 _nl_domain_bindings = new_binding;
157 binding = _nl_domain_bindings;
158 while (binding->next != NULL
159 && strcmp (domainname, binding->next->domainname) > 0)
160 binding = binding->next;
162 new_binding->next = binding->next;
163 binding->next = new_binding;
166 binding = new_binding;
169 return binding->dirname;
173 /* Alias for function name in GNU C Library. */
174 weak_alias (__bindtextdomain, bindtextdomain);