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