Example in ILE C: Using keys with List Spooled Files API

This example shows the steps necessary to process keyed output from an API

Note: Read the Code license and disclaimer information for important legal information.

Refer to Example in OPM RPG: Using keys with List Spooled Files API for the original example.

/******************************************************************/
/*                                                                */
/*   Program:      List Spooled Files for Current User            */
/*                                                                */
/*   Language:     ILE C                                          */
/*                                                                */
/*   Description:  This example shows the steps necessary to      */
/*                 process keyed output from an API               */
/*                                                                */
/*   APIs Used:    QUSLSPL  - List Spooled Files                  */
/*                 QUSCRTUS - Create User Space                   */
/*                 QUSPTRUS - Retrieve Pointer to User Space      */
/*                                                                */
/******************************************************************/

#include <stdio.h>
#include <string.h>
#include <quslspl.h>     /* QUSLSPL API header                    */
#include <quscrtus.h>    /* QUSCRTUS API header                   */
#include <qusptrus.h>    /* QUSPTRUS API header                   */
#include <qusgen.h>      /* Format Structures for User Space (11) */
#include <qusec.h>       /* Error Code parameter include for APIs */
#include <qliept.h>      /* Entry Point Table include for APIs    */

/******************************************************************/
/* Global variables                                               */
/******************************************************************/
char     spc_name[20] = "SPCNAME   QTEMP     ";
int      spc_size = 2000;
char     spc_init = 0x00;
char    *spcptr, *lstptr, *lstptr2;
int      pages;
struct   keys { int key1;                                   (7)
                int key2;
                int key3;} keys = {201, 211, 216};          (8)
int      number_of_keys = 3;
char     ext_attr[10] = "QUSLSPL   ";
char     spc_aut[10] = "*ALL      ";
char     spc_text[50] = "                                        ";
char     spc_replac[10] = "*YES      ";
char     spc_domain[10] = "*USER     ";
char     format[8] = "SPLF0200";                  (4)
char     usr_prf[10] = "*CURRENT  ";
char     outq[20] = "*ALL                ";
char     formtyp[10] = "*ALL      ";
char     usrdta[10] = "*ALL      ";
char     jobnam[26] = "                          ";
char     prtfil[10];
char     opndat[7];
typedef  struct {
         Qus_LSPL_Key_Info_t Key_Info;
         char                Data_Field[100];
         } var_record_t;
Qus_EC_t error_code;
int      i, j;
char     prtlin[100];
FILE    *record;

main()
{
   /***************************************************************/
   /* Open print file for report                                  */
   /***************************************************************/

   if((record = fopen("QPRINT", "wb, lrecl=132, type=record")) == NULL)
     { printf("File QPRINT could not be opened\n");
       exit();
     }
   /***************************************************************/
   /* Set Error Code structure to use exceptions                  */
   /***************************************************************/

   error_code.Bytes_Provided = 0;                           (1)

   /***************************************************************/
   /* Create a User Space for the List generated by QUSLSPL       */
   /***************************************************************/

   QUSCRTUS(spc_name,        /* User space name and library  (2)  */
            ext_attr,        /* Extended attribute                */
            spc_size,        /* Initial space size                */
            &spc_init,       /* Initialize value for space        */
            spc_aut,         /* Public authorization              */
            spc_text,        /* Text description                  */
            spc_replac,      /* Replace option                    */
            error_code,      /* Error code structure              */
            spc_domain);     /* Domain of space                   */

   /***************************************************************/
   /* Call QUSLSPL to get all spooled files for *CURRENT user     */
   /***************************************************************/

   QUSLSPL( spc_name,        /* User space name and library  (3)  */
            format,          /* API format          (4)           */
            usr_prf,         /* User profile                      */
            outq,            /* Output Queue                      */
            formtyp,         /* Form type                         */
            usrdta,          /* User data                         */
            error_code,      /* Error code structure              */
            jobnam,          /* Job name                          */
            keys,            /* Keys to return      (5)           */
            number_of_keys); /* Number of keys      (6)           */

   /***************************************************************/
   /* Get a resolved pointer to the User Space                    */
   /***************************************************************/

   QUSPTRUS(spc_name,        /* User space name and library  (9)  */
            &spcptr,         /* Space pointer                     */
            error_code);     /* Error code structure              */

   /***************************************************************/
   /* If valid information returned                               */
   /***************************************************************/

   if(memcmp\
     (((Qus_Generic_Header_0100_t *)spcptr)->Structure_Release_Level,  (12)
        "0100", 4) != 0) { printf("Unknown Generic Header");   (13)
                           exit();
                         }

   if((((Qus_Generic_Header_0100_t *)spcptr)->Information_Status=='C')\  (14)
        || (((Qus_Generic_Header_0100_t *)spcptr)->Information_Status\
        == 'P'))
        {
         if(((Qus_Generic_Header_0100_t *)spcptr)->Number_List_Entries\  (16)
              > 0)

   /***************************************************************/
   /* address current list entry                                  */
   /***************************************************************/

         {
          lstptr = spcptr + (((Qus_Generic_Header_0100_t *)spcptr)\
                   ->Offset_List_Data);

   /***************************************************************/
   /* process all the entries                                     */
   /***************************************************************/

          for(i = 0; i < (((Qus_Generic_Header_0100_t *)spcptr)\ (20)
                            ->Number_List_Entries); i++)
            {

   /***************************************************************/
   /* set lstptr2 to first variable length record for this entry  */
   /***************************************************************/

             lstptr2 = lstptr + 4;

   /***************************************************************/
   /* process all the variable length records for this entry      */
   /***************************************************************/

             for(j = 0; j < (((Qus_SPLF0200_t *)lstptr)\  (22) (23)
                               ->Num_Fields_Retd); j++)
               {

   /***************************************************************/
   /* extract spooled file name for report                        */
   /***************************************************************/

                if((((Qus_LSPL_Key_Info_t *)lstptr2)\  (24) (25)
                      ->Key_Field_for_Field_Retd) == 201)
                      { memcpy(prtfil, "          ", 10);
                        memcpy(prtfil, (((var_record_t *)\
                              lstptr2)->Data_Field),
                              (((Qus_LSPL_Key_Info_t *)lstptr2)\
                                 ->Data_Length));
                      }

   /***************************************************************/
   /* extract number of pages for report                          */
   /***************************************************************/

                if((((Qus_LSPL_Key_Info_t *)lstptr2)\  (24) (25)
                      ->Key_Field_for_Field_Retd) == 211)
                      { memcpy(&pages, (((var_record_t *)\
                              lstptr2)->Data_Field),
                              (((Qus_LSPL_Key_Info_t *)lstptr2)\
                                 ->Data_Length));
                      }

   /***************************************************************/
   /* extract age of spooled file for report                      */
   /***************************************************************/

                if((((Qus_LSPL_Key_Info_t *)lstptr2)\  (24) (25)
                      ->Key_Field_for_Field_Retd) == 216)
                      { memcpy(opndat, "       ", 7);
                        memcpy(opndat, (((var_record_t *)\
                              lstptr2)->Data_Field),
                              (((Qus_LSPL_Key_Info_t *)lstptr2)\
                                 ->Data_Length));
                      }

   /***************************************************************/
   /* bump lstptr2 to next variable length record                 */
   /***************************************************************/

                lstptr2 = lstptr2 +
                              (((Qus_LSPL_Key_Info_t *)lstptr2)\
                                 ->Len_Field_Info_Retd);
               }

   /***************************************************************/
   /* print collected information                                 */
   /***************************************************************/

             sprintf(prtlin, "%.10s     %.10d     %.7s",  (26)
                  prtfil, pages, opndat);
             fwrite(prtlin, 1, 100, record);

   /***************************************************************/
   /* bump lstptr to next list entry                              */
   /***************************************************************/

             lstptr += (((Qus_Generic_Header_0100_t *)spcptr)\  (27)
                            ->Size_Each_Entry);
            }

   /***************************************************************/
   /* exit at end of list                                         */
   /***************************************************************/

          fclose(record);
          exit();
         }
        }
       else
        { printf("List data not valid");         (15)
          exit();
        }
}                                                (28)
Related reference
Example in OPM RPG: Using keys with List Spooled Files API