/*************************************************************************** * _ _ ____ _ * Project ___| | | | _ \| | * / __| | | | |_) | | * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * * Copyright (C) 1998 - 2005, Daniel Stenberg, , et al. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms * are also available at http://curl.haxx.se/docs/copyright.html. * * You may opt to use, copy, modify, merge, publish, distribute and/or sell * copies of the Software, and permit persons to whom the Software is * furnished to do so, under the terms of the COPYING file. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * * $Id: main.c,v 1.311 2005/03/04 00:12:02 bagder Exp $ ***************************************************************************/ #include "setup.h" #include #include #include #include #include #include #include #include #include #include "urlglob.h" #include "writeout.h" #include "getpass.h" #include "homedir.h" #ifdef USE_MANUAL #include "hugehelp.h" #endif #ifdef USE_ENVIRONMENT #include "writeenv.h" #endif #define CURLseparator "--_curl_--" #if defined(WIN32)&&!defined(__CYGWIN32__) #include #endif #ifdef __NOVELL_LIBC__ #include #endif #ifdef TIME_WITH_SYS_TIME /* We can include both fine */ #include #include #else #ifdef HAVE_SYS_TIME_H # include #else # include #endif #endif #include "version.h" #ifdef HAVE_IO_H /* typical win32 habit */ #include #endif #ifdef HAVE_UNISTD_H #include #endif #ifdef HAVE_FCNTL_H #include #endif #ifdef HAVE_UTIME_H #include #else #ifdef HAVE_SYS_UTIME_H #include #endif #endif /* HAVE_UTIME_H */ #ifdef HAVE_LIMITS_H #include #endif #ifdef HAVE_SYS_POLL_H #include #endif #ifdef HAVE_LOCALE_H #include /* for setlocale() */ #endif #define ENABLE_CURLX_PRINTF /* make the curlx header define all printf() functions to use the curlx_* versions instead */ #include /* header from the libcurl directory */ /* The last #include file should be: */ #ifdef CURLDEBUG #ifndef CURLTOOLDEBUG #define MEMDEBUG_NODEFINES #endif /* This is low-level hard-hacking memory leak tracking and similar. Using the library level code from this client-side is ugly, but we do this anyway for convenience. */ #include "memdebug.h" #endif #define DEFAULT_MAXREDIRS 50L #ifdef __DJGPP__ #include char *msdosify(char *); char *rename_if_dos_device_name(char *); /* we want to glob our own argv[] */ char **__crt0_glob_function (char *arg) { (void)arg; return (char**)0; } #endif /* __DJGPP__ */ #define CURL_PROGRESS_STATS 0 /* default progress display */ #define CURL_PROGRESS_BAR 1 /** * @def MIN * standard MIN macro */ #ifndef MIN #define MIN(X,Y) (((X) < (Y)) ? (X) : (Y)) #endif typedef enum { HTTPREQ_UNSPEC, HTTPREQ_GET, HTTPREQ_HEAD, HTTPREQ_POST, HTTPREQ_SIMPLEPOST, HTTPREQ_CUSTOM, HTTPREQ_LAST } HttpReq; /* Just a set of bits */ #define CONF_DEFAULT 0 #define CONF_AUTO_REFERER (1<<4) /* the automatic referer-system please! */ #define CONF_VERBOSE (1<<5) /* talk a lot */ #define CONF_HEADER (1<<8) /* throw the header out too */ #define CONF_NOPROGRESS (1<<10) /* shut off the progress meter */ #define CONF_NOBODY (1<<11) /* use HEAD to get http document */ #define CONF_FAILONERROR (1<<12) /* no output on http error codes >= 300 */ #define CONF_FTPLISTONLY (1<<16) /* Use NLST when listing ftp dir */ #define CONF_FTPAPPEND (1<<20) /* Append instead of overwrite on upload! */ #define CONF_NETRC (1<<22) /* read user+password from .netrc */ #define CONF_FOLLOWLOCATION (1<<23) /* use Location: Luke! */ #define CONF_GETTEXT (1<<24) /* use ASCII/text for transfer */ #define CONF_MUTE (1<<28) /* force NOPROGRESS */ #define CONF_NETRC_OPT (1<<29) /* read user+password from either * .netrc or URL*/ #define CONF_UNRESTRICTED_AUTH (1<<30) /* Send authentication (user+password) when following * locations, even when hostname changed */ #ifndef HAVE_STRDUP /* Ultrix doesn't have strdup(), so make a quick clone: */ char *strdup(char *str) { int len; char *newstr; len = strlen(str); newstr = (char *) malloc((len+1)*sizeof(char)); if (!newstr) return (char *)NULL; strcpy(newstr,str); return newstr; } #endif #ifdef WIN32 #include #define F_OK 0 #define mkdir(x,y) (mkdir)(x) #endif #ifdef VMS #include "curlmsg_vms.h" #endif /* Support uploading and resuming of >2GB files */ #if defined(WIN32) && (SIZEOF_CURL_OFF_T > 4) #define struct_stat struct _stati64 #define stat(file,st) _stati64(file,st) #else #define struct_stat struct stat #endif #ifdef WIN32 /* * Truncate a file handle at a 64-bit position 'where'. * Borland doesn't even support 64-bit types. */ #ifdef __BORLANDC__ #define _lseeki64(hnd,ofs,whence) lseek(hnd,ofs,whence) #endif static int ftruncate64 (int fd, curl_off_t where) { curl_off_t curr; int rc = 0; if ((curr = _lseeki64(fd, 0, SEEK_CUR)) < 0) return -1; if (_lseeki64(fd, where, SEEK_SET) < 0) return -1; if (write(fd, 0, 0) < 0) rc = -1; _lseeki64(fd, curr, SEEK_SET); return rc; } #define ftruncate(fd,where) ftruncate64(fd,where) #endif /* * This is the main global constructor for the app. Call this before * _any_ libcurl usage. If this fails, *NO* libcurl functions may be * used, or havoc may be the result. */ static CURLcode main_init(void) { return curl_global_init(CURL_GLOBAL_DEFAULT); } /* * This is the main global destructor for the app. Call this after * _all_ libcurl usage is done. */ static void main_free(void) { curl_global_cleanup(); } static int SetHTTPrequest(HttpReq req, HttpReq *store) { if((*store == HTTPREQ_UNSPEC) || (*store == req)) { *store = req; return 0; } fprintf(stderr, "You can only select one HTTP request!\n"); return 1; } static void helpf(const char *fmt, ...) { va_list ap; if(fmt) { va_start(ap, fmt); fputs("curl: ", stderr); /* prefix it */ vfprintf(stderr, fmt, ap); va_end(ap); } fprintf(stderr, "curl: try 'curl --help' " #ifdef USE_MANUAL "or 'curl --manual' " #endif "for more information\n"); } /* * A chain of these nodes contain URL to get and where to put the URL's * contents. */ struct getout { struct getout *next; /* next one */ char *url; /* the URL we deal with */ char *outfile; /* where to store the output */ char *infile; /* file to upload, if GETOUT_UPLOAD is set */ int flags; /* options */ }; #define GETOUT_OUTFILE (1<<0) /* set when outfile is deemed done */ #define GETOUT_URL (1<<1) /* set when URL is deemed done */ #define GETOUT_USEREMOTE (1<<2) /* use remote file name locally */ #define GETOUT_UPLOAD (1<<3) /* if set, -T has been used */ #define GETOUT_NOUPLOAD (1<<4) /* if set, -T "" has been used */ static void help(void) { int i; static const char * const helptext[]={ "Usage: curl [options...] ", "Options: (H) means HTTP/HTTPS only, (F) means FTP only", " -a/--append Append to target file when uploading (F)", " -A/--user-agent User-Agent to send to server (H)", " --anyauth Tell curl to choose authentication method (H)", " -b/--cookie Cookie string or file to read cookies from (H)", " --basic Enable HTTP Basic Authentication (H)", " -B/--use-ascii Use ASCII/text transfer", " -c/--cookie-jar Write cookies to this file after operation (H)", " -C/--continue-at Resumed transfer offset", " -d/--data HTTP POST data (H)", " --data-ascii HTTP POST ASCII data (H)", " --data-binary HTTP POST binary data (H)", " --negotiate Enable HTTP Negotiate Authentication (H)", " --digest Enable HTTP Digest Authentication (H)", " --disable-eprt Prevent curl from using EPRT or LPRT (F)", " --disable-epsv Prevent curl from using EPSV (F)", " -D/--dump-header Write the headers to this file", " --egd-file EGD socket path for random data (SSL)", " --tcp-nodelay Set the TCP_NODELAY option", #ifdef USE_ENVIRONMENT " --environment Write result codes to environment variables (RISC OS)", #endif " -e/--referer Referer URL (H)", " -E/--cert Client certificate file and password (SSL)", " --cert-type Certificate file type (DER/PEM/ENG) (SSL)", " --key Private key file name (SSL)", " --key-type Private key file type (DER/PEM/ENG) (SSL)", " --pass Pass phrase for the private key (SSL)", " --engine Crypto engine to use (SSL). \"--engine list\" for list", " --cacert CA certificate to verify peer against (SSL)", " --capath CA directory (made using c_rehash) to verify", " peer against (SSL)", " --ciphers SSL ciphers to use (SSL)", " --compressed Request compressed response (using deflate or gzip)", " --connect-timeout Maximum time allowed for connection", " --create-dirs Create necessary local directory hierarchy", " --crlf Convert LF to CRLF in upload", " -f/--fail Fail silently (no output at all) on errors (H)", " --ftp-create-dirs Create the remote dirs if not present (F)", " --ftp-pasv Use PASV instead of PORT (F)", " --ftp-ssl Enable SSL/TLS for the ftp transfer (F)", " -F/--form Specify HTTP multipart POST data (H)", " -g/--globoff Disable URL sequences and ranges using {} and []", " -G/--get Send the -d data with a HTTP GET (H)", " -h/--help This help text", " -H/--header Custom header to pass to server (H)", " -i/--include Include protocol headers in the output (H/F)", " -I/--head Show document info only", " -j/--junk-session-cookies Ignore session cookies read from file (H)", " --interface Specify network interface to use", " --krb4 Enable krb4 with specified security level (F)", " -k/--insecure Allow curl to connect to SSL sites without certs (H)", " -K/--config Specify which config file to read", " -l/--list-only List only names of an FTP directory (F)", " --limit-rate Limit transfer speed to this rate", " -L/--location Follow Location: hints (H)", " --location-trusted Follow Location: and send authentication even ", " to other hostnames (H)", " -m/--max-time Maximum time allowed for the transfer", " --max-redirs Maximum number of redirects allowed (H)", " --max-filesize Maximum file size to download (H/F)", " -M/--manual Display the full manual", " -n/--netrc Must read .netrc for user name and password", " --netrc-optional Use either .netrc or URL; overrides -n", " --ntlm Enable HTTP NTLM authentication (H)", " -N/--no-buffer Disable buffering of the output stream", " -o/--output Write output to instead of stdout", " -O/--remote-name Write output to a file named as the remote file", " -p/--proxytunnel Operate through a HTTP proxy tunnel (using CONNECT)", " --proxy-basic Enable Basic authentication on the proxy (H)", " --proxy-digest Enable Digest authentication on the proxy (H)", " --proxy-ntlm Enable NTLM authentication on the proxy (H)", " -P/--ftp-port
Use PORT with address instead of PASV (F)", " -q If used as the first parameter disables .curlrc", " -Q/--quote Send command(s) to server before file transfer (F)", " -r/--range Retrieve a byte range from a HTTP/1.1 or FTP server", " --random-file File for reading random data from (SSL)", " -R/--remote-time Set the remote file's time on the local output", " --retry Retry request times if transient problems occur", " --retry-delay When retrying, wait this many seconds between each", " --retry-max-time Retry only within this period", " -s/--silent Silent mode. Don't output anything", " -S/--show-error Show error. With -s, make curl show errors when they occur", " --socks Use SOCKS5 proxy on given host + port", " --stderr Where to redirect stderr. - means stdout", " -t/--telnet-option Set telnet option", " --trace Write a debug trace to the given file", " --trace-ascii Like --trace but without the hex output", " -T/--upload-file Transfer to remote site", " --url Spet URL to work with", " -u/--user Set server user and password", " -U/--proxy-user Set proxy user and password", " -v/--verbose Make the operation more talkative", " -V/--version Show version number and quit", #ifdef __DJGPP__ " --wdebug Turn on Watt-32 debugging under DJGPP", #endif " -w/--write-out [format] What to output after completion", " -x/--proxy Use HTTP proxy on given port", " -X/--request Specify request command to use", " -y/--speed-time Time needed to trig speed-limit abort. Defaults to 30", " -Y/--speed-limit Stop transfer if below speed-limit for 'speed-time' secs", " -z/--time-cond