You can use _sopen to open file with denyied rights then assign it to a stream using _fdopen.
A complete replacement could be:
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <errno.h>
FILE *_fsopen(const char *filename, const char *mode, int shflag)
{
unsigned int m = 0;
unsigned int c = 0;
switch (*mode++)
{
case 'r': //open for reading
m = _O_RDONLY;
c = _S_IREAD;
break;
case 'w': //open for writing
m = _O_WRONLY | _O_CREAT | _O_TRUNC;
c = _S_IREAD | _S_IWRITE;
break;
case 'a': //open for appending
m = _O_WRONLY | _O_CREAT | _O_APPEND;
c = _S_IREAD | _S_IWRITE;
break;
default: //illegal mode
errno = EINVAL;
return NULL;
}
for (; *mode; mode++)
switch (*mode)
{
case '+':
c = _S_IREAD | _S_IWRITE;
m |= _O_RDWR;
break;
case 'b':
m |= _O_BINARY;
break;
default:
break;
}
int handle = _sopen(filename, m, shflag, c);
if (!handle)
return NULL;
return _fdopen(handle, mode);
}
I just wrote it, but not checked. Please let us know if it works for you.