* dcrt0.cc (initial_env): Only use local buffer "buf" if DEBUGGING is

enabled.  Replace calls to GetEnvironmentVariable by calls to
	GetEnvironmentVariableA for clarity.  Call GetEnvironmentVariableA
	with NULL buffer.
	(cygbench): Ditto, drop local buffer.
	* environ.cc (getearly): Call GetEnvironmentVariableA.
	(environ_init): Retrieve unicode environment and convert to current
	codepage locally.
	(getwinenveq): Ditto.
	* exceptions.cc (try_to_debug): Accommodate new sys_mbstowcs calling
	convention.
	* fhandler_clipboard.cc (set_clipboard): Call sys_mbstowcs to retrieve
	required buffer length.
	* fork.cc (frok::child): Call GetEnvironmentVariableA.
	* miscfuncs.cc: Accommodate changed arguments in calls to sys_mbstowcs.
	* sec_auth.cc: Ditto.
	* strfuncs.cc (sys_wcstombs_alloc): Fix formatting.
	(sys_mbstowcs): Change arguments to allow specifying a source string
	length.
	(sys_mbstowcs_alloc): Ditto.
	* uinfo.cc (cygheap_user::ontherange): Accommodate changed arguments in
	calls to sys_mbstowcs.
	* winsup.h (sys_mbstowcs): Adjust declaration.
	(sys_mbstowcs_alloc): Ditto.
This commit is contained in:
Corinna Vinschen 2008-02-25 18:32:23 +00:00
parent ff42f5b1e3
commit 5ab0b5cf52
11 changed files with 103 additions and 65 deletions

View File

@ -1,3 +1,30 @@
2008-02-25 Corinna Vinschen <corinna@vinschen.de>
* dcrt0.cc (initial_env): Only use local buffer "buf" if DEBUGGING is
enabled. Replace calls to GetEnvironmentVariable by calls to
GetEnvironmentVariableA for clarity. Call GetEnvironmentVariableA
with NULL buffer.
(cygbench): Ditto, drop local buffer.
* environ.cc (getearly): Call GetEnvironmentVariableA.
(environ_init): Retrieve unicode environment and convert to current
codepage locally.
(getwinenveq): Ditto.
* exceptions.cc (try_to_debug): Accommodate new sys_mbstowcs calling
convention.
* fhandler_clipboard.cc (set_clipboard): Call sys_mbstowcs to retrieve
required buffer length.
* fork.cc (frok::child): Call GetEnvironmentVariableA.
* miscfuncs.cc: Accommodate changed arguments in calls to sys_mbstowcs.
* sec_auth.cc: Ditto.
* strfuncs.cc (sys_wcstombs_alloc): Fix formatting.
(sys_mbstowcs): Change arguments to allow specifying a source string
length.
(sys_mbstowcs_alloc): Ditto.
* uinfo.cc (cygheap_user::ontherange): Accommodate changed arguments in
calls to sys_mbstowcs.
* winsup.h (sys_mbstowcs): Adjust declaration.
(sys_mbstowcs_alloc): Ditto.
2008-02-20 Corinna Vinschen <corinna@vinschen.de>
* path.cc (cwdstuff::set): Revert error handling change in case

View File

@ -532,14 +532,14 @@ break_here ()
static void
initial_env ()
{
char buf[NT_MAX_PATH];
if (GetEnvironmentVariable ("CYGWIN_TESTING", buf, sizeof (buf) - 1))
if (GetEnvironmentVariableA ("CYGWIN_TESTING", NULL, 0))
_cygwin_testing = 1;
#ifdef DEBUGGING
char buf[NT_MAX_PATH];
DWORD len;
if (GetEnvironmentVariable ("CYGWIN_SLEEP", buf, sizeof (buf) - 1))
if (GetEnvironmentVariableA ("CYGWIN_SLEEP", buf, sizeof (buf) - 1))
{
DWORD ms = atoi (buf);
console_printf ("Sleeping %d, pid %u %P\n", ms, GetCurrentProcessId ());
@ -547,7 +547,7 @@ initial_env ()
if (!strace.active () && !dynamically_loaded)
strace.hello ();
}
if (GetEnvironmentVariable ("CYGWIN_DEBUG", buf, sizeof (buf) - 1))
if (GetEnvironmentVariableA ("CYGWIN_DEBUG", buf, sizeof (buf) - 1))
{
char buf1[NT_MAX_PATH];
len = GetModuleFileName (NULL, buf1, NT_MAX_PATH);
@ -1141,8 +1141,7 @@ multiple_cygwin_problem (const char *what, unsigned magic_version, unsigned vers
return;
}
char buf[1024];
if (GetEnvironmentVariable ("CYGWIN_MISMATCH_OK", buf, sizeof (buf)))
if (GetEnvironmentVariableA ("CYGWIN_MISMATCH_OK", NULL, 0))
return;
if (CYGWIN_VERSION_MAGIC_VERSION (magic_version) == version)
@ -1162,8 +1161,7 @@ are unable to find another cygwin DLL.",
void __stdcall
cygbench (const char *s)
{
char buf[1024];
if (GetEnvironmentVariable ("CYGWIN_BENCH", buf, sizeof (buf)))
if (GetEnvironmentVariableA ("CYGWIN_BENCH", NULL, 0))
small_printf ("%05d ***** %s : %10d\n", GetCurrentProcessId (), s, strace.microseconds ());
}
#endif

View File

@ -12,6 +12,7 @@ details. */
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <wchar.h>
#include <ctype.h>
#include <assert.h>
#include <sys/cygwin.h>
@ -252,9 +253,9 @@ getearly (const char * name, int *)
if (strncasematch (name, *ptr, len) && (*ptr)[len] == '=')
return *ptr + len + 1;
}
else if ((len = GetEnvironmentVariable (name, NULL, 0))
else if ((len = GetEnvironmentVariableA (name, NULL, 0))
&& (ret = (char *) cmalloc_abort (HEAP_2_STR, len))
&& GetEnvironmentVariable (name, ret, len))
&& GetEnvironmentVariableA (name, ret, len))
return ret;
return NULL;
@ -730,7 +731,7 @@ regopt (const char *name)
void
environ_init (char **envp, int envc)
{
char *rawenv;
PWCHAR rawenv, w;
int i;
char *p;
char *newp;
@ -781,21 +782,31 @@ environ_init (char **envp, int envc)
/* Allocate space for environment + trailing NULL + CYGWIN env. */
lastenviron = envp = (char **) malloc ((4 + (envc = 100)) * sizeof (char *));
rawenv = GetEnvironmentStrings ();
/* We need the CYGWIN variable content before we can loop through
the whole environment, so that the wide-char to multibyte conversion
can be done according to the "codepage" setting. */
if ((i = GetEnvironmentVariableA ("CYGWIN", NULL, 0)))
{
char *buf = (char *) alloca (i);
GetEnvironmentVariableA ("CYGWIN", buf, i);
parse_options (buf);
}
rawenv = GetEnvironmentStringsW ();
if (!rawenv)
{
system_printf ("GetEnvironmentStrings returned NULL, %E");
return;
}
debug_printf ("GetEnvironmentStrings returned %p - \"%s\"", rawenv, rawenv);
debug_printf ("GetEnvironmentStrings returned %p", rawenv);
/* Current directory information is recorded as variables of the
form "=X:=X:\foo\bar; these must be changed into something legal
(we could just ignore them but maybe an application will
eventually want to use them). */
for (i = 0, p = rawenv; *p != '\0'; p = strchr (p, '\0') + 1, i++)
for (i = 0, w = rawenv; *w != L'\0'; w = wcschr (w, L'\0') + 1, i++)
{
newp = strdup (p);
sys_wcstombs_alloc (&newp, HEAP_NOTHEAP, w);
if (i >= envc)
envp = (char **) realloc (envp, (4 + (envc += 100)) * sizeof (char *));
envp[i] = newp;
@ -816,7 +827,7 @@ environ_init (char **envp, int envc)
if (!sawTERM)
envp[i++] = strdup (cygterm);
envp[i] = NULL;
FreeEnvironmentStrings (rawenv);
FreeEnvironmentStringsW (rawenv);
out:
findenv_func = (char * (*)(const char*, int*)) my_findenv;
@ -848,14 +859,14 @@ env_sort (const void *a, const void *b)
char * __stdcall
getwinenveq (const char *name, size_t namelen, int x)
{
char dum[1];
char name0[namelen - 1];
memcpy (name0, name, namelen - 1);
name0[namelen - 1] = '\0';
int totlen = GetEnvironmentVariable (name0, dum, 0);
WCHAR name0[namelen - 1];
WCHAR valbuf[32768]; /* Max size of an env.var including trailing '\0'. */
name0[sys_mbstowcs (name0, sizeof name0, name, namelen - 1)] = L'\0';
int totlen = GetEnvironmentVariableW (name0, valbuf, 32768);
if (totlen > 0)
{
totlen++;
totlen = sys_wcstombs (NULL, 0, valbuf);
if (x == HEAP_1_STR)
totlen += namelen;
else
@ -863,14 +874,9 @@ getwinenveq (const char *name, size_t namelen, int x)
char *p = (char *) cmalloc_abort ((cygheap_types) x, totlen);
if (namelen)
strcpy (p, name);
if (GetEnvironmentVariable (name0, p + namelen, totlen))
{
debug_printf ("using value from GetEnvironmentVariable for '%s'",
name0);
return p;
}
else
cfree (p);
sys_wcstombs (p + namelen, totlen, valbuf);
debug_printf ("using value from GetEnvironmentVariable for '%W'", name0);
return p;
}
debug_printf ("warning: %s not present in environment", name);

View File

@ -395,7 +395,7 @@ try_to_debug (bool waitloop)
console_printf ("*** starting debugger for pid %u, tid %u\n",
cygwin_pid (GetCurrentProcessId ()), GetCurrentThreadId ());
BOOL dbg;
sys_mbstowcs (dbg_cmd, debugger_command, sizeof debugger_command);
sys_mbstowcs (dbg_cmd, sizeof debugger_command, debugger_command);
dbg = CreateProcessW (NULL,
dbg_cmd,
NULL,

View File

@ -121,7 +121,7 @@ set_clipboard (const void *buf, size_t len)
OpenClipboard (0);
len = MultiByteToWideChar (get_cp (), 0, (const char *) buf, len, NULL, 0);
len = sys_mbstowcs (NULL, 0, (const char *) buf, len);
if (!len)
{
system_printf ("Invalid string");
@ -134,7 +134,7 @@ set_clipboard (const void *buf, size_t len)
return -1;
}
clipbuf = GlobalLock (hmem);
sys_mbstowcs ((PWCHAR) clipbuf, (const char *) buf, len);
sys_mbstowcs ((PWCHAR) clipbuf, len, (const char *) buf);
*((PWCHAR) clipbuf + len) = L'\0';
GlobalUnlock (hmem);
if (!SetClipboardData (CF_UNICODETEXT, hmem))

View File

@ -197,13 +197,12 @@ frok::child (volatile char * volatile here)
cygheap->user.reimpersonate ();
#ifdef DEBUGGING
char c;
if (GetEnvironmentVariable ("FORKDEBUG", &c, 1))
if (GetEnvironmentVariableA ("FORKDEBUG", NULL, 0))
try_to_debug ();
char buf[80];
/* This is useful for debugging fork problems. Use gdb to attach to
the pid reported here. */
if (GetEnvironmentVariable ("CYGWIN_FORK_SLEEP", buf, sizeof (buf)))
if (GetEnvironmentVariableA ("CYGWIN_FORK_SLEEP", buf, sizeof (buf)))
{
small_printf ("Sleeping %d after fork, pid %u\n", atoi (buf), GetCurrentProcessId ());
Sleep (atoi (buf));

View File

@ -116,10 +116,10 @@ cygwin_strcasecmp (const char *cs, const char *ct)
len = (strlen (cs) + 1) * sizeof (WCHAR);
RtlInitEmptyUnicodeString (&us, (PWCHAR) alloca (len), len);
us.Length = sys_mbstowcs (us.Buffer, cs, us.MaximumLength) * sizeof (WCHAR);
us.Length = sys_mbstowcs (us.Buffer, us.MaximumLength, cs) * sizeof (WCHAR);
len = (strlen (ct) + 1) * sizeof (WCHAR);
RtlInitEmptyUnicodeString (&ut, (PWCHAR) alloca (len), len);
ut.Length = sys_mbstowcs (ut.Buffer, ct, ut.MaximumLength) * sizeof (WCHAR);
ut.Length = sys_mbstowcs (ut.Buffer, ut.MaximumLength, ct) * sizeof (WCHAR);
return RtlCompareUnicodeString (&us, &ut, TRUE);
}
@ -134,14 +134,14 @@ cygwin_strncasecmp (const char *cs, const char *ct, size_t n)
++ls;
len = ls * sizeof (WCHAR);
RtlInitEmptyUnicodeString (&us, (PWCHAR) alloca (len), len);
us.Length = MultiByteToWideChar (get_cp (), 0, cs, ls, us.Buffer,
us.MaximumLength) * sizeof (WCHAR);
us.Length = sys_mbstowcs (us.Buffer, us.MaximumLength / sizeof (WCHAR),
cs, ls) * sizeof (WCHAR);
while (ct[lt] && lt < n)
++lt;
len = lt * sizeof (WCHAR);
RtlInitEmptyUnicodeString (&ut, (PWCHAR) alloca (len), len);
ut.Length = MultiByteToWideChar (get_cp (), 0, ct, lt, ut.Buffer,
ut.MaximumLength) * sizeof (WCHAR);
ut.Length = sys_mbstowcs (ut.Buffer, ut.MaximumLength / sizeof (WCHAR),
ct, lt) * sizeof (WCHAR);
return RtlCompareUnicodeString (&us, &ut, TRUE);
}
@ -172,7 +172,7 @@ cygwin_strlwr (char *string)
size_t len = (strlen (string) + 1) * sizeof (WCHAR);
us.MaximumLength = len; us.Buffer = (PWCHAR) alloca (len);
us.Length = sys_mbstowcs (us.Buffer, string, len) * sizeof (WCHAR)
us.Length = sys_mbstowcs (us.Buffer, len, string) * sizeof (WCHAR)
- sizeof (WCHAR);
RtlDowncaseUnicodeString (&us, &us, FALSE);
sys_wcstombs (string, len / sizeof (WCHAR), us.Buffer);
@ -186,7 +186,7 @@ cygwin_strupr (char *string)
size_t len = (strlen (string) + 1) * sizeof (WCHAR);
us.MaximumLength = len; us.Buffer = (PWCHAR) alloca (len);
us.Length = sys_mbstowcs (us.Buffer, string, len) * sizeof (WCHAR)
us.Length = sys_mbstowcs (us.Buffer, len, string) * sizeof (WCHAR)
- sizeof (WCHAR);
RtlUpcaseUnicodeString (&us, &us, FALSE);
sys_wcstombs (string, len / sizeof (WCHAR), us.Buffer);

View File

@ -128,7 +128,7 @@ str2buf2uni (UNICODE_STRING &tgt, WCHAR *buf, const char *srcstr)
{
tgt.Buffer = (PWCHAR) buf;
tgt.MaximumLength = (strlen (srcstr) + 1) * sizeof (WCHAR);
tgt.Length = sys_mbstowcs (buf, srcstr, tgt.MaximumLength / sizeof (WCHAR))
tgt.Length = sys_mbstowcs (buf, tgt.MaximumLength / sizeof (WCHAR), srcstr)
* sizeof (WCHAR);
if (tgt.Length)
tgt.Length -= sizeof (WCHAR);
@ -137,8 +137,9 @@ str2buf2uni (UNICODE_STRING &tgt, WCHAR *buf, const char *srcstr)
void
str2uni_cat (UNICODE_STRING &tgt, const char *srcstr)
{
int len = sys_mbstowcs (tgt.Buffer + tgt.Length / sizeof (WCHAR), srcstr,
(tgt.MaximumLength - tgt.Length) / sizeof (WCHAR));
int len = sys_mbstowcs (tgt.Buffer + tgt.Length / sizeof (WCHAR),
(tgt.MaximumLength - tgt.Length) / sizeof (WCHAR),
srcstr);
if (len)
tgt.Length += (len - 1) * sizeof (WCHAR);
else
@ -186,7 +187,7 @@ get_logon_server (const char *domain, char *server, WCHAR *wserver,
{
server[0] = server[1] = '\\';
if (wserver)
sys_mbstowcs (wserver, server, INTERNET_MAX_HOST_NAME_LENGTH + 1);
sys_mbstowcs (wserver, INTERNET_MAX_HOST_NAME_LENGTH + 1, server);
return true;
}
@ -196,7 +197,7 @@ get_logon_server (const char *domain, char *server, WCHAR *wserver,
if (dret == ERROR_SUCCESS)
{
strcpy (server, pci->DomainControllerName);
sys_mbstowcs (wserver, server, INTERNET_MAX_HOST_NAME_LENGTH + 1);
sys_mbstowcs (wserver, INTERNET_MAX_HOST_NAME_LENGTH + 1, server);
NetApiBufferFree (pci);
debug_printf ("DC: rediscovery: %d, server: %s", rediscovery, server);
return true;
@ -204,7 +205,7 @@ get_logon_server (const char *domain, char *server, WCHAR *wserver,
else if (dret == ERROR_PROC_NOT_FOUND)
{
/* NT4 w/o DSClient */
sys_mbstowcs (wdomain, domain, INTERNET_MAX_HOST_NAME_LENGTH + 1);
sys_mbstowcs (wdomain, INTERNET_MAX_HOST_NAME_LENGTH + 1, domain);
if (rediscovery)
dret = NetGetAnyDCName (NULL, wdomain, (LPBYTE *) &buf);
else
@ -230,7 +231,7 @@ get_user_groups (WCHAR *wlogonserver, cygsidlist &grp_list, char *user,
{
char dgroup[INTERNET_MAX_HOST_NAME_LENGTH + GNLEN + 2];
WCHAR wuser[UNLEN + 1];
sys_mbstowcs (wuser, user, UNLEN + 1);
sys_mbstowcs (wuser, UNLEN + 1, user);
LPGROUP_USERS_INFO_0 buf;
DWORD cnt, tot, len;
NET_API_STATUS ret;

View File

@ -64,7 +64,7 @@ sys_wcstombs_alloc (char **tgt_p, int type, const PWCHAR src, int slen)
{
int ret;
ret = WideCharToMultiByte (get_cp (), 0, src, slen, NULL, 0,NULL, NULL);
ret = WideCharToMultiByte (get_cp (), 0, src, slen, NULL, 0 ,NULL, NULL);
if (ret)
{
size_t tlen = (slen == -1 ? ret : ret + 1);
@ -81,28 +81,35 @@ sys_wcstombs_alloc (char **tgt_p, int type, const PWCHAR src, int slen)
}
int __stdcall
sys_mbstowcs (PWCHAR tgt, const char *src, int len)
sys_mbstowcs (PWCHAR tgt, int tlen, const char *src, int slen)
{
int res = MultiByteToWideChar (get_cp (), 0, src, -1, tgt, len);
return res;
int ret = MultiByteToWideChar (get_cp (), 0, src, slen, tgt, tlen);
if (ret && tgt)
{
ret = (ret < tlen) ? ret : tlen - 1;
tgt[ret] = L'\0';
}
return ret;
}
/* Same as sys_wcstombs_alloc, just backwards. */
int __stdcall
sys_mbstowcs_alloc (PWCHAR *tgt_p, int type, const char *src)
sys_mbstowcs_alloc (PWCHAR *tgt_p, int type, const char *src, int slen)
{
int ret;
ret = MultiByteToWideChar (get_cp (), 0, src, -1, NULL, 0);
ret = MultiByteToWideChar (get_cp (), 0, src, slen, NULL, 0);
if (ret)
{
size_t tlen = (slen == -1 ? ret : ret + 1);
if (type == HEAP_NOTHEAP)
*tgt_p = (PWCHAR) calloc (ret, sizeof (WCHAR));
*tgt_p = (PWCHAR) calloc (tlen, sizeof (WCHAR));
else
*tgt_p = (PWCHAR) ccalloc ((cygheap_types) type, ret, sizeof (WCHAR));
*tgt_p = (PWCHAR) ccalloc ((cygheap_types) type, tlen, sizeof (WCHAR));
if (!*tgt_p)
return 0;
ret = sys_mbstowcs (*tgt_p, src, ret);
ret = sys_mbstowcs (*tgt_p, tlen, src, slen);
}
return ret;
}

View File

@ -296,9 +296,9 @@ cygheap_user::ontherange (homebodies what, struct passwd *pw)
if (logsrv ())
{
WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
sys_mbstowcs (wlogsrv, logsrv (),
sizeof (wlogsrv) / sizeof (*wlogsrv));
sys_mbstowcs (wuser, winname (), sizeof (wuser) / sizeof (*wuser));
sys_mbstowcs (wlogsrv, sizeof (wlogsrv) / sizeof (*wlogsrv),
logsrv ());
sys_mbstowcs (wuser, sizeof (wuser) / sizeof (*wuser), winname ());
if (!(ret = NetUserGetInfo (wlogsrv, wuser, 3, (LPBYTE *) &ui)))
{
sys_wcstombs (homepath_env_buf, CYG_MAX_PATH,

View File

@ -139,9 +139,9 @@ int __stdcall sys_wcstombs (char *, int, const PWCHAR, int = -1)
int __stdcall sys_wcstombs_alloc (char **, int, const PWCHAR, int = -1)
__attribute__ ((regparm(3)));
int __stdcall sys_mbstowcs (PWCHAR, const char *, int)
int __stdcall sys_mbstowcs (PWCHAR, int, const char *, int = -1)
__attribute__ ((regparm(3)));
int __stdcall sys_mbstowcs_alloc (PWCHAR *, int, const char *)
int __stdcall sys_mbstowcs_alloc (PWCHAR *, int, const char *, int = -1)
__attribute__ ((regparm(3)));
/* Used to check if Cygwin DLL is dynamically loaded. */