Here's the C version. I compiled this with Cygwin, gcc encapsulator.c -mno-cygwin. You want to #define ROOT to be where you keep the top/bottom files for the site-wide default (used if a directory doesn't have a top/bottom file in it). #include #include // need trailing '/' so filename removal code below works right #define ROOT "d:/public/www.sfwa.org/style/" #define PATHSIZE 10240 #define TRUE 1 #define FALSE 0 void error(const char *msg) { printf("%s", msg); exit(0); } int cat(const char *dir, const char *file) { char path[PATHSIZE]; int c; FILE *fp; if (strlen(dir)+strlen(file) > sizeof(path)) error("Path is too long"); if (strlen(dir) == 0) strcpy(path, file); else sprintf(path, "%s/%s", dir, file); if ((fp = fopen(path, "rb")) == NULL) return(FALSE); while ((c = getc(fp)) != EOF) putchar(c); fclose(fp); return TRUE; } main(int argc, char **argv) { char *p, *ht_file, curdir[PATHSIZE]; printf("Content-type: text/html\n\n"); ht_file = getenv("PATH_TRANSLATED"); if (!ht_file) error("Can't find path to file"); if (strlen(ht_file) >= sizeof(curdir)) error("Path too long"); strcpy(curdir, ht_file ? ht_file : ROOT); if ((p=strrchr(curdir, '/')) || (p=strrchr(curdir, '\\'))) *p = '\0'; // just dir part if (!cat(curdir, "top")) cat(ROOT, "top"); cat("", ht_file); if (!cat(curdir, "bottom")) cat(ROOT, "bottom"); exit(0); }
gcc encapsulator.c -mno-cygwin
Also see: (Otherwise known as Aburt's spam-proof email.)