diff options
| -rw-r--r-- | src/config.c | 90 | ||||
| -rw-r--r-- | src/download.c | 110 | ||||
| -rw-r--r-- | src/getht.c | 82 | ||||
| -rw-r--r-- | src/getht.h | 20 | 
4 files changed, 141 insertions, 161 deletions
diff --git a/src/config.c b/src/config.c index e89bc94..1f02b4b 100644 --- a/src/config.c +++ b/src/config.c @@ -25,22 +25,14 @@  #include <curl/curl.h> -extern int proxy_type; -extern char proxy_addr[STR_MAX]; -extern long proxy_port; -extern int proxy_auth; -extern char proxy_user[STR_MAX]; -extern char proxy_pass[STR_MAX]; -extern char issue_uri[STR_MAX]; - -int loadconfig(char * htde_path, char * save_path, int * update) +int loadconfig(char * getht_path, struct config * options)  /*	Loads variables from config file to extern and passed   *	variables. */  {  	FILE * config_file;  	char filepath[STR_MAX]; -	snprintf(filepath, STR_MAX, "%s/config.ini", htde_path); +	snprintf(filepath, STR_MAX, "%s/config.ini", getht_path);  	if((config_file = fopen(filepath,"r")) == NULL)  	{ @@ -55,45 +47,45 @@ int loadconfig(char * htde_path, char * save_path, int * update)  		if(option[0] == '#');	/* ignore lines beginning with a hash */  		else if(!strcmp(option, "savepath")) -			strncpy(save_path, parameter, STR_MAX); +			strncpy(options->save_path, parameter, STR_MAX);  		else if(!strcmp(option, "startup_check")) -			*update = atoi(parameter); +			options->startup_check = atoi(parameter);  		else if(!strcmp(option, "toc_uri")) -			strncpy(issue_uri, parameter, STR_MAX); +			strncpy(options->issue_uri, parameter, STR_MAX);  		else if(!strcmp(option, "proxy_type"))  		{  			if(!strcmp(parameter, "http")) -				proxy_type = CURLPROXY_HTTP; +				options->proxy.type = CURLPROXY_HTTP;  			else if(!strcmp(parameter, "socks4")) -				proxy_type = CURLPROXY_SOCKS4; +				options->proxy.type = CURLPROXY_SOCKS4;  			else if(!strcmp(parameter, "socks5")) -				proxy_type = CURLPROXY_SOCKS5; +				options->proxy.type = CURLPROXY_SOCKS5;  			else  				fprintf(stderr,  					"Proxy type %s not known, please use either http, socks4 or socks5\n",  					parameter);  		}  		else if(!strcmp(option, "proxy_address")) -			strncpy(proxy_addr, parameter, STR_MAX); +			strncpy(options->proxy.address, parameter, STR_MAX);  		else if(!strcmp(option, "proxy_port")) -			proxy_port = (long) atoi(parameter); +			options->proxy.port = (long) atoi(parameter);  		else if(!strcmp(option, "proxy_auth"))  		{  			if(!strcmp(parameter, "basic")) -				proxy_auth = CURLAUTH_BASIC; +				options->proxy.auth = CURLAUTH_BASIC;  			else if(!strcmp(parameter, "digest")) -				proxy_auth = CURLAUTH_DIGEST; +				options->proxy.auth = CURLAUTH_DIGEST;  			else if(!strcmp(parameter, "ntlm")) -				proxy_auth = CURLAUTH_NTLM; +				options->proxy.auth = CURLAUTH_NTLM;  			else  				fprintf(stderr,  					"Proxy authentication method %s not known, please use basic, digest or ntlm",  					parameter);  		}  		else if(!strcmp(option, "proxy_user")) -			strncpy(proxy_user, parameter, STR_MAX); +			strncpy(options->proxy.user, parameter, STR_MAX);  		else if(!strcmp(option, "proxy_pass")) -			strncpy(proxy_pass, parameter, STR_MAX); +			strncpy(options->proxy.pass, parameter, STR_MAX);  		else  			fprintf(stderr, "Option '%s' not recognised, ignoring\n", option);  	} @@ -101,13 +93,13 @@ int loadconfig(char * htde_path, char * save_path, int * update)  	return 0;  } -int writefreshconfig(char * htde_path, char * save_path, int * update) +int writefreshconfig(char * getht_path, struct config * options)  /*	Write a new config file according to extern and passed variables. */  {  	FILE * config_file;  	char filepath[STR_MAX]; -	snprintf(filepath, STR_MAX, "%s/config.ini", htde_path); +	snprintf(filepath, STR_MAX, "%s/config.ini", getht_path);  	if((config_file = fopen(filepath,"w")) == NULL)  	{ @@ -117,50 +109,50 @@ int writefreshconfig(char * htde_path, char * save_path, int * update)  	else  		fprintf(stdout,"Writing a fresh config file to %s.\n",filepath); -	if(save_path[0]) -		fprintf(config_file, "%s = %s\n", "savepath", save_path); -	if(update) -		fprintf(config_file, "%s = %i\n", "startup_check", *update); -	if(issue_uri[0]) -		fprintf(config_file, "%s = %s\n", "toc_uri", issue_uri); -	if(proxy_type) +	if(options->save_path[0]) +		fprintf(config_file, "%s = %s\n", "savepath", options->save_path); +	if(options->startup_check) +		fprintf(config_file, "%s = %i\n", "startup_check", options->startup_check); +	if(options->issue_uri[0]) +		fprintf(config_file, "%s = %s\n", "toc_uri", options->issue_uri); +	if(options->proxy.type)  	{ -		if(proxy_type == CURLPROXY_HTTP) +		if(options->proxy.type == CURLPROXY_HTTP)  			fprintf(config_file, "%s = %s\n", "proxy_type", "http"); -		else if(proxy_type == CURLPROXY_SOCKS4) +		else if(options->proxy.type == CURLPROXY_SOCKS4)  			fprintf(config_file, "%s = %s\n", "proxy_type", "socks4"); -		else if(proxy_type == CURLPROXY_SOCKS5) +		else if(options->proxy.type == CURLPROXY_SOCKS5)  			fprintf(config_file, "%s = %s\n", "proxy_type", "socks5");  	} -	if(proxy_addr[0]) -		fprintf(config_file, "%s = %s\n", "proxy_address", proxy_addr); -	if(proxy_port) -		fprintf(config_file, "%s = %i\n", "proxy_port", proxy_port); -	if(proxy_auth) +	if(options->proxy.address[0]) +		fprintf(config_file, "%s = %s\n", "proxy_address", options->proxy.address); +	if(options->proxy.port) +		fprintf(config_file, "%s = %i\n", "proxy_port", options->proxy.port); +	if(options->proxy.auth)  	{ -		if(proxy_auth = CURLAUTH_BASIC) +		if(options->proxy.auth = CURLAUTH_BASIC)  			fprintf(config_file, "%s = %s\n", "proxy_auth", "basic"); -		else if(proxy_auth = CURLAUTH_DIGEST) +		else if(options->proxy.auth = CURLAUTH_DIGEST)  			fprintf(config_file, "%s = %s\n", "proxy_auth", "digest"); -		else if(proxy_auth = CURLAUTH_NTLM) +		else if(options->proxy.auth = CURLAUTH_NTLM)  			fprintf(config_file, "%s = %s\n", "proxy_auth", "ntlm");  	} -	if(proxy_user[0]) -		fprintf(config_file, "%s = %s\n", "proxy_user", proxy_user); -	if(proxy_pass[0]) -		fprintf(config_file, "%s = %s\n", "proxy_pass", proxy_pass); +	if(options->proxy.user[0]) +		fprintf(config_file, "%s = %s\n", "proxy_user", options->proxy.user); +	if(options->proxy.pass[0]) +		fprintf(config_file, "%s = %s\n", "proxy_pass", options->proxy.pass);  	return 0;  } -int updateconfig(char * htde_path, char * save_path, int * update) +int updateconfig(char * getht_path, struct config * options)  /*	Read existing config file, and rewrite any variables which differ   *	in memory. */  {  	FILE * config_file;  	char filepath[STR_MAX]; -	snprintf(filepath, STR_MAX, "%s/config.ini", htde_path); +	snprintf(filepath, STR_MAX, "%s/config.ini", getht_path);  	if((config_file = fopen(filepath,"rw")) == NULL)  	{ diff --git a/src/download.c b/src/download.c index b8fd086..b287655 100644 --- a/src/download.c +++ b/src/download.c @@ -35,24 +35,13 @@ int write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)  int update_progress(void *data, double dltotal, double dlnow,  					double ultotal, double ulnow); -extern int proxy_type; -extern char proxy_addr[STR_MAX]; -extern long proxy_port; -extern int proxy_auth; -extern char proxy_user[STR_MAX]; -extern char proxy_pass[STR_MAX]; -extern CURL *main_curl_handle; - -int save_file(CURL *curl_handle, char *uri, char *filepath, char *filetitle, long resume_offset) +int save_file(char *uri, char *filepath, char *filetitle, long resume_offset, struct config * options)  /*	Save the file *uri to *filepath */  {  	printf("Downloading %s       ",filetitle);  	fflush(stdout); -	if(!curl_handle) -		curl_handle = main_curl_handle; - -	if(curl_handle) { +	if(options->curl_handle) {  		FILE *file;  		file = fopen(filepath, resume_offset?"a":"w");  		if(!file) @@ -61,38 +50,38 @@ int save_file(CURL *curl_handle, char *uri, char *filepath, char *filetitle, lon  			return 1;  		} -		curl_easy_setopt(curl_handle, CURLOPT_URL, uri); -		curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, read_func); -		curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_func); -		curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, file); +		curl_easy_setopt(options->curl_handle, CURLOPT_URL, uri); +		curl_easy_setopt(options->curl_handle, CURLOPT_READFUNCTION, read_func); +		curl_easy_setopt(options->curl_handle, CURLOPT_WRITEFUNCTION, write_func); +		curl_easy_setopt(options->curl_handle, CURLOPT_WRITEDATA, file); -		if(proxy_type) +		if(options->proxy.type)  		{ -			curl_easy_setopt(curl_handle, CURLOPT_PROXYTYPE, proxy_type); -			curl_easy_setopt(curl_handle, CURLOPT_PROXY, proxy_addr); -			if(proxy_port) -				curl_easy_setopt(curl_handle, CURLOPT_PROXYPORT, proxy_port); -			if(proxy_auth) -				curl_easy_setopt(curl_handle, CURLOPT_PROXYAUTH, proxy_auth); -			if(proxy_user[0] && proxy_pass[0]) +			curl_easy_setopt(options->curl_handle, CURLOPT_PROXYTYPE, options->proxy.type); +			curl_easy_setopt(options->curl_handle, CURLOPT_PROXY, options->proxy.address); +			if(options->proxy.port) +				curl_easy_setopt(options->curl_handle, CURLOPT_PROXYPORT, options->proxy.port); +			if(options->proxy.auth) +				curl_easy_setopt(options->curl_handle, CURLOPT_PROXYAUTH, options->proxy.auth); +			if(options->proxy.user[0] && options->proxy.pass[0])  			{  				char userpass[STR_MAX]; -				snprintf(userpass, STR_MAX, "%s:%s", proxy_user, proxy_pass); -				curl_easy_setopt(curl_handle, CURLOPT_PROXYUSERPWD, userpass); +				snprintf(userpass, STR_MAX, "%s:%s", options->proxy.user, options->proxy.pass); +				curl_easy_setopt(options->curl_handle, CURLOPT_PROXYUSERPWD, userpass);  			}  		} -		curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 0); -		curl_easy_setopt(curl_handle, CURLOPT_PROGRESSFUNCTION, update_progress); -		curl_easy_setopt(curl_handle, CURLOPT_PROGRESSDATA, &resume_offset); +		curl_easy_setopt(options->curl_handle, CURLOPT_NOPROGRESS, 0); +		curl_easy_setopt(options->curl_handle, CURLOPT_PROGRESSFUNCTION, update_progress); +		curl_easy_setopt(options->curl_handle, CURLOPT_PROGRESSDATA, &resume_offset); -		curl_easy_setopt(curl_handle, CURLOPT_RESUME_FROM, resume_offset); +		curl_easy_setopt(options->curl_handle, CURLOPT_RESUME_FROM, resume_offset);  		/* create a buffer to hold any curl errors */  		char errorinfo[CURL_ERROR_SIZE]; -		curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, errorinfo); +		curl_easy_setopt(options->curl_handle, CURLOPT_ERRORBUFFER, errorinfo); -		if(curl_easy_perform(curl_handle)) +		if(curl_easy_perform(options->curl_handle))  		{  			remove(filepath);  			fprintf(stderr,"\nError, could not download %s: %s\n",uri, errorinfo); @@ -133,54 +122,51 @@ int update_progress(void *data, double dltotal, double dlnow,  	return 0;  } -double getremotefilesize(CURL *curl_handle, char *uri) +double getremotefilesize(char *uri, struct config * options)  {  	double filesize; -	if(!curl_handle) -		curl_handle = main_curl_handle; - -	if(curl_handle) { +	if(options->curl_handle) { -		curl_easy_setopt(curl_handle, CURLOPT_URL, uri); -		curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, read_func); +		curl_easy_setopt(options->curl_handle, CURLOPT_URL, uri); +		curl_easy_setopt(options->curl_handle, CURLOPT_READFUNCTION, read_func);  		/* don't download or return either body or header */ -		curl_easy_setopt(curl_handle, CURLOPT_NOBODY, 1); -		curl_easy_setopt(curl_handle, CURLOPT_HEADER, 0); +		curl_easy_setopt(options->curl_handle, CURLOPT_NOBODY, 1); +		curl_easy_setopt(options->curl_handle, CURLOPT_HEADER, 0); -		if(proxy_type) +		if(options->proxy.type)  		{ -			curl_easy_setopt(curl_handle, CURLOPT_PROXYTYPE, proxy_type); -			curl_easy_setopt(curl_handle, CURLOPT_PROXY, proxy_addr); -			if(proxy_port) -				curl_easy_setopt(curl_handle, CURLOPT_PROXYPORT, proxy_port); -			if(proxy_auth) -				curl_easy_setopt(curl_handle, CURLOPT_PROXYAUTH, proxy_auth); -			if(proxy_user[0] && proxy_pass[0]) +			curl_easy_setopt(options->curl_handle, CURLOPT_PROXYTYPE, options->proxy.type); +			curl_easy_setopt(options->curl_handle, CURLOPT_PROXY, options->proxy.address); +			if(options->proxy.port) +				curl_easy_setopt(options->curl_handle, CURLOPT_PROXYPORT, options->proxy.port); +			if(options->proxy.auth) +				curl_easy_setopt(options->curl_handle, CURLOPT_PROXYAUTH, options->proxy.auth); +			if(options->proxy.user[0] && options->proxy.pass[0])  			{  				char userpass[STR_MAX]; -				snprintf(userpass, STR_MAX, "%s:%s", proxy_user, proxy_pass); -				curl_easy_setopt(curl_handle, CURLOPT_PROXYUSERPWD, userpass); +				snprintf(userpass, STR_MAX, "%s:%s", options->proxy.user, options->proxy.pass); +				curl_easy_setopt(options->curl_handle, CURLOPT_PROXYUSERPWD, userpass);  			}  		} -		curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1); +		curl_easy_setopt(options->curl_handle, CURLOPT_NOPROGRESS, 1); -		if(curl_easy_perform(curl_handle)) +		if(curl_easy_perform(options->curl_handle))  			filesize = -1; -		curl_easy_getinfo(curl_handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &filesize); +		curl_easy_getinfo(options->curl_handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &filesize);  	}  	else  		filesize = -1; -	curl_easy_reset(curl_handle); +	curl_easy_reset(options->curl_handle);  	return filesize;  } -void downloadissue(CURL *curl_handle, char * directory, iss * issue, int force) +void downloadissue(struct config * options, iss * issue, int force)  /*	Download issue pointed to */  {  	sec * cur_section; @@ -188,7 +174,7 @@ void downloadissue(CURL *curl_handle, char * directory, iss * issue, int force)  	char filename[STR_MAX];  	char filepath[STR_MAX]; -	snprintf(newdir,STR_MAX,"%s/%i_%i-%i",directory, +	snprintf(newdir,STR_MAX,"%s/%i_%i-%i",options->save_path,  		issue->date.year,issue->date.firstmonth,issue->date.lastmonth);  	printf("Downloading %s to %s\n",issue->title, newdir); @@ -211,7 +197,7 @@ void downloadissue(CURL *curl_handle, char * directory, iss * issue, int force)  			struct stat fileinfo;  			/* see if local file exists */  			if(stat(filepath, &fileinfo)) -				save_file(curl_handle, cur_section->uri, filepath, filename, 0); +				save_file(cur_section->uri, filepath, filename, 0, options);  			else  			{  				/* get size of local file */ @@ -220,16 +206,16 @@ void downloadissue(CURL *curl_handle, char * directory, iss * issue, int force)  				/* get size of remote file */  				long remotesize = 0; -				remotesize = (long) getremotefilesize(curl_handle, cur_section->uri); +				remotesize = (long) getremotefilesize(cur_section->uri, options);  				/* if size of local file != size of remote file, resume */  				if(remotesize > 0 && localsize < remotesize) -					save_file(curl_handle, cur_section->uri, filepath, filename, localsize); +					save_file(cur_section->uri, filepath, filename, localsize, options);  				else  					printf("Skipping download of completed section %i\n", cur_section->number);  			}  		}  		else -			save_file(curl_handle, cur_section->uri, filepath, filename, 0); +			save_file(cur_section->uri, filepath, filename, 0, options);  	}  } diff --git a/src/getht.c b/src/getht.c index e1aa847..ec90f0b 100644 --- a/src/getht.c +++ b/src/getht.c @@ -30,19 +30,22 @@  #include "issue.h"  #include "getht.h" -int update_contents_files(); - -int proxy_type; char proxy_addr[STR_MAX]; long proxy_port; -int proxy_auth;  -char proxy_user[STR_MAX]; char proxy_pass[STR_MAX]; -char toc_xml[STR_MAX]; -char issue_uri[STR_MAX]; -CURL *main_curl_handle; +int update_contents_files(struct config * options);  int main(int argc, char *argv[])  { +	struct config options;  	char getht_path[STR_MAX]; -	char save_path[STR_MAX]; + +	/* initialise appropriate options vars */ +	options.startup_check = 0; +	options.verbose = 0; +	options.proxy.type = 0; +	options.proxy.auth = 0; +	options.proxy.address[0] = '\0'; +	options.proxy.port = 0; +	options.proxy.user[0] = '\0'; +	options.proxy.pass[0] = '\0';  	/* Define & set up paths */  	snprintf(getht_path,STR_MAX,"%s/.getht",getenv("HOME")); @@ -55,36 +58,29 @@ int main(int argc, char *argv[])  			scanf("%s", getht_path);  		} -	snprintf(toc_xml,STR_MAX,"%s/%s",getht_path,ISS_XML_FILE); +	snprintf(options.toc_xml,STR_MAX,"%s/%s",getht_path,ISS_XML_FILE); -	strncpy(issue_uri,XML_TOC_URI,STR_MAX); +	strncpy(options.issue_uri,XML_TOC_URI,STR_MAX); -	snprintf(save_path,STR_MAX,"%s/hinduism_today",getenv("HOME")); +	snprintf(options.save_path,STR_MAX,"%s/hinduism_today",getenv("HOME"));  	int downall = 0;  	int downissue = 0, downissueno = -1;  	int listissues = 0; -	int force = 0, update = 0; -	int verbose = 0, option = 0; - -	proxy_type = 0; -	proxy_port = 0; -	proxy_addr[0] = '\0'; -	proxy_auth = 0; -	proxy_user[0] = '\0'; -	proxy_pass[0] = '\0'; +	int force = 0; +	int option = 0; -	if(loadconfig(getht_path, &save_path, &update) != 0) -		writefreshconfig(getht_path, &save_path, &update, &issue_uri); +	if(loadconfig(getht_path, &options) != 0) +		writefreshconfig(getht_path, &options); -	if(!opendir(save_path)) -		if(mkdir(save_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) +	if(!opendir(options.save_path)) +		if(mkdir(options.save_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))  		{ -			fprintf(stderr,"Cannot open/create directory %s",save_path); +			fprintf(stderr,"Cannot open/create directory %s",options.save_path);  			printf("Please enter the path of a directory to save issues in: "); -			scanf("%s", save_path); +			scanf("%s", options.save_path); -			updateconfig(getht_path, &save_path, NULL); +			updateconfig(getht_path, &options);  		}  	/* Parse command line options */ @@ -122,7 +118,7 @@ int main(int argc, char *argv[])  				option = 1;  				break;  			case 'u': -				update = 1; +				options.startup_check = 1;  				option = 1;  				break;  			case 'h': @@ -130,7 +126,7 @@ int main(int argc, char *argv[])  				return 0;  				break;  			case 'v': -				verbose++; +				options.verbose++;  				option = 1;  				break;  			case 'V': @@ -147,11 +143,11 @@ int main(int argc, char *argv[])  		return 0;  	} -	main_curl_handle = curl_easy_init(); +	options.curl_handle = curl_easy_init(); -	if(update) +	if(options.startup_check)  	{ -		if(update_contents_files()) +		if(update_contents_files(&options))  			fprintf(stderr,"Could not update contents files\n");  	} @@ -163,16 +159,16 @@ int main(int argc, char *argv[])  	if(downissue || listissues)  	{ -		issue = parsetoc(toc_xml, &no_of_issues); +		issue = parsetoc(options.toc_xml, &no_of_issues);  		if(!issue)  		{ -			if(!update) +			if(!options.startup_check)  			{  				printf("Cannot open contents file, trying to update contents\n"); -				if(update_contents_files()) +				if(update_contents_files(&options))  					return 1; -				issue = parsetoc(toc_xml, &no_of_issues); +				issue = parsetoc(options.toc_xml, &no_of_issues);  			}  			else  			{ @@ -184,26 +180,26 @@ int main(int argc, char *argv[])  		if(downall)  		{  			for(i = 0; i < no_of_issues; i++) -				downloadissue(NULL, save_path, issue[i], force); +				downloadissue(&options, issue[i], force);  		}  		else if(downissueno >= 0 && downissueno <= no_of_issues) -			downloadissue(NULL, save_path, issue[downissueno], force); +			downloadissue(&options, issue[downissueno], force);  	}  	if(listissues) -		list_issues(issue, no_of_issues, verbose); +		list_issues(issue, no_of_issues, options.verbose);  	/* Ensure curl cleans itself up */ -	curl_easy_cleanup(main_curl_handle); +	curl_easy_cleanup(options.curl_handle);  	return 0;  } -int update_contents_files() +int update_contents_files(struct config * options)  /* Returns 0 on success, 1 on failure */  { -	if(save_file(NULL, issue_uri, toc_xml, "contents", 0)) +	if(save_file(options->issue_uri, options->toc_xml, "contents", 0, options))  		return 1;  	else  		return 0; diff --git a/src/getht.h b/src/getht.h index f2315d4..4ec0f4a 100644 --- a/src/getht.h +++ b/src/getht.h @@ -26,20 +26,26 @@  #include <curl/curl.h> -struct config { -	char * proxytype; -	char * proxyauth; -	char proxy_addr[STR_MAX]; -	long proxy_port; -	char proxy_user[STR_MAX]; -	char proxy_pass[STR_MAX]; +struct proxy_options { +	char type; +	char auth; +	char address[STR_MAX]; +	long port; +	char user[STR_MAX]; +	char pass[STR_MAX]; +}; +struct config {  	char toc_xml[STR_MAX];  	char issue_uri[STR_MAX];  	char save_path[STR_MAX]; +	struct proxy_options proxy; +  	int startup_check; +	int verbose; +  	CURL *curl_handle;  };  | 
