generated html version of listall.cHOME

#include "dirent.h"
#include "stdio.h"
#include "stdlib.h"
#include "sys/stat.h"
#define then

int isdir(const char * path) {
	struct stat statbuf;
	if(!stat(path,&statbuf)){
		return S_ISDIR(statbuf.st_mode);
	}
	return 0;
}

int indent = 0;
void listindir(const char *path){
	DIR * d = opendir(path);
	struct dirent *de;
	int i;
	while(de = readdir(d)){
		if(!strcmp(de->d_name,"."))continue;
		if(!strcmp(de->d_name,".."))continue;
		char *newpath = malloc(strlen(path) + strlen(de->d_name) + 2);
		strcpy(newpath,path);
		strcat(newpath,"/");
		strcat(newpath,de->d_name);
		if(isdir(newpath)){
			fputs("<h2>",stdout);
			for(i=0;i<indent;i++)fputs("░",stdout);
			fputs(newpath+14,stdout);
			fputs("</h2>\n",stdout);
			indent++;
			listindir(newpath);
			indent--;
		}else if(
			!strcmp(newpath + strlen(newpath) - 2,".c")
			|| !strcmp(newpath + strlen(newpath) - 3,".pl")
			|| !strcmp(newpath + strlen(newpath) - 2,".h")
		) then {
			fputs("<p>",stdout);
			for(i=0;i<indent;i++)fputs("░",stdout);
			fputs("<a href=\"",stdout);
			fputs(newpath+14,stdout);
			fputs(".htm\">",stdout);
			fputs(newpath+14,stdout);
			fputs("</a></p>\n",stdout);
		}else if(
			!strcmp(newpath + strlen(newpath) - 4,".txt")
		) then {
			fputs("<p>",stdout);
			for(i=0;i<indent;i++)fputs("░",stdout);
			fputs("<a href=\"",stdout);
			newpath[strlen(newpath)-4]=0;
			fputs(newpath+14,stdout);
			fputs(".htm\">",stdout);
			fputs(newpath+14,stdout);
			fputs(".txt</a></p>\n",stdout);
		} else {
			fputs("<p>",stdout);
			for(i=0;i<indent;i++)fputs("░",stdout);
			fputs("<a href=\"",stdout);
			fputs(newpath+14,stdout);
			fputs("\">",stdout);
			fputs(newpath+14,stdout);
			fputs("</a></p>\n",stdout);
		}
		free(newpath);
	}
}

int main(){
	fputs(
		"Content-type: text/html\r\n\r\n"
		"<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\" />\n"
		"<title>all files - www.orenwatson.be</title>\n"
		"<link rel=\"stylesheet\" href=\"my.css\">\n"
		"<h1>A list of all the files on my site. <a class=\"right\" href=\"/\">HOME</a></h1>\n"
		"<p>Warning, most of this is useless.</p>\n"
	,stdout);
	listindir("/var/www/html");

}