]> sourceware.org Git - newlib-cygwin.git/blob - winsup/cygwin/miscfuncs.cc
* environ.cc (environ_init): Avoid a compiler warning.
[newlib-cygwin.git] / winsup / cygwin / miscfuncs.cc
1 /* miscfuncs.cc: misc funcs that don't belong anywhere else
2
3 Copyright 1996, 1997, 1998, 1999, 2000 Red Hat, Inc.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #include "winsup.h"
12 #include "cygerrno.h"
13 #include <sys/errno.h>
14 #include <sys/uio.h>
15 #include <assert.h>
16 #include <limits.h>
17 #include <winbase.h>
18 #include <winnls.h>
19
20 long tls_ix = -1;
21
22 const char case_folded_lower[] NO_COPY = {
23 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
24 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
25 32, '!', '"', '#', '$', '%', '&', 39, '(', ')', '*', '+', ',', '-', '.', '/',
26 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
27 '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
28 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '[', 92, ']', '^', '_',
29 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
30 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', 127,
31 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
32 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
33 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
34 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
35 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
36 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
37 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
38 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
39 };
40
41 const char case_folded_upper[] NO_COPY = {
42 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
43 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
44 32, '!', '"', '#', '$', '%', '&', 39, '(', ')', '*', '+', ',', '-', '.', '/',
45 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
46 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
47 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', 92, ']', '^', '_',
48 '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
49 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '{', '|', '}', '~', 127,
50 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
51 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
52 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
53 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
54 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
55 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
56 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
57 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
58 };
59
60 #define ch_case_eq(ch1, ch2) (cyg_tolower(ch1) == cyg_tolower(ch2))
61
62 /* Return TRUE if two strings match up to length n */
63 extern "C" int __stdcall
64 strncasematch (const char *s1, const char *s2, size_t n)
65 {
66 if (s1 == s2)
67 return 1;
68
69 n++;
70 while (--n && *s1)
71 {
72 if (!ch_case_eq (*s1, *s2))
73 return 0;
74 s1++; s2++;
75 }
76 return !n || *s2 == '\0';
77 }
78
79 /* Return TRUE if two strings match */
80 extern "C" int __stdcall
81 strcasematch (const char *s1, const char *s2)
82 {
83 if (s1 == s2)
84 return 1;
85
86 while (*s1)
87 {
88 if (!ch_case_eq (*s1, *s2))
89 return 0;
90 s1++; s2++;
91 }
92 return *s2 == '\0';
93 }
94
95 extern "C" char * __stdcall
96 strcasestr (const char *searchee, const char *lookfor)
97 {
98 if (*searchee == 0)
99 {
100 if (*lookfor)
101 return NULL;
102 return (char *) searchee;
103 }
104
105 while (*searchee)
106 {
107 int i = 0;
108 while (1)
109 {
110 if (lookfor[i] == 0)
111 return (char *) searchee;
112
113 if (!ch_case_eq (lookfor[i], searchee[i]))
114 break;
115 lookfor++;
116 }
117 searchee++;
118 }
119
120 return NULL;
121 }
122
123 int __stdcall
124 check_null_str (const char *name)
125 {
126 if (name && !IsBadStringPtr (name, MAX_PATH))
127 return 0;
128
129 return EFAULT;
130 }
131
132 int __stdcall
133 check_null_empty_str (const char *name)
134 {
135 if (name && !IsBadStringPtr (name, MAX_PATH))
136 return !*name ? ENOENT : 0;
137
138 return EFAULT;
139 }
140
141 int __stdcall
142 check_null_empty_str_errno (const char *name)
143 {
144 int __err;
145 if ((__err = check_null_empty_str (name)))
146 set_errno (__err);
147 return __err;
148 }
149
150 int __stdcall
151 check_null_str_errno (const char *name)
152 {
153 int __err;
154 if ((__err = check_null_str (name)))
155 set_errno (__err);
156 return __err;
157 }
158
159 int __stdcall
160 __check_null_invalid_struct (void *s, unsigned sz)
161 {
162 if (s && !IsBadWritePtr (s, sz))
163 return 0;
164
165 return EFAULT;
166 }
167
168 int __stdcall
169 __check_null_invalid_struct_errno (void *s, unsigned sz)
170 {
171 int err;
172 if ((err = __check_null_invalid_struct (s, sz)))
173 set_errno (err);
174 return err;
175 }
176
177 int __stdcall
178 __check_invalid_read_ptr_errno (const void *s, unsigned sz)
179 {
180 if (s && !IsBadReadPtr (s, sz))
181 return 0;
182 return set_errno (EFAULT);
183 }
184
185 ssize_t
186 check_iovec_for_read (const struct iovec *iov, int iovcnt)
187 {
188 if (iovcnt <= 0 || iovcnt > IOV_MAX)
189 {
190 set_errno (EINVAL);
191 return -1;
192 }
193
194 if (__check_invalid_read_ptr_errno (iov, iovcnt * sizeof (*iov)))
195 return -1;
196
197 size_t tot = 0;
198
199 while (iovcnt != 0)
200 {
201 if (iov->iov_len > SSIZE_MAX || (tot += iov->iov_len) > SSIZE_MAX)
202 {
203 set_errno (EINVAL);
204 return -1;
205 }
206
207 if (iov->iov_len
208 && __check_null_invalid_struct_errno (iov->iov_base, iov->iov_len))
209 return -1;
210
211 iov += 1;
212 iovcnt -= 1;
213 }
214
215 assert (tot <= SSIZE_MAX);
216
217 return (ssize_t) tot;
218 }
219
220 ssize_t
221 check_iovec_for_write (const struct iovec *iov, int iovcnt)
222 {
223 if (iovcnt <= 0 || iovcnt > IOV_MAX)
224 {
225 set_errno (EINVAL);
226 return -1;
227 }
228
229 if (__check_invalid_read_ptr_errno (iov, iovcnt * sizeof (*iov)))
230 return -1;
231
232 size_t tot = 0;
233
234 while (iovcnt != 0)
235 {
236 if (iov->iov_len > SSIZE_MAX || (tot += iov->iov_len) > SSIZE_MAX)
237 {
238 set_errno (EINVAL);
239 return -1;
240 }
241
242 if (iov->iov_len
243 && __check_invalid_read_ptr_errno (iov->iov_base, iov->iov_len))
244 return -1;
245
246 iov += 1;
247 iovcnt -= 1;
248 }
249
250 assert (tot <= SSIZE_MAX);
251
252 return (ssize_t) tot;
253 }
254
255 UINT
256 get_cp ()
257 {
258 return current_codepage == ansi_cp ? GetACP() : GetOEMCP();
259 }
260
261 int __stdcall
262 sys_wcstombs (char *tgt, const WCHAR *src, int len)
263 {
264 return WideCharToMultiByte (get_cp (), 0, src, -1, tgt, len, NULL, NULL);
265 }
266
267 int __stdcall
268 sys_mbstowcs (WCHAR *tgt, const char *src, int len)
269 {
270 return MultiByteToWideChar (get_cp (), 0, src, -1, tgt, len);
271 }
This page took 0.052093 seconds and 5 git commands to generate.