星期一, 11月 06, 2023

2003-09-25 如何取得 IFS 檔案的日期資訊?(C API stat)(Command IFSSTAT)


如何取得 IFS 檔案的日期資訊?(C API stat)

在現在的網路環境中,時常會有資料儲存於 AS/400 上的 IFS 目錄中,你可以使用
WRKLNK 指令瀏覽 AS/400 系統的 IFS 檔案系統目錄。

有時候可能需要於程式中檢查 IFS 檔案的日期,即可以使用 C API stat() 函數取得檔案的資訊。


File  : QRPGLESRC
Member: STATR
Type  : RPGLE
Usage : CRTBNDRPG STATR
OS Version: V4 或 V5

     H DFTACTGRP(*NO) ACTGRP(*NEW) DEBUG
     D**********************************************************************
     D* File Information Structure (stat)
     D*
     D* struct stat {
     D*  mode_t         st_mode;       /* File mode                       */
     D*  ino_t          st_ino;        /* File serial number              */
     D*  nlink_t        st_nlink;      /* Number of links                 */
     D*  uid_t          st_uid;        /* User ID of the owner of file    */
     D*  gid_t          st_gid;        /* Group ID of the group of file   */
     D*  off_t          st_size;       /* For regular files, the file
     D*                                 * size in bytes                   */
     D*  time_t         st_atime;      /* Time of last access             */
     D*  time_t         st_mtime;      /* Time of last data modification  */
     D*  time_t         st_ctime;      /* Time of last file status change */
     D*  dev_t          st_dev;        /* ID of device containing file    */
     D*  size_t         st_blksize;    /* Size of a block of the file     */
     D*  unsigned long  st_allocsize;  /* Allocation size of the file     */
     D*  qp0l_objtype_t st_objtype;    /* AS/400 object type              */
     D*  unsigned short st_codepage;   /* Object data codepage            */
     D*  char           st_reserved1[66]; /* Reserved                     */
     D* };
     D*
     D p_statds        S               *
     D statds          DS                  BASED(p_statds)
     D  st_mode                      10U 0
     D  st_ino                       10U 0
     D  st_nlink                      5U 0
     D  st_pad                        2A
     D  st_uid                       10U 0
     D  st_gid                       10U 0
     D  st_size                      10I 0
     D  st_atime                     10I 0
     D  st_mtime                     10I 0
     D  st_ctime                     10I 0
     D  st_dev                       10U 0
     D  st_blksize                   10U 0
     D  st_alctize                   10U 0
     D  st_objtype                   12A
     D  st_codepag                    5U 0
     D  st_resv11                    62A
     D  st_ino_gen_id                10U 0

     D*--------------------------------------------------------------------
     D* Main procedure declaration
     D*
     D*--------------------------------------------------------------------
     D STATR           PR                  EXTPGM('STATR')
     D  FileName                    100a   const
     D STATR           PI
     D  FileName                    100a   const

     D*--------------------------------------------------------------------
     D* Get File Information
     D*
     D* int stat(const char *path, struct stat *buf)
     D*--------------------------------------------------------------------
     D stat            PR            10I 0 ExtProc('stat')
     D   path                          *   value options(*string)
     D   buf                           *   value

     D GetTimeZone     PR             5A

     D timezone        DS
     D   tzDir                        1A
     D   tzHour                       2S 0
     D   tzFrac                       2S 0


     D statsize        S             10I 0
     d Msg             S             50A
     D AccessTime      S               Z
     D ModifyTime      S               Z
     D ChgStsTime      S               Z
     D charTS          S             26A
     D Epoch           S               Z   INZ(z'1970-01-01-00.00.00')

     c                   eval      *inlr = *on

     c                   eval      statsize = %size(statds)
     c                   alloc     statsize      p_statds

     c*                  if        stat('/JAVA/mail.jar' : p_statds) < 0
     c                   if        stat(%trimr(FileName) : p_statds) < 0
     c                   dump
     c                   eval      Msg = 'stat() failed?  Check errno!'
     c                   dsply                   Msg
     c                   return
     c                   endif

      ** times in statds are seconds from the "epoch" (Jan 1, 1970)
      **   and are in GMT (Greenwich Mean Time)...
      ** Hey!  Lets convert them to RPG timestamps!
     c     Epoch         adddur    st_atime:*S   AccessTime
     c     Epoch         adddur    st_mtime:*S   ModifyTime
     c     Epoch         adddur    st_ctime:*S   ChgStsTime

      ** adjust timestamps for timezone:
     c                   eval      timezone = GetTimeZone
     c                   if        tzDir = '-'
     c                   subdur    tzHour:*H     AccessTime
     c                   subdur    tzHour:*H     ModifyTime
     c                   subdur    tzHour:*H     ChgStsTime
     c                   else
     c                   adddur    tzHour:*H     AccessTime
     c                   adddur    tzHour:*H     ModifyTime
     c                   adddur    tzHour:*H     ChgStsTime
     c                   endif

     C* display the relevant times:
     c                   move      AccessTime    charTS
     c                   eval      Msg = 'Last Access ' + charTS
     c                   dsply                   Msg

     c                   move      ModifyTime    charTS
     c                   eval      Msg = 'Last Modified ' + charTS
     c                   dsply                   Msg

     c                   move      ChgStsTime    charTS
     c                   eval      Msg = 'Status Changed ' + charTS
     c                   dsply                   Msg

     c                   dealloc                 p_statds
     c                   eval      *inlr = *on


     P*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     P*  This gets the offset from Universal Coordinated Time (UTC)
     P*    from the system value QUTCOFFSET
     P*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     P GetTimeZone     B
     D GetTimeZone     PI             5A
     D peRcvVar        S              1A   DIM(100)
     D peRVarLen       S             10I 0
     D peNumVals       S             10I 0
     D peSysValNm      S             10A
     D p_Offset        S               *
     D wkOffset        S             10I 0 BASED(p_Offset)
     D p_SV            S               *
     D dsSV            ds                  BASED(p_SV)
     D   dsSVSysVal                  10A
     D   dsSVDtaTyp                   1A
     D   dsSVDtaSts                   1A
     D   dsSVDtaLen                  10I 0
     D   dsSVData                     5A
     D dsErrCode       DS
     D  dsBytesPrv             1      4B 0 INZ(256)
     D  dsBytesAvl             5      8B 0 INZ(0)
     D  dsExcpID               9     15
     D  dsReserved            16     16
     D  dsExcpData            17    256
     C                   CALL      'QWCRSVAL'                           99
     C                   PARM                    peRcvVar
     C                   PARM      100           peRVarLen
     c                   PARM      1             peNumVals
     c                   PARM      'QUTCOFFSET'  peSysValNm
     c                   PARM                    dsErrCode
     c                   if        dsBytesAvl > 0  or  *IN99 = *On
     c                   return    *blanks
     c                   endif
     c                   eval      p_Offset = %addr(peRcvVar(5))
     c                   eval      p_SV = %addr(peRcvVar(wkOffset+1))
     c                   return    dsSVData
     P                 E




File  : QCMDSRC
Member: IFSSTAT
Type  : CMD
Usage : CRTCMD CMD(IFSSTAT) PGM(STATR)
        IFSSTAT IFSPATHNME('/tmp/***')
        '*' 星號表示位於目錄 /tmp 下的檔案,
        例如使用 WRKLNK ('/tmp') 即可找到要查詢的檔名。

OS Version: V4 或 V5


             CMD        PROMPT(' Display IFS stat ')
             PARM       KWD(IFSPATHNME) TYPE(*CHAR) LEN(100) +
                          PROMPT('Enter IFS PATH and File name')




沒有留言: