1 #include <windows.h>
2 #include <io.h>
3 #include <errno.h>
4 #include <msvcrt.h>
5
6 static errno_t __cdecl _int_sopen_s(int *, const char *, int, int, int);
7 static errno_t __cdecl _stub(int *, const char *, int, int, int);
8
9 errno_t __cdecl (*__MINGW_IMP_SYMBOL(_sopen_s))(int *, const char *, int, int, int) = _stub;
10
11 static errno_t __cdecl
_stub(int * pfh,const char * filename,int oflag,int shflag,int pmode)12 _stub (int* pfh, const char *filename, int oflag, int shflag, int pmode)
13 {
14 errno_t __cdecl (*f)(int *, const char *, int, int, int) = __MINGW_IMP_SYMBOL(_sopen_s);
15
16 if (f == _stub) {
17 f = (errno_t __cdecl (*)(int *, const char *, int, int, int))
18 GetProcAddress (__mingw_get_msvcrt_handle (), "_sopen_s");
19 if (f == NULL)
20 f = _int_sopen_s;
21 __MINGW_IMP_SYMBOL(_sopen_s) = f;
22 }
23
24 return (*f)(pfh, filename, oflag, shflag, pmode);
25 }
26
_int_sopen_s(int * pfh,const char * filename,int oflag,int shflag,int pmode)27 static errno_t __cdecl _int_sopen_s(int* pfh, const char *filename, int oflag, int shflag, int pmode)
28 {
29 if (pfh == NULL || filename == NULL) {
30 if (pfh != NULL) *pfh = -1;
31 errno = EINVAL;
32 return EINVAL;
33 }
34
35 *pfh = _sopen(filename, oflag, shflag, pmode);
36 return errno;
37 }
38
_sopen_s(int * pfh,const char * filename,int oflag,int shflag,int pmode)39 errno_t __cdecl _sopen_s(int* pfh, const char *filename, int oflag, int shflag, int pmode)
40 {
41 return _stub (pfh, filename, oflag, shflag, pmode);
42 }
43