#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "socklib.h"
#include "pullers.h"

int main(void) {
	FILE *fp;
	char rx[256];
	tArticle a;
	FILE *netin, *netout;

	if(!fpopensocket("www.slashdot.org",80,30,&netin,&netout)) {
		fprintf(stderr,"Error on connect.\n");
		exit(1);
	}

	fprintf(netout,"GET /ultramode.txt\r\n");
	fflush(netout);

	fp=fopen(SLASHDATA,"wb");

	/* skip to first record.. %% */
	while(fgets_strip(rx,256,netin)) {
		if(!strcmp(rx,"%%")) break;
	}

	/* now read the records */
	while(fgets_strip(rx,256,netin)) {
		strncpy(a.title,rx,255);
		fgets_strip(a.link,255,netin);
		fgets_strip(a.time,255,netin);
		fgets_strip(a.author,255,netin);
		fgets_strip(a.dept,255,netin);
		fgets_strip(a.topic,255,netin);
		fgets_strip(rx,255,netin);
		a.numcomments=atoi(rx);
		fgets_strip(a.storytype,255,netin);
		fgets_strip(a.imagename,255,netin);
		fgets(rx,255,netin); /* terminator */
		fwrite(&a,sizeof(a),1,fp);
	}

	fclose(netin); fclose(netout);

	fclose(fp);

	return(0);
}
