Back to index

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

Go to the source code of this file.

Defines

#define NMODS   5

Functions

static void do_other_work ()
static void free_mods (LDAPMod **mods)
int main (int argc, char **argv)

Variables

unsigned long global_counter = 0

Define Documentation

#define NMODS   5

Definition at line 57 of file add.c.


Function Documentation

static void do_other_work ( ) [static]

Definition at line 187 of file add.c.

Here is the caller graph for this function:

static void free_mods ( LDAPMod **  mods) [static]

Definition at line 171 of file add.c.

{
    int i;

    for ( i = 0; i < NMODS; i++ ) {
       free( mods[ i ] );
    }
    free( mods );
}

Here is the caller graph for this function:

int main ( int  argc,
char **  argv 
)

Definition at line 60 of file add.c.

{
    LDAP             *ld;
    LDAPMessage             *result;
    char             *dn;
    int                     i;
    int                     rc;
    int                     msgid;
    int                     finished;
    struct timeval   zerotime;
    LDAPMod          **mods;

    char *objectclass_values[] = { "top", "person", "organizationalPerson",
                               "inetOrgPerson", NULL };
    char *cn_values[] = { "William B Jensen", "William Jensen", "Bill Jensen",
                       NULL };
    char *sn_values[] = { "Jensen", NULL };
    char *givenname_values[] = { "William", "Bill", NULL };
    char *telephonenumber_values[] = { "+1 415 555 1212", NULL };

    zerotime.tv_sec = zerotime.tv_usec = 0L;

    /* Specify the DN we're adding */
    dn = "cn=William B Jensen, " PEOPLE_BASE;    /* see examples.h */

    /* get a handle to an LDAP connection */
    if ( (ld = ldap_init( MY_HOST, MY_PORT )) == NULL ) {
       perror( "ldap_init" );
       return( 1 );
    }

    /* authenticate to the directory as the Directory Manager */
    if ( ldap_simple_bind_s( ld, MGR_DN, MGR_PW ) != LDAP_SUCCESS ) {
       ldap_perror( ld, "ldap_simple_bind_s" );
       return( 1 );
    }

    /* Construct the array of values to add */
    mods = ( LDAPMod ** ) malloc(( NMODS + 1 ) * sizeof( LDAPMod * ));
    if ( mods == NULL ) {
       fprintf( stderr, "Cannot allocate memory for mods array\n" );
    }
    for ( i = 0; i < NMODS; i++ ) {
       if (( mods[ i ] = ( LDAPMod * ) malloc( sizeof( LDAPMod ))) == NULL ) {
           fprintf( stderr, "Cannot allocate memory for mods element\n" );
           exit( 1 );
       }
    }
    mods[ 0 ]->mod_op = 0;
    mods[ 0 ]->mod_type = "objectclass";
    mods[ 0 ]->mod_values = objectclass_values;
    mods[ 1 ]->mod_op = 0;
    mods[ 1 ]->mod_type = "cn";
    mods[ 1 ]->mod_values = cn_values;
    mods[ 2 ]->mod_op = 0;
    mods[ 2 ]->mod_type = "sn";
    mods[ 2 ]->mod_values = sn_values;
    mods[ 3 ]->mod_op = 0;
    mods[ 3 ]->mod_type = "givenname";
    mods[ 3 ]->mod_values = givenname_values;
    mods[ 4 ]->mod_op = 0;
    mods[ 4 ]->mod_type = "telephonenumber";
    mods[ 4 ]->mod_values = telephonenumber_values;
    mods[ 5 ] = NULL;


    /* Initiate the add operation */
    if (( msgid = ldap_add( ld, dn, mods )) < 0 ) {
       ldap_perror( ld, "ldap_add" );
       free_mods( mods );
       return( 1 );
    }

    /* Poll for the result */
    finished = 0;
    while ( !finished ) {
       rc = ldap_result( ld, msgid, LDAP_MSG_ONE, &zerotime, &result );
       switch ( rc ) {
       case -1:
           /* some error occurred */
           ldap_perror( ld, "ldap_result" );
           free_mods( mods );
           return( 1 );
       case 0:
           /* Timeout was exceeded.  No entries are ready for retrieval */
           break;
       default:
           /* Should be finished here */
           finished = 1;
           if (( rc = ldap_result2error( ld, result, 0 )) == LDAP_SUCCESS ) {
              printf( "Entry added successfully.  I counted to %ld "
                     "while waiting.\n", global_counter );
           } else {
              printf( "Error while adding entry: %s\n",
                     ldap_err2string( rc ));
           }
           ldap_msgfree( result );
       }
       do_other_work();
    }
    ldap_unbind( ld );
    free_mods( mods );
    return 0;
}

Here is the call graph for this function:


Variable Documentation

unsigned long global_counter = 0

Definition at line 54 of file add.c.