Now, Get RPG Codes in all three formats only on this website on every RPGLE related programming article.

Fixed, /Free and Fully Free RPG


How to retrieve stream file stats in RPGLE

How to retrieve stream file stats using C api stat() in RPGLE .
How to retrieve stream file stat Information using C stat API in RPGLE , stat()api, open() api write() api, read() api, close()api, as400, ibmi, iseries, systemi, as400andsqltricks, as400 tutorials, ibmi tutorials
How to retrieve stream file stat Information using C stat API in RPGLE

What is stat() API?

The stat() function gets information about a stream file in the IFS such as file size, access permissions, and the time it was last modified etc.

Prototype for the stat() API in C Language

int stat(const char *path, struct stat *buf);
  1. The stat() function gets status information about a specified file.
  2. The return value is an int (integer) and values are 0 which indicate success, or -1 if an error occurred.
  3. const char *path:First parameter is input path. A pointer to the null-terminated path name of the object from which information is required.
  4. struct stat *buf:Second parameter is output buf. A pointer to the stat data structure. For this parameter we need to create a data structure same as stat data structure in C.

Prototype for the stat() API in RPG

     D stat            PR            10I 0 ExtProc('stat')                   
     D ifspath                         *   value options(*string)            
     D statds                          *   value            

Layout of the stat data structure in C Language

struct stat {                                                        
         mode_t         st_mode;       // File mode                      
         ino_t          st_ino;        // File serial number          
         nlink_t        st_nlink;      // Number of links            
         uid_t          st_uid;        // User ID of the owner of file  
         gid_t          st_gid;        // Group ID of the group of file  
         off_t          st_size;       // For regular files, the file (size in bytes)                      
         time_t         st_atime;      // Time of last access           
         time_t         st_mtime;      // Time of last data modification 
         time_t         st_ctime;      // Time of last file status change
         dev_t          st_dev;        // ID of device containing file    
         size_t         st_blksize;    // Size of a block of the file    
         unsigned long  st_allocsize;  // Allocation size of the file   
         qp0l_objtype_t st_objtype;    // AS/400 object type             
         unsigned short st_codepage;   // Object data codepage          
         char           st_reserved1[62]; // Reserved                      
         unsigned int   st_ino_gen_id  // file serial number generation id 
        };      

Above defined is the structure definition of stat data structure in C language. You can refer more about stat data structure layout from the IBM documentation.

Layout of the stat data structure in RPG

     D statds          DS                       
     D  filemode                     10U 0      
     D  fileid                       10U 0      
     D  Numoflinks                    5U 0      
     D  reserved2                     5U 0      
     D  useridofowner                 2A        
     D  grpidofgroup                 10U 0      
     D  regularfiles                 10U 0      
     D  size                         10I 0      
     D  time_access                  10I 0      
     D  time_change                  10I 0      
     D  time_stschang                10I 0      
     D  filesysid                    10U 0      
     D  blocksize                    10U 0      
     D  allocatedsize                10U 0      
     D  objecttype                   12A        
     D  codepage                      5U 0      
     D  ccsid                         5U 0      
     D  reserved1                    62A        
     D  generationid                 10U 0    

RPGLE program for retrieve stream file stat

RPG Code in Fixed format to retrieve stream file stat Information using C stat() API in RPGLE.
     HDFTACTGRP(*NO)                                                         
     D stat            PR            10I 0 ExtProc('stat')                   
     D ifspath                         *   value options(*string)            
     D statds                          *   value                             
                                                                             
     D errorifs        PR              *   ExtProc('__errno')                
                                                                             
     D strerror        PR              *   ExtProc('strerror')               
     D error_num                     10I 0 value                             
                                                                             
                                                                             
      *                                                                      
     Difspath          s            512a                                     
     Dreturn_stat      s             10i 0 inz                               
     Derror_ptr        S               *                                     
     Derror_num        S             10I 0 based(error_ptr)                  
                                                                             
     Derrormsg_ptr     S               *                                     
     Derror_msg        S            500a   based(errormsg_ptr)               
                                                 
     D statds          DS                        
     D  filemode                     10U 0       
     D  fileid                       10U 0       
     D  Numoflinks                    5U 0       
     D  reserved2                     5U 0       
     D  useridofowner                 2A         
     D  grpidofgroup                 10U 0       
     D  regularfiles                 10U 0       
     D  size                         10I 0       
     D  time_access                  10I 0       
     D  time_change                  10I 0       
     D  time_stschang                10I 0       
     D  filesysid                    10U 0       
     D  blocksize                    10U 0       
     D  allocatedsize                10U 0       
     D  objecttype                   12A         
     D  codepage                      5U 0       
     D  ccsid                         5U 0       
     D  reserved1                    62A         
     D  generationid                 10U 0                                    
     C                   EVAL      ifspath = '/home/easyclass/helloworld'     
     C                   EVAL      return_stat = stat(%trim(ifspath):         
     C                                                %addr(statds))          
     C                   IF        return_stat < 0                            
     C                   EVAL      error_ptr = errorIFS()                     
     C                   EVAL      errormsg_ptr = strerror(error_num)         
     C                   ENDIF                                                
     C                   EVAL      *inlr = *ON                                
     C                   RETURN                                                                                              
RPG Code in /Free and /End-Free format to retrieve stream file stat Information using C stat() API in RPGLE.
     HDFTACTGRP(*NO)                                               
     D stat            PR            10I 0 ExtProc('stat')         
     D ifspath                         *   value options(*string)  
     D statds                          *   value                   
                                                                   
     D errorifs        PR              *   ExtProc('__errno')      
                                                                   
     D strerror        PR              *   ExtProc('strerror')     
     D error_num                     10I 0 value                   
                                                                   
                                                                   
      *                                                            
     Difspath          s            512a                           
     Dreturn_stat      s             10i 0 inz                     
     Derror_ptr        S               *                           
     Derror_num        S             10I 0 based(error_ptr)        
                                                                   
     Derrormsg_ptr     S               *                           
     Derror_msg        S            500a   based(errormsg_ptr)     
                                                 
     D statds          DS                        
     D  filemode                     10U 0       
     D  fileid                       10U 0       
     D  Numoflinks                    5U 0       
     D  reserved2                     5U 0       
     D  useridofowner                 2A         
     D  grpidofgroup                 10U 0       
     D  regularfiles                 10U 0       
     D  size                         10I 0       
     D  time_access                  10I 0       
     D  time_change                  10I 0       
     D  time_stschang                10I 0       
     D  filesysid                    10U 0       
     D  blocksize                    10U 0       
     D  allocatedsize                10U 0       
     D  objecttype                   12A         
     D  codepage                      5U 0       
     D  ccsid                         5U 0       
     D  reserved1                    62A         
     D  generationid                 10U 0             
                                                       
      /free                                            
       ifspath = '/home/easyclass/helloworld';         
                                                       
       return_stat = stat(%trim(ifspath):              
                          %addr(statds));              
       if return_stat < 0;                             
         error_ptr = errorIFS();                       
         errormsg_ptr = strerror(error_num);           
       endif;                                          
                                                       
       *inlr = *on;                                    
       return;                                         
      /end-free                                                                                                                   
RPG Code in Fully Free format to retrieve stream file stat Information using C stat() API in RPGLE.
**FREE                                        
CTL-OPT DFTACTGRP(*NO);                       
DCL-PR stat int(10) EXTPROC('stat');          
  ifspath pointer VALUE options(*string);     
  statds  pointer VALUE;                      
END-PR;                                       
                                              
DCL-PR errorifs pointer EXTPROC('__errno');   
END-PR;                                       
                                              
DCL-PR strerror pointer EXTPROC('strerror');  
  error_num int(10) VALUE;                    
END-PR;                                       
                                              
DCL-S ifspath CHAR(512);                      
DCL-S return_stat int(10);                    
DCL-S error_ptr pointer;                      
DCL-S error_num int(10) based(error_ptr);     
DCL-S errormsg_ptr pointer;                   
DCL-S error_msg char(500) based(errormsg_ptr);       
                                                     
DCL-DS statds;                                       
  filemode uns(10);                                  
  fileid   uns(10);                                  
  Numoflinks uns(5);                                 
  reserved2  uns(5);                                 
  useridofowner char(2);                             
  grpidofgroup uns(10);                              
  regularfiles uns(10);                              
  size int(10);                                      
  time_access int(10);                               
  time_change int(10);                               
  time_stschang int(10);                             
  filesysid uns(10);                                 
  blocksize uns(10);                                 
  allocatedsize uns(10);                             
  objecttype char(12);                               
  codepage uns(5);                                   
  ccsid uns(5);                                      
  reserved1 char(62);                             
  generationid uns(10);                           
END-DS;                                           
                                                  
       ifspath = '/home/easyclass/helloworld';    
                                                  
       return_stat = stat(%trim(ifspath):         
                          %addr(statds));         
       if return_stat < 0;                        
         error_ptr = errorIFS();                  
         errormsg_ptr = strerror(error_num);      
       endif;                                     
                                                  
       *inlr = *on;                               
       return;                                                             

Here, in the above source code, we are retrieving the information of the file helloworld in user home directory to the statds data structure by calling stat() C api in RPGLE and returning the integer value. If return is 0 means stat retrieved successfully and if its -1 then failed to retrieve stats and then we call ifs error handling apis to get the error number and corresponding error messages.

Post a Comment

© AS400 and SQL Tricks. All rights reserved. Developed by Jago Desain