Update to 2.1.x development version
[platform/upstream/glibc.git] / intl / bindtextdom.c
1 /* bindtextdom.c -- implementation of the bindtextdomain(3) function
2    Copyright (C) 1995, 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.  The master source lives in /gd/gnu/lib.
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 #endif
41
42 #ifdef _LIBC
43 # include <libintl.h>
44 #else
45 # include "libgettext.h"
46 #endif
47 #include "gettext.h"
48 #include "gettextP.h"
49
50 /* @@ end of prolog @@ */
51
52 /* Contains the default location of the message catalogs.  */
53 extern const char _nl_default_dirname[];
54
55 /* List with bindings of specific domains.  */
56 extern struct binding *_nl_domain_bindings;
57
58
59 /* Names for the libintl functions are a problem.  They must not clash
60    with existing names and they should follow ANSI C.  But this source
61    code is also used in GNU C Library where the names have a __
62    prefix.  So we have to make a difference here.  */
63 #ifdef _LIBC
64 # define BINDTEXTDOMAIN __bindtextdomain
65 #else
66 # define BINDTEXTDOMAIN bindtextdomain__
67 #endif
68
69 /* Specify that the DOMAINNAME message catalog will be found
70    in DIRNAME rather than in the system locale data base.  */
71 char *
72 BINDTEXTDOMAIN (domainname, dirname)
73      const char *domainname;
74      const char *dirname;
75 {
76   struct binding *binding;
77
78   /* Some sanity checks.  */
79   if (domainname == NULL || domainname[0] == '\0')
80     return NULL;
81
82   for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
83     {
84       int compare = strcmp (domainname, binding->domainname);
85       if (compare == 0)
86         /* We found it!  */
87         break;
88       if (compare < 0)
89         {
90           /* It is not in the list.  */
91           binding = NULL;
92           break;
93         }
94     }
95
96   if (dirname == NULL)
97     /* The current binding has be to returned.  */
98     return binding == NULL ? (char *) _nl_default_dirname : binding->dirname;
99
100   if (binding != NULL)
101     {
102       /* The domain is already bound.  Replace the old binding.  */
103       char *new_dirname;
104
105       if (strcmp (dirname, _nl_default_dirname) == 0)
106         new_dirname = (char *) _nl_default_dirname;
107       else
108         {
109           size_t len = strlen (dirname) + 1;
110           new_dirname = (char *) malloc (len);
111           if (new_dirname == NULL)
112             return NULL;
113
114           memcpy (new_dirname, dirname, len);
115         }
116
117       if (strcmp (binding->dirname, _nl_default_dirname) != 0)
118         free (binding->dirname);
119
120       binding->dirname = new_dirname;
121     }
122   else
123     {
124       /* We have to create a new binding.  */
125       size_t len;
126       struct binding *new_binding =
127         (struct binding *) malloc (sizeof (*new_binding));
128
129       if (new_binding == NULL)
130         return NULL;
131
132       len = strlen (domainname) + 1;
133       new_binding->domainname = (char *) malloc (len);
134       if (new_binding->domainname == NULL)
135           return NULL;
136       memcpy (new_binding->domainname, domainname, len);
137
138       if (strcmp (dirname, _nl_default_dirname) == 0)
139         new_binding->dirname = (char *) _nl_default_dirname;
140       else
141         {
142           len = strlen (dirname) + 1;
143           new_binding->dirname = (char *) malloc (len);
144           if (new_binding->dirname == NULL)
145             return NULL;
146           memcpy (new_binding->dirname, dirname, len);
147         }
148
149       /* Now enqueue it.  */
150       if (_nl_domain_bindings == NULL
151           || strcmp (domainname, _nl_domain_bindings->domainname) < 0)
152         {
153           new_binding->next = _nl_domain_bindings;
154           _nl_domain_bindings = new_binding;
155         }
156       else
157         {
158           binding = _nl_domain_bindings;
159           while (binding->next != NULL
160                  && strcmp (domainname, binding->next->domainname) > 0)
161             binding = binding->next;
162
163           new_binding->next = binding->next;
164           binding->next = new_binding;
165         }
166
167       binding = new_binding;
168     }
169
170   return binding->dirname;
171 }
172
173 #ifdef _LIBC
174 /* Alias for function name in GNU C Library.  */
175 weak_alias (__bindtextdomain, bindtextdomain);
176 #endif