66a83f3eac
where appropriate. * globals.cc: New file for generic global variables. * mkglobals_h: New file to generate globals.h. * mkstatic: New Script used to build a (currently non-working) static libcygwin_s.a. * Makefile.in: Add unused rule to build a non-working libcygwin_s.a. (DLL_OFILES): Add globals.o. Make all objects rely on globals.h. (globals.h): New target. Generate globals.h. * cygtls.h: Honor new CYGTLS_HANDLE define to control when the HANDLE operator is allowed in _cygtls. * dcrt0.cc: Move most globals to globals.cc. * init.cc: Ditto. * environ.cc (strip_title_path): Remove now-unneeded extern. * fhandler_serial.cc (fhandler_serial::open): Ditto. * pinfo.cc: Ditto. (commune_process): Ditto. * shared.cc: Ditto. * glob.cc: Ditto. * strace.cc: Ditto. * exceptions.cc: Define CYGTLS_HANDLE before including winsup.h. * path.cc (stat_suffixes): Move here. * security.h: Add forward class path_conv declaration. * smallprint.cc (__small_vsprintf): Make a true c++ function. (__small_sprintf): Ditto. (small_printf): Ditto. (console_printf): Ditto. (__small_vswprintf): Ditto. (__small_swprintf): Ditto. * spawn.cc (spawn_guts): Remove _stdcall decoration in favor of regparm. (hExeced): Move to globals.cc * strfuncs.cc (current_codepage): Ditto. (active_codepage): Ditto. * sync.cc (lock_process::locker): Move here from dcrt0.cc. * syscalls.cc (stat_suffixes): Move to path.cc. * tty.cc (tty::create_master): Uncapitalize fatal warning for consistency. * winsup.h: Include globals.h to declare most of the grab bag list of globals which were previously defined here. * mount.h: Move USER_* defines back to shared_info.h. * speclib: Force temporary directory cleanup.
144 lines
3.9 KiB
C++
144 lines
3.9 KiB
C++
/* strfuncs.cc: misc funcs that don't belong anywhere else
|
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
|
|
2005, 2006, 2007, 2008, 2009 Red Hat, Inc.
|
|
|
|
This file is part of Cygwin.
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
details. */
|
|
|
|
#include "winsup.h"
|
|
#include <stdlib.h>
|
|
#include <wchar.h>
|
|
#include <winnls.h>
|
|
#include <ntdll.h>
|
|
#include "cygerrno.h"
|
|
#include "security.h"
|
|
#include "path.h"
|
|
#include "fhandler.h"
|
|
#include "dtable.h"
|
|
#include "cygheap.h"
|
|
|
|
UINT
|
|
get_cp ()
|
|
{
|
|
if (!active_codepage)
|
|
codepage_init ("ansi");
|
|
return active_codepage;
|
|
}
|
|
|
|
/* tlen is always treated as the maximum buffer size, including the '\0'
|
|
character. sys_wcstombs will always return a 0-terminated result, no
|
|
matter what. */
|
|
int __stdcall
|
|
sys_wcstombs (char *tgt, int tlen, const PWCHAR src, int slen)
|
|
{
|
|
int ret;
|
|
|
|
/* Convert UNICODE private use area. Reverse functionality (only for
|
|
path names) is transform_chars in path.cc. */
|
|
if (slen < 0)
|
|
slen = wcslen (src) + 1;
|
|
WCHAR sbuf[slen];
|
|
memcpy (sbuf, src, slen * sizeof (WCHAR));
|
|
const unsigned char *end = (unsigned char *) (sbuf + slen);
|
|
for (unsigned char *s = ((unsigned char *) sbuf) + 1; s < end;
|
|
s += sizeof (WCHAR))
|
|
if (*s == 0xf0)
|
|
*s = 0;
|
|
ret = WideCharToMultiByte (get_cp (), 0, sbuf, slen, tgt, tlen, NULL, NULL);
|
|
if (ret && tgt)
|
|
{
|
|
ret = (ret < tlen) ? ret : tlen - 1;
|
|
tgt[ret] = '\0';
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
/* Allocate a buffer big enough for the string, always including the
|
|
terminating '\0'. The buffer pointer is returned in *tgt_p, the return
|
|
value is the number of bytes written to the buffer, as usual.
|
|
The "type" argument determines where the resulting buffer is stored.
|
|
It's either one of the cygheap_types values, or it's "HEAP_NOTHEAP".
|
|
In the latter case the allocation uses simple calloc.
|
|
|
|
Note that this code is shared by cygserver (which requires it via
|
|
__small_vsprintf) and so when built there plain calloc is the
|
|
only choice. */
|
|
int __stdcall
|
|
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);
|
|
if (ret)
|
|
{
|
|
size_t tlen = (slen == -1) ? ret : ret + 1;
|
|
|
|
if (type == HEAP_NOTHEAP)
|
|
*tgt_p = (char *) calloc (tlen, sizeof (char));
|
|
else
|
|
*tgt_p = (char *) ccalloc ((cygheap_types) type, tlen, sizeof (char));
|
|
if (!*tgt_p)
|
|
return 0;
|
|
ret = sys_wcstombs (*tgt_p, tlen, src, slen);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int __stdcall
|
|
sys_mbstowcs (PWCHAR tgt, int tlen, const char *src, int slen)
|
|
{
|
|
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, int slen)
|
|
{
|
|
int ret;
|
|
|
|
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 (tlen, sizeof (WCHAR));
|
|
else
|
|
*tgt_p = (PWCHAR) ccalloc ((cygheap_types) type, tlen, sizeof (WCHAR));
|
|
if (!*tgt_p)
|
|
return 0;
|
|
ret = sys_mbstowcs (*tgt_p, tlen, src, slen);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
static WCHAR hex_wchars[] = L"0123456789abcdef";
|
|
|
|
NTSTATUS NTAPI
|
|
RtlInt64ToHexUnicodeString (ULONGLONG value, PUNICODE_STRING dest,
|
|
BOOLEAN append)
|
|
{
|
|
USHORT len = append ? dest->Length : 0;
|
|
if (dest->MaximumLength - len < 16 * (int) sizeof (WCHAR))
|
|
return STATUS_BUFFER_OVERFLOW;
|
|
PWCHAR end = (PWCHAR) ((PBYTE) dest->Buffer + len);
|
|
register PWCHAR p = end + 16;
|
|
while (p-- > end)
|
|
{
|
|
*p = hex_wchars[value & 0xf];
|
|
value >>= 4;
|
|
}
|
|
dest->Length += 16 * sizeof (WCHAR);
|
|
return STATUS_SUCCESS;
|
|
}
|