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

  1. /* EXTDIR.C illustrates wild card handling using functions:
  2.  *      _dos_findfirst      _dos_findnext       sprintf
  3.  */
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <conio.h>
  8. #include <ctype.h>
  9. #include <dos.h>
  10. #include <io.h>
  11. #include <sys\types.h>
  12. #include <sys\utime.h>
  13. #include <sys\stat.h>
  14.  
  15. long fileinfo( struct find_t *find );       /* Prototypes */
  16. char *timestr( unsigned d, char *buf );
  17. char *datestr( unsigned d, char *buf );
  18.  
  19. main( int argc, char *argv[] )
  20. {
  21.     struct find_t find;
  22.     long size;
  23.  
  24.     /* Find first matching file, then find additional matches. */
  25.     if( !_dos_findfirst( argv[1], 0xffff, &find ) )
  26.     {
  27.         printf( "%-12s   %-8s    %-8s   %-8s   %-9s   %s %s %s %s\n",
  28.                 "FILE", "SIZE", "TIME", "DATE", "KIND",
  29.                 "RDO", "HID", "SYS", "ARC" );
  30.         size = fileinfo( &find );
  31.     }
  32.     else
  33.     {
  34.         printf( "  SYNTAX: TOUCH <wildfilespec>" );
  35.         exit( 1 );
  36.     }
  37.     while( !_dos_findnext( &find ) )
  38.         size += fileinfo( &find );
  39.     printf( "%-12s   %8ld\n\n", "Total", size );
  40.     exit( 0 );
  41. }
  42.  
  43. long fileinfo( struct find_t *pfind )
  44. {
  45.     char timebuf[10], datebuf[10], *pkind;
  46.  
  47.     datestr( pfind->wr_date, datebuf );
  48.     timestr( pfind->wr_time, timebuf );
  49.  
  50.     if( pfind->attrib & _A_SUBDIR )
  51.         pkind = "Directory";
  52.     else if( pfind->attrib & _A_VOLID )
  53.         pkind = "Label";
  54.     else
  55.         pkind = "File";
  56.  
  57.     printf( "%-12s   %8ld    %8s   %8s   %-9s    %c   %c   %c   %c\n",
  58.             pfind->name, pfind->size, timebuf, datebuf, pkind,
  59.             (pfind->attrib & _A_RDONLY) ? 'Y' : 'N',
  60.             (pfind->attrib & _A_HIDDEN) ? 'Y' : 'N',
  61.             (pfind->attrib & _A_SYSTEM) ? 'Y' : 'N',
  62.             (pfind->attrib & _A_ARCH)   ? 'Y' : 'N' );
  63.     return pfind->size;
  64. }
  65.  
  66. /* Take unsigned time in the format:                fedcba9876543210
  67.  * s=2 sec incr, m=0-59, h=23                       hhhhhmmmmmmsssss
  68.  * Change to a 9-byte string (ignore seconds):      hh:mm ?m
  69.  */
  70. char *timestr( unsigned t, char *buf )
  71. {
  72.     int h = (t >> 11) & 0x1f, m = (t >> 5) & 0x3f;
  73.  
  74.     sprintf( buf, "%2.2d:%02.2d %cm", h % 12, m,  h > 11 ? 'p' : 'a' );
  75.     return buf;
  76. }
  77.  
  78. /* Take unsigned date in the format:    fedcba9876543210
  79.  * d=1-31, m=1-12, y=0-119 (1980-2099)  yyyyyyymmmmddddd
  80.  * Change to a 9-byte string:           mm/dd/yy
  81.  */
  82. char *datestr( unsigned d, char *buf )
  83. {
  84.     sprintf( buf, "%2.2d/%02.2d/%02.2d",
  85.              (d >> 5) & 0x0f, d & 0x1f, (d >> 9) + 80 );
  86.     return buf;
  87. }
  88.