Unix to Windows Porting Dictionary for HPC |
|
|
RSS
LinksFunction List
|
Table of Contents The Unix and Windows versions of this function are similar; the biggest difference between the two are the symbols used to determine the permissions of the file. In Windows, these are _S_IWRITE, _S_IREAD, or both. Also, note that while the Unix version may change the permissions for users, groups, or other, Windows does not make those distinctions. Windows also provides a wide-character version of this function, _wchmod().
// crt_chmod.c
// This program uses _chmod to
// change the mode of a file to read-only.
// It then attempts to modify the file.
//
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
// Change the mode and report error or success
void set_mode_and_report(char * filename, int mask)
{
// Check for failure
if( _chmod( filename, mask ) == -1 )
{
// Determine cause of failure and report.
switch (errno)
{
case EINVAL:
fprintf( stderr, "Invalid parameter to chmod.\n");
break;
case ENOENT:
fprintf( stderr, "File %s not found\n", filename );
break;
default:
// Should never be reached
fprintf( stderr, "Unexpected error in chmod.\n" );
}
}
else
{
if (mask == _S_IREAD)
printf( "Mode set to read-only\n" );
else if (mask & _S_IWRITE)
printf( "Mode set to read/write\n" );
}
fflush(stderr);
}
int main( void )
{
// Create or append to a file.
system( "echo /* End of file */ >> crt_chmod.c_input" );
// Set file mode to read-only:
set_mode_and_report("crt_chmod.c_input ", _S_IREAD );
system( "echo /* End of file */ >> crt_chmod.c_input " );
// Change back to read/write:
set_mode_and_report("crt_chmod.c_input ", _S_IWRITE );
system( "echo /* End of file */ >> crt_chmod.c_input " );
}
|
|
|
|