xref: /openbsd-src/gnu/usr.bin/binutils-2.17/intl/textdomain.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /* Implementation of the textdomain(3) function.
2*3d8817e4Smiod    Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3*3d8817e4Smiod    Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
4*3d8817e4Smiod 
5*3d8817e4Smiod    This program is free software; you can redistribute it and/or modify
6*3d8817e4Smiod    it under the terms of the GNU General Public License as published by
7*3d8817e4Smiod    the Free Software Foundation; either version 2, or (at your option)
8*3d8817e4Smiod    any later version.
9*3d8817e4Smiod 
10*3d8817e4Smiod    This program is distributed in the hope that it will be useful,
11*3d8817e4Smiod    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*3d8817e4Smiod    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*3d8817e4Smiod    GNU General Public License for more details.
14*3d8817e4Smiod 
15*3d8817e4Smiod    You should have received a copy of the GNU General Public License
16*3d8817e4Smiod    along with this program; if not, write to the Free Software Foundation,
17*3d8817e4Smiod    Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
18*3d8817e4Smiod 
19*3d8817e4Smiod #ifdef HAVE_CONFIG_H
20*3d8817e4Smiod # include <config.h>
21*3d8817e4Smiod #endif
22*3d8817e4Smiod 
23*3d8817e4Smiod #if defined STDC_HEADERS || defined _LIBC
24*3d8817e4Smiod # include <stdlib.h>
25*3d8817e4Smiod #endif
26*3d8817e4Smiod 
27*3d8817e4Smiod #if defined STDC_HEADERS || defined HAVE_STRING_H || defined _LIBC
28*3d8817e4Smiod # include <string.h>
29*3d8817e4Smiod #else
30*3d8817e4Smiod # include <strings.h>
31*3d8817e4Smiod # ifndef memcpy
32*3d8817e4Smiod #  define memcpy(Dst, Src, Num) bcopy (Src, Dst, Num)
33*3d8817e4Smiod # endif
34*3d8817e4Smiod #endif
35*3d8817e4Smiod 
36*3d8817e4Smiod #ifdef _LIBC
37*3d8817e4Smiod # include <libintl.h>
38*3d8817e4Smiod #else
39*3d8817e4Smiod # include "libgettext.h"
40*3d8817e4Smiod #endif
41*3d8817e4Smiod 
42*3d8817e4Smiod /* @@ end of prolog @@ */
43*3d8817e4Smiod 
44*3d8817e4Smiod /* Name of the default text domain.  */
45*3d8817e4Smiod extern const char _nl_default_default_domain[];
46*3d8817e4Smiod 
47*3d8817e4Smiod /* Default text domain in which entries for gettext(3) are to be found.  */
48*3d8817e4Smiod extern const char *_nl_current_default_domain;
49*3d8817e4Smiod 
50*3d8817e4Smiod 
51*3d8817e4Smiod /* Names for the libintl functions are a problem.  They must not clash
52*3d8817e4Smiod    with existing names and they should follow ANSI C.  But this source
53*3d8817e4Smiod    code is also used in GNU C Library where the names have a __
54*3d8817e4Smiod    prefix.  So we have to make a difference here.  */
55*3d8817e4Smiod #ifdef _LIBC
56*3d8817e4Smiod # define TEXTDOMAIN __textdomain
57*3d8817e4Smiod # ifndef strdup
58*3d8817e4Smiod #  define strdup(str) __strdup (str)
59*3d8817e4Smiod # endif
60*3d8817e4Smiod #else
61*3d8817e4Smiod # define TEXTDOMAIN textdomain__
62*3d8817e4Smiod #endif
63*3d8817e4Smiod 
64*3d8817e4Smiod /* Set the current default message catalog to DOMAINNAME.
65*3d8817e4Smiod    If DOMAINNAME is null, return the current default.
66*3d8817e4Smiod    If DOMAINNAME is "", reset to the default of "messages".  */
67*3d8817e4Smiod char *
TEXTDOMAIN(domainname)68*3d8817e4Smiod TEXTDOMAIN (domainname)
69*3d8817e4Smiod      const char *domainname;
70*3d8817e4Smiod {
71*3d8817e4Smiod   char *old;
72*3d8817e4Smiod 
73*3d8817e4Smiod   /* A NULL pointer requests the current setting.  */
74*3d8817e4Smiod   if (domainname == NULL)
75*3d8817e4Smiod     return (char *) _nl_current_default_domain;
76*3d8817e4Smiod 
77*3d8817e4Smiod   old = (char *) _nl_current_default_domain;
78*3d8817e4Smiod 
79*3d8817e4Smiod   /* If domain name is the null string set to default domain "messages".  */
80*3d8817e4Smiod   if (domainname[0] == '\0'
81*3d8817e4Smiod       || strcmp (domainname, _nl_default_default_domain) == 0)
82*3d8817e4Smiod     _nl_current_default_domain = _nl_default_default_domain;
83*3d8817e4Smiod   else
84*3d8817e4Smiod     {
85*3d8817e4Smiod       /* If the following malloc fails `_nl_current_default_domain'
86*3d8817e4Smiod 	 will be NULL.  This value will be returned and so signals we
87*3d8817e4Smiod 	 are out of core.  */
88*3d8817e4Smiod #if defined _LIBC || defined HAVE_STRDUP
89*3d8817e4Smiod       _nl_current_default_domain = strdup (domainname);
90*3d8817e4Smiod #else
91*3d8817e4Smiod       size_t len = strlen (domainname) + 1;
92*3d8817e4Smiod       char *cp = (char *) malloc (len);
93*3d8817e4Smiod       if (cp != NULL)
94*3d8817e4Smiod 	memcpy (cp, domainname, len);
95*3d8817e4Smiod       _nl_current_default_domain = cp;
96*3d8817e4Smiod #endif
97*3d8817e4Smiod     }
98*3d8817e4Smiod 
99*3d8817e4Smiod   if (old != _nl_default_default_domain)
100*3d8817e4Smiod     free (old);
101*3d8817e4Smiod 
102*3d8817e4Smiod   return (char *) _nl_current_default_domain;
103*3d8817e4Smiod }
104*3d8817e4Smiod 
105*3d8817e4Smiod #ifdef _LIBC
106*3d8817e4Smiod /* Alias for function name in GNU C Library.  */
107*3d8817e4Smiod weak_alias (__textdomain, textdomain);
108*3d8817e4Smiod #endif
109