What is the equivalent to _fsopen in pellesc?

Started by rxantos, May 17, 2014, 07:19:00 AM

Previous topic - Next topic

rxantos

For logging purposes I need to allow reading from a file on another process while writing to it on the current process.

On Visual C++ I use _fsopen with the _SH_DENYWR flag for that purpose. (as fopen locks the file for both reading and writing on visual c++)  What would be the equivalent on Pelles C?

_fsopen
http://msdn.microsoft.com/en-us/library/8f30b0db.aspx


Vortex

Hi rxantos,

_fsopen is exported by msvcrt.dll :

podump.exe /EXPORTS C:\WINDOWS\system32\msvcrt.dll | findstr "_fsopen"
            113   112  77C2EFAF  _fsopen


You should link against msvcrt.lib
Code it... That's all...

frankie

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.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide