]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif.c
Y2k copyright update
[openldap] / servers / slapd / tools / ldif.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 #include "portable.h"
7
8 #include <stdio.h>
9
10 #include <ac/stdlib.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14 #include <ac/unistd.h>
15
16 #ifdef HAVE_IO_H
17 #include <io.h>
18 #endif
19
20 #include <ldap.h>
21
22 #include "ldif.h"
23
24 static void
25 usage( char *name )
26 {
27         fprintf( stderr, "usage: %s [-b] <attrtype>\n", name );
28         exit( EXIT_FAILURE );
29 }
30
31 int
32 main( int argc, char **argv )
33 {
34         char    buf[BUFSIZ];
35         char    *type, *out;
36         int     len, binary = 0;
37
38         if (argc < 2 || argc > 3 ) {
39                 usage( argv[0] );
40         }
41         if ( argc == 3 ) {
42                 if ( strcmp( argv[1], "-b" ) != 0 ) {
43                         usage( argv[0] );
44                 }
45                 binary = 1;
46                 type = argv[2];
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 }