22
Sun, Dec
3 New Articles

The CL Corner: Isn't Recursion Great?

CL
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Find out how to easily check the contents of IFS subdirectories.

 

In last month's article, "What IFS Files Have Not Been Used For Three or More Days?," we saw how a CL program can easily determine how many days it's been since a file has been used. We also looked briefly at how we could automate the removal of files that have not been accessed within a user-determined number of days. Today, we will expand on last month's program with more specific handling of the stream files (*STMF) and directories (*DIR) that are processed by the DIR3 program.

 

The USEDIRPGM command currently lists the contents of the directory specified by the DIR parameter. First, we'll add a parameter to the USEDIRPGM command to optionally list the contents of any subdirectories found in the specified directory. This might be useful if, for instance, you have a main directory for transaction files and, within this directory, subdirectories for transaction files by company, day, or some other tracking mechanism.

 

In keeping with how the USEDIRPGM command is currently defined, we'll first add message description DIR0004 to the USERMSGF message file using the following command.

 

ADDMSGD MSGID(DIR0004) MSGF(USERMSGF) MSG('Directory subtree')

 

The new SUBTREE parameter is defined within the USEDIRPGM command as shown below in bold. The parameter is defined as optional with two valid values: *ALL and *NONE. The default is *ALL, indicating that all subdirectories of the directory identified by the DIR parameter should be processed.

 

CMD        PROMPT(DIR0001)                                

PARM       KWD(DIR) TYPE(*PNAME) LEN(1024) DFT(*CURDIR) + 

             SPCVAL((*CURDIR '.')) PROMPT(DIR0002)        

PARM       KWD(DAYS) TYPE(*UINT4) DFT(3) PROMPT(DIR0003)  

PARM       KWD(SUBTREE) TYPE(*CHAR) LEN(10) RSTD(*YES) +  

             DFT(*ALL) VALUES(*ALL *NONE) PROMPT(DIR0004) 

 

To create the USEDIRPGM command, you can use the following command.

 

CRTCMD CMD(USEDIRPGM) PGM(DIR3) PMTFILE(USERMSGF)

 

The CPP for USEDIRPGM, DIR3, is shown below with the changes bolded.

 

Pgm        Parm(&Dir_In &Days &SubTree)                

Dcl        Var(&Dir_In)     Type(*Char) Len(1024)      

Dcl        Var(&Days)       Type(*UInt)                

Dcl        Var(&SubTree)    Type(*Char) Len(10)        

                                                       

Dcl        Var(&InlPath)    Type(*Char) Len(1025)      

Dcl        Var(&Dir_Ptr)    Type(*Ptr)                 

                                                       

Dcl        Var(&DirEnt_Ptr) Type(*Ptr)                 

Dcl        Var(&DirEnt)     Type(*Char) Len(696) +     

             Stg(*Based) BasPtr(&DirEnt_Ptr)           

Dcl        Var(&LenOfName)  Type(*UInt) Stg(*Defined) +

             DefVar(&DirEnt 53)                        

Dcl        Var(&Name)       Type(*Char) Len(640) +     

             Stg(*Defined) DefVar(&DirEnt 57)          

                                                        

Dcl        Var(&FileInfo)   Type(*Char) Len(128)                      

Dcl        Var(&LstAccess)  Type(*Int) +                              

             Stg(*Defined) DefVar(&FileInfo 25)       /* Last open   */

Dcl        Var(&LstDtaChg)  Type(*Int) +                              

             Stg(*Defined) DefVar(&FileInfo 29)       /* Last changed*/

Dcl        Var(&ObjTyp)     Type(*Char) Len(10) +                     

             Stg(*Defined) DefVar(&FileInfo 49)       /* Object type */

                                                                      

Dcl        Var(&DatTim_Ptr) Type(*Ptr)                                

Dcl        Var(&DatTim)     Type(*Char) Len(24) +                     

             Stg(*Based) BasPtr(&DatTim_Ptr)                          

                                                                      

Dcl        Var(&SecsInDay)  Type(*Int)  Value(86400)                  

Dcl        Var(&TimeFilter) Type(*Int)                                 

                                                                      

Dcl        Var(&Path)       Type(*Char) Len(10000)                    

Dcl        Var(&MsgTxt)     Type(*Char) Len(300)                      

Dcl        Var(&Status)     Type(*Int)                        

Dcl        Var(&Null)       Type(*Char) Len(1) +              

             Value(x'00')                                     

Dcl        Var(&Null_Ptr)   Type(*Ptr)                        

                                                               

ChgVar     Var(&InlPath) Value(&Dir_In)                       

ChgVar     Var(&Path) Value(&InlPath *TCat &Null)             

CallPrc    Prc('opendir') Parm(&Path) RtnVal(&Dir_Ptr)        

If         Cond(&Dir_Ptr = &Null_Ptr) Then(Do)                

           SndPgmMsg Msg('Directory not found') +             

             ToPgmQ(*Ext)                                     

           Return                                             

           EndDo                                               

                                                              

CallPrc    Prc('time') Parm((*Omit)) RtnVal(&TimeFilter)      

ChgVar     Var(&TimeFilter) +                                 

             Value(&TimeFilter - (&Days * &SecsInDay))        

                                                                  

CallPrc    Prc('readdir') +                                       

             Parm((&Dir_Ptr *ByVal)) RtnVal(&DirEnt_Ptr)          

                                                                   

DoWhile    Cond(&DirEnt_Ptr *NE &Null_Ptr)                        

                                                                  

           If Cond(%sst(&Name 1 1) *NE '.') Then(Do)               

              ChgVar Var(&Path) Value(&InlPath *TCat '/' *TCat +  

                         %sst(&Name 1 &LenOfName) *TCat &Null)    

              CallPrc Prc('stat') +                               

                Parm(&Path &FileInfo) RtnVal(&Status)             

                                                                  

              If Cond(&Status = 0) Then(Do)                       

                                                                  

                 Select                                           

                    When Cond((&ObjTyp = '*DIR') *And +           

                              (&SubTree = '*ALL')) Then(Do)       

                         ChgVar Var(&MsgTxt) +                       

                           Value('Starting imbedded directory: ' +  

                                 *Cat %sst(&Name 1 &LenOfName))     

                         SndPgmMsg Msg(&MsgTxt) ToPgmQ(*Ext)        

                                                                     

                         ChgVar Var(&Path) Value(&InlPath *TCat +   

                           '/' *TCat +                              

                           %sst(&Name 1 &LenOfName))                

                         UseDirPgm Dir(&Path) Days(&Days) SubTree(*ALL)    

                                                                    

                         ChgVar Var(&MsgTxt) +                      

                           Value('Ended imbedded directory: ' +     

                                 *Cat %sst(&Name 1 &LenOfName))     

                         SndPgmMsg Msg(&MsgTxt) ToPgmQ(*Ext)        

                         EndDo                                      

                                                                     

                    When Cond((&ObjTyp = '*STMF') *And +            

                              (&LstAccess < &TimeFilter)) +         

                         Then(CallSubr Subr(DspStmF))                

                                                                    

                    When Cond(&ObjTyp = '*STMF') Then(Do)           

                         ChgVar Var(&MsgTxt) +                      

                           Value(&ObjTyp *TCat ' ' *Cat +           

                                 %sst(&Name 1 &LenOfName) *TCat +   

                                 ' recently accessed')              

                         SndPgmMsg Msg(&MsgTxt) ToPgmQ(*Ext)        

                         EndDo                                      

                                                                    

                    Otherwise Cmd(Do)                               

                         ChgVar Var(&MsgTxt) +                       

                           Value(&ObjTyp *TCat ' ' *Cat +           

                                 %sst(&Name 1 &LenOfName) *TCat +   

                                 ' not processed')                  

                         SndPgmMsg Msg(&MsgTxt) ToPgmQ(*Ext)        

                         EndDo                                   

                 EndSelect                                       

                 EndDo                                           

                                                                  

              Else Cmd(Do)                                       

                   ChgVar Var(&MsgTxt) Value('** ERROR **' +     

                     *Cat %sst(&Name 1 &LenOfName))              

                   SndPgmMsg Msg(&MsgTxt) ToPgmQ(*Ext)           

                   EndDo                                         

                                                                 

              EndDo                                               

                                                                 

           CallPrc Prc('readdir') +                              

             Parm((&Dir_Ptr *ByVal)) RtnVal(&DirEnt_Ptr)         

           EndDo                                                  

                                                                 

CallPrc    Prc('closedir') Parm((&Dir_Ptr *ByVal))               

                                                                  

Subr       Subr(DspStmF)                                           

           ChgVar Var(&MsgTxt) +                                  

                    Value(&ObjTyp *Cat %sst(&Name 1 &LenOfName))  

           SndPgmMsg Msg(&MsgTxt) ToPgmQ(*Ext)                    

                                                                   

           CallPrc Prc('ctime') +                                 

                     Parm(&LstDtaChg) RtnVal(&DatTim_Ptr)         

           ChgVar Var(&MsgTxt) +                                   

                    Value('Last change: ' *Cat &DatTim)           

           SndPgmMsg Msg(&MsgTxt) ToPgmQ(*Ext)                    

                                                                  

           CallPrc Prc('ctime') +                                  

                     Parm(&LstAccess) RtnVal(&DatTim_Ptr)         

           ChgVar Var(&MsgTxt) +                                  

                    Value('Last access: ' *Cat &DatTim)           

          SndPgmMsg Msg(&MsgTxt) ToPgmQ(*Ext)                     

EndSubr                                          

EndPgm                                           

 

As with last month's article, you can create the DIR3 program on V5R4 using the two following commands.

 

CRTCLMOD MODULE(DIR3)

CRTPGM PGM(DIR3) BNDDIR(QC2LE)

 

If your system is V6R1 or later, you can also use the single command shown below.

 

CRTBNDCL PGM(DIR3)

 

Two changes that are related to the new SUBTREE keyword of the USEDIRPGM command are the addition of the parameter &SUBTREE on the PGM command and the DCL for &SUBTREE.

 

The next changes within the program are not until the DO group associated with a successful return value from the stat API. As the DIR3 program will now be doing specific processing based on the characteristics of the object encountered in the directory, I elected to implement a SELECT group.

 

Within the SELECT group, the first WHEN command checks if a subdirectory has been encountered and if the SUBTREE special value *ALL was used when running the USEDIRPGM command. If so, the program performs the following processing.

 

  1. Sends a message indicating that processing of the imbedded directory is starting
  2. Formats the &Path variable by concatenating the directory specified by the DIR parameter of the USEDIRPGM command with the slash character (/) and the name of the subdirectory found
  3. Runs the USEDIRPGM command using the &Path variable formatted in step 2 for the DIR parameter, the &DAYS value initially passed to the program for the DAYS parameter, and SUBTREE(*ALL)
  4. Sends a message indicating that the processing of the imbedded directory is complete when the USEDIRPGM command run at step 3 returns
  5. Passes control to the associated ENDSELECT command as the WHEN-related DO group is finished. At this point, the next directory entry is read and the DOWHILE group processes the next entry.

 

The running of the USEDIRPGM command in step 3 above processes the specified (sub) directory in the same manner the initial directory was processed, including running further USEDIRPGM commands as additional subdirectories are discovered at lower levels of the directory tree. Coding SUBTREE(*ALL)—or SUBTREE(&SubTree), which is equivalent given the previous WHEN check that &SubTree is *ALL—is done just in case someone has used the Change Command Default (CHGCMDDFT) command and changed the default for SUBTREE to *NONE. As we know that *ALL was specified when initially running the USEDIRPGM command (in order to get to this part of the program), we want to ensure that *ALL is continued for all subsequent uses of the command.

 

This ability for a command, or more accurately the command CPP, to call itself can, as you can see, really streamline some of our processing. And a nice thing about CL is that CL programs are, by default, recursive. That is, a CL program can safely call itself multiple times. This characteristic unfortunately is not true, by default, for all languages. An RPG program, for instance, if it directly or indirectly calls itself, will fail with the escape message RNX8888—the program was called recursively—if the program is created using default creation values. RPG does have recursive capabilities, but you need to put a bit (though not a lot) more thought into it.

 

One caution concerning recursive use of the USEDIRPGM command, though: the DIR parameter of the command is arbitrarily defined with a length of 1024 bytes. If you have deeply nested directories and/or long directory names, you may want to increase the size of this keyword definition when using SUBTREE(*ALL).

 

The additional changes found in the DIR3 program are unrelated to the processing of subdirectories when running the USEDIRPGM command.

 

The second WHEN command checks if a *STMF has been encountered and if the time of last access is earlier than the value of &TimeFilter. If so, the subroutine DspStmF is run. The actual processing found in the DspStmF subroutine is the same as last month. Moving this processing into a subroutine was simply an easy way to get the code out of the mainline, which was beginning to look a little cluttered.

 

The third WHEN command identifies those *STMFs that have been accessed more recently than the value of &TimeFilter. In this situation, DIR3 sends a message indicating that the *STMF was processed and that it has been recently accessed.

 

The OTHERWISE command catches all situations not previously matched up with a WHEN command. This might be a subdirectory where SUBTREE(*NONE) was specified or an object type other than *DIR and *STMF. In this case, DIR3 sends a message informing the user that the object was not processed.

 

To demonstrate running the enhanced USEDIRPGM command, imagine that there is a directory /MyPlayDir with the following contents:

 

.                      DIR  

..                     DIR  

file1.txt              STMF 

file2.txt              STMF 

MyImbeddedDir          DIR  

 

Within the directory MyImbeddedDir, which is found in /MyPlayDir, is:

 

.                      DIR    

..                     DIR    

delete                 DIR    

imbedded_test_file     STMF   

 

And within the directory Delete, which is found in /MyPlayDir/MyImbeddedDir, is:

 

.                      DIR  

..                     DIR  

 

Running the command USEDIRPGM DIR('/MyPlayDir') will result in messages similar to the following (assuming that all of the *STMFs have been accessed within the last three days).

                  

*STMF file2.txt recently accessed            

Starting imbedded directory: MyImbeddedDir   

Starting imbedded directory: delete          

Ended imbedded directory: delete             

*STMF imbedded_test_file recently accessed   

Ended imbedded directory: MyImbeddedDir      

*STMF file1.txt recently accessed            

 

Running the command USEDIRPGM DIR('/MyPlayDir') SUBTREE(*NONE) will result in messages similar to the following:

 

*STMF file2.txt recently accessed          

*DIR MyImbeddedDir not processed           

*STMF file1.txt recently accessed          

 

Next month, we'll look at some additional capabilities that can be added to the USEDIRPGM command to help you manage your IFS.

More CL Questions?

Wondering how to accomplish a function in CL? Send your CL-related questions to me at This email address is being protected from spambots. You need JavaScript enabled to view it.. I'll try to answer your burning questions in future columns.

as/400, os/400, iseries, system i, i5/os, ibm i, power systems, 6.1, 7.1, V7, V6R1

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: