r25068: Older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for every opcode on the
[Samba.git] / source / lib / ldb / examples / ldifreader.c
blob3b8591e73fe8967468e0fcc12653879713bb29f8
1 /*
2 example code for the ldb database library
4 Copyright (C) Brad Hards (bradh@frogmouth.net) 2005-2006
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
8 ** under the LGPL
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301 USA
26 /** \example ldifreader.c
28 The code below shows a simple LDB application.
30 It lists / dumps the entries in an LDIF file to standard output.
34 #include "includes.h"
35 #include "ldb/include/ldb.h"
36 #include "ldb/include/ldb_errors.h"
39 ldb_ldif_write takes a function pointer to a custom output
40 function. This version is about as simple as the output function can
41 be. In a more complex example, you'd likely be doing something with
42 the private data function (e.g. holding a file handle).
44 static int vprintf_fn(void *private_data, const char *fmt, ...)
46 int retval;
47 va_list ap;
49 va_start(ap, fmt);
50 /* We just write to standard output */
51 retval = vprintf(fmt, ap);
52 va_end(ap);
53 /* Note that the function should return the number of
54 bytes written, or a negative error code */
55 return retval;
58 int main(int argc, const char **argv)
60 struct ldb_context *ldb;
61 FILE *fileStream;
62 struct ldb_ldif *ldifMsg;
64 if (argc != 2) {
65 printf("Usage %s filename.ldif\n", argv[0]);
66 exit(1);
70 This is the always the first thing you want to do in an LDB
71 application - initialise up the context structure.
73 Note that you can use the context structure as a parent
74 for talloc allocations as well
76 ldb = ldb_init(NULL);
78 fileStream = fopen(argv[1], "r");
79 if (0 == fileStream) {
80 perror(argv[1]);
81 exit(1);
85 We now work through the filestream to get each entry.
87 while ( (ldifMsg = ldb_ldif_read_file(ldb, fileStream)) ) {
89 Each message has a particular change type. For Add,
90 Modify and Delete, this will also appear in the
91 output listing (as changetype: add, changetype:
92 modify or changetype:delete, respectively).
94 switch (ldifMsg->changetype) {
95 case LDB_CHANGETYPE_NONE:
96 printf("ChangeType: None\n");
97 break;
98 case LDB_CHANGETYPE_ADD:
99 printf("ChangeType: Add\n");
100 break;
101 case LDB_CHANGETYPE_MODIFY:
102 printf("ChangeType: Modify\n");
103 break;
104 case LDB_CHANGETYPE_DELETE:
105 printf("ChangeType: Delete\n");
106 break;
107 default:
108 printf("ChangeType: Unknown\n");
112 We can now write out the results, using our custom
113 output routine as defined at the top of this file.
115 ldb_ldif_write(ldb, vprintf_fn, NULL, ldifMsg);
118 Clean up the message
120 ldb_ldif_read_free(ldb, ldifMsg);
124 Clean up the context
126 talloc_free(ldb);
128 return 0;