home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 14 / CD_ASCQ_14_0694.iso / maj / 653 / rfind1st.c < prev    next >
C/C++ Source or Header  |  1994-04-03  |  6KB  |  157 lines

  1. /*
  2. **  RFIND1ST.C - Our own non-compiler specific find first/next calls
  3. **
  4. **  Original Copyright 1988-1991 by Bob Stout as part of
  5. **  the MicroFirm Function Library (MFL)
  6. **
  7. **  This subset version is functionally identical to the
  8. **  version originally published by the author in Tech Specialist
  9. **  magazine and is hereby donated to the public domain.
  10. */
  11.  
  12. #include        <stdio.h>
  13. #include        <stdlib.h>
  14. #include        <dos.h>
  15. #include        "dirent.h"
  16. #ifndef OS2
  17.  
  18. /************************************************************************/
  19. /*                                                                      */
  20. /* rfind_1st() - Find first matching file                               */
  21. /*                                                                      */
  22. /* Parameters: 1 - Drive, path and filename of file to be found. May    */
  23. /*                 include wildcards                                    */
  24. /*             2 - Attribute of file to search for. Attributes are      */
  25. /*                 described in the MS-DOS manual. The search strategy  */
  26. /*                 is described under DOS call 0x4Eh.                   */
  27. /*             3 - Disk transfer area buffer. If NULL, one will be      */
  28. /*                 malloc'ed.                                           */
  29. /* Returns: Pointer to a struct DSTRUCT. If error, NULL is returned and */
  30. /*          _doserrno is set to the error #.                            */
  31. /*                                                                      */
  32. /************************************************************************/
  33.  
  34. struct DSTRUCT *rfind_1st(char *name, unsigned attribute, struct DSTRUCT *dta)
  35. {
  36.       struct DSTRUCT *my_dta;
  37.       union REGS regs;
  38.  
  39.       if (NULL == dta)
  40.             my_dta = (struct DSTRUCT *)malloc(sizeof(struct DSTRUCT));
  41.       else  my_dta = dta;
  42.  
  43.       bdos(0x1A, (unsigned)my_dta, 0);    /* set DTA to my_dta          */
  44.       regs.x.ax = 0x4E00;                 /* find first                 */
  45.       regs.x.dx = (unsigned)name;
  46.       regs.x.cx = attribute;
  47.       intdos(®s, ®s);
  48.       if (regs.x.cflag)                   /* if error                   */
  49.       {
  50.             _doserrno = regs.x.ax;
  51.             if (NULL == dta && my_dta != NULL)
  52.                   free(my_dta);
  53.             return (struct DSTRUCT *) NULL;
  54.       }
  55.       return my_dta;
  56. }
  57.  
  58. /************************************************************************/
  59. /*                                                                      */
  60. /* rfind_nxt() - Find next matching file                                */
  61. /*                                                                      */
  62. /* Parameters: 1 - Pointer to DSTRUCT structure to use                  */
  63. /*                                                                      */
  64. /* Returns: Pointer to struct DSTRUCT,                                  */
  65. /*          NULL if no more matching files found                        */
  66. /*                                                                      */
  67. /************************************************************************/
  68.  
  69. struct DSTRUCT *rfind_nxt(struct DSTRUCT *dta)
  70. {
  71.       union REGS regs;
  72.  
  73.       bdos(0x1A, (unsigned)dta, 0);       /* set DTA to dta             */
  74.       regs.x.ax = 0x4F00;
  75.       intdos(®s,®s);
  76.       if (regs.x.cflag)                   /* if error                   */
  77.       {
  78.             _doserrno = regs.x.ax;
  79.             return (struct DSTRUCT *) NULL;
  80.       }
  81.       return dta;
  82. }
  83.  
  84. #else
  85.  
  86. /************************************************************************/
  87. /*                                                                      */
  88. /*  OS/2 Versions follow - poorly tested!                               */
  89. /*                                                                      */
  90. /************************************************************************/
  91.  
  92. #if OS2 < 2
  93.  typedef USHORT UWORD
  94. #else
  95.  typedef ULONG UWORD
  96. #endif
  97.  
  98. static HDIR hdir_ptr = DSIR_CREATE;
  99. #if OS2 < 2
  100.  static FILEFINDBUF flist;
  101. #else
  102.  static FILEFINDBUF3 flist;
  103. #endif
  104.  
  105. static PSZ fname;
  106. static UWORD count = 1;
  107.  
  108. struct DSTRUCT *rfind_1st(char *name, unsigned attribute, struct DSTRUCT *dta)
  109. {
  110.       struct DSTRUCT *my_dta;
  111.       short retval;
  112.  
  113.       if (NULL == dta)
  114.             my_dta = (struct DSTRUCT *)malloc(sizeof(struct DSTRUCT));
  115.       else  my_dta = dta;
  116.  
  117.       fname = (PSZ)name;
  118. #if OS2 < 2
  119.       if (DosFindFirst(fname, &hdir_ptr, attribute, &flist, sizeof(flist),
  120.             &count, 0L))
  121. #else
  122.       if (DosFindFirst(fname, &hdir_ptr, attribute, &flist, sizeof(flist),
  123.             &count, FIL_STANDARD))
  124. #endif
  125.       {
  126.             return NULL;
  127.       }
  128.       else
  129.       {
  130.             my_dta->ATTRIBUTE = (BYTE)(flist.attrFile & 0xff);
  131.             my_dta->TIME      = flist.ftimeCreation;
  132.             my_dta->DATE      = flist.fdateCreation;
  133.             my_dta->FSIZE     = flist.cbFile;
  134.             strcpy(my_dta->NAME, flist.achName);
  135.             return my_dta;
  136.       }
  137. }
  138.  
  139. struct DSTRUCT *rfind_nxt(struct DSTRUCT *dta)
  140. {
  141.       struct DSTRUCT *my_dta;
  142.  
  143.       if (DosFindNext(hdir_ptr, &flist, sizeof(flist), & count))
  144.             return NULL;
  145.       else
  146.       {
  147.             my_dta->ATTRIBUTE = (BYTE)(flist.attrFile & 0xff);
  148.             my_dta->TIME      = flist.ftimeCreation;
  149.             my_dta->DATE      = flist.fdateCreation;
  150.             my_dta->FSIZE     = flist.cbFile;
  151.             strcpy(my_dta->NAME, flist.achName);
  152.             return my_dta;
  153.       }
  154. }
  155.  
  156. #endif
  157.