Example: Listing subdirectories

This program lists the subdirectories of the path passed to the program to a spooled file. You should call this program with only one parameter, the parameter that represents the directory you want to list.

Note: Read the Code license and disclaimer information for important legal information.
/******************************************************************/
/******************************************************************/
/* FUNCTION:                            */
/*                                                                */
/* LANGUAGE:  ILE C                                    */
/*                                                                */
/*                                                                */
/* APIs USED:  QHFOPNDR, QHFRDDR, QHFCLODR                        */
/*                                                                */
/******************************************************************/
/******************************************************************/

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

char    write_string[100];
FILE    *stream;

void print_subdir(char name[100], int numtabs)
{

/******************************************************************/
/* 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                 att_len;
        char                att_name[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[2];
        char                attinfo[180];
} read_buffer;

read_buffer buffer;
int    result_count;
int    bytes_returned;

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

/******************************************************************/
/* Other declarations                                             */
/******************************************************************/
char   *att;
char   attname[30];
char   attval[30];
int    attnamelen;
int    attvallen;
char   newname[30];
int    newnamelen;
int    filesize;
char   fileatt[10];
char   tab[5];
int    bytes_used;
int    i, j;
char   charbin4[4];
char   tempname[100];

  error_code.ec_fields.Bytes_Provided = 0;

  /****************************************************************/
  /* Build the attribute selection table for QHFOPNDR.            */
  /****************************************************************/
  select.fixed.Number_Attributes = 1;
  select.fixed.Offset_First_Attr = 8;
  select.att_len = 8;
  memcpy(select.att_name, "QFILATTR", 8);
  selectionlen = 20;
  memcpy(openinfo, "10    ", 6);
  memcpy(tab,"     ", 5);

  /****************************************************************/
  /* 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, 200, 1, &result_count, &bytes_returned,
          &error_code);

  fprintf(stream,"\n");
  for(i=0;i<numtabs;i++)
    fprintf(stream, tab);
  fprintf(stream, name);

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

    /**************************************************************/
    /* 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 and          */
      /* newnamelen just in case the entry is a directory.        */
      /************************************************************/
      if(!memcmp(attname, "QNAME", 5))
      {
        memcpy(newname, attval, attvallen);
        newnamelen = attvallen;
      }

      /************************************************************/
      /* Else the attribute was QFILATTR, so set fileatt.         */
      /************************************************************/
      else
        memcpy(fileatt, attval, 10);
    }

    /**************************************************************/
    /* If the entry was a directory, construct new path name and  */
    /* print_subdir to print the subdirectory.                    */
    /**************************************************************/
    if(fileatt[3] == '1')
    {
      memcpy(tempname, name, 100);
      strcat(name, "/");
      strcat(name, newname);
      memcpy(newname, name, namelen + newnamelen + 1);
      print_subdir(newname, numtabs + 1);
      memcpy(name, tempname, 100);
    }

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

  } /* while */

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

}/* print_subdir */


main(int argc, char *argv[])
{
  char    dir_name[100];

  /****************************************************************/
  /* 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(dir_name, ' ', 100);
  memcpy(dir_name, argv[1], 100);
  if(!memcmp(dir_name, " ", 1))
  {
  fprintf(stream,"No directory specified");
  }
  else
  {
  fprintf(stream,"Directory substructure starting at %.100s", dir_name);
  print_subdir(dir_name, 0);
  }

  fclose(stream);

} /* main */