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