NO

Author Topic: What is the equivalent to _fsopen in pellesc?  (Read 3050 times)

rxantos

  • Guest
What is the equivalent to _fsopen in pellesc?
« on: May 17, 2014, 07:19:00 AM »
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


Offline Vortex

  • Member
  • *
  • Posts: 803
    • http://www.vortex.masmcode.com
Re: What is the equivalent to _fsopen in pellesc?
« Reply #1 on: May 17, 2014, 10:33:46 AM »
Hi rxantos,

_fsopen is exported by msvcrt.dll :

Code: [Select]
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...

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2097
Re: What is the equivalent to _fsopen in pellesc?
« Reply #2 on: May 18, 2014, 06:11:32 PM »
You can use _sopen to open file with denyied rights then assign it to a stream using _fdopen.
A complete replacement could be:
Code: [Select]
#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