309 lines
12 KiB
HTML
309 lines
12 KiB
HTML
|
<?xml version="1.0" encoding="UTF-8"?>
|
||
|
<!DOCTYPE html
|
||
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
<html lang="en-us" xml:lang="en-us">
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||
|
<meta name="security" content="public" />
|
||
|
<meta name="Robots" content="index,follow" />
|
||
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
||
|
<meta name="DC.Type" content="reference" />
|
||
|
<meta name="DC.Title" content="Example: Listing subdirectories" />
|
||
|
<meta name="abstract" content="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." />
|
||
|
<meta name="description" content="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." />
|
||
|
<meta name="DC.Relation" scheme="URI" content="apiexmp.htm" />
|
||
|
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
|
||
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
|
||
|
<meta name="DC.Format" content="XHTML" />
|
||
|
<meta name="DC.Identifier" content="apiexlissub" />
|
||
|
<meta name="DC.Language" content="en-us" />
|
||
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
||
|
<!-- US Government Users Restricted Rights -->
|
||
|
<!-- Use, duplication or disclosure restricted by -->
|
||
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
||
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
||
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
||
|
<title>Example: Listing subdirectories</title>
|
||
|
</head>
|
||
|
<body id="apiexlissub"><a name="apiexlissub"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Example: Listing subdirectories</h1>
|
||
|
<div><p>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.</p>
|
||
|
<div class="section"><div class="note"><span class="notetitle">Note:</span> Read the <a href="codedisclaimer.htm">Code license and disclaimer information</a> for important
|
||
|
legal information.</div>
|
||
|
<pre>/******************************************************************/
|
||
|
/******************************************************************/
|
||
|
/* 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 */
|
||
|
|
||
|
</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div>
|
||
|
<div class="familylinks">
|
||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="apiexmp.htm" title="Contains example programs that use APIs and exit programs.">Examples: APIs</a></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|