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

  1. /* TOUCH.C illustrates wild card handling and changing file time and
  2.  * attribute. Functions used include:
  3.  *      _dos_findfirst      _dos_findnext
  4.  *      utime               chmod
  5.  */
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <conio.h>
  10. #include <ctype.h>
  11. #include <dos.h>
  12. #include <io.h>
  13. #include <sys\types.h>
  14. #include <sys\utime.h>
  15. #include <sys\stat.h>
  16.  
  17. void fileinfo( struct find_t *find );
  18. char *timestr( unsigned d, char *buf );
  19. char *datestr( unsigned d, char *buf );
  20.  
  21. main( int argc, char *argv[] )
  22. {
  23.     struct find_t find;
  24.  
  25.     /* Find first matching file, then find additional matches. */
  26.     if( !_dos_findfirst( argv[1], 0xffff, &find ) )
  27.         fileinfo( &find );
  28.     else
  29.     {
  30.         printf( "  SYNTAX: TOUCH <wildfilespec>" );
  31.         return 1;
  32.     }
  33.     while( !_dos_findnext( &find ) )
  34.         fileinfo( &find );
  35. }
  36.  
  37. void fileinfo( struct find_t *pfind )
  38. {
  39.     char timebuf[10], datebuf[10], *pkind;
  40.     int ch;
  41.  
  42.     datestr( pfind->wr_date, datebuf );
  43.     timestr( pfind->wr_time, timebuf );
  44.  
  45.     if( pfind->attrib & _A_SUBDIR )
  46.         pkind = "Directory";
  47.     else if( pfind->attrib & _A_VOLID )
  48.         pkind = "Label";
  49.     else
  50.         pkind = "File";
  51.  
  52.     printf( "%-12s   %6ld   %8s   %9s   %-9s    %c %c %c %c\n",
  53.             pfind->name, pfind->size, timebuf, datebuf, pkind,
  54.             (pfind->attrib & _A_RDONLY) ? 'R' : ' ',
  55.             (pfind->attrib & _A_HIDDEN) ? 'H' : ' ',
  56.             (pfind->attrib & _A_SYSTEM) ? 'S' : ' ',
  57.             (pfind->attrib & _A_ARCH) ? 'A' : ' ' );
  58.  
  59.     printf( "(T)ime update   (R)ead only   (Q)uit   Any other next\n" );
  60.     ch = getch();
  61.     switch( toupper( ch ) )
  62.     {
  63.         case 'T':               /* Set to current time */
  64.             utime( pfind->name, NULL );
  65.             break;
  66.         case 'R':               /* Toggle read only */
  67.             if( pfind->attrib & _A_RDONLY )
  68.                 chmod( pfind->name, S_IWRITE );
  69.             else
  70.                 chmod( pfind->name, S_IREAD );
  71.             break;
  72.         case 'Q':               /* Quit */
  73.             exit( 1 );
  74.     }
  75. }
  76.  
  77. /* Take unsigned date in the format:    fedcba9876543210
  78.  * (year is 1980 - year)                yyyyyyymmmmddddd
  79.  * Change to a 9-byte string:           mm/dd/yy
  80.  */
  81. char *datestr( unsigned d, char *buf )
  82. {
  83.     sprintf( buf, "%2.2d/%02.2d/%02.2d",
  84.              (d >> 5) & 0x0f, d & 0x1f, (d >> 9) + 80 );
  85.     return buf;
  86. }
  87.  
  88. /* Take unsigned time in the format:                fedcba9876543210
  89.  * (hours and minutes 0-based)                      hhhhhmmmmmmsssss
  90.  * Change to a 9-byte string (ignore seconds):      hh:mm ?m
  91.  */
  92. char *timestr( unsigned t, char *buf )
  93. {
  94.     int h = (t >> 11) & 0x1f, m = (t >> 5) & 0x3f;
  95.  
  96.     sprintf( buf, "%2.2d:%02.2d %cm", h % 12, m,  h > 11 ? 'p' : 'a' );
  97.     return buf;
  98. }
  99.