blob: d8907ebb1a805c9f5d9534ef1a0dac25488e0cb6 [file] [log] [blame] [raw]
/* A part of the Windows CE C Extra Library (celibc)
Copyright 2007-2015 PC GO Ld.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/
#include <sys/stat.h>
#include <windows.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
int chmod(const char *path, mode_t mode) {
if(!path) {
errno = EFAULT;
return -1;
}
size_t len = strlen(path) + 1;
if(len == 1) {
errno = ENOENT;
return -1;
}
if(len > PATH_MAX + 1) {
errno = ENAMETOOLONG;
return -1;
}
wchar_t wpath[len];
if((int)mbstowcs(wpath, path, len) < 0) return -1;
unsigned long int attrib = GetFileAttributesW(wpath);
if(attrib == 0xffffffff) return -1;
if(mode & S_IWRITE) attrib &= ~FILE_ATTRIBUTE_READONLY;
else attrib |= FILE_ATTRIBUTE_READONLY;
return SetFileAttributes(wpath, attrib) ? 0 : -1;
}