OSDN Git Service

* environ.cc (regopt): Change the first argument to wide char string.
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / miscfuncs.cc
1 /* miscfuncs.cc: misc funcs that don't belong anywhere else
2
3    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4    2005, 2006, 2007, 2008 Red Hat, Inc.
5
6 This file is part of Cygwin.
7
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
10 details. */
11
12 #include "winsup.h"
13 #include "miscfuncs.h"
14 #include <sys/uio.h>
15 #include <assert.h>
16 #include <alloca.h>
17 #include <limits.h>
18 #include <wchar.h>
19 #include <wingdi.h>
20 #include <winuser.h>
21 #include <winnls.h>
22 #include "cygtls.h"
23 #include "ntdll.h"
24
25 long tls_ix = -1;
26
27 const char case_folded_lower[] NO_COPY = {
28    0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,  15,
29   16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,
30   32, '!', '"', '#', '$', '%', '&',  39, '(', ')', '*', '+', ',', '-', '.', '/',
31  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
32  '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
33  'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '[',  92, ']', '^', '_',
34  '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
35  'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', 127,
36  128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
37  144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
38  160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
39  176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
40  192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
41  208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
42  224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
43  240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
44 };
45
46 const char case_folded_upper[] NO_COPY = {
47    0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,  15,
48   16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,
49   32, '!', '"', '#', '$', '%', '&',  39, '(', ')', '*', '+', ',', '-', '.', '/',
50  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
51  '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
52  'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[',  92, ']', '^', '_',
53  '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
54  'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '{', '|', '}', '~', 127,
55  128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
56  144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
57  160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
58  176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
59  192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
60  208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
61  224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
62  240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
63 };
64
65 const char isalpha_array[] NO_COPY = {
66    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
67    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
68    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
69    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
70    0,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
71 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,   0,   0,   0,   0,   0,
72    0,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
73 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,   0,   0,   0,   0,   0,
74    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
75    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
76    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
77    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
78    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
79    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
80    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
81    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0
82 };
83
84 extern "C" int __stdcall
85 cygwin_wcscasecmp (const wchar_t *ws, const wchar_t *wt)
86 {
87   UNICODE_STRING us, ut;
88
89   RtlInitUnicodeString (&us, ws);
90   RtlInitUnicodeString (&ut, wt);
91   return RtlCompareUnicodeString (&us, &ut, TRUE);
92 }
93
94 extern "C" int __stdcall
95 cygwin_wcsncasecmp (const wchar_t  *ws, const wchar_t *wt, size_t n)
96 {
97   UNICODE_STRING us, ut;
98   size_t ls = 0, lt = 0;
99
100   while (ws[ls] && ls < n)
101     ++ls;
102   RtlInitCountedUnicodeString (&us, ws, ls * sizeof (WCHAR));
103   while (wt[lt] && lt < n)
104     ++lt;
105   RtlInitCountedUnicodeString (&ut, wt, lt * sizeof (WCHAR));
106   return RtlCompareUnicodeString (&us, &ut, TRUE);
107 }
108
109 extern "C" int __stdcall
110 cygwin_strcasecmp (const char *cs, const char *ct)
111 {
112   UNICODE_STRING us, ut;
113   ULONG len;
114
115   len = (strlen (cs) + 1) * sizeof (WCHAR);
116   RtlInitEmptyUnicodeString (&us, (PWCHAR) alloca (len), len);
117   us.Length = sys_mbstowcs (us.Buffer, us.MaximumLength, cs) * sizeof (WCHAR);
118   len = (strlen (ct) + 1) * sizeof (WCHAR);
119   RtlInitEmptyUnicodeString (&ut, (PWCHAR) alloca (len), len);
120   ut.Length = sys_mbstowcs (ut.Buffer, ut.MaximumLength, ct) * sizeof (WCHAR);
121   return RtlCompareUnicodeString (&us, &ut, TRUE);
122 }
123
124 extern "C" int __stdcall
125 cygwin_strncasecmp (const char *cs, const char *ct, size_t n)
126 {
127   UNICODE_STRING us, ut;
128   ULONG len;
129   size_t ls = 0, lt = 0;
130
131   while (cs[ls] && ls < n)
132     ++ls;
133   len = (ls + 1) * sizeof (WCHAR);
134   RtlInitEmptyUnicodeString (&us, (PWCHAR) alloca (len), len);
135   us.Length = sys_mbstowcs (us.Buffer, ls + 1, cs, ls) * sizeof (WCHAR);
136   while (ct[lt] && lt < n)
137     ++lt;
138   len = (lt + 1) * sizeof (WCHAR);
139   RtlInitEmptyUnicodeString (&ut, (PWCHAR) alloca (len), len);
140   ut.Length = sys_mbstowcs (ut.Buffer, lt + 1, ct, lt)  * sizeof (WCHAR);
141   return RtlCompareUnicodeString (&us, &ut, TRUE);
142 }
143
144 extern "C" char * __stdcall
145 cygwin_strlwr (char *string)
146 {
147   UNICODE_STRING us;
148   size_t len = (strlen (string) + 1) * sizeof (WCHAR);
149
150   us.MaximumLength = len; us.Buffer = (PWCHAR) alloca (len);
151   us.Length = sys_mbstowcs (us.Buffer, len, string) * sizeof (WCHAR)
152               - sizeof (WCHAR);
153   RtlDowncaseUnicodeString (&us, &us, FALSE);
154   sys_wcstombs (string, len / sizeof (WCHAR), us.Buffer);
155   return string;
156 }
157
158 extern "C" char * __stdcall
159 cygwin_strupr (char *string)
160 {
161   UNICODE_STRING us;
162   size_t len = (strlen (string) + 1) * sizeof (WCHAR);
163
164   us.MaximumLength = len; us.Buffer = (PWCHAR) alloca (len);
165   us.Length = sys_mbstowcs (us.Buffer, len, string) * sizeof (WCHAR)
166               - sizeof (WCHAR);
167   RtlUpcaseUnicodeString (&us, &us, FALSE);
168   sys_wcstombs (string, len / sizeof (WCHAR), us.Buffer);
169   return string;
170 }
171
172 int __stdcall
173 check_invalid_virtual_addr (const void *s, unsigned sz)
174 {
175   MEMORY_BASIC_INFORMATION mbuf;
176   const void *end;
177
178   for (end = (char *) s + sz; s < end;
179        s = (char *) mbuf.BaseAddress + mbuf.RegionSize)
180     if (!VirtualQuery (s, &mbuf, sizeof mbuf))
181       return EINVAL;
182   return 0;
183 }
184
185 static char __attribute__ ((noinline))
186 dummytest (volatile char *p)
187 {
188   return *p;
189 }
190
191 ssize_t
192 check_iovec (const struct iovec *iov, int iovcnt, bool forwrite)
193 {
194   if (iovcnt <= 0 || iovcnt > IOV_MAX)
195     {
196       set_errno (EINVAL);
197       return -1;
198     }
199
200   myfault efault;
201   if (efault.faulted (EFAULT))
202     return -1;
203
204   size_t tot = 0;
205
206   while (iovcnt != 0)
207     {
208       if (iov->iov_len > SSIZE_MAX || (tot += iov->iov_len) > SSIZE_MAX)
209         {
210           set_errno (EINVAL);
211           return -1;
212         }
213
214       volatile char *p = ((char *) iov->iov_base) + iov->iov_len - 1;
215       if (!iov->iov_len)
216         /* nothing to do */;
217       else if (!forwrite)
218         *p  = dummytest (p);
219       else
220         dummytest (p);
221
222       iov++;
223       iovcnt--;
224     }
225
226   assert (tot <= SSIZE_MAX);
227
228   return (ssize_t) tot;
229 }
230
231 void
232 yield ()
233 {
234   for (int i = 0; i < 3; i++)
235     SwitchToThread ();
236 }
237
238 /* Get a default value for the nice factor.  When changing these values,
239    have a look into the below function nice_to_winprio.  The values must
240    match the layout of the static "priority" array. */
241 int
242 winprio_to_nice (DWORD prio)
243 {
244   switch (prio)
245     {
246       case REALTIME_PRIORITY_CLASS:
247         return -20;
248       case HIGH_PRIORITY_CLASS:
249         return -16;
250       case ABOVE_NORMAL_PRIORITY_CLASS:
251         return -8;
252       case NORMAL_PRIORITY_CLASS:
253         return 0;
254       case BELOW_NORMAL_PRIORITY_CLASS:
255         return 8;
256       case IDLE_PRIORITY_CLASS:
257         return 16;
258     }
259   return 0;
260 }
261
262 /* Get a Win32 priority matching the incoming nice factor.  The incoming
263    nice is limited to the interval [-NZERO,NZERO-1]. */
264 DWORD
265 nice_to_winprio (int &nice)
266 {
267   static const DWORD priority[] NO_COPY =
268     {
269       REALTIME_PRIORITY_CLASS,          /*  0 */
270       HIGH_PRIORITY_CLASS,              /*  1 */
271       HIGH_PRIORITY_CLASS,
272       HIGH_PRIORITY_CLASS,
273       HIGH_PRIORITY_CLASS,
274       HIGH_PRIORITY_CLASS,
275       HIGH_PRIORITY_CLASS,
276       HIGH_PRIORITY_CLASS,              /*  7 */
277       ABOVE_NORMAL_PRIORITY_CLASS,      /*  8 */
278       ABOVE_NORMAL_PRIORITY_CLASS,
279       ABOVE_NORMAL_PRIORITY_CLASS,
280       ABOVE_NORMAL_PRIORITY_CLASS,
281       ABOVE_NORMAL_PRIORITY_CLASS,
282       ABOVE_NORMAL_PRIORITY_CLASS,
283       ABOVE_NORMAL_PRIORITY_CLASS,
284       ABOVE_NORMAL_PRIORITY_CLASS,      /* 15 */
285       NORMAL_PRIORITY_CLASS,            /* 16 */
286       NORMAL_PRIORITY_CLASS,
287       NORMAL_PRIORITY_CLASS,
288       NORMAL_PRIORITY_CLASS,
289       NORMAL_PRIORITY_CLASS,
290       NORMAL_PRIORITY_CLASS,
291       NORMAL_PRIORITY_CLASS,
292       NORMAL_PRIORITY_CLASS,            /* 23 */
293       BELOW_NORMAL_PRIORITY_CLASS,      /* 24 */
294       BELOW_NORMAL_PRIORITY_CLASS,
295       BELOW_NORMAL_PRIORITY_CLASS,
296       BELOW_NORMAL_PRIORITY_CLASS,
297       BELOW_NORMAL_PRIORITY_CLASS,
298       BELOW_NORMAL_PRIORITY_CLASS,
299       BELOW_NORMAL_PRIORITY_CLASS,
300       BELOW_NORMAL_PRIORITY_CLASS,      /* 31 */
301       IDLE_PRIORITY_CLASS,              /* 32 */
302       IDLE_PRIORITY_CLASS,
303       IDLE_PRIORITY_CLASS,
304       IDLE_PRIORITY_CLASS,
305       IDLE_PRIORITY_CLASS,
306       IDLE_PRIORITY_CLASS,
307       IDLE_PRIORITY_CLASS,
308       IDLE_PRIORITY_CLASS               /* 39 */
309     };
310   if (nice < -NZERO)
311     nice = -NZERO;
312   else if (nice > NZERO - 1)
313     nice = NZERO - 1;
314   DWORD prio = priority[nice + NZERO];
315   if (!wincap.has_extended_priority_class ()
316       && (prio == BELOW_NORMAL_PRIORITY_CLASS
317           || prio == ABOVE_NORMAL_PRIORITY_CLASS))
318     prio = NORMAL_PRIORITY_CLASS;
319   return prio;
320 }
321
322 #undef CreatePipe
323 bool
324 create_pipe (PHANDLE hr,PHANDLE hw, LPSECURITY_ATTRIBUTES sa, DWORD n)
325 {
326   for (int i = 0; i < 10; i++)
327     if (CreatePipe (hr, hw, sa, n))
328       return true;
329     else if (GetLastError () == ERROR_PIPE_BUSY && i < 9)
330       Sleep (10);
331     else
332       break;
333   return false;
334 }
335
336 /* backslashify: Convert all forward slashes in src path to back slashes
337    in dst path.  Add a trailing slash to dst when trailing_slash_p arg
338    is set to 1. */
339
340 void
341 backslashify (const char *src, char *dst, bool trailing_slash_p)
342 {
343   const char *start = src;
344
345   while (*src)
346     {
347       if (*src == '/')
348         *dst++ = '\\';
349       else
350         *dst++ = *src;
351       ++src;
352     }
353   if (trailing_slash_p
354       && src > start
355       && !isdirsep (src[-1]))
356     *dst++ = '\\';
357   *dst++ = 0;
358 }
359
360 /* slashify: Convert all back slashes in src path to forward slashes
361    in dst path.  Add a trailing slash to dst when trailing_slash_p arg
362    is set to 1. */
363
364 void
365 slashify (const char *src, char *dst, bool trailing_slash_p)
366 {
367   const char *start = src;
368
369   while (*src)
370     {
371       if (*src == '\\')
372         *dst++ = '/';
373       else
374         *dst++ = *src;
375       ++src;
376     }
377   if (trailing_slash_p
378       && src > start
379       && !isdirsep (src[-1]))
380     *dst++ = '/';
381   *dst++ = 0;
382 }