merge from gcc
[binutils.git] / intl / bindtextdom.c
blobd9c3f349e04dee8aad8a06ecc76cacc4ccc187ba
1 /* Implementation of the bindtextdomain(3) function
2 Copyright (C) 1995, 1996, 1997, 1998 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)
7 any later version.
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. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #if defined STDC_HEADERS || defined _LIBC
23 # include <stdlib.h>
24 #else
25 # ifdef HAVE_MALLOC_H
26 # include <malloc.h>
27 # else
28 void free ();
29 # endif
30 #endif
32 #if defined HAVE_STRING_H || defined _LIBC
33 # include <string.h>
34 #else
35 # include <strings.h>
36 # ifndef memcpy
37 # define memcpy(Dst, Src, Num) bcopy (Src, Dst, Num)
38 # endif
39 #endif
41 #ifdef _LIBC
42 # include <libintl.h>
43 #else
44 # include "libgettext.h"
45 #endif
46 #include "gettext.h"
47 #include "gettextP.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. */
62 #ifdef _LIBC
63 # define BINDTEXTDOMAIN __bindtextdomain
64 # ifndef strdup
65 # define strdup(str) __strdup (str)
66 # endif
67 #else
68 # define BINDTEXTDOMAIN bindtextdomain__
69 #endif
71 /* Specify that the DOMAINNAME message catalog will be found
72 in DIRNAME rather than in the system locale data base. */
73 char *
74 BINDTEXTDOMAIN (domainname, dirname)
75 const char *domainname;
76 const char *dirname;
78 struct binding *binding;
80 /* Some sanity checks. */
81 if (domainname == NULL || domainname[0] == '\0')
82 return NULL;
84 for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
86 int compare = strcmp (domainname, binding->domainname);
87 if (compare == 0)
88 /* We found it! */
89 break;
90 if (compare < 0)
92 /* It is not in the list. */
93 binding = NULL;
94 break;
98 if (dirname == NULL)
99 /* The current binding has be to returned. */
100 return binding == NULL ? (char *) _nl_default_dirname : binding->dirname;
102 if (binding != NULL)
104 /* The domain is already bound. If the new value and the old
105 one are equal we simply do nothing. Otherwise replace the
106 old binding. */
107 if (strcmp (dirname, binding->dirname) != 0)
109 char *new_dirname;
111 if (strcmp (dirname, _nl_default_dirname) == 0)
112 new_dirname = (char *) _nl_default_dirname;
113 else
115 #if defined _LIBC || defined HAVE_STRDUP
116 new_dirname = strdup (dirname);
117 if (new_dirname == NULL)
118 return NULL;
119 #else
120 size_t len = strlen (dirname) + 1;
121 new_dirname = (char *) malloc (len);
122 if (new_dirname == NULL)
123 return NULL;
125 memcpy (new_dirname, dirname, len);
126 #endif
129 if (binding->dirname != _nl_default_dirname)
130 free (binding->dirname);
132 binding->dirname = new_dirname;
135 else
137 /* We have to create a new binding. */
138 #if !defined _LIBC && !defined HAVE_STRDUP
139 size_t len;
140 #endif
141 struct binding *new_binding =
142 (struct binding *) malloc (sizeof (*new_binding));
144 if (new_binding == NULL)
145 return NULL;
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
159 if (strcmp (dirname, _nl_default_dirname) == 0)
160 new_binding->dirname = (char *) _nl_default_dirname;
161 else
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
176 /* Now enqueue it. */
177 if (_nl_domain_bindings == NULL
178 || strcmp (domainname, _nl_domain_bindings->domainname) < 0)
180 new_binding->next = _nl_domain_bindings;
181 _nl_domain_bindings = new_binding;
183 else
185 binding = _nl_domain_bindings;
186 while (binding->next != NULL
187 && strcmp (domainname, binding->next->domainname) > 0)
188 binding = binding->next;
190 new_binding->next = binding->next;
191 binding->next = new_binding;
194 binding = new_binding;
197 return binding->dirname;
200 #ifdef _LIBC
201 /* Alias for function name in GNU C Library. */
202 weak_alias (__bindtextdomain, bindtextdomain);
203 #endif