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)
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. */
22 #if defined STDC_HEADERS || defined _LIBC
32 #if defined HAVE_STRING_H || defined _LIBC
41 # include "libgettext.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. */
60 # define BINDTEXTDOMAIN __bindtextdomain
62 # define BINDTEXTDOMAIN bindtextdomain__
65 /* Specify that the DOMAINNAME message catalog will be found
66 in DIRNAME rather than in the system locale data base. */
68 BINDTEXTDOMAIN (domainname
, dirname
)
69 const char *domainname
;
72 struct binding
*binding
;
74 /* Some sanity checks. */
75 if (domainname
== NULL
|| domainname
[0] == '\0')
78 for (binding
= _nl_domain_bindings
; binding
!= NULL
; binding
= binding
->next
)
80 int compare
= strcmp (domainname
, binding
->domainname
);
86 /* It is not in the list. */
93 /* The current binding has be to returned. */
94 return binding
== NULL
? (char *) _nl_default_dirname
: binding
->dirname
;
98 /* The domain is already bound. Replace the old binding. */
101 if (strcmp (dirname
, _nl_default_dirname
) == 0)
102 new_dirname
= (char *) _nl_default_dirname
;
105 size_t len
= strlen (dirname
) + 1;
106 new_dirname
= (char *) malloc (len
);
107 if (new_dirname
== 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
;
120 /* We have to create a new binding. */
122 struct binding
*new_binding
=
123 (struct binding
*) malloc (sizeof (*new_binding
));
125 if (new_binding
== NULL
)
128 len
= strlen (domainname
) + 1;
129 new_binding
->domainname
= (char *) malloc (len
);
130 if (new_binding
->domainname
== NULL
)
132 memcpy (new_binding
->domainname
, domainname
, len
);
134 if (strcmp (dirname
, _nl_default_dirname
) == 0)
135 new_binding
->dirname
= (char *) _nl_default_dirname
;
138 len
= strlen (dirname
) + 1;
139 new_binding
->dirname
= (char *) malloc (len
);
140 if (new_binding
->dirname
== 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
;
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
;
170 /* Alias for function name in GNU C Library. */
171 weak_alias (__bindtextdomain
, bindtextdomain
);