📄 main.c
字号:
/* show errors */ config->showerror ^= TRUE; /* toggle on if used with -s */ break; case 't': /* Telnet options */ config->telnet_options = curl_slist_append(config->telnet_options, nextarg); break; case 'T': /* we are uploading */ { struct getout *url; if(config->url_out || (config->url_out=config->url_list)) { /* there's a node here, if it already is filled-in continue to find an "empty" node */ while(config->url_out && (config->url_out->flags&GETOUT_UPLOAD)) config->url_out = config->url_out->next; } /* now there might or might not be an available node to fill in! */ if(config->url_out) /* existing node */ url = config->url_out; else /* there was no free node, create one! */ url=new_getout(config); if(url) { url->flags |= GETOUT_UPLOAD; /* mark -T used */ if(!*nextarg) url->flags |= GETOUT_NOUPLOAD; else { /* "-" equals stdin, but keep the string around for now */ GetStr(&url->infile, nextarg); } } } break; case 'u': /* user:password */ GetStr(&config->userpwd, nextarg); cleanarg(nextarg); checkpasswd("host", &config->userpwd); break; case 'U': /* Proxy user:password */ GetStr(&config->proxyuserpwd, nextarg); cleanarg(nextarg); checkpasswd("proxy", &config->proxyuserpwd); break; case 'v': config->conf ^= CONF_VERBOSE; /* talk a lot */ break; case 'V': { const char **proto; printf(CURL_ID "%s\n", curl_version()); if (curlinfo->protocols) { printf("Protocols: "); for (proto=curlinfo->protocols; *proto; ++proto) { printf("%s ", *proto); } puts(""); /* newline */ } if(curlinfo->features) { unsigned int i; struct feat { const char *name; int bitmask; }; struct feat feats[] = { {"IPv6", CURL_VERSION_IPV6}, {"krb4", CURL_VERSION_KERBEROS4}, {"SSL", CURL_VERSION_SSL}, {"libz", CURL_VERSION_LIBZ}, {"NTLM", CURL_VERSION_NTLM}, {"GSS-Negotiate", CURL_VERSION_GSSNEGOTIATE}, {"Debug", CURL_VERSION_DEBUG}, {"AsynchDNS", CURL_VERSION_ASYNCHDNS}, {"SPNEGO", CURL_VERSION_SPNEGO} }; printf("Features: "); for(i=0; i<sizeof(feats)/sizeof(feats[0]); i++) { if(curlinfo->features & feats[i].bitmask) printf("%s ", feats[i].name); } puts(""); /* newline */ } } return PARAM_HELP_REQUESTED; case 'w': /* get the output string */ if('@' == *nextarg) { /* the data begins with a '@' letter, it means that a file name or - (stdin) follows */ FILE *file; nextarg++; /* pass the @ */ if(curl_strequal("-", nextarg)) file = stdin; else file = fopen(nextarg, "r"); config->writeout = file2string(file); if(file && (file != stdin)) fclose(file); } else GetStr(&config->writeout, nextarg); break; case 'x': /* proxy */ GetStr(&config->proxy, nextarg); break; case 'X': /* set custom request */ GetStr(&config->customrequest, nextarg); break; case 'y': /* low speed time */ if(str2num(&config->low_speed_time, nextarg)) return PARAM_BAD_NUMERIC; if(!config->low_speed_limit) config->low_speed_limit = 1; break; case 'Y': /* low speed limit */ if(str2num(&config->low_speed_limit, nextarg)) return PARAM_BAD_NUMERIC; if(!config->low_speed_time) config->low_speed_time=30; break; case 'z': /* time condition coming up */ switch(*nextarg) { case '+': nextarg++; default: /* If-Modified-Since: (section 14.28 in RFC2068) */ config->timecond = CURL_TIMECOND_IFMODSINCE; break; case '-': /* If-Unmodified-Since: (section 14.24 in RFC2068) */ config->timecond = CURL_TIMECOND_IFUNMODSINCE; nextarg++; break; case '=': /* Last-Modified: (section 14.29 in RFC2068) */ config->timecond = CURL_TIMECOND_LASTMOD; nextarg++; break; } now=time(NULL); config->condtime=curl_getdate(nextarg, &now); if(-1 == (int)config->condtime) { /* now let's see if it is a file name to get the time from instead! */ struct stat statbuf; if(-1 == stat(nextarg, &statbuf)) { /* failed, remove time condition */ config->timecond = CURL_TIMECOND_NONE; } else { /* pull the time out from the file */ config->condtime = statbuf.st_mtime; } } break; default: /* unknown flag */ return PARAM_OPTION_UNKNOWN; } hit = -1; } while(!longopt && !singleopt && *++parse && !*usedarg); return PARAM_OK;}static int parseconfig(const char *filename, struct Configurable *config){ int res; FILE *file; char filebuffer[512]; bool usedarg; char *home; if(!filename || !*filename) { /* NULL or no file name attempts to load .curlrc from the homedir! */#define CURLRC DOT_CHAR "curlrc" filename = CURLRC; /* sensible default */ home = curl_getenv("HOME"); /* portable environment reader */ if(home) { if(strlen(home)<(sizeof(filebuffer)-strlen(CURLRC))) { snprintf(filebuffer, sizeof(filebuffer), "%s%s%s", home, DIR_CHAR, CURLRC); filename = filebuffer; } curl_free(home); /* we've used it, now free it */ } } if(strcmp(filename,"-")) file = fopen(filename, "r"); else file = stdin; if(file) { char *line; char *aline; char *option; char *param; int lineno=0; bool alloced_param;#define isseparator(x) (((x)=='=') || ((x) == ':')) while (NULL != (aline = my_get_line(file))) { lineno++; line = aline; alloced_param=FALSE; /* lines with # in the fist column is a comment! */ while(isspace((int)*line)) line++; switch(*line) { case '#': case '/': case '\r': case '\n': case '*': case '\0': free(aline); continue; } /* the option keywords starts here */ option = line; while(*line && !isspace((int)*line) && !isseparator(*line)) line++; /* ... and has ended here */ *line++=0; /* zero terminate, we have a local copy of the data */#ifdef DEBUG_CONFIG fprintf(stderr, "GOT: %s\n", option);#endif /* pass spaces and separator(s) */ while(isspace((int)*line) || isseparator(*line)) line++; /* the parameter starts here (unless quoted) */ if(*line == '\"') { char *ptr; /* quoted parameter, do the qoute dance */ line++; param=strdup(line); /* parameter */ alloced_param=TRUE; ptr=param; while(*line && (*line != '\"')) { if(*line == '\\') { char out; line++; /* default is to output the letter after the backslah */ switch(out = *line) { case '\0': continue; /* this'll break out of the loop */ case 't': out='\t'; break; case 'n': out='\n'; break; case 'r': out='\r'; break; case 'v': out='\v'; break; } *ptr++=out; line++; } else *ptr++=*line++; } *ptr=0; /* always zero terminate */ } else { param=line; /* parameter starts here */ while(*line && !isspace((int)*line)) line++; *line=0; /* zero terminate */ }#ifdef DEBUG_CONFIG fprintf(stderr, "PARAM: \"%s\"\n", param);#endif res = getparameter(option, param, &usedarg, config); if(*param && !usedarg) /* we passed in a parameter that wasn't used! */ res = PARAM_GOT_EXTRA_PARAMETER; if(res != PARAM_OK) { /* the help request isn't really an error */ if(!strcmp(filename, "-")) { filename=(char *)"<stdin>"; } if(PARAM_HELP_REQUESTED != res) { const char *reason; switch(res) { default: case PARAM_GOT_EXTRA_PARAMETER: reason = "had unsupported trailing garbage"; break; case PARAM_OPTION_UNKNOWN: reason = "is unknown"; break; case PARAM_OPTION_AMBIGUOUS: reason = "is ambiguous"; break; case PARAM_REQUIRES_PARAMETER: reason = "requires parameter"; break; case PARAM_BAD_USE: reason = "is badly used here"; break; case PARAM_BAD_NUMERIC: reason = "expected a proper numerical parameter"; break; } fprintf(stderr, "%s:%d: warning: '%s' %s\n", filename, lineno, option, reason); } } if(alloced_param) free(param); free(aline); } if(file != stdin) fclose(file); } return 0;}static void go_sleep(long ms){#ifdef HAVE_POLL /* portable subsecond "sleep" */ poll((void *)0, 0, ms);#else /* systems without poll() need other solutions */#ifdef WIN32 /* Windows offers a millisecond sleep */ Sleep(ms);#else /* Other systems must use select() for this */ struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = ms * 1000; select(0, NULL, NULL, NULL, &timeout);#endif#endif}struct OutStruct { char *filename; FILE *stream; struct Configurable *config;};int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream){ int rc; struct OutStruct *out=(struct OutStruct *)stream; struct Configurable *config = out->config; if(out && !out->stream) { /* open file for writing */ out->stream=fopen(out->filename, "wb"); if(!out->stream) return -1; /* failure */ } if(config->recvpersecond) { /* * We know when we received data the previous time. We know how much data * we get now. Make sure that this is not faster than we are told to run. * If we're faster, sleep a while *before* doing the fwrite() here. */ time_t timediff; time_t now; now = time(NULL); timediff = now - config->lastrecvtime; if( size*nmemb > config->recvpersecond*timediff) { /* figure out how many milliseconds to rest */ go_sleep ( (size*nmemb)*1000/config->recvpersecond - timediff*1000 ); now = time(NULL); } config->lastrecvtime = now; } rc = fwrite(buffer, size, nmemb, out->stream); if(config->nobuffer) /* disable output buffering */ fflush(out->stream); return rc;}struct InStruct { FILE *stream; struct Configurable *config;};int my_fread(void *buffer, size_t size, size_t nmemb, void *userp){ struct InStruct *in=(struct InStruct *)userp; struct Configurable *config = in->config; if(config->sendpersecond) { /* * We know when we sent data the previous time. We know how much data * we sent. Make sure that this was not faster than we are told to run. * If we're faster, sleep a while *before* doing the fread() here. * Also, make no larger fread() than should be sent this second! */ time_t timediff; time_t now; now = time(NULL); timediff = now - config->lastsendtime; if( config->lastsendsize > config->sendpersecond*timediff) { /* figure out how many milliseconds to rest */ go_sleep ( config->lastsendsize*1000/config->sendpersecond - timedif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -