oops - omitted in previous delta
[official-gcc.git] / gcc / intl / bindtextdom.c
blobc143cbc237b22d8175f982e2505ee336e04c2a0c
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 /* The internal variables in the standalone libintl.a must have different
50 names than the internal variables in GNU libc, otherwise programs
51 using libintl.a cannot be linked statically. */
52 #if !defined _LIBC
53 # define _nl_default_dirname _nl_default_dirname__
54 # define _nl_domain_bindings _nl_domain_bindings__
55 #endif
57 /* @@ end of prolog @@ */
59 /* Contains the default location of the message catalogs. */
60 extern const char _nl_default_dirname[];
62 /* List with bindings of specific domains. */
63 extern struct binding *_nl_domain_bindings;
66 /* Names for the libintl functions are a problem. They must not clash
67 with existing names and they should follow ANSI C. But this source
68 code is also used in GNU C Library where the names have a __
69 prefix. So we have to make a difference here. */
70 #ifdef _LIBC
71 # define BINDTEXTDOMAIN __bindtextdomain
72 # ifndef strdup
73 # define strdup(str) __strdup (str)
74 # endif
75 #else
76 # define BINDTEXTDOMAIN bindtextdomain__
77 #endif
79 /* Specify that the DOMAINNAME message catalog will be found
80 in DIRNAME rather than in the system locale data base. */
81 char *
82 BINDTEXTDOMAIN (domainname, dirname)
83 const char *domainname;
84 const char *dirname;
86 struct binding *binding;
88 /* Some sanity checks. */
89 if (domainname == NULL || domainname[0] == '\0')
90 return NULL;
92 for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
94 int compare = strcmp (domainname, binding->domainname);
95 if (compare == 0)
96 /* We found it! */
97 break;
98 if (compare < 0)
100 /* It is not in the list. */
101 binding = NULL;
102 break;
106 if (dirname == NULL)
107 /* The current binding has be to returned. */
108 return binding == NULL ? (char *) _nl_default_dirname : binding->dirname;
110 if (binding != NULL)
112 /* The domain is already bound. If the new value and the old
113 one are equal we simply do nothing. Otherwise replace the
114 old binding. */
115 if (strcmp (dirname, binding->dirname) != 0)
117 char *new_dirname;
119 if (strcmp (dirname, _nl_default_dirname) == 0)
120 new_dirname = (char *) _nl_default_dirname;
121 else
123 #if defined _LIBC || defined HAVE_STRDUP
124 new_dirname = strdup (dirname);
125 if (new_dirname == NULL)
126 return NULL;
127 #else
128 size_t len = strlen (dirname) + 1;
129 new_dirname = (char *) malloc (len);
130 if (new_dirname == NULL)
131 return NULL;
133 memcpy (new_dirname, dirname, len);
134 #endif
137 if (binding->dirname != _nl_default_dirname)
138 free (binding->dirname);
140 binding->dirname = new_dirname;
143 else
145 /* We have to create a new binding. */
146 #if !defined _LIBC && !defined HAVE_STRDUP
147 size_t len;
148 #endif
149 struct binding *new_binding =
150 (struct binding *) malloc (sizeof (*new_binding));
152 if (new_binding == NULL)
153 return NULL;
155 #if defined _LIBC || defined HAVE_STRDUP
156 new_binding->domainname = strdup (domainname);
157 if (new_binding->domainname == NULL)
158 return NULL;
159 #else
160 len = strlen (domainname) + 1;
161 new_binding->domainname = (char *) malloc (len);
162 if (new_binding->domainname == NULL)
163 return NULL;
164 memcpy (new_binding->domainname, domainname, len);
165 #endif
167 if (strcmp (dirname, _nl_default_dirname) == 0)
168 new_binding->dirname = (char *) _nl_default_dirname;
169 else
171 #if defined _LIBC || defined HAVE_STRDUP
172 new_binding->dirname = strdup (dirname);
173 if (new_binding->dirname == NULL)
174 return NULL;
175 #else
176 len = strlen (dirname) + 1;
177 new_binding->dirname = (char *) malloc (len);
178 if (new_binding->dirname == NULL)
179 return NULL;
180 memcpy (new_binding->dirname, dirname, len);
181 #endif
184 /* Now enqueue it. */
185 if (_nl_domain_bindings == NULL
186 || strcmp (domainname, _nl_domain_bindings->domainname) < 0)
188 new_binding->next = _nl_domain_bindings;
189 _nl_domain_bindings = new_binding;
191 else
193 binding = _nl_domain_bindings;
194 while (binding->next != NULL
195 && strcmp (domainname, binding->next->domainname) > 0)
196 binding = binding->next;
198 new_binding->next = binding->next;
199 binding->next = new_binding;
202 binding = new_binding;
205 return binding->dirname;
208 #ifdef _LIBC
209 /* Alias for function name in GNU C Library. */
210 weak_alias (__bindtextdomain, bindtextdomain);
211 #endif