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