Deleted Added
full compact
setrunelocale.c (101488) setrunelocale.c (101498)
1/*-
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Paul Borman at Krystal Technologies.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 21 unchanged lines hidden (view full) ---

30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Paul Borman at Krystal Technologies.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 21 unchanged lines hidden (view full) ---

30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/lib/libc/locale/setrunelocale.c 101488 2002-08-07 20:49:25Z ache $");
38__FBSDID("$FreeBSD: head/lib/libc/locale/setrunelocale.c 101498 2002-08-08 05:51:54Z ache $");
39
40#include <rune.h>
41#include <errno.h>
42#include <limits.h>
43#include <string.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <unistd.h>
47#include "setlocale.h"
48
49extern int _none_init(_RuneLocale *);
50extern int _UTF2_init(_RuneLocale *);
51extern int _EUC_init(_RuneLocale *);
52extern int _BIG5_init(_RuneLocale *);
53extern int _MSKanji_init(_RuneLocale *);
54extern _RuneLocale *_Read_RuneMagi(FILE *);
55
56int
39
40#include <rune.h>
41#include <errno.h>
42#include <limits.h>
43#include <string.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <unistd.h>
47#include "setlocale.h"
48
49extern int _none_init(_RuneLocale *);
50extern int _UTF2_init(_RuneLocale *);
51extern int _EUC_init(_RuneLocale *);
52extern int _BIG5_init(_RuneLocale *);
53extern int _MSKanji_init(_RuneLocale *);
54extern _RuneLocale *_Read_RuneMagi(FILE *);
55
56int
57setrunelocale(encoding)
58 char *encoding;
57setrunelocale(char *encoding)
59{
60 FILE *fp;
61 char name[PATH_MAX];
62 _RuneLocale *rl;
58{
59 FILE *fp;
60 char name[PATH_MAX];
61 _RuneLocale *rl;
62 int saverr, ret;
63 static char ctype_encoding[ENCODING_LEN + 1];
64 static _RuneLocale *CachedRuneLocale;
65 static int Cached__mb_cur_max;
63
64 if (!encoding || !*encoding || strlen(encoding) > ENCODING_LEN ||
65 (encoding[0] == '.' &&
66 (encoding[1] == '\0' ||
67 (encoding[1] == '.' && encoding[2] == '\0'))) ||
68 strchr(encoding, '/') != NULL)
69 return (EINVAL);
70
71 /*
72 * The "C" and "POSIX" locale are always here.
73 */
66
67 if (!encoding || !*encoding || strlen(encoding) > ENCODING_LEN ||
68 (encoding[0] == '.' &&
69 (encoding[1] == '\0' ||
70 (encoding[1] == '.' && encoding[2] == '\0'))) ||
71 strchr(encoding, '/') != NULL)
72 return (EINVAL);
73
74 /*
75 * The "C" and "POSIX" locale are always here.
76 */
74 if (!strcmp(encoding, "C") || !strcmp(encoding, "POSIX")) {
77 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
75 _CurrentRuneLocale = &_DefaultRuneLocale;
76 __mb_cur_max = 1;
77 return (0);
78 }
79
78 _CurrentRuneLocale = &_DefaultRuneLocale;
79 __mb_cur_max = 1;
80 return (0);
81 }
82
83 /*
84 * If the locale name is the same as our cache, use the cache.
85 */
86 if (CachedRuneLocale != NULL &&
87 strcmp(encoding, ctype_encoding) == 0) {
88 _CurrentRuneLocale = CachedRuneLocale;
89 __mb_cur_max = Cached__mb_cur_max;
90 return (0);
91 }
92
93 /*
94 * Slurp the locale file into the cache.
95 */
80 if (_PathLocale == NULL) {
81 char *p = getenv("PATH_LOCALE");
82
83 if (p != NULL
84#ifndef __NETBSD_SYSCALLS
85 && !issetugid()
86#endif
87 ) {
88 if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
89 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
90 return (ENAMETOOLONG);
91 _PathLocale = strdup(p);
92 if (_PathLocale == NULL)
96 if (_PathLocale == NULL) {
97 char *p = getenv("PATH_LOCALE");
98
99 if (p != NULL
100#ifndef __NETBSD_SYSCALLS
101 && !issetugid()
102#endif
103 ) {
104 if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
105 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
106 return (ENAMETOOLONG);
107 _PathLocale = strdup(p);
108 if (_PathLocale == NULL)
93 return (errno);
109 return (ENOMEM);
94 } else
95 _PathLocale = _PATH_LOCALE;
96 }
97 /* Range checking not needed, encoding length already checked above */
98 (void) strcpy(name, _PathLocale);
99 (void) strcat(name, "/");
100 (void) strcat(name, encoding);
101 (void) strcat(name, "/LC_CTYPE");
102
103 if ((fp = fopen(name, "r")) == NULL)
104 return (errno);
105
110 } else
111 _PathLocale = _PATH_LOCALE;
112 }
113 /* Range checking not needed, encoding length already checked above */
114 (void) strcpy(name, _PathLocale);
115 (void) strcat(name, "/");
116 (void) strcat(name, encoding);
117 (void) strcat(name, "/LC_CTYPE");
118
119 if ((fp = fopen(name, "r")) == NULL)
120 return (errno);
121
106 if ((rl = _Read_RuneMagi(fp)) == 0) {
107 fclose(fp);
108 return (EFTYPE);
122 if ((rl = _Read_RuneMagi(fp)) == NULL) {
123 saverr = errno;
124 (void)fclose(fp);
125 return (saverr);
109 }
126 }
110 fclose(fp);
127 (void)fclose(fp);
111
128
112 if (!rl->encoding[0])
113 return (EFTYPE);
114 else if (!strcmp(rl->encoding, "NONE"))
115 return (_none_init(rl));
116 else if (!strcmp(rl->encoding, "UTF2"))
117 return (_UTF2_init(rl));
118 else if (!strcmp(rl->encoding, "EUC"))
119 return (_EUC_init(rl));
120 else if (!strcmp(rl->encoding, "BIG5"))
121 return (_BIG5_init(rl));
122 else if (!strcmp(rl->encoding, "MSKanji"))
123 return (_MSKanji_init(rl));
129 if (strcmp(rl->encoding, "NONE") == 0)
130 ret = _none_init(rl);
131 else if (strcmp(rl->encoding, "UTF2") == 0)
132 ret = _UTF2_init(rl);
133 else if (strcmp(rl->encoding, "EUC") == 0)
134 ret = _EUC_init(rl);
135 else if (strcmp(rl->encoding, "BIG5") == 0)
136 ret = _BIG5_init(rl);
137 else if (strcmp(rl->encoding, "MSKanji") == 0)
138 ret = _MSKanji_init(rl);
124 else
139 else
125 return (EFTYPE);
140 ret = EFTYPE;
141 if (ret == 0) {
142 if (CachedRuneLocale != NULL) {
143 /* See euc.c */
144 if (strcmp(CachedRuneLocale->encoding, "EUC") == 0)
145 free(CachedRuneLocale->variable);
146 free(CachedRuneLocale);
147 }
148 CachedRuneLocale = _CurrentRuneLocale;
149 Cached__mb_cur_max = __mb_cur_max;
150 (void)strcpy(ctype_encoding, encoding);
151 } else
152 free(rl);
153
154 return (ret);
126}
127
155}
156