aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/mingw/secapi/_sopen_s.c
blob: b4f8f6af5b0ceb9d0ad7cd48d15d2c2c7524afe5 (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
#include <windows.h>
#include <io.h>
#include <errno.h>
#include <msvcrt.h>

static errno_t __cdecl _int_sopen_s(int *, const char *, int, int, int);
static errno_t __cdecl _stub(int *, const char *, int, int, int);

errno_t __cdecl (*__MINGW_IMP_SYMBOL(_sopen_s))(int *, const char *, int, int, int) = _stub;

static errno_t __cdecl
_stub (int* pfh, const char *filename, int oflag, int shflag, int pmode)
{
    errno_t __cdecl (*f)(int *, const char *, int, int, int) = __MINGW_IMP_SYMBOL(_sopen_s);

    if (f == _stub) {
        f = (errno_t __cdecl (*)(int *, const char *, int, int, int))
            GetProcAddress (__mingw_get_msvcrt_handle (), "_sopen_s");
        if (f == NULL)
            f = _int_sopen_s;
        __MINGW_IMP_SYMBOL(_sopen_s) = f;
    }

    return (*f)(pfh, filename, oflag, shflag, pmode);
}

static errno_t __cdecl _int_sopen_s(int* pfh, const char *filename, int oflag, int shflag, int pmode)
{
    if (pfh == NULL || filename == NULL) {
        if (pfh != NULL) *pfh = -1;
        errno = EINVAL;
        return EINVAL;
    }

    *pfh = _sopen(filename, oflag, shflag, pmode);
    return errno;
}

errno_t __cdecl _sopen_s(int* pfh, const char *filename, int oflag, int shflag, int pmode)
{
    return _stub (pfh, filename, oflag, shflag, pmode);
}