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