| 1 | /* Implementation of the bindtextdomain(3) function | 
|---|
| 2 | Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. | 
|---|
| 3 |  | 
|---|
| 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. | 
|---|
| 8 |  | 
|---|
| 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. | 
|---|
| 13 |  | 
|---|
| 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.  */ | 
|---|
| 17 |  | 
|---|
| 18 | #ifdef HAVE_CONFIG_H | 
|---|
| 19 | # include <config.h> | 
|---|
| 20 | #endif | 
|---|
| 21 |  | 
|---|
| 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 | 
|---|
| 31 |  | 
|---|
| 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 | 
|---|
| 40 |  | 
|---|
| 41 | #ifdef _LIBC | 
|---|
| 42 | # include <libintl.h> | 
|---|
| 43 | #else | 
|---|
| 44 | # include "libgettext.h" | 
|---|
| 45 | #endif | 
|---|
| 46 | #include "gettext.h" | 
|---|
| 47 | #include "gettextP.h" | 
|---|
| 48 |  | 
|---|
| 49 | /* @@ end of prolog @@ */ | 
|---|
| 50 |  | 
|---|
| 51 | /* Contains the default location of the message catalogs.  */ | 
|---|
| 52 | extern const char _nl_default_dirname[]; | 
|---|
| 53 |  | 
|---|
| 54 | /* List with bindings of specific domains.  */ | 
|---|
| 55 | extern struct binding *_nl_domain_bindings; | 
|---|
| 56 |  | 
|---|
| 57 |  | 
|---|
| 58 | /* Names for the libintl functions are a problem.  They must not clash | 
|---|
| 59 | with existing names and they should follow ANSI C.  But this source | 
|---|
| 60 | code is also used in GNU C Library where the names have a __ | 
|---|
| 61 | prefix.  So we have to make a difference here.  */ | 
|---|
| 62 | #ifdef _LIBC | 
|---|
| 63 | # define BINDTEXTDOMAIN __bindtextdomain | 
|---|
| 64 | # define strdup(str) __strdup (str) | 
|---|
| 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.  If the new value and the old | 
|---|
| 103 | one are equal we simply do nothing.  Otherwise replace the | 
|---|
| 104 | old binding.  */ | 
|---|
| 105 | if (strcmp (dirname, binding->dirname) != 0) | 
|---|
| 106 | { | 
|---|
| 107 | char *new_dirname; | 
|---|
| 108 |  | 
|---|
| 109 | if (strcmp (dirname, _nl_default_dirname) == 0) | 
|---|
| 110 | new_dirname = (char *) _nl_default_dirname; | 
|---|
| 111 | else | 
|---|
| 112 | { | 
|---|
| 113 | #if defined _LIBC || defined HAVE_STRDUP | 
|---|
| 114 | new_dirname = strdup (dirname); | 
|---|
| 115 | if (new_dirname == NULL) | 
|---|
| 116 | return NULL; | 
|---|
| 117 | #else | 
|---|
| 118 | size_t len = strlen (dirname) + 1; | 
|---|
| 119 | new_dirname = (char *) malloc (len); | 
|---|
| 120 | if (new_dirname == NULL) | 
|---|
| 121 | return NULL; | 
|---|
| 122 |  | 
|---|
| 123 | memcpy (new_dirname, dirname, len); | 
|---|
| 124 | #endif | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | if (binding->dirname != _nl_default_dirname) | 
|---|
| 128 | free (binding->dirname); | 
|---|
| 129 |  | 
|---|
| 130 | binding->dirname = new_dirname; | 
|---|
| 131 | } | 
|---|
| 132 | } | 
|---|
| 133 | else | 
|---|
| 134 | { | 
|---|
| 135 | /* We have to create a new binding.  */ | 
|---|
| 136 | size_t len; | 
|---|
| 137 | struct binding *new_binding = | 
|---|
| 138 | (struct binding *) malloc (sizeof (*new_binding)); | 
|---|
| 139 |  | 
|---|
| 140 | if (new_binding == NULL) | 
|---|
| 141 | return NULL; | 
|---|
| 142 |  | 
|---|
| 143 | #if defined _LIBC || defined HAVE_STRDUP | 
|---|
| 144 | new_binding->domainname = strdup (domainname); | 
|---|
| 145 | if (new_binding->domainname == NULL) | 
|---|
| 146 | return NULL; | 
|---|
| 147 | #else | 
|---|
| 148 | len = strlen (domainname) + 1; | 
|---|
| 149 | new_binding->domainname = (char *) malloc (len); | 
|---|
| 150 | if (new_binding->domainname == NULL) | 
|---|
| 151 | return NULL; | 
|---|
| 152 | memcpy (new_binding->domainname, domainname, len); | 
|---|
| 153 | #endif | 
|---|
| 154 |  | 
|---|
| 155 | if (strcmp (dirname, _nl_default_dirname) == 0) | 
|---|
| 156 | new_binding->dirname = (char *) _nl_default_dirname; | 
|---|
| 157 | else | 
|---|
| 158 | { | 
|---|
| 159 | #if defined _LIBC || defined HAVE_STRDUP | 
|---|
| 160 | new_binding->dirname = strdup (dirname); | 
|---|
| 161 | if (new_binding->dirname == NULL) | 
|---|
| 162 | return NULL; | 
|---|
| 163 | #else | 
|---|
| 164 | len = strlen (dirname) + 1; | 
|---|
| 165 | new_binding->dirname = (char *) malloc (len); | 
|---|
| 166 | if (new_binding->dirname == NULL) | 
|---|
| 167 | return NULL; | 
|---|
| 168 | memcpy (new_binding->dirname, dirname, len); | 
|---|
| 169 | #endif | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | /* Now enqueue it.  */ | 
|---|
| 173 | if (_nl_domain_bindings == NULL | 
|---|
| 174 | || strcmp (domainname, _nl_domain_bindings->domainname) < 0) | 
|---|
| 175 | { | 
|---|
| 176 | new_binding->next = _nl_domain_bindings; | 
|---|
| 177 | _nl_domain_bindings = new_binding; | 
|---|
| 178 | } | 
|---|
| 179 | else | 
|---|
| 180 | { | 
|---|
| 181 | binding = _nl_domain_bindings; | 
|---|
| 182 | while (binding->next != NULL | 
|---|
| 183 | && strcmp (domainname, binding->next->domainname) > 0) | 
|---|
| 184 | binding = binding->next; | 
|---|
| 185 |  | 
|---|
| 186 | new_binding->next = binding->next; | 
|---|
| 187 | binding->next = new_binding; | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | binding = new_binding; | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 | return binding->dirname; | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | #ifdef _LIBC | 
|---|
| 197 | /* Alias for function name in GNU C Library.  */ | 
|---|
| 198 | weak_alias (__bindtextdomain, bindtextdomain); | 
|---|
| 199 | #endif | 
|---|