Temporarily change a list of concatenated tableBASE libraries

It may occasionally be necessary to temporarily modify the concatenated library list and/or the status switches during processing, and their original values may not be known. Therefore, in order to restore them, the active library list and/or processing status must be saved first.

In COBOL
 DATA DIVISION.
 01  LIB-LIST-SAVE                       PIC X(80).
 01  STATUS-SWITCHES-SAVE                PIC X(8).

 01  LIB-LIST-NEW.
    05  LIBRARY-1                        PIC X(8)  VALUE 'TEST1'.
    05  LIBRARY-2                        PIC X(8)  VALUE 'TEST2'.
    05  LIBRARY-3                        PIC X(8)  VALUE 'TEST3'.
    05  LIBRARY-4                        PIC X(8)  VALUE 'PRODLIB'.
    05  FILLER                           PIC X(48) VALUE SPACES.
	
 01  STATUS-SWITCHES-NEW                 PIC X(8)  VALUE 'YN'.
 
 PROCEDURE DIVISION.
 
*** SAVE CURRENT TABLE BASE LIBRARY LIST
    MOVE 'LL' TO xxxx-COMMAND
    CALL 'TBLBASE' USING TB-PARM
                         xxxx-COMMAND-AREA
                         LIB-LIST-SAVE

*** SAVE CURRENT PROCESSING STATUS
    MOVE 'LS' TO xxxx-COMMAND
    CALL 'TBLBASE' USING TB-PARM
                         xxxx-COMMAND-AREA
                         STATUS-SWITCHES-SAVE

*** LOAD NEW TABLE BASE LIBRARY LIST
    MOVE 'ML' TO xxxx-COMMAND
    CALL 'TBLBASE' USING TB-PARM
                         xxxx-COMMAND-AREA
                         LIB-LIST-NEW

*** LOAD NEW PROCESSING STATUS
    MOVE 'CS' TO xxxx-COMMAND
    CALL 'TBLBASE' USING TB-PARM
                         xxxx-COMMAND-AREA
                         STATUS-SWITCHES-NEW
*   (normal processing until end of routine)

*** RESTORE PREVIOUS TABLE BASE LIBRARY LIST
    MOVE 'ML' TO xxxx-COMMAND
    CALL 'TBLBASE' USING TB-PARM
                         xxxx-COMMAND-AREA
                         LIB-LIST-SAVE

*** RESTORE PREVIOUS PROCESSING STATUS
    MOVE 'CS' TO xxxx-COMMAND
    CALL 'TBLBASE' USING TB-PARM
                         xxxx-COMMAND-AREA
                         STATUS-SWITCHES-SAVE
In C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "dkh.h"

/*
 * DK1TEX8C
 *
 * Temporarily change a concatenated list of tableBASE libraries.
 */
 
/*
 * Assume these are user inputs.
 */
static char szTableName[6] = "AARON";
static char szStatus[6] = "NYYYN";
/*
 * Assume you have these DDName defined in your JCL.
 */
static char szTestLib[8] = "TESTLIB";
static char szProLib[7] = "PROLIB";

int main(void)
{
    TbParmStruct tbParm;
    TbCommandAreaStruct tbCommArea;
    TbLibListStruct tbLibListSaved;
    TbLibListStruct tbLibList;
    char sTestLib[8];
    char sProLib[8];
    char sTableName[8];
    char sStatus[8];
    char sStatusSaved[8];
    
    /*
     * Initialize the parameters.
     */
    fixStringLength( szTableName, sTableName, 8 );
    InitTbParm( &tbParm );
    InitTbCommandArea( &tbCommArea, sTableName );
    InitLibList( &tbLibListSaved );
    InitLibList( &tbLibList );
    
    /*
     * Save the current library list with LL.
     */
    memcpy( tbCommArea.tbCommand, "LL", 2 );
    TBLBASE( &tbParm, &tbCommArea, &tbLibListSaved );
    if( tbCommArea.tbError != TB_SUCCESS )
    {
        printf( "LL\n");
        printf( "Found code: %c\n", tbCommArea.tbFound );
        printf( "Error code: %d\n", tbCommArea.tbError );
        printf( "Sub code: %d\n", tbCommArea.tbErrorSubcode );
        return tbCommArea.tbError;
    }
    
    /*
     * Save the current status with LS.
     */
    memcpy( tbCommArea.tbCommand, "LS", 2 );
    TBLBASE( &tbParm, &tbCommArea, sStatusSaved );
    if( tbCommArea.tbError != TB_SUCCESS )
    {
        printf( "LS\n");
        printf( "Found code: %c\n", tbCommArea.tbFound );
        printf( "Error code: %d\n", tbCommArea.tbError );
        printf( "Sub code: %d\n", tbCommArea.tbErrorSubcode );
        return tbCommArea.tbError;
    }
    
    /*
     * Change the library list with ML.
     */
    fixStringLength( sTestLib, szTestLib, 8 );
    fixStringLength( sProLib, szProLib, 8 );
    memcpy( tbLibList.lib1, sTestLib, 8 );
    memcpy( tbLibList.lib2, sProLib, 8 );
    memcpy( tbCommArea.tbCommand, "ML", 2 );
    TBLBASE( &tbParm, &tbCommArea, &tbLibList );
    if( tbCommArea.tbError != TB_SUCCESS )
    {
        printf( "ML\n");
        printf( "Found code: %c\n", tbCommArea.tbFound );
        printf( "Error code: %d\n", tbCommArea.tbError );
        printf( "Sub code: %d\n", tbCommArea.tbErrorSubcode );
        return tbCommArea.tbError;
    }
    
    /*
     * Change to the new status with CS.
     */
    fixStringLength( szStatus, sStatus, 8 );
    memcpy( tbCommArea.tbCommand, "CS", 2 );
    TBLBASE( &tbParm, &tbCommArea, sStatus );
    if( tbCommArea.tbError != TB_SUCCESS )
    {
        printf( "CS\n");
        printf( "Found code: %c\n", tbCommArea.tbFound );
        printf( "Error code: %d\n", tbCommArea.tbError );
        printf( "Sub code: %d\n", tbCommArea.tbErrorSubcode );
        return tbCommArea.tbError;
    }
    
    /*
     * Insert all the normal processing here.
     */
 
    /*
     * At the end of normal processing restore both status
     * and library search order.
     */
     
    memcpy( tbCommArea.tbCommand, "ML", 2 );
    TBLBASE( &tbParm, &tbCommArea, &tbLibListSaved );
    if( tbCommArea.tbError != TB_SUCCESS )
    {
        printf( "ML\n");
        printf( "Found code: %c\n", tbCommArea.tbFound );
        printf( "Error code: %d\n", tbCommArea.tbError );
        printf( "Sub code: %d\n", tbCommArea.tbErrorSubcode );
        return tbCommArea.tbError;
    }
    
    memcpy( tbCommArea.tbCommand, "CS", 2 );
    TBLBASE( &tbParm, &tbCommArea, sStatusSaved );
    if( tbCommArea.tbError != TB_SUCCESS )
    {
        printf( "CS\n");
        printf( "Found code: %c\n", tbCommArea.tbFound );
        printf( "Error code: %d\n", tbCommArea.tbError );
        printf( "Sub code: %d\n", tbCommArea.tbErrorSubcode );
        return tbCommArea.tbError;
    }
    
    printf( "Restored the original LibList and Status-switch\n");
    
    return TB_SUCCESS;
}