Exemple #1
0
int wget_close_verbose(struct wgetfile *finfo, FILE *f, int verbose)
{
	int ret = 0;

	if(f){
		long fpos = ftell(f);

		if(fpos == -1)
			fpos = 0;

		if(f != stdout && fclose(f)){
			if(verbose)
				output_perror("close()");
			ret = 1;
		}

		if(ret && !fpos)
			remove(finfo->outname);
			/*
			 * Got a problem and didn't write to the file,
			 * otherwise
			 * leave it be for a -c operation, otherwise unlink it
			 */
	}

	return ret;
}
Exemple #2
0
void dempa_shinbunsha_joystick_device::update_perror()
{
	int perror = (~m_data & ~m_lptjoy->read() & 0x3f) == 0;

	if (m_perror != perror)
	{
		m_perror = perror;

		output_perror(perror);
	}
}
Exemple #3
0
int generic_transfer(struct wgetfile *finfo, FILE *out, size_t len,
		size_t sofar, int closefd)
{
#define RET(n) do{ ret = n; goto fin; }while(0)
	int ret = 0;
	long last_progress, last_speed_calc;
	long chunk = 0; /* for bps */
	long speed = 0;

	last_progress = last_speed_calc = mstime();
	if(!len)
		progress_unknown(sofar, 0);

	do{
		ssize_t nread;
		char buffer[BSIZ];
		long t;

		switch((nread = recv(finfo->sock, buffer, sizeof buffer, 0))){
			case -1:
				if(errno == EINTR)
					continue;

				/* TODO: retry */
				if(len && sofar == len)
					goto end_of_stream;
				connection_close_fd(finfo->sock);
				output_perror("recv()");
				RET(1);

			case 0:
end_of_stream:
				connection_close_fd(finfo->sock);

				if(len){
					if(sofar == len){
						RET(0);
					}else{
						/* TODO: goto retry */
						progress_incomplete();
						RET(1);
					}
				}else{
					/* no length, assume we have the whole file */
					RET(0);
				}
				/* unreachable */

			default:
			{
				int trunc = 0;

				if(len && sofar + nread > len){
					output_err(OUT_WARN, "too much data, truncating by %zu bytes", sofar + nread - len);
					trunc = 1;
					nread = len - sofar;
					sofar = len;
				}else{
					sofar += nread;
				}

				while(fwrite(buffer, sizeof(buffer[0]), nread, out) != (unsigned)nread){
					if(errno == EINTR)
						continue;
					output_perror("fwrite()");
					RET(1);
				}

				if(sofar == len)
					RET(0); /* don't wait for more, maybe be pipelining */

				chunk += nread;

				if(trunc)
					goto end_of_stream;
			}
		}

		t = mstime();
		if(last_progress + 250 < t){
			if(last_speed_calc + 1000 < t){
				long tdiff = t - last_speed_calc;

				if(tdiff){
					speed = 1000.0f /* kbps */ * (float)chunk / (float)tdiff;
					chunk = 0;
					last_speed_calc = t;
				}
			}

			last_progress = t;
			if(len)
				progress_show(sofar, len, speed);
			else
				progress_unknown(sofar, speed);
		}
	}while(1);

fin:
	if(len)
		progress_fin(sofar, len);
	else
		progress_fin(0, 0);

	if(closefd) {
		ret |= wget_close(finfo, out);
		if(ret)
			wget_failure(finfo);
		else
			wget_success(finfo);
	}

	return ret;
}
Exemple #4
0
int cookies_load(const char *fname)
{
#define MAL_LINE(s) \
	{ \
		/*fputs(s "\n", stderr); */ \
		continue; \
	}

	FILE *f = fopen(fname, "r");
	char buffer[256];
	struct cookie **cur_cookie = &cookies;

	if(!f){
		output_err(OUT_WARN, "Couldn't load cookie file \"%s\"", fname);
		return 1;
	}

	while(fgets(buffer, sizeof buffer, f)){
		struct cookie *c;
		char *iter;
		int i;

		if((iter = strchr(buffer, '\n')))
			*iter = '\0';

		iter = strchr(buffer, '\t');
		if(iter){
			char *host, *nam, *val;

			char *prev;
			*iter = '\0';
			host = buffer;
			if(!*host)
				MAL_LINE("Empty host");

			for(i = 0; i < 5 && iter; i++){
				prev = iter + 1;
				iter = strchr(prev, '\t');
			}

			if(iter){
				nam = prev;

				iter = strchr(nam, '\t');

				if(iter){
					/* val */
					*iter = '\0';
					val = iter + 1;

					NEW_COOKIE(*cur_cookie);
					c = *cur_cookie;
					c->host = xstrdup(host);
					c->nam = xstrdup(nam);
					c->val = xstrdup(val);
				}else{
					MAL_LINE("No value");
				}
			}else{
				MAL_LINE("No name");
			}
		}else{
			MAL_LINE("No host");
		}

		cur_cookie = &c->next;
	}

	if(ferror(f)){
		output_perror("read()");
		goto bail;
	}

	fclose(f);
	return 0;

bail:
	fclose(f);
	cookies_free(cookies, 1);
	return 1;
}