Add bug 18604 to the correct section
[glibc.git] / intl / explodename.c
blob83feef3ffc8f81dc630109443077683a7a1a8da3
1 /* Copyright (C) 1995-2015 Free Software Foundation, Inc.
2 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
25 #include "loadinfo.h"
27 /* On some strange systems still no definition of NULL is found. Sigh! */
28 #ifndef NULL
29 # if defined __STDC__ && __STDC__
30 # define NULL ((void *) 0)
31 # else
32 # define NULL 0
33 # endif
34 #endif
36 /* @@ end of prolog @@ */
38 /* Split a locale name NAME into a leading language part and all the
39 rest. Return a pointer to the first character after the language,
40 i.e. to the first byte of the rest. */
41 static char *_nl_find_language (const char *name);
43 static char *
44 _nl_find_language (const char *name)
46 while (name[0] != '\0' && name[0] != '_' && name[0] != '@' && name[0] != '.')
47 ++name;
49 return (char *) name;
53 int
54 _nl_explode_name (char *name,
55 const char **language, const char **modifier,
56 const char **territory, const char **codeset,
57 const char **normalized_codeset)
59 char *cp;
60 int mask;
62 *modifier = NULL;
63 *territory = NULL;
64 *codeset = NULL;
65 *normalized_codeset = NULL;
67 /* Now we determine the single parts of the locale name. First
68 look for the language. Termination symbols are `_', '.', and `@'. */
69 mask = 0;
70 *language = cp = name;
71 cp = _nl_find_language (*language);
73 if (*language == cp)
74 /* This does not make sense: language has to be specified. Use
75 this entry as it is without exploding. Perhaps it is an alias. */
76 cp = strchr (*language, '\0');
77 else
79 if (cp[0] == '_')
81 /* Next is the territory. */
82 cp[0] = '\0';
83 *territory = ++cp;
85 while (cp[0] != '\0' && cp[0] != '.' && cp[0] != '@')
86 ++cp;
88 mask |= XPG_TERRITORY;
91 if (cp[0] == '.')
93 /* Next is the codeset. */
94 cp[0] = '\0';
95 *codeset = ++cp;
97 while (cp[0] != '\0' && cp[0] != '@')
98 ++cp;
100 mask |= XPG_CODESET;
102 if (*codeset != cp && (*codeset)[0] != '\0')
104 *normalized_codeset = _nl_normalize_codeset (*codeset,
105 cp - *codeset);
106 if (*normalized_codeset == NULL)
107 return -1;
108 else if (strcmp (*codeset, *normalized_codeset) == 0)
109 free ((char *) *normalized_codeset);
110 else
111 mask |= XPG_NORM_CODESET;
116 if (cp[0] == '@')
118 /* Next is the modifier. */
119 cp[0] = '\0';
120 *modifier = ++cp;
122 if (cp[0] != '\0')
123 mask |= XPG_MODIFIER;
126 if (*territory != NULL && (*territory)[0] == '\0')
127 mask &= ~XPG_TERRITORY;
129 if (*codeset != NULL && (*codeset)[0] == '\0')
130 mask &= ~XPG_CODESET;
132 return mask;