]> git.ipfire.org Git - thirdparty/glibc.git/blame - intl/bindtextdom.c
Update.
[thirdparty/glibc.git] / intl / bindtextdom.c
CommitLineData
8f2ece69 1/* Implementation of the bindtextdomain(3) function
cb343854 2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
24906b43 3
c84142e8 4 This file is part of the GNU C Library. Its master source is NOT part of
40a55d20 5 the C library, however.
24906b43 6
c84142e8
UD
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.
0393dfd6 11
c84142e8
UD
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.
24906b43 16
c84142e8
UD
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. */
24906b43
RM
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
32void free ();
33# endif
34#endif
35
36#if defined HAVE_STRING_H || defined _LIBC
37# include <string.h>
38#else
39# include <strings.h>
8f2ece69
UD
40# ifndef memcpy
41# define memcpy(Dst, Src, Num) bcopy (Src, Dst, Num)
42# endif
24906b43
RM
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. */
56extern const char _nl_default_dirname[];
57
58/* List with bindings of specific domains. */
59extern 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
cb343854
UD
68# ifndef strdup
69# define strdup(str) __strdup (str)
70# endif
24906b43
RM
71#else
72# define BINDTEXTDOMAIN bindtextdomain__
73#endif
74
75/* Specify that the DOMAINNAME message catalog will be found
76 in DIRNAME rather than in the system locale data base. */
77char *
78BINDTEXTDOMAIN (domainname, dirname)
79 const char *domainname;
80 const char *dirname;
81{
82 struct binding *binding;
83
84 /* Some sanity checks. */
85 if (domainname == NULL || domainname[0] == '\0')
86 return NULL;
87
88 for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
89 {
90 int compare = strcmp (domainname, binding->domainname);
91 if (compare == 0)
92 /* We found it! */
93 break;
94 if (compare < 0)
95 {
96 /* It is not in the list. */
97 binding = NULL;
98 break;
99 }
100 }
101
102 if (dirname == NULL)
103 /* The current binding has be to returned. */
104 return binding == NULL ? (char *) _nl_default_dirname : binding->dirname;
105
106 if (binding != NULL)
107 {
40a55d20
UD
108 /* The domain is already bound. If the new value and the old
109 one are equal we simply do nothing. Otherwise replace the
110 old binding. */
111 if (strcmp (dirname, binding->dirname) != 0)
24906b43 112 {
40a55d20
UD
113 char *new_dirname;
114
115 if (strcmp (dirname, _nl_default_dirname) == 0)
116 new_dirname = (char *) _nl_default_dirname;
117 else
118 {
119#if defined _LIBC || defined HAVE_STRDUP
120 new_dirname = strdup (dirname);
121 if (new_dirname == NULL)
122 return NULL;
123#else
124 size_t len = strlen (dirname) + 1;
125 new_dirname = (char *) malloc (len);
126 if (new_dirname == NULL)
127 return NULL;
24906b43 128
40a55d20
UD
129 memcpy (new_dirname, dirname, len);
130#endif
131 }
24906b43 132
40a55d20
UD
133 if (binding->dirname != _nl_default_dirname)
134 free (binding->dirname);
24906b43 135
40a55d20
UD
136 binding->dirname = new_dirname;
137 }
24906b43
RM
138 }
139 else
140 {
141 /* We have to create a new binding. */
142 size_t len;
143 struct binding *new_binding =
144 (struct binding *) malloc (sizeof (*new_binding));
145
146 if (new_binding == NULL)
147 return NULL;
148
40a55d20
UD
149#if defined _LIBC || defined HAVE_STRDUP
150 new_binding->domainname = strdup (domainname);
151 if (new_binding->domainname == NULL)
152 return NULL;
153#else
24906b43
RM
154 len = strlen (domainname) + 1;
155 new_binding->domainname = (char *) malloc (len);
156 if (new_binding->domainname == NULL)
40a55d20 157 return NULL;
24906b43 158 memcpy (new_binding->domainname, domainname, len);
40a55d20 159#endif
24906b43
RM
160
161 if (strcmp (dirname, _nl_default_dirname) == 0)
162 new_binding->dirname = (char *) _nl_default_dirname;
163 else
164 {
40a55d20
UD
165#if defined _LIBC || defined HAVE_STRDUP
166 new_binding->dirname = strdup (dirname);
167 if (new_binding->dirname == NULL)
168 return NULL;
169#else
24906b43
RM
170 len = strlen (dirname) + 1;
171 new_binding->dirname = (char *) malloc (len);
172 if (new_binding->dirname == NULL)
173 return NULL;
174 memcpy (new_binding->dirname, dirname, len);
40a55d20 175#endif
24906b43
RM
176 }
177
178 /* Now enqueue it. */
179 if (_nl_domain_bindings == NULL
180 || strcmp (domainname, _nl_domain_bindings->domainname) < 0)
181 {
182 new_binding->next = _nl_domain_bindings;
183 _nl_domain_bindings = new_binding;
184 }
185 else
186 {
187 binding = _nl_domain_bindings;
188 while (binding->next != NULL
189 && strcmp (domainname, binding->next->domainname) > 0)
190 binding = binding->next;
191
192 new_binding->next = binding->next;
193 binding->next = new_binding;
194 }
195
196 binding = new_binding;
197 }
198
199 return binding->dirname;
200}
201
202#ifdef _LIBC
203/* Alias for function name in GNU C Library. */
204weak_alias (__bindtextdomain, bindtextdomain);
205#endif