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