]> git.sur5r.net Git - openldap/blob - libraries/libldif/fetch.c
cb4d81beed8e93924a413b59713d6405f22bd660
[openldap] / libraries / libldif / fetch.c
1 /* line64.c - routines for dealing with the slapd line format */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/stdlib.h>
13
14 #include <ac/string.h>
15 #include <ac/socket.h>
16 #include <ac/time.h>
17
18 #ifdef HAVE_FETCH
19 #include <fetch.h>
20 #endif
21
22 #include "ldap_log.h"
23 #include "lber_pvt.h"
24 #include "ldap_pvt.h"
25 #include "ldap_config.h"
26 #include "ldif.h"
27
28 int
29 ldif_fetch_url(
30     LDAP_CONST char     *urlstr,
31     char        **valuep,
32     ber_len_t *vlenp
33 )
34 {
35         FILE *url;
36         char buffer[1024];
37         char *p = NULL;
38         size_t total;
39         size_t bytes;
40
41         *valuep = NULL;
42         *vlenp = 0;
43
44 #ifdef HAVE_FETCH
45         url = fetchGetURL( (char*) urlstr, "" );
46
47 #else
48         if( strncasecmp( "file://", urlstr, sizeof("file://")-1 ) == 0 ) {
49                 p = strchr( &urlstr[sizeof("file://")-1], '/' );
50                 if( p == NULL ) {
51                         return -1;
52                 }
53
54                 /* we don't check for LDAP_DIRSEP since URLs should contain '/' */
55                 if( *p != '/' ) {
56                         /* skip over false root */
57                         p++;
58                 }
59
60                 p = ber_strdup( p );
61                 ldap_pvt_hex_unescape( p );
62
63                 url = fopen( p, "rb" );
64
65         } else {
66                 return -1;
67         }
68 #endif
69
70         if( url == NULL ) {
71                 return -1;
72         }
73
74         total = 0;
75
76         while( (bytes = fread( buffer, 1, sizeof(buffer), url )) != 0 ) {
77                 char *newp = ber_memrealloc( p, total + bytes + 1 );
78                 if( newp == NULL ) {
79                         ber_memfree( p );
80                         fclose( url );
81                         return -1;
82                 }
83                 p = newp;
84                 AC_MEMCPY( &p[total], buffer, bytes );
85                 total += bytes;
86         }
87
88         fclose( url );
89
90         if( total == 0 ) {
91                 char *newp = ber_memrealloc( p, 1 );
92                 if( newp == NULL ) {
93                         ber_memfree( p );
94                         return -1;
95                 }
96                 p = newp;
97         }
98
99         p[total] = '\0';
100         *valuep = p;
101         *vlenp = total;
102
103         return 0;
104 }