1

I am using the code example shown in post 592448 to try grant full file permisison. When I compile the code snippet using:

 gcc -shared -mno-cygwin -Wall -o native.dll native.c

I get below error:

native.c:8: error: conflicting types for 'mode_t'
/usr/i686-pc-mingw32/sys-root/mingw/include/sys/types.h:99: error: previous declaration of 'mode_t' was here
native.c:21: error: parse error before numeric constant
native.c:22: error: parse error before numeric constant
native.c:23: error: parse error before numeric constant
native.c:25: error: parse error before "mode_t"
native.c:26: error: parse error before "mode_t"
native.c:28: error: parse error before "mode_t"
native.c:29: error: parse error before "mode_t"

I stripped the code down to reduce to below, which compiles fine but do not seem to change the file permission as required.

#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>

#ifdef _WIN32
#   include <io.h>

typedef signed int md_t;
static const md_t MS_MODE_MASK = 0x0000ffff;           ///< low word

int fchmod(const char * path, md_t mode)
{
    int result = _chmod(path, (mode & MS_MODE_MASK));

    if (result != 0)
    {
        result = errno;
    }

    return (result);
}
#else
int fchmod(const char * path, md_t mode)
{
    int result = chmod(path, mode);

    if (result != 0)
    {
        result = errno;
    }

    return (result);
}
#endif

Any pointers on how to get this working?

Community
  • 1
  • 1
Bitmap
  • 12,402
  • 16
  • 64
  • 91

1 Answers1

1

Note that on windows all this can do is set the file to readonly or not, Windows file permissions are different from UNIX type file permissions.

If this is all you want to do: in what way is it not working?

EDIT: regarding the initial error you had mode_t defined elsewhere: /usr/i686-pc-mingw32/sys-root/mingw/include/sys/types.h:99 and tried to redefine it as typedef int mode_t;

from MSDN:

If write permission is not given, the file is read-only. Note that all files are always readable; it is not possible to give write-only permission. Thus the modes _S_IWRITE and _S_IREAD | _S_IWRITE are equivalent.

msam
  • 4,259
  • 3
  • 19
  • 32
  • Calling `fchmod` with my file and mode 777 do not alter the permission of the file to `read,write,exec`. – Bitmap May 04 '12 at 11:19
  • are you compiling this for windows or *nix? – msam May 04 '12 at 11:20
  • compiling and executing in windows – Bitmap May 04 '12 at 11:31
  • That's your problem then. In windows permissions work completely different than on unix. `_chmod` can only change a file to/from readonly. See the [documentation](http://msdn.microsoft.com/en-us/library/1z319a54(v=vs.71).aspx) – msam May 04 '12 at 11:38