Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions
crtfilt.c File Reference
#include "examples.h"

Go to the source code of this file.

Classes

struct  filt_words

Defines

#define FILT_BUFSIZ   4096

Functions

static int get_string_input (const char *prompt, const char *defaultval, char *resultbuf)
static struct filt_wordsval2words (const char *value, const char *delims)
static void freewords (struct filt_words *words)
int main (int argc, char **argv)

Class Documentation

struct filt_words

Definition at line 46 of file crtfilt.c.

Class Members
char * f_internal
char ** f_words

Define Documentation

#define FILT_BUFSIZ   4096

Definition at line 44 of file crtfilt.c.


Function Documentation

static void freewords ( struct filt_words words) [static]

Definition at line 174 of file crtfilt.c.

{
    if ( words != NULL ) {
       if ( words->f_words != NULL ) {
           free( words->f_words );
       }
       if ( words->f_internal != NULL ) {
           free( words->f_internal );
       }
       free( words );
    }
}

Here is the caller graph for this function:

static int get_string_input ( const char *  prompt,
const char *  defaultval,
char *  resultbuf 
) [static]

Definition at line 109 of file crtfilt.c.

{
    char inbuf[ FILT_BUFSIZ ];

    inbuf[0] = '\0';
    printf( prompt, defaultval );
    if ( fgets( inbuf, sizeof( inbuf ), stdin ) == NULL ) {
       return( -1 );
    }
    inbuf[ strlen( inbuf ) - 1 ] = '\0';  /* strip trailing newline */
    if ( inbuf[ 0 ] == '\0' ) {                  /* use default value */
       if ( defaultval != resultbuf ) {
           strcpy( resultbuf, defaultval );
       }
    } else {                              /* use newly entered value */
       strcpy( resultbuf, inbuf );
    }

    return( 0 );
}

Here is the caller graph for this function:

int main ( int  argc,
char **  argv 
)

Definition at line 58 of file crtfilt.c.

{
    int                     rc;
    char             patbuf[ FILT_BUFSIZ ];
    char             attrbuf[ FILT_BUFSIZ ], valuebuf[ FILT_BUFSIZ ];
    char             filtbuf[ FILT_BUFSIZ ];
    struct filt_words       *fwords;

    patbuf[0] = attrbuf[0] = valuebuf[0] = '\0';

    while ( 1 ) {
       if ( get_string_input( "Enter a filter pattern: [%s] ", patbuf,
              patbuf ) != 0 ) {
           break;
       }

       if ( get_string_input( "Enter an attribute type: [%s] ", attrbuf,
              attrbuf ) != 0 ) {
           break;
       }

       if ( get_string_input( "Enter a value: [%s] ", valuebuf,
              valuebuf ) != 0 ) {
           break;
       }

       fwords = val2words( valuebuf, " " );
       rc = ldap_create_filter( filtbuf, sizeof( filtbuf ), patbuf,
              NULL, NULL, attrbuf, valuebuf, fwords->f_words );
       freewords( fwords );
       
       if ( rc != LDAP_SUCCESS ) {
           fprintf( stderr, "ldap_create_filter: failed (%d - %s)\n", rc,
                  ldap_err2string( rc ));
       } else {
           printf( "Resulting filter: %s\n", filtbuf );
       }
       putchar( '\n' );
    }

    return( 0 );
}

Here is the call graph for this function:

static struct filt_words * val2words ( const char *  value,
const char *  delims 
) [static, read]

Definition at line 132 of file crtfilt.c.

{
    struct filt_words       *fw;
    char             *word;
    int                     i;

    if (( fw = calloc( 1, sizeof( struct filt_words ))) == NULL ||
           ( fw->f_internal = strdup( value )) == NULL ) {
       perror( "calloc OR strdup" );
       exit( 1 );
    }

    word = strtok( fw->f_internal, delims );
    i = 0;

    while ( word != NULL ) {
       if ( fw->f_words == NULL ) {
           fw->f_words = (char **)malloc( (i + 2 ) * sizeof( char * ));
       } else {
           fw->f_words = (char **)realloc( fw->f_words,
                  (i + 2 ) * sizeof( char * ));
       }

       if ( fw->f_words == NULL ) {
           perror( "malloc OR realloc" );
           exit( 1 );
       }

       fw->f_words[ i ] = word;
       fw->f_words[ ++i ] = NULL;
       word = strtok( NULL, delims );
    }

    if ( i > 0 ) {
       fw->f_words[ i ] = NULL;
    }

    return( fw );
}

Here is the call graph for this function:

Here is the caller graph for this function: