]> git.sur5r.net Git - openldap/blob - libraries/libldif/fetch.c
Needs ldap_config.h to get LDAP_DIRSEP
[openldap] / libraries / libldif / fetch.c
1 /* line64.c - routines for dealing with the slapd line format */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/stdlib.h>
8
9 #include <ac/string.h>
10 #include <ac/socket.h>
11 #include <ac/time.h>
12
13 #ifdef HAVE_FETCH
14 #include <fetch.h>
15 #endif
16
17 #include "ldap_log.h"
18 #include "lber_pvt.h"
19 #include "ldap_pvt.h"
20 #include "ldap_config.h"
21 #include "ldif.h"
22
23 int
24 ldif_fetch_url(
25     LDAP_CONST char     *urlstr,
26     char        **valuep,
27     ber_len_t *vlenp
28 )
29 {
30         FILE *url;
31         char buffer[1024];
32         char *p = NULL;
33         size_t total;
34         size_t bytes;
35
36         *valuep = NULL;
37         *vlenp = 0;
38
39 #ifdef HAVE_FETCH
40         url = fetchGetURL( (char*) urlstr, "" );
41
42 #else
43         if( strncasecmp( "file://", urlstr, sizeof("file://")-1 ) == 0 ) {
44                 p = strchr( &urlstr[sizeof("file://")-1], '/' );
45                 if( p == NULL ) {
46                         return -1;
47                 }
48
49                 if( *p != *LDAP_DIRSEP ) {
50                         /* skip over false root */
51                         p++;
52                 }
53
54                 p = ber_strdup( p );
55                 ldap_pvt_hex_unescape( p );
56
57                 url = fopen( p, "r" );
58
59         } else {
60                 return -1;
61         }
62 #endif
63
64         if( url == NULL ) {
65                 return -1;
66         }
67
68         total = 0;
69
70         while( bytes = fread( buffer, 1, sizeof(buffer), url ) ) {
71                 char *newp = ber_memrealloc( p, total + bytes );
72                 if( newp == NULL ) {
73                         ber_memfree( p );
74                         fclose( url );
75                         return -1;
76                 }
77                 newp = p;
78                 SAFEMEMCPY( &p[total], buffer, bytes );
79                 total += bytes;
80         }
81
82         fclose( url );
83
84         *valuep = p;
85         *vlenp = total;
86
87         return 0;
88 }
89