]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif.c
Elimination of un-used code in bdb2i_cache_open and friends.
[openldap] / servers / slapd / tools / ldif.c
1 #include "portable.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include <ac/string.h>
7 #include <ac/socket.h>
8 #include <ac/unistd.h>                  /* Get read() */
9
10 #include "lber.h"
11 #include "ldap.h"
12 #include "ldif.h"
13
14 static void
15 usage( char *name )
16 {
17         fprintf( stderr, "usage: %s [-b] <attrtype>\n", name );
18         exit( 1 );
19 }
20
21 int
22 main( int argc, char **argv )
23 {
24         char    buf[BUFSIZ];
25         char    *type, *out;
26         int     len, binary = 0;
27
28         if (argc < 2 || argc > 3 ) {
29                 usage( argv[0] );
30         }
31         if ( argc == 3 ) {
32                 if ( strcmp( argv[1], "-b" ) != 0 ) {
33                         usage( argv[0] );
34                 } else {
35                         binary = 1;
36                         type = argv[2];
37                 }
38         } else {
39                 if ( strcmp( argv[1], "-b" ) == 0 ) {
40                         usage( argv[0] );
41                 }
42                 type = argv[1];
43         }
44
45         /* if the -b flag was used, read single binary value from stdin */
46         if ( binary ) {
47                 char    *val;
48                 int     nread, max, cur;
49
50                 if (( val = (char *) malloc( BUFSIZ )) == NULL ) {
51                         perror( "malloc" );
52                         exit( 1 );
53                 }
54                 max = BUFSIZ;
55                 cur = 0;
56                 while ( (nread = read( 0, buf, BUFSIZ )) != 0 ) {
57                         if ( nread + cur > max ) {
58                                 max += BUFSIZ;
59                                 if (( val = (char *) realloc( val, max )) ==
60                                     NULL ) {
61                                         perror( "realloc" );
62                                         exit( 1 );
63                                 }
64                         }
65                         memcpy( val + cur, buf, nread );
66                         cur += nread;
67                 }
68
69                 if (( out = ldif_type_and_value( type, val, cur )) == NULL ) {
70                         perror( "ldif_type_and_value" );
71                         exit( 1 );
72                 }
73
74                 fputs( out, stdout );
75                 free( out );
76                 free( val );
77                 exit( 0 );
78         }
79
80         /* not binary:  one value per line... */
81         while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
82                 if( buf[len=strlen(buf)] == '\n') buf[len] = '\0';
83
84                 if (( out = ldif_type_and_value( type, buf, strlen( buf ) ))
85                     == NULL ) {
86                         perror( "ldif_type_and_value" );
87                         exit( 1 );
88                 }
89                 fputs( out, stdout );
90                 free( out );
91         }
92
93         exit( 0 );
94 }