1 // 2 // wchmod.cpp 3 // 4 // Copyright (c) Microsoft Corporation. All rights reserved. 5 // 6 // The _wchmod() function, which changes file attributes. 7 // 8 #include <corecrt_internal.h> 9 #include <io.h> 10 #include <sys/stat.h> 11 12 13 14 // Changes the mode of a file. The only supported mode bit is _S_IWRITE, which 15 // controls the user write (read-only) attribute of the file. Returns zero if 16 // successful; returns -1 and sets errno and _doserrno on failure. 17 extern "C" int __cdecl _wchmod(wchar_t const* const path, int const mode) 18 { 19 _VALIDATE_CLEAR_OSSERR_RETURN(path != nullptr, EINVAL, -1); 20 21 WIN32_FILE_ATTRIBUTE_DATA attributes; 22 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &attributes)) 23 { 24 __acrt_errno_map_os_error(GetLastError()); 25 return -1; 26 } 27 28 // Set or clear the read-only flag: 29 if (mode & _S_IWRITE) 30 { 31 attributes.dwFileAttributes &= ~FILE_ATTRIBUTE_READONLY; 32 } 33 else 34 { 35 attributes.dwFileAttributes |= FILE_ATTRIBUTE_READONLY; 36 } 37 38 if (!SetFileAttributesW(path, attributes.dwFileAttributes)) 39 { 40 __acrt_errno_map_os_error(GetLastError()); 41 return -1; 42 } 43 44 return 0; 45 } 46