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