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 3 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, see <http://www.gnu.org/licenses/>. 22*/ 23 24/** \example ldifreader.c 25 26The code below shows a simple LDB application. 27 28It lists / dumps the entries in an LDIF file to standard output. 29 30*/ 31 32#include "includes.h" 33#include "ldb/include/ldb.h" 34#include "ldb/include/ldb_errors.h" 35 36/* 37 ldb_ldif_write takes a function pointer to a custom output 38 function. This version is about as simple as the output function can 39 be. In a more complex example, you'd likely be doing something with 40 the private data function (e.g. holding a file handle). 41*/ 42static int vprintf_fn(void *private_data, const char *fmt, ...) 43{ 44 int retval; 45 va_list ap; 46 47 va_start(ap, fmt); 48 /* We just write to standard output */ 49 retval = vprintf(fmt, ap); 50 va_end(ap); 51 /* Note that the function should return the number of 52 bytes written, or a negative error code */ 53 return retval; 54} 55 56int main(int argc, const char **argv) 57{ 58 struct ldb_context *ldb; 59 FILE *fileStream; 60 struct ldb_ldif *ldifMsg; 61 62 if (argc != 2) { 63 printf("Usage %s filename.ldif\n", argv[0]); 64 exit(1); 65 } 66 67 /* 68 This is the always the first thing you want to do in an LDB 69 application - initialise up the context structure. 70 71 Note that you can use the context structure as a parent 72 for talloc allocations as well 73 */ 74 ldb = ldb_init(NULL); 75 76 fileStream = fopen(argv[1], "r"); 77 if (0 == fileStream) { 78 perror(argv[1]); 79 exit(1); 80 } 81 82 /* 83 We now work through the filestream to get each entry. 84 */ 85 while ( (ldifMsg = ldb_ldif_read_file(ldb, fileStream)) ) { 86 /* 87 Each message has a particular change type. For Add, 88 Modify and Delete, this will also appear in the 89 output listing (as changetype: add, changetype: 90 modify or changetype:delete, respectively). 91 */ 92 switch (ldifMsg->changetype) { 93 case LDB_CHANGETYPE_NONE: 94 printf("ChangeType: None\n"); 95 break; 96 case LDB_CHANGETYPE_ADD: 97 printf("ChangeType: Add\n"); 98 break; 99 case LDB_CHANGETYPE_MODIFY: 100 printf("ChangeType: Modify\n"); 101 break; 102 case LDB_CHANGETYPE_DELETE: 103 printf("ChangeType: Delete\n"); 104 break; 105 default: 106 printf("ChangeType: Unknown\n"); 107 } 108 109 /* 110 We can now write out the results, using our custom 111 output routine as defined at the top of this file. 112 */ 113 ldb_ldif_write(ldb, vprintf_fn, NULL, ldifMsg); 114 115 /* 116 Clean up the message 117 */ 118 ldb_ldif_read_free(ldb, ldifMsg); 119 } 120 121 /* 122 Clean up the context 123 */ 124 talloc_free(ldb); 125 126 return 0; 127} 128