home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / BTMTSRC3.ZIP / DIRFIND.C < prev    next >
Text File  |  1990-07-14  |  6KB  |  198 lines

  1. /*--------------------------------------------------------------------------*/
  2. /* Include files                                                            */
  3. /*--------------------------------------------------------------------------*/
  4.  
  5. #include    <ctype.h>
  6. #include    <errno.h>
  7.  
  8. #include    <stdlib.h>
  9. #include    <fcntl.h>
  10. #include    <sys\types.h>
  11. #include    <sys\stat.h>
  12. #include    <io.h>
  13.  
  14. #define     INCL_DOSPROCESS
  15. #include    <os2.h>
  16.  
  17. #include    <string.h>
  18. #include    <dos.h>
  19. #include    <time.h>
  20.  
  21. /*--------------------------------------------------------------------------*/
  22. /* Static function declarations                                             */
  23. /*--------------------------------------------------------------------------*/
  24.  
  25. /*    ... NONE ...  */
  26.  
  27. /*--------------------------------------------------------------------------*/
  28. /* Static variable definitions                                                */
  29. /*--------------------------------------------------------------------------*/
  30.  
  31. struct FILEINFO {
  32.    char rsvd[21];
  33.    char attrib;
  34.    unsigned wr_date;
  35.    unsigned wr_time;
  36.    long size;
  37.    char name[13];
  38.    char nill;
  39.    };
  40.  
  41. struct FileFindBuf {
  42.         unsigned create_date;            /* date of file creation */
  43.         unsigned create_time;            /* time of file creation */
  44.         unsigned access_date;            /* date of last access */
  45.         unsigned access_time;            /* time of last access */
  46.         unsigned wr_date;                /* date of last write */
  47.         unsigned wr_time;                /* time of last write */
  48.         unsigned long size;             /* file size (end of data) */
  49.         unsigned long falloc_size;        /* file allocated size */
  50.         unsigned attrib;                /* attributes of the file */
  51.         unsigned char string_len;        /* returned length of ascii name str. */
  52.                                         /* length does not include null byte */
  53.         char name[13];                    /* name string */
  54.         };
  55.  
  56. static struct FileFindBuf InfoBuf;
  57. static struct FileFindBuf InfoBufA;
  58.  
  59. /*--------------------------------------------------------------------------*/
  60. /* External variable declarations                                            */
  61. /*--------------------------------------------------------------------------*/
  62.  
  63. /*    ... NONE ...  */
  64.  
  65. /*--------------------------------------------------------------------------*/
  66. /* Locally defined globals                                                    */
  67. /*--------------------------------------------------------------------------*/
  68.  
  69. HDIR                    hDir;
  70. HDIR                    hDirA;
  71. USHORT                    cSearch;
  72. USHORT                    usAttrib;
  73.  
  74. /*--------------------------------------------------------------------------*/
  75. /* Local constants                                                            */
  76. /*--------------------------------------------------------------------------*/
  77.  
  78. #define FILENAMELEN 13
  79.  
  80. /****************************************************************************/
  81.  
  82.  
  83. /*--------------------------------------------------------------------------*/
  84. /* DIR_FINDFIRST                                     FOR PORTABILITY        */
  85. /*--------------------------------------------------------------------------*/
  86.  
  87. int dir_findfirst(char far * filename, int attribute, struct FILEINFO * dta)
  88. {
  89.     hDir     = 0x0001;
  90.     usAttrib = attribute;
  91.     cSearch  = 1;
  92.  
  93.     if ((DosFindFirst( filename
  94.                      , &hDir
  95.                      , usAttrib
  96.                      , (PFILEFINDBUF) &InfoBuf
  97.                      , (USHORT) (sizeof(InfoBuf) * cSearch)
  98.                      , &cSearch
  99.                      , (ULONG) NULL) != 0) || cSearch != 1)
  100.     {
  101.         (void) DosFindClose( hDir );
  102.         errno = ENOENT;
  103.         return (1);
  104.     } else {
  105.         dta->wr_date   = InfoBuf.wr_date;
  106.         dta->wr_time   = InfoBuf.wr_time;
  107.         dta->attrib    = (char) InfoBuf.attrib;
  108.         dta->size       = InfoBuf.size;
  109.         strcpy( dta->name, InfoBuf.name);
  110.         errno = 0;
  111.         return (0);
  112.     }
  113. }
  114.  
  115. int dir_findfirsta(char far * filename, int attribute, struct FILEINFO * dta)
  116. {
  117.     hDirA     = 0xffff;
  118.     usAttrib = attribute;
  119.     cSearch  = 1;
  120.  
  121.     if ((DosFindFirst( filename
  122.                      , &hDirA
  123.                      , usAttrib
  124.                      , (PFILEFINDBUF) &InfoBufA
  125.                      , (USHORT) (sizeof(InfoBufA) * cSearch)
  126.                      , &cSearch
  127.                      , (ULONG) NULL) != 0) || cSearch != 1)
  128.     {
  129.         (void) DosFindClose( hDirA );
  130.         errno = ENOENT;
  131.         return (1);
  132.     } else {
  133.         dta->wr_date   = InfoBufA.wr_date;
  134.         dta->wr_time   = InfoBufA.wr_time;
  135.         dta->attrib    = (char) InfoBufA.attrib;
  136.         dta->size       = InfoBufA.size;
  137.         strcpy( dta->name, InfoBufA.name);
  138.         errno = 0;
  139.         return (0);
  140.     }
  141. }
  142.  
  143. /*--------------------------------------------------------------------------*/
  144. /* DIR_FINDNEXT                                      FOR PORTABILITY        */
  145. /*--------------------------------------------------------------------------*/
  146.  
  147. int dir_findnext(struct FILEINFO * dta)
  148. {
  149.  
  150.     cSearch  = 1;
  151.  
  152.     if ((DosFindNext( hDir
  153.                     , (PFILEFINDBUF) &InfoBuf
  154.                     , (USHORT) (sizeof(InfoBuf) * cSearch)
  155.                     , &cSearch) != 0) || (cSearch < 1))
  156.     {
  157.         (void) DosFindClose( hDir );
  158.         errno = ENOENT;
  159.         return (1);
  160.     } else {
  161.         dta->wr_date   = InfoBuf.wr_date;
  162.         dta->wr_time   = InfoBuf.wr_time;
  163.         dta->attrib    = (char) InfoBuf.attrib;
  164.         dta->size       = InfoBuf.size;
  165.         strcpy( dta->name, InfoBuf.name);
  166.         errno = 0;
  167.         return (0);
  168.     }
  169. }
  170.  
  171. int dir_findnexta(struct FILEINFO * dta)
  172. {
  173.  
  174.     cSearch  = 1;
  175.  
  176.     if ((DosFindNext( hDirA
  177.                     , (PFILEFINDBUF) &InfoBufA
  178.                     , (USHORT) (sizeof(InfoBufA) * cSearch)
  179.                     , &cSearch) != 0) || (cSearch < 1))
  180.     {
  181.         (void) DosFindClose( hDirA );
  182.         errno = ENOENT;
  183.         return (1);
  184.     } else {
  185.         dta->wr_date   = InfoBufA.wr_date;
  186.         dta->wr_time   = InfoBufA.wr_time;
  187.         dta->attrib    = (char) InfoBufA.attrib;
  188.         dta->size       = InfoBufA.size;
  189.         strcpy( dta->name, InfoBufA.name);
  190.         errno = 0;
  191.         return (0);
  192.     }
  193. }
  194.  
  195. /*--------------------------------------------------------------------------*/
  196. /*                                  END OF FILE                                */
  197. /*--------------------------------------------------------------------------*/
  198.