_chmod

Description

Changes the file-permission settings.

#include <sys\types.h>    
#include <sys\stat.h>    
#include <errno.h>    
#include <io.h> Required only for function declarations  

int _chmod( char *filename, intpmode );

filename Path name of existing file  
pmode Permission setting for file  

Remarks

The _chmod function changes the permission setting of the file specified by filename. The permission setting controls read and write access to the file. The constant expression pmode contains one or both of the manifest constants _S_IWRITE and _S_IREAD, defined in SYS\STAT.H. Any other values for pmode are ignored. When both constants are given, they are joined with the bitwise-OR operator (|). The meaning of the pmode argument is as follows:

Value Meaning

_S_IWRITE Writing permitted
_S_IREAD Reading permitted
_S_IREAD|_S_IWRITE Reading and writing permitted

If write permission is not given, the file is read-only. Note that all files are always readable; it is not possible to give write-only permission. Thus the modes _S_IWRITE and _S_IREAD| _S_IWRITE are equivalent.

Return Value

The _chmod function returns the value 0 if the permission setting is successfully changed. A return value of –1 indicates an error; in this case, errno is set to ENOENT, indicating that the specified file could not be found.

Compatibility

Standards:UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

Use _chmod for compatibility with ANSI naming conventions of non-ANSI functions. Use chmod and link with OLDNAMES.LIB for UNIX compatibility.

See Also

_access, _creat, _fstat, _open, _stat

Example

/* 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>

void main( void )

{

/* Make file read-only: */

if( _chmod( "CHMOD.C", _S_IREAD ) == -1 )

perror( "File not found\n" );

else

printf( "Mode changed to read-only\n" );

system( "echo /* End of file */ >> CHMOD.C" );

/* Change back to read/write: */

if( _chmod( "CHMOD.C", _S_IWRITE ) == -1 )

perror( "File not found\n" );

else

printf( "Mode changed to read/write\n" );

}

Output

Mode changed to read-only

Access denied

Mode changed to read/write