]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcat.c
fix previous commit
[openldap] / servers / slapd / slapcat.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 The OpenLDAP Foundation.
5  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
6  * Portions Copyright 2003 IBM Corporation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Kurt Zeilenga for inclusion
19  * in OpenLDAP Software.  Additional signficant contributors include
20  *    Jong Hyuk Choi
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/stdlib.h>
28 #include <ac/ctype.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
31
32 #include "slapcommon.h"
33 #include "ldif.h"
34
35 int
36 slapcat( int argc, char **argv )
37 {
38         ID id;
39         int rc = EXIT_SUCCESS;
40         Operation op = {0};
41         const char *progname = "slapcat";
42
43         slap_tool_init( progname, SLAPCAT, argc, argv );
44
45         if( !be->be_entry_open ||
46                 !be->be_entry_close ||
47                 !be->be_entry_first ||
48                 !be->be_entry_next ||
49                 !be->be_entry_get )
50         {
51                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
52                         progname );
53                 exit( EXIT_FAILURE );
54         }
55
56         if( be->be_entry_open( be, 0 ) != 0 ) {
57                 fprintf( stderr, "%s: could not open database.\n",
58                         progname );
59                 exit( EXIT_FAILURE );
60         }
61
62         for ( id = be->be_entry_first( be );
63                 id != NOID;
64                 id = be->be_entry_next( be ) )
65         {
66                 char *data;
67                 int len;
68                 Entry* e = be->be_entry_get( be, id );
69                 op.o_bd = be;
70
71                 if ( e == NULL ) {
72                         printf("# no data for entry id=%08lx\n\n", (long) id );
73                         rc = EXIT_FAILURE;
74                         if( continuemode ) continue;
75                         break;
76                 }
77
78                 if( sub_ndn.bv_len && !dnIsSuffix( &e->e_nname, &sub_ndn ) ) {
79                         be_entry_release_r( &op, e );
80                         continue;
81                 }
82
83                 if( filter != NULL ) {
84                         int rc = test_filter( NULL, e, filter );
85                         if( rc != LDAP_COMPARE_TRUE ) {
86                                 be_entry_release_r( &op, e );
87                                 continue;
88                         }
89                 }
90
91                 if( verbose ) {
92                         printf( "# id=%08lx\n", (long) id );
93                 }
94
95                 data = entry2str( e, &len );
96                 be_entry_release_r( &op, e );
97
98                 if ( data == NULL ) {
99                         printf("# bad data for entry id=%08lx\n\n", (long) id );
100                         rc = EXIT_FAILURE;
101                         if( continuemode ) continue;
102                         break;
103                 }
104
105                 fputs( data, ldiffp->fp );
106                 fputs( "\n", ldiffp->fp );
107         }
108
109         be->be_entry_close( be );
110
111         slap_tool_destroy();
112         return rc;
113 }