Back to index

lightning-sunbird  0.9+nobinonly
del.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * Delete an entry from the directory.
00040  *
00041  * Instead of calling the synchronous ldap_delete_s() routine, we call
00042  * the asynchronous routine ldap_delete() and poll for results using
00043  * ldap_result().
00044  *
00045  * Since it is an error to attempt to delete an entry which does not
00046  * exist, you cannot run this example until you have added the entry
00047  * with the aadd.c example program.
00048  *
00049  */
00050 
00051 #include "examples.h"
00052 
00053 static void do_other_work();
00054 unsigned long global_counter = 0;
00055 
00056 int
00057 main( int argc, char **argv )
00058 {
00059     LDAP             *ld;
00060     LDAPMessage             *result;
00061     char             *dn;
00062     int                     rc;
00063     int                     msgid;
00064     int                     finished;
00065     struct timeval   zerotime;
00066 
00067     zerotime.tv_sec = zerotime.tv_usec = 0L;
00068 
00069     /* Specify the DN we're deleting */
00070     dn = "cn=William B Jensen, " PEOPLE_BASE;    /* see examples.h */
00071 
00072     /* get a handle to an LDAP connection */
00073     if ( (ld = ldap_init( MY_HOST, MY_PORT )) == NULL ) {
00074        perror( "ldap_init" );
00075        return( 1 );
00076     }
00077     /* authenticate to the directory as the Directory Manager */
00078     if ( ldap_simple_bind_s( ld, MGR_DN, MGR_PW ) != LDAP_SUCCESS ) {
00079        ldap_perror( ld, "ldap_simple_bind_s" );
00080        return( 1 );
00081     }
00082     /* Initiate the delete operation */
00083     if (( msgid = ldap_delete( ld, dn )) < 0 ) {
00084        ldap_perror( ld, "ldap_delete" );
00085        return( 1 );
00086     }
00087 
00088     /* Poll for the result */
00089     finished = 0;
00090     while ( !finished ) {
00091        rc = ldap_result( ld, msgid, LDAP_MSG_ONE, &zerotime, &result );
00092        switch ( rc ) {
00093        case -1:
00094            /* some error occurred */
00095            ldap_perror( ld, "ldap_result" );
00096            return( 1 );
00097        case 0:
00098            /* Timeout was exceeded.  No entries are ready for retrieval */
00099            break;
00100        default:
00101            /* Should be finished here */
00102            finished = 1;
00103            if (( rc = ldap_result2error( ld, result, 0 )) == LDAP_SUCCESS ) {
00104               printf( "Entry deleted successfully.  I counted to %ld "
00105                      "while waiting.\n", global_counter );
00106            } else {
00107               printf( "Error while deleting entry: %s\n",
00108                      ldap_err2string( rc ));
00109            }
00110            ldap_msgfree( result );
00111        }
00112        do_other_work();
00113     }
00114     ldap_unbind( ld );
00115     return 0;
00116 }
00117 
00118 
00119 /*
00120  * Perform other work while polling for results.  This doesn't do anything
00121  * useful, but it could.
00122  */
00123 static void
00124 do_other_work()
00125 {
00126     global_counter++;
00127 }