Using _umask() to set the file-permission mask
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: none
To do: File attributes manipulation
To show: Using _umask() to set the file-permission mask
/* This program uses _umask() function to set the file-permission mask so that all future files will be created as read-only files.
Then we create a file for write and test opening the file for writing. Don't forget to add the .h to the header files */
/* for GetLastError() */
#include <windows.h>
#include <fcntl.h>
/* for _S_IWRITE and _S_IREAD */
#include <sys.h>
/* for _SH_DENYNO */
#include <share.h>
#include <io.h>
#include <stdio.h>
int main(void)
{
int fh, oldmask;
/* the file in the given directory */
char test[30] = "e:\\testfolder\\test.txt";
/* Create a read-only files and sets the default file-permission mask. Using the safe version, _umask_s(). If a bit in the mask is 1, the
corresponding bit in the file's requested permission value is set to 0 (disallowed). If a bit in the mask is 0, the corresponding bit is left unchanged.
The permission setting for a new file is not set until the file is closed for the first time. */
/* oldmask = _umask(_S_IWRITE); */
oldmask = _umask_s(_S_IWRITE, &oldmask);
/* fh = _creat(test, _S_IWRITE); */
_sopen_s(&fh, test, _O_CREAT, _SH_DENYNO, _S_IWRITE);
/* non-zero is error */
printf("\nCreating a file and set it as read only...\n");
if(fh == -1)
printf("Couldn't create %s file! error code: %d.\n", test, GetLastError());
else
{
printf("%s file successfully created.\n", test);
_close(fh);
}
printf("Oldmask = 0x%.4x\n", oldmask);
/* Try opening file read only file for write */
/* fh = _open(test, _O_WRONLY); */
_sopen_s(&fh, test, _O_WRONLY, _SH_DENYNO, _S_IWRITE);
printf("\nTry opening the read only file for writing...\n");
if(fh == -1)
printf("%s opening failed!, error code: %d\n", test, GetLastError());
else
{
printf("%s opening succeeded!\n", test);
if(_close(fh) == 0)
printf("%s closed successfully!\n", test);
else
printf("%s cannot be closed, error code: %d\n", test, GetLastError());
}
printf("\nOldmask = 0x%.4x\n", oldmask);
return 0;
}
Output example:
Creating a file and set it as read only...
e:\testfolder\test.txt file successfully created.
Oldmask = 0x0000
Try opening the read only file for writing...
e:\testfolder\test.txt opening failed!, error code: 5
Oldmask = 0x0000
Press any key to continue . . .
System error code 5 means invalid access. Change the following code:
oldmask = _umask_s(_S_IWRITE, &oldmask);
to
oldmask = _umask_s(_S_IREAD, &oldmask);
And change other related literal strings, rebuild and re-run the program. The following output should be expected.
Oldmask = 0x0000
e:\testfolder\test.txt file successfully created.
Oldmask = 0x0000
e:\testfolder\test.txt opening succeeded!
e:\testfolder\test.txt closed successfully!
Oldmask = 0x0000
Press any key to continue . .