xref: /reactos/sdk/lib/ucrt/lowio/umask.cpp (revision 2eb31495)
1 /***
2 *umask.c - set file permission mask
3 *
4 *       Copyright (c) Microsoft Corporation. All rights reserved.
5 *
6 *Purpose:
7 *       defines _umask() - sets file permission mask of current process*
8 *       affecting files created by creat, open, or sopen.
9 *
10 *******************************************************************************/
11 
12 #include <corecrt_internal_lowio.h>
13 #include <sys/stat.h>
14 
15 extern "C" { int _umaskval = 0; }
16 
17 /***
18 *errno_t _umask(mode, poldmode) - set the file mode mask
19 *
20 *Purpose :
21 *    Works similiar to umask except it validates input params.
22 *
23 *
24 *******************************************************************************/
25 
_umask_s(int const mode,int * const old_mode)26 extern "C" errno_t __cdecl _umask_s(
27     int  const mode,
28     int* const old_mode
29     )
30 {
31     _VALIDATE_RETURN_ERRCODE(old_mode != nullptr, EINVAL);
32     *old_mode = _umaskval;
33     _VALIDATE_RETURN_ERRCODE((mode & ~(_S_IREAD | _S_IWRITE)) == 0, EINVAL);
34 
35     _umaskval = mode;
36     return 0;
37 }
38 
39 /***
40 *int _umask(mode) - set the file mode mask
41 *
42 *Purpose:
43 *       Sets the file-permission mask of the current process* which
44 *       modifies the permission setting of new files created by creat,
45 *       open, or sopen.
46 *
47 *Entry:
48 *       int mode - new file permission mask
49 *                  may contain _S_IWRITE, _S_IREAD, _S_IWRITE | _S_IREAD.
50 *                  The S_IREAD bit has no effect under Win32
51 *
52 *Exit:
53 *       returns the previous setting of the file permission mask.
54 *
55 *Exceptions:
56 *
57 *******************************************************************************/
_umask(int const mode)58 extern "C" int __cdecl _umask(int const mode)
59 {
60     // Silently ignore non-Windows modes:
61     int const masked_mode = mode & (_S_IREAD | _S_IWRITE);
62 
63     int old_mode = 0;
64     _umask_s(masked_mode, &old_mode);
65     return old_mode;
66 }
67