home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 4.ddi / C / DRSFIRST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-05  |  3.8 KB  |  105 lines

  1. /**
  2. *
  3. * Name        drsfirst -- Find the first matching file
  4. *
  5. * Synopsis    ercode = drsfirst(pname,sattr,pfile);
  6. *
  7. *        int   ercode      Returned DOS function error code
  8. *        char  *pname      Filename specification string
  9. *        int   sattr      Attribute which files must match
  10. *        FSPEC *pfile      File information for matching file
  11. *
  12. * Description    DRSFIRST finds the first occurrence of a file which
  13. *        matches both the specified attribute and the file
  14. *        "name." If a file is found that matches the filename
  15. *        (including drive, path and attribute), the file's
  16. *        attribute, time and date stamp, size, and filename are
  17. *        returned in the FSPEC structure.
  18. *
  19. *        The name specification can be any legal DOS path name,
  20. *        possibly with embedded wild card characters ('*' or '?')
  21. *        in the last portion of the name (the filename).  The
  22. *        name must be terminated by a NUL character ('\0').
  23. *
  24. *        The search attribute parameter specifies which files are
  25. *        potential candidates for a match.  The acceptable search
  26. *        attributes are:
  27. *
  28. *            AT_GENERAL ( 0) - Normal file
  29. *            AT_RDONLY  ( 1) - Read-only file
  30. *            AT_HIDDEN  ( 2) - Hidden file
  31. *            AT_SYSTEM  ( 4) - System file
  32. *            AT_DIR     (16) - Subdirectory
  33. *
  34. *        Attribute values may be combined by adding them
  35. *        together.  Files are found which match a subset (some or
  36. *        none) of the specified attributes.  For example, search
  37. *        attribute 23 qualifies all files, while 16 finds
  38. *        subdirectories and ordinary files.
  39. *
  40. *        If the value of the function is 0 (i.e., no error), then
  41. *        a file was found and DRSNEXT can be used to find further
  42. *        files matching the specified name and attribute.  If the
  43. *        value of the function is 18, then no matching files were
  44. *        found, and neither will DRSNEXT find any matching files.
  45. *
  46. * Method    DRSFIRST is used in conjunction with DRSNEXT to search
  47. *        through the file directory.  DOS uses a portion of the
  48. *        current Disk Transfer Area for a scratch area and to
  49. *        communicate when subsequent calls are made to DRSNEXT.
  50. *        Because the current DTA can be overwritten if defined in
  51. *        another function (it can be set by by I/O calls and
  52. *        placed on the stack), DRSFIRST changes the DTA to a
  53. *        static area and then returns the DTA to its previous
  54. *        location.  DRSNEXT uses the same communication area,
  55. *        performing a similar DTA switch.
  56. *
  57. * Returns    ercode          DOS function error code
  58. *        *pfile          FSPEC structure containing file information
  59. *                  for match file.
  60. *
  61. * Version    3.0  (C)Copyright Blaise Computing Inc.  1983, 1984, 1986
  62. *
  63. **/
  64.  
  65. #include <bdirect.h>
  66. #include <bfile.h>
  67.  
  68. char b_drbuffer[48] = {0};          /* DRSFIRST, DRSNEXT communica- */
  69.                       /* tion buffer.              */
  70.  
  71. int drsfirst(pname,sattr,pfile)
  72. char  *pname;
  73. int   sattr;
  74. FSPEC *pfile;
  75. {
  76.     ADS      dta_ads,buf_ads,file_ads;
  77.     int      ercode;
  78.     DOSREG   dos_reg;
  79.  
  80.     flretdta(&dta_ads);           /* Return DTA location, and set */
  81.     utabsptr(b_drbuffer,&buf_ads);    /* the new DTA location.          */
  82.     flsetdta(&buf_ads);
  83.  
  84.     dos_reg.ax = 0x4e00;          /* DOS function 4E          */
  85.     dos_reg.cx = sattr;
  86.     utabsptr(pname,&file_ads);          /* Set the pointer to the       */
  87.     dos_reg.ds = file_ads.s;          /* search name.              */
  88.     dos_reg.dx = file_ads.r;
  89.  
  90.     ercode = dos(&dos_reg);
  91.  
  92.     if (ercode == 0)              /* Found a match, now get info  */
  93.     {                      /* and move into the FSPEC      */
  94.                       /* structure.              */
  95.        utabsptr((char *) pfile,&file_ads);
  96.        buf_ads.r += 20;           /* File info is at offset 20    */
  97.        utslmove(&buf_ads,&file_ads,23);
  98.        pfile->fattr = uthibyte(pfile->fattr); /* Move attribute value */
  99.     }                      /* into the proper byte.          */
  100.  
  101.     flsetdta(&dta_ads);           /* Restore the DTA location     */
  102.  
  103.     return(ercode);
  104. }
  105.