/*static char *SCCSID = "@(#)caseprsv.c 6.4 92/01/31";*/
#define _BTXT _BTXT

#define INCL_DOSERRORS
#include <os2def.h>
#include <bse.h>
#include <fsh.h>
#include "api.h"
#include "const.h"
#include "misc.h"
#include "chain.h"
#include "buf.h"
#include "dir.h"
#include "storage.h"
#include "volume.h"
#include "tables.h"
#include "lw.h"
#include "frame.h"
#include "fsdata.h"
#include "pbmacros.h"
#include "assert.h"
#include "notify.h"
#include "log.h"
#include "heap.h"
#include "pr_CACHE.h"
#include "pr_debug.h"
#include "pr_DIR.h"
#include "pr_FCN.h"
#include "pr_FSSUB.h"
#include "pr_MISC.h"
#include "pr_HEAP.h"
#include "pr_WRSUB.h"


/*
 *      HISTORY
 *  @76193  23-Nov-1993 by D.Azzarito - Change holds for LOG support
 */

USHORT
QueryPathCasePerserve (
    USHORT hVPB,                  /* volume path is relative to */
    PBYTE  pbString,             /* path name as an ASCIIZ string */
    PCHAR  pData,
    USHORT cbData,
    PFRAME pfrm)
{
    PCRACKEDPATH pcp;
    PBYTE        pbBuffer,pbTemp,pbHold;
    USHORT       erc = NO_ERROR;
    PBUFNODE     pbuf;
    PDIRBLK      pdb;
    PDIRENT      pde;
    USHORT       cbResult;
    BOOL         LstComp = FALSE;
    int          cbName;


    if ((pbHold = pbBuffer = GHS(CPATHBUFSIZE,pfrm)) == NULL)
       return (ERROR_NOT_ENOUGH_MEMORY);

    memcpy(pbBuffer,pbString,3);
    pbBuffer[3] = '\0';

    if (cbData >= 4) {

       FSH_UPPERCASE(pbBuffer,CPATHBUFSIZE,pData);

    } else {

       erc = ERROR_BUFFER_OVERFLOW;

    } /* endif */

    pbBuffer += 3;         /* skip over driver letter, colon, and root */
    pbString += 3;         /* skip over driver letter, colon, and root */
    cbResult = 3;
    pData += 3;

    if (erc == NO_ERROR) {

       do {

          pbTemp = pbString;

          if (FSH_FINDCHAR(1,"\\",&pbTemp) == NO_ERROR) {
             LstComp = FALSE;
             cbName = (pbTemp - pbString);
          } else {
             LstComp = TRUE;
             cbName = fstrlen(pbTemp);
          } /* endif */

          memcpy(pbBuffer,pbString,cbName);
          pbString += cbName;

          ((PBYTE)pbBuffer)[cbName] = '\0';

          if ((erc = CrackPath(hVPB,pbHold,&pcp,pfrm)) != NO_ERROR) {
             break;
          }

          if ((erc = LookFile(pcp,&pdb,&pde,&pbuf,pfrm)) != NO_ERROR) {
             break;
          }

          IncrHoldsBUF(pbuf);  // @76193

          cbResult += (USHORT)(pde->cchName+1);

          if (cbData >= cbResult) {


             MAYBLOCK(erc = FSH_PROBEBUF(PB_OPWRITE,
                                         pData,
                                         pde->cchName+1))
             if (erc != NO_ERROR) {
                DecrHoldsBUF (pbuf);  // @76193
                UnHoldDir();      /*  Release the hold placed by LookFile  */
                break;
             }

          } else {

             erc = ERROR_BUFFER_OVERFLOW;
             DecrHoldsBUF (pbuf);  // @76193
             UnHoldDir();        /*  Release the hold placed by LookFile  */
             break;
          }

          if (erc == NO_ERROR) {

             BYTE_COPY(pde->bName,&((PBYTE)pData)[0],pde->cchName);

             if (LstComp) {
                ((PBYTE)pData)[pde->cchName] = '\0';
             } else {
                ((PBYTE)pData)[pde->cchName] = '\\';
                pbBuffer += cbName;
                *pbBuffer = *pbTemp;
                pbBuffer++;
             } /* endif */

             pData += pde->cchName + 1;

          } /* endif */

          DecrHoldsBUF (pbuf);  // @76193
          UnHoldDir();           /*  Release the hold placed by LookFile  */
          if (pcp != NULL)
             RetHeap(pcp);

       } while (*pbString++ != 0); /* enddo */
    } /* endif */

    if (pbHold != NULL)
        RetHeap(pbHold);

    return(erc);
}
