Newer
Older
Daniel Stenberg
committed
}
if(res)
break; /* error detected */
Daniel Stenberg
committed
}
free(line);
}
struct OutStruct {
char *filename;
FILE *stream;
};
/* having this global is a bit dirty, but hey, who said we weren't? ;-) */
struct Configurable config;
int my_fwrite(void *buffer, size_t size, size_t nmemb, FILE *stream)
{
struct OutStruct *out=(struct OutStruct *)stream;
if(out && !out->stream) {
/* open file for writing */
out->stream=fopen(out->filename, "wb");
if(!out->stream)
return -1; /* failure */
if(config.nobuffer) {
/* disable output buffering */
#ifdef HAVE_SETVBUF
setvbuf(out->stream, NULL, _IONBF, 0);
#endif
}
}
return fwrite(buffer, size, nmemb, out->stream);
}
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
struct ProgressData {
size_t total;
size_t prev;
size_t point;
int width;
};
int myprogress (void *clientp,
size_t dltotal,
size_t dlnow,
size_t ultotal,
size_t ulnow)
{
/* The original progress-bar source code was written for curl by Lars Aas,
and this new edition inherites some of his concepts. */
char line[256];
char outline[256];
char format[40];
float frac;
float percent;
int barwidth;
int num;
int i;
int prevblock;
int thisblock;
struct ProgressData *bar = (struct ProgressData *)clientp;
size_t total = dltotal + ultotal;
bar->point = dlnow + ulnow; /* we've come this far */
if(0 == total) {
int prevblock = bar->prev / 1024;
int thisblock = bar->point / 1024;
while ( thisblock > prevblock ) {
fprintf( stderr, "#" );
prevblock++;
}
}
else {
frac = (float) bar->point / (float) total;
percent = frac * 100.0f;
barwidth = bar->width - 7;
num = (int) (((float)barwidth) * frac);
i = 0;
for ( i = 0; i < num; i++ ) {
line[i] = '#';
}
line[i] = '\0';
sprintf( format, "%%-%ds %%5.1f%%%%", barwidth );
sprintf( outline, format, line, percent );
fprintf( stderr, "\r%s", outline );
}
bar->prev = bar->point;
return 0;
}
void progressbarinit(struct ProgressData *bar)
{
#ifdef __EMX__
/* 20000318 mgs */
int scr_size [2];
#endif
char *colp;
memset(bar, 0, sizeof(struct ProgressData));
/* TODO: get terminal width through ansi escapes or something similar.
try to update width when xterm is resized... - 19990617 larsa */
#ifndef __EMX__
/* 20000318 mgs
* OS/2 users most likely won't have this env var set, and besides that
* we're using our own way to determine screen width */
colp = curl_getenv("COLUMNS");
if (colp != NULL) {
bar->width = atoi(colp);
free(colp);
}
else
bar->width = 79;
#else
/* 20000318 mgs
* We use this emx library call to get the screen width, and subtract
* one from what we got in order to avoid a problem with the cursor
* advancing to the next line if we print a string that is as long as
* the screen is wide. */
_scrsize(scr_size);
bar->width = scr_size[0] - 1;
#endif
}
char errorbuffer[CURL_ERROR_SIZE];
struct ProgressData progressbar;
Daniel Stenberg
committed
struct OutStruct heads;
int separator = 0;
FILE *infd = stdin;
FILE *headerfilep = NULL;
char *urlbuffer=NULL;
int infilesize=-1; /* -1 means unknown */
bool stillflags=TRUE;
int i;
outs.stream = stdout;
memset(&config, 0, sizeof(struct Configurable));
/* set non-zero default values: */
config.useragent= maprintf(CURL_NAME "/" CURL_VERSION " (" OS ") "
"%s", curl_version());
config.showerror=TRUE;
config.conf=CONF_DEFAULT;
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
if(argc>1 &&
(!strnequal("--", argv[1], 2) && (argv[1][0] == '-')) &&
strchr(argv[1], 'q')) {
/*
* The first flag, that is not a verbose name, but a shortname
* and it includes the 'q' flag!
*/
#if 0
fprintf(stderr, "I TURNED OFF THE CRAP\n");
#endif
;
}
else {
res = parseconfig(NULL, &config);
if(res)
return res;
}
if ((argc < 2) && !config.url) {
helpf(NULL);
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
}
/* Parse options */
for (i = 1; i < argc; i++) {
if(stillflags &&
('-' == argv[i][0])) {
char *nextarg;
bool passarg;
char *flag = &argv[i][1];
if(strequal("--", argv[i]))
/* this indicates the end of the flags and thus enables the
following (URL) argument to start with -. */
stillflags=FALSE;
else {
nextarg= (i < argc - 1)? argv[i+1]: NULL;
res = getparameter ( flag,
nextarg,
&passarg,
&config );
if(res)
return res;
if(passarg) /* we're supposed to skip this */
i++;
}
}
else {
if(url) {
helpf("only one URL is supported!\n");
}
url = argv[i];
}
}
/* if no URL was specified and there was one in the config file, get that
one */
if(!url && config.url)
url = config.url;
if(!url) {
helpf("no URL specified!\n");
Daniel Stenberg
committed
fprintf(stderr, "URL: %s PROXY: %s\n", url, config.proxy?config.proxy:"none");
Daniel Stenberg
committed
/* expand '{...}' and '[...]' expressions and return total number of URLs
in pattern set */
res = glob_url(&urls, url, &urlnum);
Daniel Stenberg
committed
return res;
outfiles = config.outfile; /* save outfile pattern befor expansion */
if (!outfiles && !config.remotefile && urlnum > 1) {
#ifdef CURL_SEPARATORS
/* multiple files extracted to stdout, insert separators! */
separator = 1;
#endif
#ifdef MIME_SEPARATORS
/* multiple files extracted to stdout, insert MIME separators! */
separator = 1;
printf("MIME-Version: 1.0\n");
printf("Content-Type: multipart/mixed; boundary=%s\n\n", MIMEseparator);
#endif
}
for (i = 0; (url = next_url(urls)); ++i) {
if (outfiles)
config.outfile = strdup(outfiles);
if(config.outfile && config.infile) {
helpf("you can't both upload and download!\n");
}
if (config.outfile || config.remotefile) {
/*
* We have specified a file name to store the result in, or we have
* decided we want to use the remote file name.
*/
if(config.remotefile) {
/* Find and get the remote file name */
config.outfile=strstr(url, "://");
if(config.outfile)
config.outfile+=3;
else
config.outfile=url;
config.outfile = strrchr(config.outfile, '/');
if(!config.outfile || !strlen(++config.outfile)) {
helpf("Remote file name has no length!\n");
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
}
}
else /* fill '#1' ... '#9' terms from URL pattern */
config.outfile = match_url(config.outfile, *urls);
if((0 == config.resume_from) && config.use_resume) {
/* we're told to continue where we are now, then we get the size of the
file as it is now and open it for append instead */
struct stat fileinfo;
if(0 == stat(config.outfile, &fileinfo)) {
/* set offset to current file size: */
config.resume_from = fileinfo.st_size;
}
/* else let offset remain 0 */
}
if(config.resume_from) {
/* open file for output: */
outs.stream=(FILE *) fopen(config.outfile, config.resume_from?"ab":"wb");
if (!outs.stream) {
helpf("Can't open '%s'!\n", config.outfile);
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
}
}
else {
outs.filename = config.outfile;
outs.stream = NULL; /* open when needed */
}
}
if (config.infile) {
/*
* We have specified a file to upload
*/
struct stat fileinfo;
/* If no file name part is given in the URL, we add this file name */
char *ptr=strstr(url, "://");
if(ptr)
ptr+=3;
else
ptr=url;
ptr = strrchr(ptr, '/');
if(!ptr || !strlen(++ptr)) {
/* The URL has no file name part, add the local file name. In order
to be able to do so, we have to create a new URL in another buffer.*/
urlbuffer=(char *)malloc(strlen(url) + strlen(config.infile) + 3);
if(!urlbuffer) {
helpf("out of memory\n");
}
if(ptr)
/* there is a trailing slash on the URL */
sprintf(urlbuffer, "%s%s", url, config.infile);
else
/* thers is no trailing slash on the URL */
sprintf(urlbuffer, "%s/%s", url, config.infile);
url = urlbuffer; /* use our new URL instead! */
}
infd=(FILE *) fopen(config.infile, "rb");
if (!infd || stat(config.infile, &fileinfo)) {
helpf("Can't open '%s'!\n", config.infile);
}
infilesize=fileinfo.st_size;
}
if((config.conf&CONF_UPLOAD) &&
config.use_resume &&
(0==config.resume_from)) {
config.resume_from = -1; /* -1 will then force get-it-yourself */
}
if(config.headerfile) {
/* open file for output: */
if(strcmp(config.headerfile,"-"))
{
Daniel Stenberg
committed
heads.filename = config.headerfile;
headerfilep=NULL;
Daniel Stenberg
committed
heads.stream = headerfilep;
Daniel Stenberg
committed
if(outs.stream && isatty(fileno(outs.stream)) &&
!(config.conf&(CONF_UPLOAD|CONF_HTTPPOST)))
/* we send the output to a tty and it isn't an upload operation, therefore
we switch off the progress meter */
if (urlnum > 1) {
fprintf(stderr, "\n[%d/%d]: %s --> %s\n", i+1, urlnum, url, config.outfile ? config.outfile : "<stdout>");
if (separator) {
#ifdef CURL_SEPARATORS
printf("%s%s\n", CURLseparator, url);
#endif
#ifdef MIME_SEPARATORS
printf("--%s\n", MIMEseparator);
printf("Content-ID: %s\n\n", url);
#endif
}
}
if(!config.errors)
config.errors = stderr;
if(!config.outfile && !(config.conf & CONF_GETTEXT)) {
/* We get the output to stdout and we have not got the ASCII/text flag,
then set stdout to be binary */
setmode( 1, O_BINARY );
}
#endif
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
#if 0
/* This is code left from the pre-v7 time, left here mainly as a reminder
and possibly as a warning! ;-) */
res = curl_urlget(CURLOPT_FILE, (FILE *)&outs, /* where to store */
CURLOPT_WRITEFUNCTION, my_fwrite, /* what call to write */
CURLOPT_INFILE, infd, /* for uploads */
CURLOPT_INFILESIZE, infilesize, /* size of uploaded file */
CURLOPT_URL, url, /* what to fetch */
CURLOPT_PROXY, config.proxy, /* proxy to use */
CURLOPT_FLAGS, config.conf, /* flags */
CURLOPT_USERPWD, config.userpwd, /* user + passwd */
CURLOPT_PROXYUSERPWD, config.proxyuserpwd, /* Proxy user + passwd */
CURLOPT_RANGE, config.range, /* range of document */
CURLOPT_ERRORBUFFER, errorbuffer,
CURLOPT_TIMEOUT, config.timeout,
CURLOPT_POSTFIELDS, config.postfields,
CURLOPT_REFERER, config.referer,
CURLOPT_USERAGENT, config.useragent,
CURLOPT_FTPPORT, config.ftpport,
CURLOPT_LOW_SPEED_LIMIT, config.low_speed_limit,
CURLOPT_LOW_SPEED_TIME, config.low_speed_time,
CURLOPT_RESUME_FROM, config.use_resume?config.resume_from:0,
CURLOPT_COOKIE, config.cookie,
CURLOPT_HTTPHEADER, config.headers,
CURLOPT_HTTPPOST, config.httppost,
CURLOPT_SSLCERT, config.cert,
CURLOPT_SSLCERTPASSWD, config.cert_passwd,
CURLOPT_CRLF, config.crlf,
CURLOPT_QUOTE, config.quote,
CURLOPT_POSTQUOTE, config.postquote,
CURLOPT_WRITEHEADER, config.headerfile?&heads:NULL,
CURLOPT_COOKIEFILE, config.cookiefile,
CURLOPT_SSLVERSION, config.ssl_version,
CURLOPT_TIMECONDITION, config.timecond,
CURLOPT_TIMEVALUE, config.condtime,
CURLOPT_CUSTOMREQUEST, config.customrequest,
CURLOPT_STDERR, config.errors,
CURLOPT_PROGRESSMODE, config.progressmode,
CURLOPT_WRITEINFO, config.writeout,
CURLOPT_DONE); /* always terminate the list of tags */
#endif
/* The new, v7-style easy-interface! */
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_FILE, (FILE *)&outs); /* where to store */
/* what call to write: */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
curl_easy_setopt(curl, CURLOPT_INFILE, infd); /* for uploads */
/* size of uploaded file: */
curl_easy_setopt(curl, CURLOPT_INFILESIZE, infilesize);
curl_easy_setopt(curl, CURLOPT_URL, url); /* what to fetch */
curl_easy_setopt(curl, CURLOPT_PROXY, config.proxy); /* proxy to use */
curl_easy_setopt(curl, CURLOPT_VERBOSE, config.conf&CONF_VERBOSE);
curl_easy_setopt(curl, CURLOPT_HEADER, config.conf&CONF_HEADER);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, config.conf&CONF_NOPROGRESS);
curl_easy_setopt(curl, CURLOPT_NOBODY, config.conf&CONF_NOBODY);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, config.conf&CONF_FAILONERROR);
curl_easy_setopt(curl, CURLOPT_UPLOAD, config.conf&CONF_UPLOAD);
curl_easy_setopt(curl, CURLOPT_POST, config.conf&CONF_POST);
curl_easy_setopt(curl, CURLOPT_FTPLISTONLY, config.conf&CONF_FTPLISTONLY);
curl_easy_setopt(curl, CURLOPT_FTPAPPEND, config.conf&CONF_FTPAPPEND);
curl_easy_setopt(curl, CURLOPT_NETRC, config.conf&CONF_NETRC);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,
config.conf&CONF_FOLLOWLOCATION);
curl_easy_setopt(curl, CURLOPT_TRANSFERTEXT, config.conf&CONF_GETTEXT);
curl_easy_setopt(curl, CURLOPT_PUT, config.conf&CONF_PUT);
curl_easy_setopt(curl, CURLOPT_MUTE, config.conf&CONF_MUTE);
curl_easy_setopt(curl, CURLOPT_USERPWD, config.userpwd);
curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, config.proxyuserpwd);
curl_easy_setopt(curl, CURLOPT_RANGE, config.range);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorbuffer);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, config.timeout);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, config.postfields);
/* new in libcurl 7.2: */
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, config.postfieldsize);
curl_easy_setopt(curl, CURLOPT_REFERER, config.referer);
curl_easy_setopt(curl, CURLOPT_AUTOREFERER, config.conf&CONF_AUTO_REFERER);
curl_easy_setopt(curl, CURLOPT_USERAGENT, config.useragent);
curl_easy_setopt(curl, CURLOPT_FTPPORT, config.ftpport);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, config.low_speed_limit);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, config.low_speed_time);
curl_easy_setopt(curl, CURLOPT_RESUME_FROM,
config.use_resume?config.resume_from:0);
curl_easy_setopt(curl, CURLOPT_COOKIE, config.cookie);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, config.headers);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, config.httppost);
curl_easy_setopt(curl, CURLOPT_SSLCERT, config.cert);
curl_easy_setopt(curl, CURLOPT_SSLCERTPASSWD, config.cert_passwd);
curl_easy_setopt(curl, CURLOPT_CRLF, config.crlf);
curl_easy_setopt(curl, CURLOPT_QUOTE, config.quote);
curl_easy_setopt(curl, CURLOPT_POSTQUOTE, config.postquote);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, config.headerfile?&heads:NULL);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, config.cookiefile);
curl_easy_setopt(curl, CURLOPT_SSLVERSION, config.ssl_version);
curl_easy_setopt(curl, CURLOPT_TIMECONDITION, config.timecond);
curl_easy_setopt(curl, CURLOPT_TIMEVALUE, config.condtime);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, config.customrequest);
curl_easy_setopt(curl, CURLOPT_STDERR, config.errors);
curl_easy_setopt(curl, CURLOPT_WRITEINFO, config.writeout);
/* three new ones in libcurl 7.3: */
curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, config.proxytunnel);
curl_easy_setopt(curl, CURLOPT_INTERFACE, config.iface);
curl_easy_setopt(curl, CURLOPT_KRB4LEVEL, config.krb4level);
if((config.progressmode == CURL_PROGRESS_BAR) &&
!(config.conf&(CONF_NOPROGRESS|CONF_MUTE))) {
/* we want the alternative style, then we have to implement it
ourselves! */
progressbarinit(&progressbar);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, myprogress);
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &progressbar);
}
res = curl_easy_perform(curl);
if(config.writeout) {
ourWriteOut(curl, config.writeout);
}
/* always cleanup */
curl_easy_cleanup(curl);
if((res!=CURLE_OK) && config.showerror)
fprintf(config.errors, "curl: (%d) %s\n", res, errorbuffer);
}
else
fprintf(config.errors, "curl: failed to init libcurl!\n");
main_free();
if((config.errors != stderr) &&
(config.errors != stdout))
/* it wasn't directed to stdout or stderr so close the file! */
fclose(config.errors);
if(config.headerfile && !headerfilep && heads.stream)
Daniel Stenberg
committed
fclose(heads.stream);
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
if(urlbuffer)
free(urlbuffer);
if (config.outfile && outs.stream)
fclose(outs.stream);
if (config.infile)
fclose(infd);
if(headerfilep)
fclose(headerfilep);
if(config.url)
free(config.url);
if(url)
free(url);
if(config.outfile && !config.remotefile)
free(config.outfile);
}
#ifdef MIME_SEPARATORS
if (separator)
printf("--%s--\n", MIMEseparator);
#endif
curl_slist_free_all(config.quote); /* the checks for config.quote == NULL */
curl_slist_free_all(config.postquote); /* */
curl_slist_free_all(config.headers); /* */
Daniel Stenberg
committed
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
static char *my_get_line(FILE *fp)
{
char buf[4096];
char *nl = NULL;
char *retval = NULL;
do
{
if (NULL == fgets(buf, sizeof(buf), fp))
break;
if (NULL == retval)
retval = strdup(buf);
else
{
if (NULL == (retval = realloc(retval,
strlen(retval) + strlen(buf) + 1)))
break;
strcat(retval, buf);
}
}
while (NULL == (nl = strchr(retval, '\n')));
if (NULL != nl)
*nl = '\0';
return retval;
}
static char *my_get_token(const char *line)
{
static const char *save = NULL;
const char *first = NULL;
const char *last = NULL;
char *retval = NULL;
size_t size;
if (NULL == line)
line = save;
if (NULL == line)
return NULL;
while (('\0' != *line) && (isspace(*line)))
line++;
first = line;
while (('\0' != *line) && (!isspace(*line)))
line++;
save = line;
while ('\0' != *line)
line++;
last = line;
size = last - first;
if (0 == size)
return NULL;
if (NULL == (retval = malloc(size + 1)))
return NULL;
memcpy(retval, first, size);
retval[size] = '\0';
return retval;
}