qc.hlp (Table of Contents; Topic list)
CHMOD1.C
                                             Up Contents Index Back
────────────────────────────────────────────────────────────────────────────
 
/* CHMOD1.C illustrates reading and changing file attribute and time
 * using functions:
 *      access          chmod           utime
 *
 * See CHMOD2.C for a more powerful variation of this program using
 * _dos_ functions.
 */
 
#include <io.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <sys\utime.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
 
enum FILEATTRIB { EXIST, WRITE = 2, READ = 4, READWRITE = 6 };
 
/* Macro uses access */
#define EXIST( name ) !access( name, EXIST )
 
void main( int argc, char *argv[] )
{
    if( !EXIST( argv[1] ) )
    {
        printf( "Syntax:  CHMOD1 <filename>" );
        exit( 1 );
    }
 
    if( !access( argv[1], WRITE ) )
    {
        printf( "File %s is read/write. Change to read only? ", argv[1] );
 
        /* NOTE: Use stdlib.h for function definition of toupper rather
         * than macro version in ctype.h. Macro side effects would cause
         * macro version to read two keys.
         */
        if( toupper( getche() ) == 'Y' )
            chmod( argv[1], S_IREAD );
    }
    else
    {
        printf( "File %s is read only. Change to read/write? ", argv[1] );
        if( toupper( getch() ) == 'Y' )
            chmod( argv[1], S_IREAD | S_IWRITE );
    }
 
    printf( "\nUpdate file time to current time? " );
    if( toupper( getche() ) == 'Y' )
        utime( argv[1], NULL );
    exit( 0 );
}