1 /*
2 example code for the ldb database library
3
4 Copyright (C) Brad Hards (bradh@frogmouth.net) 2005-2006
5
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
9
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.
14
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.
19
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
24 */
25
26 /** \example ldifreader.c
27
28 The code below shows a simple LDB application.
29
30 It lists / dumps the entries in an LDIF file to standard output.
31
32 */
33
34 #include "includes.h"
35 #include "ldb/include/ldb.h"
36 #include "ldb/include/ldb_errors.h"
37
38 /*
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).
43 */
vprintf_fn(void * private_data,const char * fmt,...)44 static int vprintf_fn(void *private_data, const char *fmt, ...)
45 {
46 int retval;
47 va_list ap;
48
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;
56 }
57
main(int argc,const char ** argv)58 int main(int argc, const char **argv)
59 {
60 struct ldb_context *ldb;
61 FILE *fileStream;
62 struct ldb_ldif *ldifMsg;
63
64 if (argc != 2) {
65 printf("Usage %s filename.ldif\n", argv[0]);
66 exit(1);
67 }
68
69 /*
70 This is the always the first thing you want to do in an LDB
71 application - initialise up the context structure.
72
73 Note that you can use the context structure as a parent
74 for talloc allocations as well
75 */
76 ldb = ldb_init(NULL);
77
78 fileStream = fopen(argv[1], "r");
79 if (0 == fileStream) {
80 perror(argv[1]);
81 exit(1);
82 }
83
84 /*
85 We now work through the filestream to get each entry.
86 */
87 while ( (ldifMsg = ldb_ldif_read_file(ldb, fileStream)) ) {
88 /*
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).
93 */
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");
109 }
110
111 /*
112 We can now write out the results, using our custom
113 output routine as defined at the top of this file.
114 */
115 ldb_ldif_write(ldb, vprintf_fn, NULL, ldifMsg);
116
117 /*
118 Clean up the message
119 */
120 ldb_ldif_read_free(ldb, ldifMsg);
121 }
122
123 /*
124 Clean up the context
125 */
126 talloc_free(ldb);
127
128 return 0;
129 }
130