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

  1. /* CHMOD2.C illustrates reading and changing file attributes and file
  2.  * time using:
  3.  *      _dos_getftime       _dos_setftime
  4.  *      _dos_getfileattr    _dos_setfileattr
  5.  *
  6.  * See CHMOD1.C for a simpler variation of this program using the utime,
  7.  * access, and chmod functions.
  8.  */
  9.  
  10. #include <dos.h>
  11. #include <fcntl.h>
  12. #include <stdio.h>
  13. #include <conio.h>
  14. #include <stdlib.h>
  15. #include <sys\types.h>
  16. #include <sys\stat.h>
  17.  
  18. char *datestr( unsigned d, char *buf );     /* Prototypes */
  19. char *timestr( unsigned t, char *buf );
  20.  
  21. main( int argc, char *argv[] )
  22. {
  23.     unsigned fdate, ftime, fattr;
  24.     struct dosdate_t ddate;
  25.     struct dostime_t dtime;
  26.     int hsource;
  27.     char timebuf[10], datebuf[10], *pkind;
  28.  
  29.     /* Open to get file handle and test for errors (such as nonexistence). */
  30.     if( _dos_open( argv[1], O_RDONLY, &hsource ) )
  31.     {
  32.         printf( "Can't open file\n" );
  33.         exit( 1 );
  34.     }
  35.  
  36.     /* Get time, date, and attribute of file. */
  37.     _dos_getftime( hsource, &fdate, &ftime );
  38.     _dos_getfileattr( argv[1], &fattr );
  39.  
  40.     /* Convert information into formatted strings. */
  41.     datestr( fdate, datebuf );
  42.     timestr( ftime, timebuf );
  43.  
  44.     if( fattr & _A_SUBDIR )
  45.         pkind = "Directory";
  46.     else if( fattr & _A_VOLID )
  47.         pkind = "Label";
  48.     else
  49.         pkind = "File";
  50.  
  51.     printf( "%-12s   %-8s   %-9s   %-9s    %s %s %s\n",
  52.             "FILE", "TIME", "DATE", "KIND", "RDO", "HID", "SYS" );
  53.     printf( "%-12s   %8s   %8s    %-9s     %c   %c   %c\n",
  54.             argv[1], timebuf, datebuf, pkind,
  55.             (fattr & _A_RDONLY) ? 'Y' : 'N',
  56.             (fattr & _A_HIDDEN) ? 'Y' : 'N',
  57.             (fattr & _A_SYSTEM) ? 'Y' : 'N' );
  58.  
  59.     /* Update file time or attribute. */
  60.     printf( "Change: (T)ime  (R)ead only  (H)idden  (S)ystem\n" );
  61.     switch( toupper( getch() ) )    /* Use stdlib.h, not ctype.h */
  62.     {
  63.         case 'T':               /* Set to current time */
  64.             _dos_gettime( &dtime );
  65.             _dos_getdate( &ddate );
  66.             ftime = (dtime.hour << 11) | (dtime.minute << 5);
  67.             fdate = ((ddate.year - 1980) << 9) | (ddate.month << 5) |
  68.                       ddate.day;
  69.             _dos_setftime( hsource, fdate, ftime );
  70.             break;
  71.         case 'R':               /* Toggle read only */
  72.             _dos_setfileattr( argv[1], fattr ^ _A_RDONLY );
  73.             break;
  74.         case 'H':               /* Toggle hidden    */
  75.             _dos_setfileattr( argv[1], fattr ^ _A_HIDDEN );
  76.             break;
  77.         case 'S':               /* Toggle system    */
  78.             _dos_setfileattr( argv[1], fattr ^ _A_SYSTEM );
  79.             break;
  80.     }
  81.     _dos_close( hsource );
  82.     exit( 1 );
  83. }
  84.  
  85. /* Take unsigned time in the format:                fedcba9876543210
  86.  * s=2 sec incr, m=0-59, h=23                       hhhhhmmmmmmsssss
  87.  * Change to a 9-byte string (ignore seconds):      hh:mm ?m
  88.  */
  89. char *timestr( unsigned t, char *buf )
  90. {
  91.     int h = (t >> 11) & 0x1f, m = (t >> 5) & 0x3f;
  92.  
  93.     sprintf( buf, "%2.2d:%02.2d %cm", h % 12, m,  h > 11 ? 'p' : 'a' );
  94.     return buf;
  95. }
  96.  
  97. /* Take unsigned date in the format:    fedcba9876543210
  98.  * d=1-31, m=1-12, y=0-119 (1980-2099)  yyyyyyymmmmddddd
  99.  * Change to a 9-byte string:           mm/dd/yy
  100.  */
  101. char *datestr( unsigned d, char *buf )
  102. {
  103.     sprintf( buf, "%2.2d/%02.2d/%02.2d",
  104.              (d >> 5) & 0x0f, d & 0x1f, (d >> 9) + 80 );
  105.     return buf;
  106. }
  107.