]> git.sur5r.net Git - openldap/blob - libraries/libldif/fetch.c
3f1ccf18a2430634b3b5a715e368ad52de5c2eb2
[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 "ldif.h"
21
22 int
23 ldif_fetch_url(
24     LDAP_CONST char     *urlstr,
25     char        **valuep,
26     ber_len_t *vlenp
27 )
28 {
29         FILE *url;
30         char buffer[1024];
31         char *p = NULL;
32         size_t total;
33         size_t bytes;
34
35         *valuep = NULL;
36         *vlenp = 0;
37
38 #ifdef HAVE_FETCH
39         url = fetchGetURL( (char*) urlstr, "" );
40
41 #else
42         if( strncasecmp( "file://", urlstr, sizeof("file://")-1 ) == 0 ) {
43                 p = strchr( &urlstr[sizeof("file://")-1], '/' );
44                 if( p == NULL ) {
45                         return -1;
46                 }
47
48                 if( *p != *LDAP_DIRSEP ) {
49                         /* skip over false root */
50                         p++;
51                 }
52
53                 p = ber_strdup( p );
54                 ldap_pvt_hex_unescape( p );
55
56                 url = fopen( p, "r" );
57
58         } else {
59                 return -1;
60         }
61 #endif
62
63         if( url == NULL ) {
64                 return -1;
65         }
66
67         total = 0;
68
69         while( bytes = fread( buffer, 1, sizeof(buffer), url ) ) {
70                 char *newp = ber_memrealloc( p, total + bytes );
71                 if( newp == NULL ) {
72                         ber_memfree( p );
73                         fclose( url );
74                         return -1;
75                 }
76                 newp = p;
77                 SAFEMEMCPY( &p[total], buffer, bytes );
78                 total += bytes;
79         }
80
81         fclose( url );
82
83         *valuep = p;
84         *vlenp = total;
85
86         return 0;
87 }
88