1 /* miscfuncs.cc: misc funcs that don't belong anywhere else
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008 Red Hat, Inc.
6 This file is part of Cygwin.
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
13 #include "miscfuncs.h"
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
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
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
84 extern "C" int __stdcall
85 cygwin_wcscasecmp (const wchar_t *ws, const wchar_t *wt)
87 UNICODE_STRING us, ut;
89 RtlInitUnicodeString (&us, ws);
90 RtlInitUnicodeString (&ut, wt);
91 return RtlCompareUnicodeString (&us, &ut, TRUE);
94 extern "C" int __stdcall
95 cygwin_wcsncasecmp (const wchar_t *ws, const wchar_t *wt, size_t n)
97 UNICODE_STRING us, ut;
98 size_t ls = 0, lt = 0;
100 while (ws[ls] && ls < n)
102 RtlInitCountedUnicodeString (&us, ws, ls * sizeof (WCHAR));
103 while (wt[lt] && lt < n)
105 RtlInitCountedUnicodeString (&ut, wt, lt * sizeof (WCHAR));
106 return RtlCompareUnicodeString (&us, &ut, TRUE);
109 extern "C" int __stdcall
110 cygwin_strcasecmp (const char *cs, const char *ct)
112 UNICODE_STRING us, ut;
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);
124 extern "C" int __stdcall
125 cygwin_strncasecmp (const char *cs, const char *ct, size_t n)
127 UNICODE_STRING us, ut;
129 size_t ls = 0, lt = 0;
131 while (cs[ls] && ls < n)
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)
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);
144 extern "C" char * __stdcall
145 cygwin_strlwr (char *string)
148 size_t len = (strlen (string) + 1) * sizeof (WCHAR);
150 us.MaximumLength = len; us.Buffer = (PWCHAR) alloca (len);
151 us.Length = sys_mbstowcs (us.Buffer, len, string) * sizeof (WCHAR)
153 RtlDowncaseUnicodeString (&us, &us, FALSE);
154 sys_wcstombs (string, len / sizeof (WCHAR), us.Buffer);
158 extern "C" char * __stdcall
159 cygwin_strupr (char *string)
162 size_t len = (strlen (string) + 1) * sizeof (WCHAR);
164 us.MaximumLength = len; us.Buffer = (PWCHAR) alloca (len);
165 us.Length = sys_mbstowcs (us.Buffer, len, string) * sizeof (WCHAR)
167 RtlUpcaseUnicodeString (&us, &us, FALSE);
168 sys_wcstombs (string, len / sizeof (WCHAR), us.Buffer);
173 check_invalid_virtual_addr (const void *s, unsigned sz)
175 MEMORY_BASIC_INFORMATION mbuf;
178 for (end = (char *) s + sz; s < end;
179 s = (char *) mbuf.BaseAddress + mbuf.RegionSize)
180 if (!VirtualQuery (s, &mbuf, sizeof mbuf))
185 static char __attribute__ ((noinline))
186 dummytest (volatile char *p)
192 check_iovec (const struct iovec *iov, int iovcnt, bool forwrite)
194 if (iovcnt <= 0 || iovcnt > IOV_MAX)
201 if (efault.faulted (EFAULT))
208 if (iov->iov_len > SSIZE_MAX || (tot += iov->iov_len) > SSIZE_MAX)
214 volatile char *p = ((char *) iov->iov_base) + iov->iov_len - 1;
226 assert (tot <= SSIZE_MAX);
228 return (ssize_t) tot;
234 for (int i = 0; i < 3; i++)
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. */
242 winprio_to_nice (DWORD prio)
246 case REALTIME_PRIORITY_CLASS:
248 case HIGH_PRIORITY_CLASS:
250 case ABOVE_NORMAL_PRIORITY_CLASS:
252 case NORMAL_PRIORITY_CLASS:
254 case BELOW_NORMAL_PRIORITY_CLASS:
256 case IDLE_PRIORITY_CLASS:
262 /* Get a Win32 priority matching the incoming nice factor. The incoming
263 nice is limited to the interval [-NZERO,NZERO-1]. */
265 nice_to_winprio (int &nice)
267 static const DWORD priority[] NO_COPY =
269 REALTIME_PRIORITY_CLASS, /* 0 */
270 HIGH_PRIORITY_CLASS, /* 1 */
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 */
308 IDLE_PRIORITY_CLASS /* 39 */
312 else if (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;
324 create_pipe (PHANDLE hr,PHANDLE hw, LPSECURITY_ATTRIBUTES sa, DWORD n)
326 for (int i = 0; i < 10; i++)
327 if (CreatePipe (hr, hw, sa, n))
329 else if (GetLastError () == ERROR_PIPE_BUSY && i < 9)
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
341 backslashify (const char *src, char *dst, bool trailing_slash_p)
343 const char *start = src;
355 && !isdirsep (src[-1]))
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
365 slashify (const char *src, char *dst, bool trailing_slash_p)
367 const char *start = src;
379 && !isdirsep (src[-1]))