The following fragment declares an example routine, show_server_info, that prints the contents of a directory object as text.
The code uses a static array, AttributesToDisplay, that lists the attribute types (as OID strings) for the attributes whose values should be retrieved, in the order that they should be printed.
For each row in AttributesToDisplay, the example retrieves the values for the attribute type (if any) and prints them.
/*
** AttributesToDisplay is a read-only static array used by
** the show_server_info() function. It contains the Object
** Identifier (OID) strings for the server attributes to
** display, in the order that they are to be displayed.
*/
typedef struct
{
CS_CHAR type_string[CS_MAX_DS_STRING];
CS_CHAR english_name[CS_MAX_DS_STRING];
} AttrForDisplay;
#define N_ATTRIBUTES 7
CS_STATIC AttrForDisplay AttributesToDisplay[N_ATTRIBUTES + 1] =
{
{CS_OID_ATTRSERVNAME, "Server name"},
{CS_OID_ATTRSERVICE, "Service type"},
{CS_OID_ATTRVERSION, "Server entry version"},
{CS_OID_ATTRSTATUS, "Server status"},
{CS_OID_ATTRADDRESS, "Network addresses"},
{CS_OID_ATTRRETRYCOUNT, "Connection retry count"},
{CS_OID_ATTRLOOPDELAY, "Connection retry loop delay"},
{"", ""}
};
/* ** show_server_info()
** Selectively display the attributes of a server directory
** object.
**
** Parameters
** ds_object -- Pointer to the CS_DS_OBJECT that describes the
** server's directory entry.
** outfile -- Open FILE handle to write the output to.
**
** Dependencies
** Reads the contents of the AttributesToDisplay global array.
**
** Returns
** CS_SUCCEED or CS_FAIL.
*/
CS_RETCODE
show_server_info(ds_object, outfile)
CS_DS_OBJECT *ds_object;
FILE *outfile;
{
CS_RETCODE ret;
CS_CHAR scratch_str[512];
CS_INT outlen;
CS_INT cur_attr;
CS_ATTRIBUTE attr_metadata;
CS_ATTRVALUE *p_attrvals;
/*
** Distinguished name of the object.
*/
ret = ct_ds_objinfo(ds_object, CS_GET, CS_DS_DIST_NAME, CS_UNUSED,
(CS_VOID *)scratch_str, CS_SIZEOF(scratch_str),
&outlen);
if (ret != CS_SUCCEED)
{
ex_error("show_server_info: get distinguished name failed.");
return CS_FAIL;
}
fprintf(outfile, "Name in directory: %s\n", scratch_str);
for (cur_attr = 0; cur_attr < N_ATTRIBUTES; cur_attr++)
{
/*
** Look for the attribute. attr_get_by_type() fails if the object
** instance does not contain a value for the attribute. If this
** happens, we just go on to the next attribute.
*/
ret = attr_get_by_type(ds_object,
AttributesToDisplay[cur_attr].type_string,
&attr_metadata, &p_attrvals);
if (ret == CS_SUCCEED)
{
fprintf(outfile, "%s:\n",
AttributesToDisplay[cur_attr].english_name);
/*
** Display the attribute values.
*/
ret = attr_display_values(&attr_metadata, p_attrvals, outfile);
if (ret != CS_SUCCEED)
{
ex_error(
"show_server_info: display attribute values failed.");
free(p_attrvals);
return CS_FAIL;
} /* if */
free(p_attrvals);
} /* if */
} /* for */
return CS_SUCCEED;
} /* show_server_info() */