Repository navigation
Expand file tree
/
Copy pathlocaleio.c
More file actions
177 lines (155 loc) · 4.26 KB
/
Copy pathlocaleio.c
File metadata and controls
177 lines (155 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* $NetBSD: localeio.c,v 1.5 2010/06/19 13:26:52 tnozaki Exp $ */
/*
* Copyright (c) 2008, The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Brian Ginsbach.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form
8000
must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: localeio.c,v 1.5 2010/06/19 13:26:52 tnozaki Exp $");
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <locale.h>
#include <paths.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "localeio.h"
int
_localeio_map_file(const char * __restrict path,
void ** __restrict pvar, size_t * __restrict plenvar)
{
int fd, ret;
struct stat st;
void *var;
size_t lenvar;
_DIAGASSERT(path != NULL);
_DIAGASSERT(pvar != NULL);
_DIAGASSERT(plenvar != NULL);
fd = open(path, O_RDONLY);
if (fd == -1)
return errno;
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 || fstat(fd, &st) == 1) {
ret = errno;
goto error;
}
if (!S_ISREG(st.st_mode)) {
ret = EBADF;
goto error;
}
lenvar = (size_t)st.st_size;
if (lenvar < 1) {
ret = EFTYPE;
goto error;
}
var = mmap(NULL, lenvar, PROT_READ,
MAP_FILE|MAP_PRIVATE, fd, (off_t)0);
if (var == MAP_FAILED) {
ret = errno;
goto error;
}
*pvar = var;
*plenvar = lenvar;
return 0;
error:
return ret;
}
void
_localeio_unmap_file(void *var, size_t lenvar)
{
munmap(var, lenvar);
}
int
__loadlocale(const char *name, size_t nstr, size_t nbytes,
size_t localesize, void *currentlocale)
{
int fd, ret;
unsigned char **ap, *buf, *bp, *cp, *cbp, *ebp;
unsigned char ***locale;
struct stat st;
size_t i, bufsize;
_DIAGASSERT(name != NULL);
_DIAGASSERT(localesize != 0);
_DIAGASSERT(currentlocale != NULL);
if ((fd = open(name, O_RDONLY)) == -1)
return ENOENT;
if ((fstat(fd, &st) == -1) || !S_ISREG(st.st_mode) ||
(st.st_size <= 0)) {
ret = EFTYPE;
goto error1;
}
bufsize = localesize + (size_t)st.st_size;
if ((buf = malloc(bufsize)) == NULL) {
ret = ENOMEM;
goto error1;
}
bp = buf + localesize;
if (read(fd, bp, (size_t)st.st_size) != st.st_size) {
ret = EFTYPE;
goto error2;
}
ap = (unsigned char **)(void *)buf;
for (i = (size_t)0, ebp = buf + bufsize; i < nstr; i++) {
ap[i] = bp;
while (bp != ebp && *bp != '\n')
bp++;
if (bp == ebp) {
ret = EFTYPE;
goto error2;
}
*bp++ = '\0';
}
cp = buf + (sizeof(unsigned char *) * nstr);
for (i = 0, cbp = bp; i < nbytes; i++) {
int n;
while (bp != ebp && *bp != '\n')
bp++;
if (bp == ebp) {
ret = EFTYPE;
goto error2;
}
/* ignore overflow/underflow and bad characters */
n = (unsigned char)strtol((char *)cbp, NULL, 0);
cp[i] = (unsigned char)(n & CHAR_MAX);
cbp = bp;
}
locale = currentlocale;
*locale = (unsigned char **)(void *)buf;
(void)close(fd);
return 0;
error2:
free(buf);
error1:
(void)close(fd);
return ret;
}