home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / file / chmod1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-27  |  1.4 KB  |  52 lines

  1. /* CHMOD1.C illustrates reading and changing file attribute and time using:
  2.  *      access          chmod           utime
  3.  *
  4.  * See CHMOD2.C for a more powerful variation of this program using
  5.  * _dos functions.
  6.  */
  7.  
  8. #include <io.h>
  9. #include <sys\types.h>
  10. #include <sys\stat.h>
  11. #include <sys\utime.h>
  12. #include <stdio.h>
  13. #include <conio.h>
  14. #include <stdlib.h>
  15.  
  16. enum FILEATTRIB { EXIST, WRITE = 2, READ = 4, READWRITE = 6 };
  17.  
  18. /* Exist macro uses access */
  19. #define EXIST( name ) !access( name, EXIST )
  20.  
  21. main( int argc, char *argv[] )
  22. {
  23.     if( !EXIST( argv[1] ) )
  24.     {
  25.         printf( "Syntax:  CHMOD1 <filename>" );
  26.         exit( 1 );
  27.     }
  28.  
  29.     if( !access( argv[1], WRITE ) )
  30.     {
  31.         printf( "File %s is read/write. Change to read only? ", argv[1] );
  32.  
  33.         /* Note: Use stdlib.h for function definition of toupper rather
  34.          * than macro version in ctype.h. Macro side effects would cause
  35.          * macro version to read two keys.
  36.          */
  37.         if( toupper( getch() ) == 'Y' )
  38.             chmod( argv[1], S_IREAD );
  39.     }
  40.     else
  41.     {
  42.         printf( "File %s is read only. Change to read/write? ", argv[1] );
  43.         if( toupper( getch() ) == 'Y' )
  44.             chmod( argv[1], S_IREAD | S_IWRITE );
  45.     }
  46.  
  47.     printf( "\nUpdate file time to current time? " );
  48.     if( toupper( getch() ) == 'Y' )
  49.         utime( argv[1], NULL );
  50.     exit( 0 );
  51. }
  52.