Existence of an IFS File

Article ID: 10708

Q.For objects in the QSYS.LIB system, I can use CHKOBJ to determine command existence. What is the equivalent for items in the IFS?

A. Here are two solutions, one in RPG IV and one in CL.

(a) RPG IV program FileExists:

* NOTE: Compile with DFTACTGRP(*NO)
  *
  * IFS API prototypes
  *
  * Access
  *
 Daccess           PR            10I 0 extproc('access')
 Dpathptr1                         *   value
 Dmode1                          10I 0 value
  *
  * IFS API Constants
  *
 DF_OK             S             10I 0 inz(0)
  *
  * Some working environment for us
  *
 DFile_exists      S             10I 0
 Dpathptr          S               *
 Dpathname         S             21
 DExists           C                   'File Exists'
 DNotExists        C                   'File does not exist'
  *
  * Main{}
  *
 C     *entry        plist
 C                   parm                    filename         20
  * Set a character pointer to the file name string
 C                   eval      pathname = %trim(filename)+x'00'
 C                   eval      pathptr = %addr(pathname)
  * Call the IFS API
 C                   eval      File_Exists = access(pathptr:F_OK)
  * Did we find it?
 C     File_exists   ifeq      0
 C     Exists        dsply
 C                   else
 C     NotExists     dsply
 C                   endif
  * Thats all folks
 C                   move      *on           *inlr

The filename should be supplied as //dir/dir/file, so a valid call would be:

CALL FILEEXISTS ('//etc/pmap')

(b) The ChkIfsObj command is

CMD PROMPT('Validate an IFS Object')
PARM KWD(OBJECT) TYPE(*PNAME) LEN(256) MIN(1) +
EXPR(*YES) PROMPT('IFS object')

The ChkIfsObj command uses CL program ChkIfsObjC, shown below:

PGM PARM(&PARM)

DCL VAR(&PARM) TYPE(*CHAR) LEN(256)
DCL VAR(&RTNVALINT) TYPE(*CHAR) LEN(4)
DCL VAR(&RTNVAL) TYPE(*CHAR) LEN(2)
DCL VAR(&PATH) TYPE(*CHAR) LEN(100)
DCL VAR(&NULL) TYPE(*CHAR) LEN(1) VALUE(X'00')
DCL VAR(&BUF) TYPE(*CHAR) LEN(4096)

CHGVAR VAR(&PATH) VALUE(&PARM *TCAT &NULL)

CALLPRC PRC('stat') PARM(&PATH &BUF) +
RTNVAL(%BIN(&RTNVALINT 1 4))

CHGVAR VAR(&RTNVAL) VALUE(%BIN(&RTNVALINT))

IF COND(&RTNVAL *NE '00') THEN(SNDPGMMSG +
MSGID(CPF9897) MSGF(QCPFMSG) +
MSGDTA('Object ' || &PARM |< ' not +
found.') MSGTYPE(*ESCAPE))

ENDPGM

FileExists was written by Nick Roux
ChkIfsObj and ChkIfsObjC was written by David Leland

ProVIP Sponsors

ProVIP Sponsors