Example code to start a directory search

The following fragment declares an application routine, get_servers, that searches for server directory class objects:

 /*
 ** get_servers() -- Query the directory for servers and
 **    get a list of directory objects that contain details
 **    for each.
 **
 ** Parameters
 **   conn -- Pointer to allocated connection structure.
 **   pserver_list -- Address of a pointer to a SERVER_INFO_LIST.
 **       Upon successful return, the list will be initialized
 **       and contain an object for each server found in the 
 **       search.
 **
 **       NOTE: The caller must clean up the list with sil_drop_list()
 **       when done with it.
 **
 ** Returns
 **   CS_SUCCEED or CS_FAIL.
 *
 CS_RETCODE get_servers (conn, pserver_list)
 CS_CONNECTION *conn;
 SERVER_INFO_LIST **pserver_list;
 {
   CS_RETCODE          ret;
   CS_INT              reqid;
   CS_VOID             *oldcallback;
   CS_OID              oid;
   CS_DS_LOOKUP_INFO   lookup_info;
   /*
   ** Steps for synchronous-mode directory searches:
   **
   ** 1. If necessary, initialize application specific data structures
   **    (Our application collects directory objects in *pserver_list).
   ** 2. Save the old directory callback and install our own.
   ** 3. Set the base node in the directory to search beneath 
   **    (CS_DS_DITBASE property).
   ** 4. Call ct_ds_lookup to begin the search, passing any application 
   **    specific data structures as the userdata argument.
   ** 5. Client-Library invokes our callback once for each found object
   **    (or once to report that no objects were found). The callback 
   **    (directory_cb) receives pointers to found servers and appends 
   **    each to the list of servers.
   ** 6. Check the return status of ct_ds_lookup.
   ** 7. Restore callbacks and properties that we changed.
   */
   /*
   ** Step 1. Initialize the data structure (*pserver_list).
   */
   ret = sil_init_list(pserver_list);
   if (ret != CS_SUCCEED || (*pserver_list) == NULL)
   {
     ex_error("get_servers: Could not initialize list.");
     return CS_FAIL;
   }
   /*
   ** Step 2. Save the old directory callback and install our own callback,
   **   directory_cb(), to receive the found objects.
   */
   ret = ct_callback(NULL, conn, CS_GET, 
                     CS_DS_LOOKUP_CB, &oldcallback);
   if (ret == CS_SUCCEED)
   {
     ret = ct_callback(NULL, conn, CS_SET, 
                       CS_DS_LOOKUP_CB, (CS_VOID *)directory_cb);
   }
   if (ret != CS_SUCCEED)
   {
     ex_error("get_servers: Could not install directory callback.");
     return CS_FAIL;
   }
   /*
   ** Step 3. Set the base node in the directory to search beneath 
   **    (the CS_DS_DITBASE connection property).
   */
 
   ret = provider_setup(conn);
   if (ret != CS_SUCCEED)
   {
     ex_error("get_servers: Provider-specific setup failed.");
     return CS_FAIL;
   }
   /*
   ** Step 4. Call ct_ds_lookup to begin the search, passing the server list
   **    pointer as userdata.
   ** Step 5. Client-Library invokes our callback once for each found object
   **    (or once to report that no objects were found). Our callback,
   **    directory_cb, will receive a pointer to each found server object
   **    and appends it to the list.
   ** Step 6. Check the return status of ct_ds_lookup.
   */
   /*

   ** Set the CS_DS_LOOKUP_INFO structure fields. 
   */
   lookup_info.path = NULL;
   lookup_info.pathlen = 0;
   lookup_info.attrfilter = NULL;
   lookup_info.attrselect = NULL;
 
   strcpy(oid.oid_buffer, CS_OID_OBJSERVER);
   oid.oid_length = STRLEN(oid.oid_buffer);
   lookup_info.objclass = &oid;
   /*
   ** Begin the search.
   */
   ret = ct_ds_lookup(conn, CS_SET, &reqid, 
                      &lookup_info, (CS_VOID *)pserver_list);
   if (ret != CS_SUCCEED)
   {
     ex_error("get_servers: Could not run search.");
     return CS_FAIL;
   }
   /*
   ** Step 7.  Restore callbacks and properties that we changed.
   */
   ret = ct_callback(NULL, conn, CS_SET, 
                     CS_DS_LOOKUP_CB, oldcallback);
   if (ret != CS_SUCCEED)
   {
     ex_error("get_servers: Could not restore directory callback.");
     return CS_FAIL;
   }
 return CS_SUCCEED;
 
 } /* get_servers() *