xref: /reactos/sdk/lib/ucrt/filesystem/chmod.cpp (revision fe93a3f9)
1 //
2 // chmod.cpp
3 //
4 //      Copyright (c) Microsoft Corporation. All rights reserved.
5 //
6 // The _chmod() function, which changes file attributes.
7 //
8 #include <corecrt_internal.h>
9 #include <io.h>
10 #include <sys/stat.h>
11 #include <corecrt_internal_win32_buffer.h>
12 
13 // Changes the mode of a file.  The only supported mode bit is _S_IWRITE, which
14 // controls the user write (read-only) attribute of the file.  Returns zero if
15 // successful; returns -1 and sets errno and _doserrno on failure.
16 extern "C" int __cdecl _chmod(char const* const path, int const mode)
17 {
18     if (path == nullptr) {
19         return _wchmod(nullptr, mode);
20     }
21 
22     __crt_internal_win32_buffer<wchar_t> wide_path;
23 
24     errno_t const cvt = __acrt_mbs_to_wcs_cp(path, wide_path, __acrt_get_utf8_acp_compatibility_codepage());
25 
26     if (cvt != 0) {
27         return -1;
28     }
29 
30     return _wchmod(wide_path.data(), mode);
31 }
32