/*static char *SCCSID = "@(#)open.c     6.2 92/02/05";*/
/*static char *SCCSID = "@(#)open.c     6.2 92/02/05";*/

/************************************************************************
 *
 * OPEN.C - Pre and post processor for aligned data DosOpen
 *
 ************************************************************************/

/***  Change History:                                             ***/
/***   noto  11/18/93  Define which segment for Page Tuning DLL   ***/
/***                                                              ***/


#define INCL_DOS
#define INCL_32
#define INCL_NOPMAPI
#define INCL_DOSERRORS
#include <os2.h>
#include <limits.h>
#include "align32.h"




/***    Define which segment for Page Tuning DLL   ***/

APIRET  APIENTRY DOS32OPEN( PSZ,PHFILE,PULONG,ULONG,ULONG,ULONG,ULONG,PEAOP2);
#pragma code_seg("CODE32_TUNE2", "CODE")
#pragma code_seg()

#pragma alloc_text(CODE32_TUNE2, DOS32OPEN)





/*** Dos32Open
 *
 *  pre- and post- processor for aligned data API.
 *
 *  ENTRY:
 *
 *  EXIT:
 *
 *  ARGUMENTS:  PSZ     pszFileName -- pointer to filename
 *              PHFILE  phf         -- pointer to variable for file handle
 *              PULONG  pulAction   -- pointer to variable for action taken
 *              ULONG   cbFile      -- file size if created or truncated
 *              ULONG   ulAttribute -- file attribute
 *              ULONG   fsOpenFlags -- action taken if file exists/does not exist
 *              ULONG   fsOpenMode  -- open mode of file
 *              PEAOP   peaop       -- pointer to structure for extended attributes
 *
 *  RETURNS:    0 or dos error code
 *
 *  CALLS:      DosOpen
 *
 *  WARNINGS:   none.
 *
 *  PSEUO-CODE:
 *      BEFORE
 *          if (peaop)
 *              PackFEA( peaop )
 *
 *      AFTER
 *          if (peaop)
 *              UnpackFEA( peaop )
 */

APIRET  APIENTRY DOS32OPEN( PSZ     pszFileName,
                 PHFILE  phf,
                 PULONG  pulAction,
                 ULONG   cbFile,
                 ULONG   ulAttribute,
                 ULONG   fsOpenFlags,
                 ULONG   fsOpenMode,
                 PEAOP2  peaop2 ) {

    APIRET rc;

    if (peaop2) {
           if ( peaop2->fpFEA2List == NULL)
            return(ERROR_INVALID_PARAMETER);
        PackFEA( peaop2->fpFEA2List );
    }
    rc = Dos32POpen( pszFileName, phf, pulAction, cbFile, ulAttribute,
                                        fsOpenFlags, fsOpenMode, (PEAOP)peaop2 );

    /* I know that the EAs fit, 'cause they fit before packing, and none
     * have been added by DosOpen.  Therefore I don't bother remembering
     * the max. size of the FEAList.
     */

    if (peaop2) {
        UnpackFEA( ((PEAOP)peaop2)->fpFEAList, peaop2->fpFEA2List,
                                    ULONG_MAX ,(rc)?&(peaop2->oError):NULL );
    }
    return rc;
}

/*** Dos32ProtectOpen
 *
 *  pre- and post- processor for aligned data API.
 *
 *  ENTRY:
 *
 *  EXIT:
 *
 *  ARGUMENTS:  PSZ     pszFileName -- pointer to filename
 *              PHFILE  phf         -- pointer to variable for file handle
 *              PULONG  pulAction   -- pointer to variable for action taken
 *              ULONG   cbFile      -- file size if created or truncated
 *              ULONG   ulAttribute -- file attribute
 *              ULONG   fsOpenFlags -- action taken if file exists/does not exist
 *              ULONG   fsOpenMode  -- open mode of file
 *              PEAOP   peaop       -- pointer to structure for extended attributes
 *              PFHLOCK pfhFileHandleLockID  -- pointer to file handle lock ID
 *
 *  RETURNS:    0 or dos error code
 *
 *  CALLS:      DosOpen
 *
 *  WARNINGS:   none.
 *
 *  PSEUO-CODE:
 *      BEFORE
 *          if (peaop)
 *              PackFEA( peaop )
 *
 *      AFTER
 *          if (peaop)
 *              UnpackFEA( peaop )
 */

APIRET  APIENTRY DOS32PROTECTOPEN( PSZ      pszFileName,
                 PHFILE  phf,
                 PULONG  pulAction,
                 ULONG   cbFile,
                 ULONG   ulAttribute,
                 ULONG   fsOpenFlags,
                 ULONG   fsOpenMode,
                 PEAOP2  peaop2,
                 PFHLOCK         pfhFileHandleLockID ) {

    APIRET rc;

    if (peaop2) {
           if ( peaop2->fpFEA2List == NULL)
            return(ERROR_INVALID_PARAMETER);
        PackFEA( peaop2->fpFEA2List );
    }
    rc = Dos32PProtectOpen( pszFileName, phf, pulAction, cbFile, ulAttribute,
                                        fsOpenFlags, fsOpenMode, (PEAOP)peaop2, pfhFileHandleLockID );

    /* I know that the EAs fit, 'cause they fit before packing, and none
     * have been added by DosOpen.  Therefore I don't bother remembering
     * the max. size of the FEAList.
     */

    if (peaop2) {
        UnpackFEA( ((PEAOP)peaop2)->fpFEAList, peaop2->fpFEA2List,
                                    ULONG_MAX ,(rc)?&(peaop2->oError):NULL );
    }
    return rc;
}
