home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / CODE4-1.ZIP / SOURCE.ZIP / U4FILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-02  |  4.4 KB  |  231 lines

  1.  
  2. #ifndef UNIX
  3. #ifndef OS2
  4.    #define IS_DOS
  5. #endif
  6. #endif
  7.  
  8. #ifdef TURBO
  9.    #define  IS_DOS
  10. #endif
  11.  
  12.  
  13. #ifdef IS_DOS
  14.  
  15. #include "d4base.h"
  16. #include <dos.h> 
  17.  
  18. typedef struct
  19. {
  20.    char  drive ;
  21.    char  pattern[12] ;
  22.    char  reserved1 ;
  23.    int   file_position ;    /* File Position in Directory */
  24.    int   directory_position ;
  25.    char  reserved2[3] ;
  26.    char  file_attribute ;
  27.    int   file_time ;
  28.    int   file_date ;
  29.    long  file_size ;
  30.    char  file_name[14] ;
  31. }  DTA ;  /* Disk Transfer Area */
  32.  
  33. static   union
  34. {
  35.    unsigned  off_seg[2] ; /* off_seg[0] is offset, off_seg[1] is segment */
  36.    DTA far  *far_ptr ;
  37. }  dta ;
  38.  
  39.  
  40. u4file_first( pattern, first_match )
  41. char *pattern ;
  42. char *first_match ;
  43. {
  44.    union  REGS   regs ;
  45.    struct SREGS  sregs ;
  46.    int  i ;
  47.    union
  48.    {
  49.       unsigned   off_seg[2] ;
  50.       char far  *far_ptr ;
  51.    }  ptr ;
  52.  
  53.    ptr.far_ptr =  pattern ;
  54.  
  55.    /* Get the Disk Transfer Area Address */
  56.    regs.h.ah =  0x2F ;
  57.    int86x( 0x21, ®s, ®s, &sregs ) ;
  58.    dta.off_seg[0] =  regs.x.bx ;
  59.    dta.off_seg[1] =  sregs.es ;
  60.  
  61.    regs.h.ah =  0x4E ;
  62.    regs.x.cx =  0 ;
  63.  
  64.    regs.x.dx =  ptr.off_seg[0] ;
  65.    sregs.ds  =  ptr.off_seg[1] ;
  66.  
  67.    int86x( 0x21, ®s, ®s, &sregs ) ;
  68.  
  69.    for( i=0; i< 14; i++ )
  70.       first_match[i] =  dta.far_ptr->file_name[i] ;
  71.  
  72.    return ( regs.x.ax ) ;
  73. }
  74.  
  75.  
  76. u4file_next( next_match )
  77. char *next_match ;
  78. {
  79.    union  REGS   regs ;
  80.    int  i ;
  81.  
  82.    regs.h.ah =  0x4F ;
  83.  
  84.    int86( 0x21, ®s, ®s ) ;
  85.  
  86.    for( i=0; i< 14; i++ )
  87.       next_match[i] =  dta.far_ptr->file_name[i] ;
  88.  
  89.    return ( regs.x.ax ) ;
  90. }
  91. #endif
  92.  
  93.  
  94. #ifdef OS2
  95.  
  96. typedef struct
  97. {
  98.    unsigned  twosecs: 5 ;
  99.    unsigned  minutes: 6 ;
  100.    unsigned  hours:   5 ;
  101. } F_TIME ;
  102.  
  103. typedef struct
  104. {
  105.    unsigned  day:     5 ;
  106.    unsigned  month:   4 ;
  107.    unsigned  year:    7 ;
  108. }  F_DATE ;
  109.  
  110. typedef struct
  111. {
  112.    F_DATE     date_created ;
  113.    F_TIME     time_created ;
  114.    F_DATE     date_last_access ;
  115.    F_TIME     time_last_access ;
  116.    F_DATE     date_last_write ;
  117.    F_TIME     time_last_write ;
  118.    long       cb_file ;
  119.    long       cb_file_alloc ;
  120.    int        attr_file ;
  121.    char       name_length ;
  122.    char       file_name[13] ;
  123. }  FILE_INFO ;
  124.  
  125. extern pascal far  DOSFINDFIRST( char far *, int far *, int, 
  126.                                  FILE_INFO far *, int, int far *, long) ;
  127. extern pascal far  DOSFINDNEXT( int, FILE_INFO far *, int, int far *) ;
  128. extern pascal far  DOSFINDCLOSE( int ) ;
  129.  
  130. static int  file_hand =  -1 ;
  131.  
  132.  
  133. u4file_first( pattern, first_match )
  134. char *pattern, *first_match ;
  135. {
  136.    int        rc, count ;
  137.    FILE_INFO  file_info ;
  138.  
  139.    if ( file_hand >= 0 )  DOSFINDCLOSE( file_hand ) ;
  140.  
  141.    file_hand  =  -1 ;
  142.    count      =   1 ;
  143.    DOSFINDFIRST( (char far *) pattern, (int far *) &file_hand, 0,
  144.                  (FILE_INFO far *) &file_info, (int) sizeof(FILE_INFO), 
  145.                  (int far *) &count, 0L ) ;
  146.    if ( count == 0 )  
  147.    {
  148.       DOSFINDCLOSE( file_hand ) ;
  149.       file_hand =  -1 ;
  150.       return 18 ;
  151.    }
  152.    memcpy( first_match, file_info.file_name, 13 ) ;
  153.    first_match[13] = '\0' ;
  154.  
  155.    return 0 ;
  156. }
  157.  
  158. u4file_next( next_match )
  159. char *next_match ;
  160. {
  161.    int  rc, count ;
  162.    FILE_INFO  file_info ;
  163.  
  164.    if ( file_hand < 0 )   return 18 ;
  165.  
  166.    count =   1 ;
  167.    DOSFINDNEXT(  (int far *) file_hand, 
  168.                  (FILE_INFO far *) &file_info, (int) sizeof(FILE_INFO), 
  169.                  (int far *) &count ) ;
  170.  
  171.    if ( count == 0 )  
  172.    {
  173.       DOSFINDCLOSE( file_hand ) ;
  174.       file_hand =  -1 ;
  175.       return 18 ;
  176.    }
  177.  
  178.    memcpy( next_match, file_info.file_name, 13 ) ;
  179.    next_match[13] = '\0' ;
  180.  
  181.    return 0 ;
  182. }
  183. #endif
  184.  
  185.  
  186. #ifdef UNIX
  187. #ifndef TURBO
  188.  
  189. #include <sys/ndir.h>
  190.  
  191. static  DIR * dir_on =  (DIR *) 0 ;
  192.  
  193. u4file_first( pattern, first_match )
  194. char *pattern ;
  195. char *first_match ;
  196. {
  197.    if ( dir_on != (DIR *) 0 )
  198.       closedir( dir_on ) ;
  199.  
  200.    dir_on =  opendir( "." ) ;
  201.    if ( dir_on == (DIR *) 0 )
  202.       return 18 ;
  203.  
  204.    return( u4file_next( first_match ) ) ;
  205. }
  206.  
  207.  
  208. u4file_next( next_match )
  209. char *next_match ;
  210. {
  211.    struct direct  *struct_direct ;
  212.  
  213.    if ( dir_on == (DIR *) 0 )
  214.       return 18 ;
  215.  
  216.    struct_direct =  readdir( dir_on ) ;
  217.    if ( struct_direct == (struct direct *) 0 )
  218.    {
  219.       closedir( dir_on ) ;
  220.       dir_on =  (DIR *) 0 ;
  221.       return 18 ;
  222.    }
  223.  
  224.    strncpy( next_match, struct_direct->d_name, 13 ) ;
  225.    next_match[13] =  '\0' ;
  226.  
  227.    return 0 ;
  228. }
  229. #endif
  230. #endif
  231.