/* 
 * File 	- linkcounter.c
 * Date 	- June 2003
 * Author 	- Jay Scott
 * Email 	- jay@jay-scott.co.uk
 *
 *
 * Description
 * ------------
 * 
 * Link Counter is a C script that will take a HTML file as a 
 * command line argument and read every <a href> tag on the 
 * page and then display how many links were found. I wrote
 * this for a project I was working on, kind of useless :)
 *
 * Example
 * --------
 *
 * gcc linkcounter.c -o linkcounter
 *
 * ./linkcounter index.html
 *
 */


#include <stdio.h>												/* Include files */
#include <string.h>
#include <stdlib.h>

#define RED			"\E[31m\E[1m"								/* Define some colours */
#define WHITE		"\E[m\E[1m"
#define DEFAULT		"\E[m"
		
int readFile(char *fname);										/* Local functions */

int count = 0;													/* Global vars */

int main(int argc, char **argv) 								/* Main function */ 
{ 								
	char *HTMLFile;
	 
	system("clear");											/* Clear the screen */
    if (argc != 2) {											/* Check amount of args */
		printf("Please supply a file name");
		exit(1);
	}	
	
	HTMLFile = argv[1];	
	printf("%sSearching %s ",WHITE,HTMLFile);
	readFile(HTMLFile);											/* Call function */
	
	printf("\n%s%d%s links %s\n\n",RED,count,WHITE,DEFAULT);	/* Print results */
	return 0 ;
}
 

int readFile(char *fname)
{
	FILE *rHTML;
	char tmpChar;
	int marked = 0;
	
	if ((rHTML = fopen(fname,"r")) == NULL) { 					/* Open file for reading */
		printf("Error will opening HTML file\n");
		exit(1);
	}
	
	tmpChar = getc(rHTML);										/* Get first Char */
	
	while(tmpChar != EOF) {										/* While not end-of-file */
		if (tmpChar == '<' ) {									/* Possible link */
			tmpChar = getc(rHTML);								/* Next char */
			if (tmpChar == 'a')									/* Another check */									
				++count;										/* Count + 1 */	
		}
		tmpChar = getc(rHTML);									/* Next */
	}
	
	fclose(rHTML);												/* Close file */
	return 0;	
}
