Mon Dec 18 13:40:37 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[glibc.git] / intl / bindtextdom.c
blobbd9f4226548e170ffc8332ba939592dba94bf9ec
1 /* bindtextdom.c -- implementation of the bindtextdomain(3) function
2 Copyright (C) 1995 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
16 Foundation, 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 #endif
38 #ifdef _LIBC
39 # include <libintl.h>
40 #else
41 # include "libgettext.h"
42 #endif
43 #include "gettext.h"
44 #include "gettextP.h"
46 /* @@ end of prolog @@ */
48 /* Contains the default location of the message catalogs. */
49 extern const char _nl_default_dirname[];
51 /* List with bindings of specific domains. */
52 extern struct binding *_nl_domain_bindings;
55 /* Names for the libintl functions are a problem. They must not clash
56 with existing names and they should follow ANSI C. But this source
57 code is also used in GNU C Library where the names have a __
58 prefix. So we have to make a difference here. */
59 #ifdef _LIBC
60 # define BINDTEXTDOMAIN __bindtextdomain
61 #else
62 # define BINDTEXTDOMAIN bindtextdomain__
63 #endif
65 /* Specify that the DOMAINNAME message catalog will be found
66 in DIRNAME rather than in the system locale data base. */
67 char *
68 BINDTEXTDOMAIN (domainname, dirname)
69 const char *domainname;
70 const char *dirname;
72 struct binding *binding;
74 /* Some sanity checks. */
75 if (domainname == NULL || domainname[0] == '\0')
76 return NULL;
78 for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next)
80 int compare = strcmp (domainname, binding->domainname);
81 if (compare == 0)
82 /* We found it! */
83 break;
84 if (compare < 0)
86 /* It is not in the list. */
87 binding = NULL;
88 break;
92 if (dirname == NULL)
93 /* The current binding has be to returned. */
94 return binding == NULL ? (char *) _nl_default_dirname : binding->dirname;
96 if (binding != NULL)
98 /* The domain is already bound. Replace the old binding. */
99 char *new_dirname;
101 if (strcmp (dirname, _nl_default_dirname) == 0)
102 new_dirname = (char *) _nl_default_dirname;
103 else
105 size_t len = strlen (dirname) + 1;
106 new_dirname = (char *) malloc (len);
107 if (new_dirname == NULL)
108 return NULL;
110 memcpy (new_dirname, dirname, len);
113 if (strcmp (binding->dirname, _nl_default_dirname) != 0)
114 free (binding->dirname);
116 binding->dirname = new_dirname;
118 else
120 /* We have to create a new binding. */
121 size_t len;
122 struct binding *new_binding =
123 (struct binding *) malloc (sizeof (*new_binding));
125 if (new_binding == NULL)
126 return NULL;
128 len = strlen (domainname) + 1;
129 new_binding->domainname = (char *) malloc (len);
130 if (new_binding->domainname == NULL)
131 return NULL;
132 memcpy (new_binding->domainname, domainname, len);
134 if (strcmp (dirname, _nl_default_dirname) == 0)
135 new_binding->dirname = (char *) _nl_default_dirname;
136 else
138 len = strlen (dirname) + 1;
139 new_binding->dirname = (char *) malloc (len);
140 if (new_binding->dirname == NULL)
141 return NULL;
142 memcpy (new_binding->dirname, dirname, len);
145 /* Now enqueue it. */
146 if (_nl_domain_bindings == NULL
147 || strcmp (domainname, _nl_domain_bindings->domainname) < 0)
149 new_binding->next = _nl_domain_bindings;
150 _nl_domain_bindings = new_binding;
152 else
154 binding = _nl_domain_bindings;
155 while (binding->next != NULL
156 && strcmp (domainname, binding->next->domainname) > 0)
157 binding = binding->next;
159 new_binding->next = binding->next;
160 binding->next = new_binding;
163 binding = new_binding;
166 return binding->dirname;
169 #ifdef _LIBC
170 /* Alias for function name in GNU C Library. */
171 weak_alias (__bindtextdomain, bindtextdomain);
172 #endif