Example: Listing directories

This ILE C program creates a report listing the contents of a directory.

You should call this program with only one parameter, the parameter that represents the directory you want to list.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

/******************************************************************/
/******************************************************************/
/* FUNCTION:  This program lists a directory to a spooled file.   */
/*                                                                */
/* LANGUAGE:  ILE C                                    */
/*                                                                */
/*                                                                */
/* APIs USED:  QHFOPNDR, QHFRDDR, QHFCLODR, QHFLSTFS, QUSCRTUS,   */
/*             QUSRTVUS                                           */
/*                                                                */
/******************************************************************/
/******************************************************************/

/******************************************************************/
/* INCLUDE FILES                                                  */
/******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <qhfopndr.h>
#include <qhfrddr.h>
#include <qhfclodr.h>
#include <qhflstfs.h>
#include <quscrtus.h>
#include <qusrtvus.h>
#include <qusec.h>

/******************************************************************/
/* STRUCTURE AND VARIABLE DECLARATIONS                            */
/******************************************************************/

/******************************************************************/
/* Parameters for QHFOPNDR                                        */
/******************************************************************/
char   dir_handle[16];           /* Directory handle              */
int    namelen;                  /* Length of path name           */
char   openinfo[6];              /* Open information              */

typedef struct {
        Qhf_Attr_Selec_Tbl_t fixed;
        int                 offset2;
        int                 offset3;
        int                 att_len1;
        char                att_name1[8];
        int                 att_len2;
        char                att_name2[8];
        int                 att_len3;
        char                att_name3[8];
} selection_struct;

selection_struct select;
int              selectionlen;

/******************************************************************/
/* Error Code Structure                                           */
/*                                                                */
/* This shows how the user can define the variable length portion */
/* of error code for the exception data.                          */
/*                                                                */
/******************************************************************/
typedef struct {
     Qus_EC_t   ec_fields;
     char       Exception_Data[100];
        } error_code_t;

error_code_t  error_code;

/******************************************************************/
/* Parameters for QHFRDDR                                         */
/******************************************************************/
/* The directory handle is the same as for QHFOPNDR               */

typedef struct {
        Qhf_Data_Buffer_t   fixed;
        int                 num_att;
        int                 offsets[4];
        char                attinfo[276];
} read_buffer;

read_buffer buffer;
int    result_count;
int    bytes_returned;

/******************************************************************/
/* Parameters for QHFCLODR                                        */
/******************************************************************/
/* No additional ones need to be declared                         */

/******************************************************************/
/* Parameters for QUSCRTUS                                        */
/******************************************************************/
int    size;
char   text[50];

/******************************************************************/
/* Parameters for QHFLSTFS                                        */
/******************************************************************/
/* No additional ones need to be declared                         */

/******************************************************************/
/* Parameters for QUSRTVUS                                        */
/******************************************************************/
int    startpos;
int    len;
char   charbin4[4];
char   FSname[10];

/******************************************************************/
/* Other declarations                                             */
/******************************************************************/
int    entrypos;
int    numentries;
int    entrylen;
char   *att;
char   name[100];
char   attname[30];
char   attval[30];
int    attnamelen;
int    attvallen;
char   newname[30];
int    filesize;
char   fileatt[10];

typedef struct {
        char     century;
        char     year[2];
        char     month[2];
        char     day[2];
        char     hour[2];
        char     minute[2];
        char     second[2];
} charval;

charval chartime;

int    bytes_used;
int    i;

main(int argc, char *argv[])
{
  char    write_string[100];
  FILE    *stream;

  error_code.ec_fields.Bytes_Provided = 0;

  /****************************************************************/
  /* Make sure we received the correct number of parameters. The  */
  /* argc parameter will contain the number of parameters that    */
  /* was passed to this program. This number also includes the    */
  /* program itself, so we need to evaluate argc-1.               */
  /****************************************************************/

  if (((argc - 1) < 1) || ((argc - 1 > 1)))
  /****************************************************************/
  /* We did not receive all of the required parameters, or        */
  /* received too many. Exit from the program.                    */
  /****************************************************************/
  {
    exit(1);
  }

  /****************************************************************/
  /* Open QPRINT file so that data can be written to it.  If the  */
  /* file cannot be opened, print a message and exit.             */
  /****************************************************************/
  if((stream = fopen("QPRINT", "wb")) == NULL)
  {
    printf("File could not be opened\n");
    exit(1);
  }

  memset(name, ' ', 100);
  memcpy(name, argv[1], 100);
  if(!memcmp(name, " ", 1))
  {
  memcpy(name,"ROOT",4);
  fprintf(stream,"Directory listing for path %.100s\n", name);
  size = 1;
  memcpy(text, "temporary user space used by program DIR          ",
          50);
  /****************************************************************/
  /* Create the user space for QHFLSTFS to use.                   */
  /****************************************************************/
  QUSCRTUS("FSLST     QTEMP     ", "TEMPSPACE ", size, " ",
           "*USE      ", text, "*YES      ", &error_code);

  /****************************************************************/
  /* List the file systems into that space.                       */
  /****************************************************************/
  QHFLSTFS("FSLST     QTEMP     ", "HFSL0100", &error_code);

  /****************************************************************/
  /* Get the starting point for the file system entries.          */
  /****************************************************************/
  startpos = 125;
  len = 4;
  QUSRTVUS("FSLST     QTEMP     ", startpos, len, charbin4,
           &error_code);

  entrypos = *(int *)charbin4;

  /****************************************************************/
  /* Get the number of entries in the user space.                 */
  /****************************************************************/
  startpos = 133;
  len = 4;

  QUSRTVUS("FSLST     QTEMP     ", startpos, len, charbin4,
           &error_code);

  numentries = *(int *)charbin4;

  /****************************************************************/
  /* Find the length of the entries.                              */
  /****************************************************************/
  startpos = 137;
  len = 4;

  QUSRTVUS("FSLST     QTEMP     ", startpos, len, charbin4,
           &error_code);

  entrylen = *(int *)charbin4;

  /****************************************************************/
  /* Loop through the entries and get the names of the file       */
  /* systems.                                                     */
  /****************************************************************/
  for(i=0;i<numentries;++i)
  {
     startpos = entrypos + 1;
     len = 10;
     QUSRTVUS("FSLST     QTEMP     ", startpos, len, FSname,
              &error_code);
     /*************************************************************/
     /* List the names into the spooled file.                     */
     /*************************************************************/
     sprintf(write_string," %.10s <DIR>", FSname);
     fprintf(stream, write_string);
     entrypos = entrypos + entrylen;
  }

  }
  else
  {
  fprintf(stream,"Directory listing for path %.100s\n", name);
  /****************************************************************/
  /* Build the attribute selection table for QHFOPNDR.            */
  /****************************************************************/
  select.fixed.Number_Attributes = 3;
  select.fixed.Offset_First_Attr = 16;
  select.offset2 = 28;
  select.offset3 = 40;
  select.att_len1 = 8;
  memcpy(select.att_name1, "QFILSIZE", 8);
  select.att_len2 = 8;
  memcpy(select.att_name2, "QCRTDTTM", 8);
  select.att_len3 = 8;
  memcpy(select.att_name3, "QFILATTR", 8);
  selectionlen = 52;
  memcpy(openinfo, "10    ", 6);

  /****************************************************************/
  /* Find the length of the directory name.                       */
  /****************************************************************/
  for(i=0;i<100;i++)
  {
    if((name[i] == ' ') || (name[i] == '\x00'))
      break;
  }
  namelen = i;

  /****************************************************************/
  /* Open the directory.                                          */
  /****************************************************************/
  QHFOPNDR(dir_handle, name, namelen, openinfo, &select, selectionlen,
           &error_code);

  /****************************************************************/
  /* Read one entry from the directory.                           */
  /****************************************************************/
  QHFRDDR(dir_handle, &buffer, 300, 1, &result_count, &bytes_returned,
          &error_code);

  while(result_count > 0)
  {
    memcpy(attname,"                              ",30);
    memcpy(attval,"                              ",30);
    att = buffer.attinfo;
    bytes_used = 20;

    /**************************************************************/
    /* Loop for the number of attributes in the entry.            */
    /**************************************************************/
    for(i=0;i<buffer.num_att;i++)
    {
      memcpy(charbin4, att, 4);
      attnamelen = *(int *)charbin4;
      att += 4;
      bytes_used += 4;
      memcpy(charbin4, att, 4);
      attvallen = *(int *)charbin4;
      att += 8;
      bytes_used += 8;
      memcpy(attname, att, attnamelen);
      att += attnamelen;
      bytes_used += attnamelen;
      memcpy(attval, att, attvallen);
      att += attvallen;
      bytes_used += attvallen;

      /************************************************************/
      /* Update att so that its first character is the first      */
      /* character of the next attribute entry.                   */
      /************************************************************/
      if ((bytes_used == buffer.offsets[i+1]) &&
          ((i+1) == buffer.num_att))
        att += (buffer.offsets[i] - bytes_used);

      /************************************************************/
      /* If the attribute is QNAME, then set newname.             */
      /************************************************************/
      if(!memcmp(attname, "QNAME", 5))
      {
        memset(newname, ' ', 12);
        memcpy(newname, attval, attvallen);
      }

      /************************************************************/
      /* If the attribute is QFILSIZE, then set filesize.         */
      /************************************************************/
      else if(!memcmp(attname, "QFILSIZE", 8))
      {
        memcpy(charbin4, attval, 4);
        filesize = *(int *)charbin4;
      }
      /************************************************************/
      /* If it was QCRTDTTM, then set the time.                   */
      /************************************************************/
      else if(!memcmp(attname, "QCRTDTTM", 8))
        memcpy(&chartime, attval, 13);
      /************************************************************/
      /* Else the attribute was QFILATTR, so set fileatt.         */
      /************************************************************/
      else
        memcpy(fileatt, attval, 10);
    }

    /**************************************************************/
    /* If the entry was a directory, list its name and <DIR>.     */
    /**************************************************************/
    if(fileatt[3] == '1')
    {
      sprintf(write_string," %s <DIR>", newname);
      fprintf(stream, write_string);
    }
    /**************************************************************/
    /* If the entry is not a hidden file, list its name and size. */
    /**************************************************************/
    else if(fileatt[1] == '0')
    {
      sprintf(write_string," %s %d", newname, filesize);
      fprintf(stream, write_string);
    }
    /**************************************************************/
    /* If the entry is not a hidden file or directory, list its   */
    /* date of creation.                                          */
    /**************************************************************/
    if(fileatt[1] == '0')
    {
      sprintf(write_string," %.2s-%.2s-%.2s", chartime.month,
              chartime.day, chartime.year);
      fprintf(stream, write_string);
      sprintf(write_string," %.2s:%.2s:%.2s\n", chartime.hour,
              chartime.minute, chartime.second);
      fprintf(stream, write_string);
    }

    QHFRDDR(dir_handle, &buffer, 200, 1, &result_count, &bytes_returned,
            &error_code);

  } /* while */

  } /* else */

  /****************************************************************/
  /* Close the directory.                                         */
  /****************************************************************/
  QHFCLODR(dir_handle, &error_code);

  fclose(stream);

} /* main */