70 #include <sys/types.h>
74 # define PATH_MAX 1024
77 # include <curl/curl.h>
108 newsearch = (
char *)malloc((len = strlen(searchpath))+5);
112 for (i = 0, j = 0; i < len; i++) {
114 if (i < len-1 && searchpath[i] ==
':' && searchpath[i+1] ==
':') {
115 newsearch[j++] =
':';
120 if (searchpath[i] == path_sep) {
122 if (j && newsearch[j-1] != 0)
125 newsearch[j++] = searchpath[i];
131 newsearch[j++] =
'.';
132 newsearch[j++] =
'/';
142 mFILE *mf = NULL, *headers = NULL;
143 int maxlen = 8190 - strlen(file);
144 static CURL *handle = NULL;
145 static int curl_init = 0;
146 char errbuf[CURL_ERROR_SIZE];
151 if (curl_global_init(CURL_GLOBAL_ALL))
154 if (NULL == (handle = curl_easy_init()))
161 for (cp = buf; *url && cp - buf < maxlen; url++) {
162 if (*url ==
'%' && *(url+1) ==
's') {
164 cp += strlen(strcpy(cp, file));
172 if (NULL == (mf =
mfcreate(NULL, 0)) ||
173 NULL == (headers =
mfcreate(NULL, 0)))
176 if (0 != curl_easy_setopt(handle, CURLOPT_URL, buf))
178 if (0 != curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, 60L))
180 if (0 != curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION,
183 if (0 != curl_easy_setopt(handle, CURLOPT_WRITEDATA, mf))
185 if (0 != curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION,
186 (curl_write_callback)mfwrite))
188 if (0 != curl_easy_setopt(handle, CURLOPT_WRITEHEADER, headers))
190 if (0 != curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errbuf))
194 if (0 != curl_easy_perform(handle))
205 if (2 == sscanf(headers->data,
"HTTP/%f %d", &version, &response)) {
206 if (response != 200) {
208 fprintf(stderr,
"%.*s\n",
209 (
int)headers->size, headers->data);
229 fprintf(stderr,
"%s\n", errbuf);
242 static mFILE *find_file_dir(
char *file,
char *dirname) {
244 size_t len = strlen(dirname);
247 if (dirname[len-1] ==
'/')
251 if (*file ==
'/' || (len==1 && *dirname ==
'.')) {
252 sprintf(path,
"%s", file);
255 char *path_end = path;
257 while ((cp = strchr(dirname,
'%'))) {
259 long l = strtol(cp+1, &endp, 10);
261 strncpy(path_end, dirname, (endp+1)-dirname);
262 path_end += (endp+1)-dirname;
267 strncpy(path_end, dirname, cp-dirname);
268 path_end += cp-dirname;
270 strncpy(path_end, file, l);
271 path_end +=
MIN(strlen(file), l);
272 file +=
MIN(strlen(file), l);
274 strcpy(path_end, file);
275 path_end += strlen(file);
276 file += strlen(file);
278 len -= (endp+1) - dirname;
281 strncpy(path_end, dirname, len);
282 path_end +=
MIN(strlen(dirname), len);
286 strcpy(path_end, file);
293 return mfopen(path,
"rb");
326 path = getenv(
"RAWDATA");
335 for (ele = newsearch; *ele; ele += strlen(ele)+1) {
337 char *suffix[6] = {
"",
".gz",
".bz2",
".sz",
".Z",
".bz2"};
338 for (i = 0; i < 6; i++) {
355 sprintf(file2,
"%s%s", file, suffix[i]);
357 #if defined(HAVE_LIBCURL)
358 if (0 == strncmp(ele2,
"URL=", 4)) {
365 if (valid && (fp = find_file_dir(file2, ele2))) {
378 strcpy(relative_path, relative_to);
379 if ((cp = strrchr(relative_path,
'/')))
381 if ((fp = find_file_dir(file, relative_path)))