home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / MSGDP206.SZH / DIR.C < prev    next >
Text File  |  1991-01-07  |  6KB  |  242 lines

  1. /*
  2.  * released into the PUBLIC DOMAIN 30 jul 1990 by jim nutt
  3. */
  4.  
  5. #include <stdlib.h>
  6. #include <errno.h>
  7. #include <dos.h>
  8.  
  9. #define _pascal pascal
  10.  
  11. #ifdef __OS2__
  12. #include <sys\types.h>
  13. #define INCL_DOSPROCESS
  14. #include <os2.h>
  15. #include <sys\stat.h>
  16. #include <ctype.h>
  17. #include <string.h>
  18. #include <io.h>
  19. #include <fcntl.h>
  20. #include <time.h>
  21.  
  22. static struct _FILEFINDBUF InfoBuf;
  23. static struct find_t dta;
  24.  
  25. HDIR    hDir;
  26. USHORT  cSearch;
  27. USHORT  usAttrib;
  28.  
  29. #define FILENAMELEN 13
  30.  
  31. int _pascal dir_findfirst(char * filename, int attribute, struct find_t * dta)
  32. {
  33.     hDir     = 0x0001;
  34.     usAttrib = attribute;
  35.     cSearch  = 1;
  36.  
  37. #ifdef DEBUG
  38.     printf("\nDIR_FINDFIRST Inputs: '%s' %d.\n", filename, attribute);
  39. #endif /* of DEBUG */
  40.  
  41.     if (DosFindFirst( filename
  42.                     , &hDir
  43.                     , usAttrib
  44.                     , &InfoBuf
  45.                     , (USHORT)( sizeof(InfoBuf) * cSearch )
  46.                     , &cSearch
  47.                     , (ULONG)NULL ) != 0 )
  48.     {
  49. #ifdef DEBUG
  50.     printf("DIR_FINDFIRST:  DosFindFirst returned <>0.\n");
  51. #endif /* of DEBUG */
  52.         DosFindClose( hDir );
  53.         errno = ENOENT;
  54.         return (-1);
  55.     } else {
  56. #ifdef DEBUG
  57.     printf("DIR_FINDFIRST:  DosFindFirst returned 0.\n");
  58.     printf("DIR_FINDFIRST:  attrFile = %d.\n", InfoBuf.attrFile);
  59. #endif /* of DEBUG */
  60.         dta->attrib    = (char)InfoBuf.attrFile;           /**OS2**/
  61.         dta->size      = InfoBuf.cbFile;
  62.         strcpy( dta->name, InfoBuf.achName);
  63.         errno = 0;
  64.         return (0);
  65.     }
  66. }
  67.  
  68. int _pascal dir_findnext(struct find_t * dta)
  69. {
  70.  
  71.     if ((DosFindNext( hDir
  72.                     , &InfoBuf
  73.                     , (USHORT)(FILENAMELEN + 23)
  74.                     , &cSearch)
  75.                     ) || (cSearch != 1))
  76.     {
  77.         DosFindClose( hDir );
  78.         errno = ENOENT;
  79.         return (-1);
  80.     } else {
  81.         dta->attrib    = (char)InfoBuf.attrFile;           /**OS2**/
  82.         dta->size      = InfoBuf.cbFile;
  83.         strcpy(  dta->name, InfoBuf.achName);
  84.         errno = 0;
  85.         return (0);
  86.     }
  87. }
  88.  
  89. static struct _FILEFINDBUF InfoBuf1;
  90.  
  91. HDIR    hDir1;
  92. USHORT  cSearch1;
  93. USHORT  usAttrib1;
  94.  
  95. int _pascal dir_findfirst1(char * filename, int attribute, struct find_t * dta)
  96. {
  97.     hDir1     = 0xffff;
  98.     usAttrib1 = attribute;
  99.     cSearch1  = 1;
  100.  
  101. #ifdef DEBUG
  102.     printf("\nDIR_FINDFIRST1 Inputs: '%s' %d.\n", filename, attribute);
  103. #endif /* of DEBUG */
  104.  
  105.     if (DosFindFirst( filename
  106.                     , &hDir1
  107.                     , usAttrib1
  108.                     , &InfoBuf1
  109.                     , (USHORT)( sizeof(InfoBuf1) * cSearch1 )
  110.                     , &cSearch1
  111.                     , (ULONG)NULL ) != 0 )
  112.     {
  113. #ifdef DEBUG
  114.     printf("DIR_FINDFIRST1:  DosFindFirst returned <>0.\n");
  115. #endif /* of DEBUG */
  116.         DosFindClose( hDir1 );
  117.         errno = ENOENT;
  118.         return (-1);
  119.     } else {
  120. #ifdef DEBUG
  121.     printf("DIR_FINDFIRST1:  DosFindFirst returned 0.\n");
  122.     printf("DIR_FINDFIRST1:  attrFile = %d.\n", InfoBuf1.attrFile);
  123. #endif /* of DEBUG */
  124.         dta->attrib    = (char)InfoBuf1.attrFile;           /**OS2**/
  125.         dta->size      = InfoBuf1.cbFile;
  126.         strcpy( dta->name, InfoBuf1.achName);
  127.         errno = 0;
  128.         return (0);
  129.     }
  130. }
  131.  
  132. int _pascal dir_findnext1(struct find_t * dta)
  133. {
  134.  
  135.     if ((DosFindNext( hDir1
  136.                     , &InfoBuf1
  137.                     , (USHORT)(FILENAMELEN + 23)
  138.                     , &cSearch1)
  139.                     ) || (cSearch1 != 1))
  140.     {
  141.         DosFindClose( hDir1 );
  142.         errno = ENOENT;
  143.         return (-1);
  144.     } else {
  145.         dta->attrib    = (char)InfoBuf1.attrFile;           /**OS2**/
  146.         dta->size      = InfoBuf1.cbFile;
  147.         strcpy(  dta->name, InfoBuf1.achName);
  148.         errno = 0;
  149.         return (0);
  150.     }
  151. }
  152.  
  153. #else /* of __OS2__ */
  154.  
  155. struct _dta   {
  156.     char        reserved[21];
  157.     char        attribute;
  158.     unsigned    time;
  159.     unsigned    date;
  160.     long        size;
  161.     char        name[13];
  162. };
  163.  
  164. static int olddta = 0;
  165.  
  166. int _pascal dir_findnext(struct _dta * dta);
  167. int _pascal dir_findfirst(char * filename, int attribute, struct _dta * dta);
  168.  
  169. int _pascal dir_findfirst(char * filename, int attribute, struct _dta * dta)
  170.  
  171. {
  172.     union REGS  ir;
  173.     union REGS  or;
  174. #ifndef SPTR
  175.     struct SREGS    sr;
  176. #endif
  177.  
  178.     ir.h.ah = 0x1a;
  179. #ifdef SPTR
  180.     ir.x.dx = (unsigned int) dta;
  181.     intdos(&ir,&or);
  182. #else
  183.     ir.x.dx = FP_OFF(dta);
  184.     sr.ds = FP_SEG(dta);
  185.     intdosx(&ir, &or, &sr);
  186. #endif
  187.     ir.h.ah = 0x4e;
  188.     ir.x.cx = (unsigned int) attribute;
  189. #ifdef SPTR
  190.     ir.x.dx = (unsigned int) filename;
  191.     intdos(&ir,&or);
  192. #else
  193.     ir.x.dx = FP_OFF(filename);
  194.     sr.ds = FP_SEG(filename);
  195.     intdosx(&ir, &or, &sr);
  196. #endif
  197.     if (or.x.cflag) {
  198.         errno = ENOENT;
  199.         return (-1);
  200.     }
  201.  
  202.     errno = 0;
  203.     return (0);
  204. }
  205.  
  206. int _pascal dir_findnext(struct _dta * dta)
  207.  
  208. {
  209.     union REGS ir, or;
  210. #ifndef SPTR
  211.     struct SREGS    sr;
  212. #endif
  213.  
  214.     ir.h.ah = 0x1a;
  215.  
  216.     if (!olddta) {
  217.  
  218. #ifdef SPTR
  219.         ir.x.dx = (unsigned int) dta;
  220.         intdos(&ir,&or);
  221. #else
  222.         ir.x.dx = FP_OFF(dta);
  223.         sr.ds = FP_SEG(dta);
  224.         intdosx(&ir, &or, &sr);
  225. #endif
  226.         olddta = 1;
  227.     }
  228.  
  229.     ir.h.ah = 0x4f;
  230.     intdos(&ir,&or);
  231.  
  232.     if (or.x.cflag) {
  233.         errno = ENOENT;
  234.         return (-1);
  235.     } else {
  236.         errno = 0;
  237.         return (0);
  238.     }
  239. }
  240. #endif /* of __OS2__ */
  241.  
  242.