if ( flush_after_each_entry ) {  fflush( stdout ); }
dosearch( ld, base, scope, attrs, attrsonly, filtpatt, value )    LDAP	*ld;    char	*base;    int		scope;    char	**attrs;    int		attrsonly;    char	*filtpatt;    char	*value;{    char		**refs = NULL, filter[ BUFSIZ ], *filterp = NULL;    int			rc, first, matches;    LDAPMessage		*res, *e;    LDAPControl		*ldctrl;    LDAPControl		**ctrl_response_array = NULL;    LDAPVirtualList	vlv_data;    int			msgid = 0;    int			length = 0;    int			mallocd_filter = 0;    if ( strstr( filtpatt, "%s" ) == NULL ) {	/* no need to sprintf() */	filterp = filtpatt;    } else {	length = strlen( filtpatt ) + strlen ( value ) +1;	if ( length > BUFSIZ ) {	    if ((filterp = (char *)		    malloc ( length )) == NULL) {		    perror( "filter and/or pattern too long?" );		    exit (LDAP_PARAM_ERROR);	    }	    mallocd_filter = 1;	} else {	    filterp = filter;	}     #ifdef HAVE_SNPRINTF	if ( snprintf( filterp, length, filtpatt, value ) < 0 ) {	    perror( "snprintf filter (filter and/or pattern too long?)" );	    exit( LDAP_PARAM_ERROR );	}#else	sprintf( filterp, filtpatt, value );#endif    }    if ( *filterp == '\0' ) {	/* treat empty filter is a shortcut for oc=* */	if (mallocd_filter) {	    free(filterp);	    mallocd_filter = 0;	}	filterp = "(objectclass=*)";    }    if ( ldaptool_verbose ) {	/*	 * Display the filter that will be used.  Add surrounding parens.	 * if they are missing.	 */	if ( '(' == *filterp ) {	    printf( "filter is: %s\n", filterp );	} else {	    printf( "filter is: (%s)\n", filterp );	}    }    if ( ldaptool_not ) {	if (mallocd_filter) free(filterp);	return( LDAP_SUCCESS );    }    if (( ldctrl = ldaptool_create_manage_dsait_control()) != NULL ) {	ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);    }    if ((ldctrl = ldaptool_create_proxyauth_control(ld)) !=NULL) {	ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);    }        if (use_psearch) {	if ( ldap_create_persistentsearch_control( ld, chgtype,                changesonly, return_echg_ctls, 		1, &ldctrl ) != LDAP_SUCCESS )	{		ldap_perror( ld, "ldap_create_persistentsearch_control" );		return (1);	}	ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);    }    if (server_sort) {	/* First make a sort key list from the attribute list we have */	LDAPsortkey **keylist = NULL;	int i = 0;	char *sortattrs = NULL;	char *s = NULL;	int string_length = 0;	/* Count the sort strings */	for (i = 0; i < sortsize - 1 ; i++) {	    string_length += strlen(sortattr[i]) + 1;	}	sortattrs = (char *) malloc(string_length + 1);	if (NULL == sortattrs) {	    fprintf( stderr, "Out of memory\n" );	    exit( LDAP_NO_MEMORY );	}		s = sortattrs;	for (i = 0; i < sortsize - 1 ; i++) {	    memcpy(s, sortattr[i], strlen(sortattr[i]));	    s += strlen(sortattr[i]);	    *s++ = ' ';	}	sortattrs[string_length] = '\0';	ldap_create_sort_keylist(&keylist,sortattrs);	free(sortattrs);	sortattrs = NULL;	/* Then make a control for the sort attributes we have */	rc = ldap_create_sort_control(ld,keylist,0,&ldctrl);	ldap_free_sort_keylist(keylist);	if ( rc != LDAP_SUCCESS ) {	    if (mallocd_filter) free(filterp);	    return( ldaptool_print_lderror( ld, "ldap_create_sort_control",		LDAPTOOL_CHECK4SSL_IF_APPROP ));	}	ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);    }    /* remember server side sorting must be available for vlv!!!! */    if (use_vlv)    {	vlv_data.ldvlist_before_count = vlv_before;	vlv_data.ldvlist_after_count = vlv_after;	if ( ldaptool_verbose ) {	    printf( "vlv data %lu, %lu, ", 		    vlv_data.ldvlist_before_count,		    vlv_data.ldvlist_after_count 		);	}	if (vlv_value)	{	    vlv_data.ldvlist_attrvalue = vlv_value;	    vlv_data.ldvlist_size = 0;	    vlv_data.ldvlist_index = 0;	    if ( ldaptool_verbose ) {		printf( "%s, 0, 0\n", vlv_data.ldvlist_attrvalue);	    }	}	else	{	    vlv_data.ldvlist_attrvalue = NULL;	    vlv_data.ldvlist_size = vlv_count;	    vlv_data.ldvlist_index = vlv_index;	    if ( ldaptool_verbose ) {		printf( "(null), %lu, %lu\n", vlv_data.ldvlist_size, vlv_data.ldvlist_index );	    }	}		if ( rc != LDAP_SUCCESS ) {	    if (mallocd_filter) free(filterp);	    return( ldaptool_print_lderror( ld, "ldap_create_sort_control",		LDAPTOOL_CHECK4SSL_IF_APPROP ));	}	if (LDAP_SUCCESS != (rc = ldap_create_virtuallist_control(ld,		&vlv_data, &ldctrl)))	{	    if (mallocd_filter) free(filterp);	    return( ldaptool_print_lderror( ld,		"ldap_create_virtuallist_control",		LDAPTOOL_CHECK4SSL_IF_APPROP ));	}	ldaptool_add_control_to_array(ldctrl, ldaptool_request_ctrls);	    }    if ( ldap_search_ext( ld, base, scope, filterp, attrs, attrsonly, 	    ldaptool_request_ctrls, NULL, NULL, -1, &msgid )	    != LDAP_SUCCESS ) {	if (mallocd_filter) free(filterp);	return( ldaptool_print_lderror( ld, "ldap_search",		LDAPTOOL_CHECK4SSL_IF_APPROP ));    }    matches = 0;    first = 1;    if ( sortattr && !server_sort ) {        rc = ldap_result( ld, LDAP_RES_ANY, 1, NULL, &res );    } else {        while ( (rc = ldap_result( ld, LDAP_RES_ANY, 0, NULL, &res )) != 		LDAP_RES_SEARCH_RESULT && rc != -1 ) {	    if ( rc != LDAP_RES_SEARCH_ENTRY ) {		if ( rc == LDAP_RES_SEARCH_REFERENCE ) {		    parse_and_display_reference( ld, res );		} else if ( rc == LDAP_RES_EXTENDED			&& ldap_msgid( res ) == LDAP_RES_UNSOLICITED ) {		    (void)ldaptool_print_extended_response( ld, res,			    "Unsolicited response" );		} else {		    fprintf( stderr, "%s: ignoring LDAP response message"			    " type 0x%x (%s)\n",			    ldaptool_progname, rc, msgtype2str( rc ));		}		ldap_msgfree( res );		continue;	    }	    matches++;	    e = ldap_first_entry( ld, res );	    if ( !first ) {	        putchar( '\n' );	    } else {	        first = 0;	    }	    print_entry( ld, e, attrsonly );	    ldap_msgfree( res );        }    }    if ( rc == -1 ) {	if (mallocd_filter) free(filterp);	return( ldaptool_print_lderror( ld, "ldap_result",		LDAPTOOL_CHECK4SSL_IF_APPROP ));    }    if ( ldap_parse_result( ld, res, &rc, NULL, NULL, &refs,	    &ctrl_response_array, 0 ) != LDAP_SUCCESS ) {	(void)ldaptool_print_lderror( ld, "ldap_parse_result",		LDAPTOOL_CHECK4SSL_IF_APPROP );    } else if ( rc != LDAP_SUCCESS ) {                                          	(void)ldaptool_print_lderror( ld, "ldap_search",		LDAPTOOL_CHECK4SSL_IF_APPROP );    }     /* Parse the returned sort control */    if (server_sort) {	unsigned long result = 0;	char *attribute;		if ( LDAP_SUCCESS != ldap_parse_sort_control(ld,ctrl_response_array,&result,&attribute) ) {	    (void)ldaptool_print_lderror(ld, "ldap_parse_sort_control",		    LDAPTOOL_CHECK4SSL_IF_APPROP );	    ldap_controls_free(ctrl_response_array);	    ldap_msgfree(res);	    if (mallocd_filter) free(filterp);	    return ( ldap_get_lderrno( ld, NULL, NULL ) );	}		if (0 == result) {	    if ( ldaptool_verbose ) {		printf( "Server indicated results sorted OK\n");	    }	} else {	    if (NULL != attribute) {		printf("Server reported sorting error %ld: %s, attribute in error\"%s\"\n",result,sortresult2string(result),attribute);	    } else {		printf("Server reported sorting error %ld: %s\n",result,sortresult2string(result));	    }	}    }    if (use_vlv)    {	unsigned long vpos, vcount;	int vresult;	if ( LDAP_SUCCESS != ldap_parse_virtuallist_control(ld,ctrl_response_array,&vpos, &vcount,&vresult) ) {	    (void)ldaptool_print_lderror( ld, "ldap_parse_virtuallist_control",		    LDAPTOOL_CHECK4SSL_IF_APPROP );	    ldap_controls_free(ctrl_response_array);	    ldap_msgfree(res);	    if (mallocd_filter) free(filterp);	    return ( ldap_get_lderrno( ld, NULL, NULL ) );	}		if (0 == vresult) {	    if ( ldaptool_verbose ) {		printf( "Server indicated virtual list positioning OK\n");	    }	    printf("index %lu content count %lu\n", vpos, vcount);	    	} else {	    printf("Server reported sorting error %d: %s\n",vresult,sortresult2string(vresult));	}    }        ldap_controls_free(ctrl_response_array);           if ( sortattr != NULL && !server_sort) {		(void) ldap_multisort_entries( ld, &res,				       ( *sortattr == NULL ) ? NULL : sortattr, 				       (LDAP_CMP_CALLBACK *)strcasecmp );	matches = 0;	first = 1;	for ( e = ldap_first_entry( ld, res ); e != NULLMSG;	      e = ldap_next_entry( ld, e ) ) {	    matches++;	    if ( !first ) {		putchar( '\n' );	    } else {		first = 0;	    }	    print_entry( ld, e, attrsonly );	}    }        if ( ldaptool_verbose ) {	printf( "%d matches\n", matches );    }    if ( refs != NULL ) {	ldaptool_print_referrals( refs );	ldap_value_free( refs );    }    if (mallocd_filter) free(filterp);    ldap_msgfree( res );    return( rc );}