home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / ramfs102.zip / src / findfirs.c < prev    next >
C/C++ Source or Header  |  2002-09-28  |  3KB  |  132 lines

  1. #include "includes.h"
  2.  
  3.  
  4.  
  5.  
  6.  
  7. APIRET EXPENTRY FS_FINDFIRST (
  8.     struct cdfsi *pcdfsi,
  9.     struct cdfsd *pcdfsd,
  10.     PSZ        pName,
  11.     USHORT    iCurDirEnd,
  12.     USHORT    attr,
  13.     struct fsfsi *pfsfsi,    /* not used */
  14.     struct fsfsd *pfsfsd,
  15.     PCHAR    pData,
  16.     USHORT    cbData,
  17.     PUSHORT    pcMatch,
  18.     USHORT    level,
  19.     USHORT    flags )
  20. {
  21.   int      rc;
  22.   PVOLUME  pVolume;
  23.   PSEARCH  pSearch;
  24.   DIRENTRY Entry;
  25.   FLAT     flatEntry;
  26.   FLAT     flatBlkDir;
  27.   struct vpfsi *pvpfsi;
  28.   struct vpfsd *pvpfsd;
  29.   BLOCK    BlkDir;
  30.   PEAOP    pEAOP;
  31.  
  32.   UtilEnterRamfs();
  33.   DEBUG_PRINTF4 ("FS_FINDFIRST  pName='%s' attr=%04X level=%d flags=%d",
  34.          pName, attr, level, flags);
  35.  
  36.   FSH_GETVOLPARM (pcdfsi->cdi_hVPB, &pvpfsi, &pvpfsd);
  37.   pVolume = pvpfsd->pVolume;
  38.   flatBlkDir = pVolume->flatBlkRootDir;
  39.   pName += 3;
  40.   if (iCurDirEnd != 0xFFFF)
  41.   {
  42.     flatBlkDir = pcdfsd->pCurdir->flatBlkDir;
  43.     pName += iCurDirEnd-3;
  44.   }
  45.  
  46.   switch (UtilLocate (&flatBlkDir, &flatEntry, &Entry, pName))
  47.   {
  48.     case LOC_NOPATH:
  49.        rc = ERROR_PATH_NOT_FOUND;
  50.        break;
  51.  
  52.  
  53.     case LOC_FILEENTRY:
  54.     case LOC_DIRENTRY:
  55.        /* exact name match */
  56.        pfsfsd->pSearch = 0;        /* no need to allocate a SEARCH */
  57.        if (UtilAttrMatch (attr, Entry.fDOSattr) != NO_ERROR)
  58.        {
  59.          /* attribute didn't match, don't return the entry */
  60.          rc = ERROR_NO_MORE_FILES;
  61.          break;
  62.        }
  63.  
  64.        /* attribute is ok, try to return the entry */
  65.        pEAOP = NULL;
  66.  
  67.            if (*pcMatch == 0)
  68.            {
  69.              /* we aren't allowed to return even a single entry */
  70.              rc = ERROR_BUFFER_OVERFLOW;
  71.              break;
  72.            }
  73.  
  74.        if (level == FIL_QUERYEASFROMLIST)
  75.        {
  76.          /* Data starts with an EAOP - skip it, remembering pEAOP */
  77.          if (cbData < sizeof(EAOP))
  78.          {
  79.            rc = ERROR_BUFFER_OVERFLOW;
  80.            break;
  81.          }
  82.          pEAOP   = (PEAOP)pData;
  83.          pData  += sizeof(EAOP);
  84.          cbData -= sizeof(EAOP);
  85.        }
  86.        rc = FindStoreEntry (&pData, &cbData, level, flags, attr, pEAOP, &Entry);
  87.            if (rc == NO_ERROR)
  88.            {
  89.              /* ok, stored one entry */
  90.              *pcMatch = 1;
  91.            }
  92.        break;
  93.  
  94.  
  95.     case LOC_NOENTRY:
  96.        if (memchr (Entry.achName, '*', Entry.cbName) == 0  &&
  97.            memchr (Entry.achName, '?', Entry.cbName) == 0)
  98.        {
  99.          /* no exact match and no wildcards */
  100.          rc = ERROR_NO_MORE_FILES;
  101.          break;
  102.        }
  103.  
  104.        /* wildcards present, allocate and build a SEARCH */
  105.        rc = NearAlloc ((PNEARBLOCK *) &pSearch, sizeof(SEARCH)+Entry.cbName);
  106.        if (rc)  break;
  107.        pSearch->flatBlkDir = flatBlkDir;
  108.        VMReadBlk (&BlkDir, flatBlkDir);
  109.        pSearch->flatEntry = BlkDir.flatAddr;
  110.        pSearch->usAttr = attr;
  111.        FSH_UPPERCASE (Entry.achName, Entry.cbName+1, pSearch->szPattern);
  112.        rc = FindEntries (pSearch, &Entry, pData, cbData, pcMatch, level, flags);
  113.        if (rc == NO_ERROR)
  114.        {
  115.          /* some files were found, link the SEARCH into the list of SEARCHes */
  116.          pfsfsd->pSearch = pSearch;
  117.          pSearch->pNextSearch = pVolume->pFirstSearch;
  118.          pVolume->pFirstSearch = pSearch;
  119.        }
  120.        else
  121.        {
  122.          /* no files were found, forget everything */
  123.          NearFree (pSearch);
  124.        }
  125.        break;
  126.   }
  127.  
  128.   DEBUG_PRINTF1 (" => %d\r\n", rc);
  129.   UtilExitRamfs();
  130.   return rc;
  131. }
  132.