]> git.ipfire.org Git - thirdparty/glibc.git/blob - intl/bindtextdom.c
0540d1e1626e7526bbcebd7a78b4a5b371e9cfb8
[thirdparty/glibc.git] / intl / bindtextdom.c
1 /* Implementation of the bindtextdomain(3) function
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3
4 This file is part of the GNU C Library. Its master source is NOT part of
5 the C library, however.
6
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #if defined STDC_HEADERS || defined _LIBC
27 # include <stdlib.h>
28 #else
29 # ifdef HAVE_MALLOC_H
30 # include <malloc.h>
31 # else
32 void free ();
33 # endif
34 #endif
35
36 #if defined HAVE_STRING_H || defined _LIBC
37 # include <string.h>
38 #else
39 # include <strings.h>
40 # ifndef memcpy
41 # define memcpy(Dst, Src, Num) bcopy (Src, Dst, Num)
42 # endif
43 #endif
44
45 #ifdef _LIBC
46 # include <libintl.h>
47 #else
48 # include "libgettext.h"
49 #endif
50 #include "gettext.h"
51 #include "gettextP.h"
52
53 /* @@ end of prolog @@ */
54
55 /* Contains the default location of the message catalogs. */
56 extern const char _nl_default_dirname[];
57
58 /* List with bindings of specific domains. */
59 extern struct binding *_nl_domain_bindings;
60
61
62 /* Names for the libintl functions are a problem. They must not clash
63 with existing names and they should follow ANSI C. But this source
64 code is also used in GNU C Library where the names have a __
65 prefix. So we have to make a difference here. */
66 #ifdef _LIBC
67 # define BINDTEXTDOMAIN __bindtextdomain
68 # define strdup(str) __strdup (str)
69 #else
70 # define BINDTEXTDOMAIN bindtextdomain__
71 #endif
72
73 /* Specify that the DOMAINNAME message catalog will be found
74 in DIRNAME rather than in the system locale data base. */
75 char *
76 BINDTEXTDOMAIN (domainname, dirname)
77 const char *domainname;
78 const char *dirname;
79 {
80 struct binding *binding;
81
82 /* Some sanity checks. */
83 if (domainname == NULL || domainname[0] == '\0')
84 return NULL;
85
86 for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
87 {
88 int compare = strcmp (domainname, binding->domainname);
89 if (compare == 0)
90 /* We found it! */
91 break;
92 if (compare < 0)
93 {
94 /* It is not in the list. */
95 binding = NULL;
96 break;
97 }
98 }
99
100 if (dirname == NULL)
101 /* The current binding has be to returned. */
102 return binding == NULL ? (char *) _nl_default_dirname : binding->dirname;
103
104 if (binding != NULL)
105 {
106 /* The domain is already bound. If the new value and the old
107 one are equal we simply do nothing. Otherwise replace the
108 old binding. */
109 if (strcmp (dirname, binding->dirname) != 0)
110 {
111 char *new_dirname;
112
113 if (strcmp (dirname, _nl_default_dirname) == 0)
114 new_dirname = (char *) _nl_default_dirname;
115 else
116 {
117 #if defined _LIBC || defined HAVE_STRDUP
118 new_dirname = strdup (dirname);
119 if (new_dirname == NULL)
120 return NULL;
121 #else
122 size_t len = strlen (dirname) + 1;
123 new_dirname = (char *) malloc (len);
124 if (new_dirname == NULL)
125 return NULL;
126
127 memcpy (new_dirname, dirname, len);
128 #endif
129 }
130
131 if (binding->dirname != _nl_default_dirname)
132 free (binding->dirname);
133
134 binding->dirname = new_dirname;
135 }
136 }
137 else
138 {
139 /* We have to create a new binding. */
140 size_t len;
141 struct binding *new_binding =
142 (struct binding *) malloc (sizeof (*new_binding));
143
144 if (new_binding == NULL)
145 return NULL;
146
147 #if defined _LIBC || defined HAVE_STRDUP
148 new_binding->domainname = strdup (domainname);
149 if (new_binding->domainname == NULL)
150 return NULL;
151 #else
152 len = strlen (domainname) + 1;
153 new_binding->domainname = (char *) malloc (len);
154 if (new_binding->domainname == NULL)
155 return NULL;
156 memcpy (new_binding->domainname, domainname, len);
157 #endif
158
159 if (strcmp (dirname, _nl_default_dirname) == 0)
160 new_binding->dirname = (char *) _nl_default_dirname;
161 else
162 {
163 #if defined _LIBC || defined HAVE_STRDUP
164 new_binding->dirname = strdup (dirname);
165 if (new_binding->dirname == NULL)
166 return NULL;
167 #else
168 len = strlen (dirname) + 1;
169 new_binding->dirname = (char *) malloc (len);
170 if (new_binding->dirname == NULL)
171 return NULL;
172 memcpy (new_binding->dirname, dirname, len);
173 #endif
174 }
175
176 /* Now enqueue it. */
177 if (_nl_domain_bindings == NULL
178 || strcmp (domainname, _nl_domain_bindings->domainname) < 0)
179 {
180 new_binding->next = _nl_domain_bindings;
181 _nl_domain_bindings = new_binding;
182 }
183 else
184 {
185 binding = _nl_domain_bindings;
186 while (binding->next != NULL
187 && strcmp (domainname, binding->next->domainname) > 0)
188 binding = binding->next;
189
190 new_binding->next = binding->next;
191 binding->next = new_binding;
192 }
193
194 binding = new_binding;
195 }
196
197 return binding->dirname;
198 }
199
200 #ifdef _LIBC
201 /* Alias for function name in GNU C Library. */
202 weak_alias (__bindtextdomain, bindtextdomain);
203 #endif