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