[go: up one dir, main page]

Menu

[d1f406]: / libarc / url.c  Maximize  Restore  History

Download this file

568 lines (502 with data), 11.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
TiMidity++ -- MIDI to WAVE converter and player
Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#ifndef NO_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#include "timidity.h"
#include "common.h"
#include "url.h"
/* #define DEBUG */
int url_errno;
static struct URL_module *url_mod_list = NULL;
char *user_mailaddr = NULL;
char *url_user_agent = NULL;
int url_newline_code = '\n';
char *url_lib_version = URL_LIB_VERSION;
int uudecode_unquote_html = 0;
void url_add_module(struct URL_module *m)
{
m->chain = url_mod_list;
url_mod_list = m;
}
void url_add_modules(struct URL_module *m, ...)
{
va_list ap;
struct URL_module *mod;
if(m == NULL)
return;
url_add_module(m);
va_start(ap, m);
while((mod = va_arg(ap, struct URL_module *)) != NULL)
url_add_module(mod);
}
static int url_init_nop(void)
{
/* Do nothing any more */
return 1;
}
int url_check_type(char *s)
{
struct URL_module *m;
for(m = url_mod_list; m; m = m->chain)
if(m->type != URL_none_t && m->name_check && m->name_check(s))
return m->type;
return -1;
}
URL url_open(char *s)
{
struct URL_module *m;
for(m = url_mod_list; m; m = m->chain)
{
#ifdef DEBUG
fprintf(stderr, "Check URL type=%d\n", m->type);
#endif /* DEBUG */
if(m->type != URL_none_t && m->name_check && m->name_check(s))
{
#ifdef DEBUG
fprintf(stderr, "open url (type=%d, name=%s)\n", m->type, s);
#endif /* DEBUG */
if(m->url_init != url_init_nop)
{
if(m->url_init && m->url_init() < 0)
return NULL;
m->url_init = url_init_nop;
}
url_errno = URLERR_NONE;
errno = 0;
return m->url_open(s);
}
}
url_errno = URLERR_NOURL;
errno = ENOENT;
return NULL;
}
long url_read(URL url, void *buff, long n)
{
if(n <= 0)
return 0;
url_errno = URLERR_NONE;
errno = 0;
if(url->nread >= url->readlimit) {
url->eof = 1;
return 0;
}
if(url->nread + n > url->readlimit)
n = (long)(url->readlimit - url->nread);
n = url->url_read(url, buff, n);
if(n > 0)
url->nread += n;
return n;
}
long url_safe_read(URL url, void *buff, long n)
{
long i;
if(n <= 0)
return 0;
do /* Ignore signal intruption */
{
errno = 0;
i = url_read(url, buff, n);
} while(i == -1 && errno == EINTR);
#if 0
/* Already done in url_read!! */
if(i > 0)
url->nread += i;
#endif
return i;
}
long url_nread(URL url, void *buff, long n)
{
long insize = 0;
char *s = (char *)buff;
do
{
long i;
i = url_safe_read(url, s + insize, n - insize);
if(i <= 0)
{
if(insize == 0)
return i;
break;
}
insize += i;
} while(insize < n);
return insize;
}
char *url_gets(URL url, char *buff, int n)
{
if(url->nread >= url->readlimit)
return NULL;
if(url->url_gets == NULL)
{
int maxlen, i, c;
int newline = url_newline_code;
maxlen = n - 1;
if(maxlen == 0)
*buff = '\0';
if(maxlen <= 0)
return buff;
i = 0;
do
{
if((c = url_getc(url)) == EOF)
break;
buff[i++] = c;
} while(c != newline && i < maxlen);
if(i == 0)
return NULL; /* EOF */
buff[i] = '\0';
return buff;
}
url_errno = URLERR_NONE;
errno = 0;
if(url->nread + n > url->readlimit)
n = (long)(url->readlimit - url->nread) + 1;
buff = url->url_gets(url, buff, n);
if(buff != NULL)
url->nread += strlen(buff);
return buff;
}
int url_readline(URL url, char *buff, int n)
{
int maxlen, i, c;
maxlen = n - 1;
if(maxlen == 0)
*buff = '\0';
if(maxlen <= 0)
return 0;
do
{
i = 0;
do
{
if((c = url_getc(url)) == EOF)
break;
buff[i++] = c;
} while(c != '\r' && c != '\n' && i < maxlen);
if(i == 0)
return 0; /* EOF */
} while(i == 1 && (c == '\r' || c == '\n'));
if(c == '\r' || c == '\n')
i--;
buff[i] = '\0';
return i;
}
int url_fgetc(URL url)
{
if(url->nread >= url->readlimit)
return EOF;
url->nread++;
if(url->url_fgetc == NULL)
{
unsigned char c;
if(url_read(url, &c, 1) <= 0)
return EOF;
return (int)c;
}
url_errno = URLERR_NONE;
errno = 0;
return url->url_fgetc(url);
}
long url_seek(URL url, long offset, int whence)
{
long pos, savelimit;
if(url->url_seek == NULL)
{
if(whence == SEEK_CUR && offset >= 0)
{
pos = url_tell(url);
if(offset == 0)
return pos;
savelimit = (long)url->readlimit;
url->readlimit = URL_MAX_READLIMIT;
url_skip(url, offset);
url->readlimit = savelimit;
url->nread = 0;
return pos;
}
if(whence == SEEK_SET)
{
pos = url_tell(url);
if(pos != -1 && pos <= offset)
{
if(pos == offset)
return pos;
savelimit = (long)url->readlimit;
url->readlimit = URL_MAX_READLIMIT;
url_skip(url, offset - pos);
url->readlimit = savelimit;
url->nread = 0;
return pos;
}
}
url_errno = errno = EPERM;
return -1;
}
url_errno = URLERR_NONE;
errno = 0;
url->nread = 0;
return url->url_seek(url, offset, whence);
}
long url_tell(URL url)
{
url_errno = URLERR_NONE;
errno = 0;
if(url->url_tell == NULL)
return (long)url->nread;
return url->url_tell(url);
}
void url_skip(URL url, long n)
{
char tmp[BUFSIZ];
if(url->url_seek != NULL)
{
long savenread;
savenread = (long)url->nread;
if(savenread >= url->readlimit)
return;
if(savenread + n > url->readlimit)
n = (long)(url->readlimit - savenread);
if(url->url_seek(url, n, SEEK_CUR) != -1)
{
url->nread = savenread + n;
return;
}
url->nread = savenread;
}
while(n > 0)
{
long c;
c = n;
if(c > sizeof(tmp))
c = sizeof(tmp);
c = url_read(url, tmp, c);
if(c <= 0)
break;
n -= c;
}
}
void url_rewind(URL url)
{
if(url->url_seek != NULL)
url->url_seek(url, 0, SEEK_SET);
url->nread = 0;
}
void url_set_readlimit(URL url, long readlimit)
{
if(readlimit < 0)
url->readlimit = URL_MAX_READLIMIT;
else
url->readlimit = (unsigned long)readlimit;
url->nread = 0;
}
URL alloc_url(int size)
{
URL url;
#ifdef HAVE_SAFE_MALLOC
url = (URL)safe_malloc(size);
memset(url, 0, size);
#else
url = (URL)malloc(size);
if(url != NULL)
memset(url, 0, size);
else
url_errno = errno;
#endif /* HAVE_SAFE_MALLOC */
url->nread = 0;
url->readlimit = URL_MAX_READLIMIT;
url->eof = 0;
return url;
}
void url_close(URL url)
{
int save_errno = errno;
if(url == NULL)
{
fprintf(stderr, "URL stream structure is NULL?\n");
#ifdef ABORT_AT_FATAL
abort();
#endif /* ABORT_AT_FATAL */
}
else if(url->url_close == NULL)
{
fprintf(stderr, "URL Error: Already URL is closed (type=%d)\n",
url->type);
#ifdef ABORT_AT_FATAL
abort();
#endif /* ABORT_AT_FATAL */
}
else
{
url->url_close(url);
#if 0
url->url_close = NULL;
#endif /* unix */
}
errno = save_errno;
}
#if defined(TILD_SCHEME_ENABLE)
#include <pwd.h>
char *url_expand_home_dir(char *fname)
{
static char path[BUFSIZ];
char *dir;
int dirlen;
if(fname[0] != '~')
return fname;
if(IS_PATH_SEP(fname[1])) /* ~/... */
{
fname++;
if((dir = getenv("HOME")) == NULL)
if((dir = getenv("home")) == NULL)
return fname;
}
else /* ~user/... */
{
struct passwd *pw;
int i;
fname++;
for(i = 0; i < sizeof(path) - 1 && fname[i] && !IS_PATH_SEP(fname[i]); i++)
path[i] = fname[i];
path[i] = '\0';
if((pw = getpwnam(path)) == NULL)
return fname - 1;
fname += i;
dir = pw->pw_dir;
}
dirlen = strlen(dir);
strncpy(path, dir, sizeof(path) - 1);
if(sizeof(path) > dirlen)
strncat(path, fname, sizeof(path) - dirlen - 1);
path[sizeof(path) - 1] = '\0';
return path;
}
char *url_unexpand_home_dir(char *fname)
{
static char path[BUFSIZ];
char *dir, *p;
int dirlen;
if(!IS_PATH_SEP(fname[0]))
return fname;
if((dir = getenv("HOME")) == NULL)
if((dir = getenv("home")) == NULL)
return fname;
dirlen = strlen(dir);
if(dirlen == 0 || dirlen >= sizeof(path) - 2)
return fname;
memcpy(path, dir, dirlen);
if(!IS_PATH_SEP(path[dirlen - 1]))
path[dirlen++] = PATH_SEP;
#ifndef __W32__
if(strncmp(path, fname, dirlen) != 0)
#else
if(strncasecmp(path, fname, dirlen) != 0)
#endif /* __W32__ */
return fname;
path[0] = '~';
path[1] = '/';
p = fname + dirlen;
if(strlen(p) >= sizeof(path) - 3)
return fname;
path[2] = '\0';
strcat(path, p);
return path;
}
#else
char *url_expand_home_dir(char *fname)
{
return fname;
}
char *url_unexpand_home_dir(char *fname)
{
return fname;
}
#endif
static char *url_strerror_txt[] =
{
"", /* URLERR_NONE */
"Unknown URL", /* URLERR_NOURL */
"Operation not permitted", /* URLERR_OPERM */
"Can't open a URL", /* URLERR_CANTOPEN */
"Invalid URL form", /* URLERR_IURLF */
"URL too long", /* URLERR_URLTOOLONG */
"No mail address", /* URLERR_NOMAILADDR */
""
};
char *url_strerror(int no)
{
if(no <= URLERR_NONE)
return strerror(no);
if(no >= URLERR_MAXNO)
return "Internal error";
return url_strerror_txt[no - URLERR_NONE];
}
void *url_dump(URL url, long nbytes, long *read_size)
{
long allocated, offset, read_len;
char *buff;
if(read_size != NULL)
*read_size = 0;
if(nbytes == 0)
return NULL;
if(nbytes >= 0)
{
buff = (void *)safe_malloc(nbytes);
if(nbytes == 0)
return buff;
read_len = url_nread(url, buff, nbytes);
if(read_size != NULL)
*read_size = read_len;
if(read_len <= 0)
{
free(buff);
return NULL;
}
return buff;
}
allocated = 1024;
buff = (char *)safe_malloc(allocated);
offset = 0;
read_len = allocated;
while((nbytes = url_read(url, buff + offset, read_len)) > 0)
{
offset += nbytes;
read_len -= nbytes;
if(offset == allocated)
{
read_len = allocated;
allocated *= 2;
buff = (char *)safe_realloc(buff, allocated);
}
}
if(offset == 0)
{
free(buff);
return NULL;
}
if(read_size != NULL)
*read_size = offset;
return buff;
}