aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/mingw/secapi/_cgetws_s.c
blob: 01ee3d1b32ba135ebe790a0a2666bea90c4aa7b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <windows.h>
#include <malloc.h>
#include <errno.h>
#include <msvcrt.h>
#include <sec_api/conio_s.h>

static errno_t __cdecl _int_cgetws_s (wchar_t *, size_t, size_t *);
static errno_t __cdecl _stub (wchar_t *, size_t, size_t *);

errno_t __cdecl (*__MINGW_IMP_SYMBOL(_cgetws_s))(wchar_t *, size_t, size_t *) = 
 _stub;

static errno_t __cdecl
_stub (wchar_t *s, size_t l, size_t *r_len)
{
  errno_t __cdecl (*f)(wchar_t *, size_t, size_t *) = __MINGW_IMP_SYMBOL(_cgetws_s);

  if (f == _stub)
    {
	f = (errno_t __cdecl (*)(wchar_t *, size_t, size_t *))
	    GetProcAddress (__mingw_get_msvcrt_handle (), "_cgetws_s");
	if (!f)
	  f = _int_cgetws_s;
	__MINGW_IMP_SYMBOL(_cgetws_s) = f;
    }
  return (*f)(s, l, r_len);
}

errno_t __cdecl
_cgetws_s (wchar_t *s, size_t l, size_t *r_len)
{
  return _stub (s, l, r_len);
}

static errno_t __cdecl
_int_cgetws_s (wchar_t *s, size_t l, size_t *r_len)
{
  wchar_t *h, *p;

  if (s && l)
    s[0] = 0;
  if (!s || !l || !r_len)
    {
      _cgetws (NULL);
      return EINVAL;
    }
  p = (wchar_t *) alloca ((l + 2) * sizeof (wchar_t));
  p[0] = l;
  h = _cgetws (s); 
  if (!h)
    return EINVAL;
  *r_len = (size_t) p[1];
  memcpy (s, &p[2], *r_len);
  return 0;
}