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