123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <dirent.h>
- #include <time.h>
- static char* skip_whitespace(char *s)
- {
- while (*s == ' ' || *s == '\t') ++s;
- return s;
- }
- static char line[64 * 1024];
- static void process_includes(const char *filename)
- {
- int curdir_fd;
- char *end;
- FILE *fp = fopen(filename, "r");
- if (!fp)
- exit(1);
-
- curdir_fd = -1;
- end = strrchr(filename, '/');
- if (end) {
- curdir_fd = open(".", O_RDONLY);
-
- end[1] = '\0';
- chdir(filename);
- }
- #define INCLUDE "<!--#include"
- while (fgets(line, sizeof(line), fp)) {
- unsigned preceding_len;
- char *include_directive;
- include_directive = strstr(line, INCLUDE);
- if (!include_directive) {
- fputs(line, stdout);
- continue;
- }
- preceding_len = include_directive - line;
- if (memchr(line, '\"', preceding_len)
- || memchr(line, '\'', preceding_len)
- ) {
-
- fputs(line, stdout);
- continue;
- }
-
- include_directive = skip_whitespace(include_directive + sizeof(INCLUDE)-1);
- if (strncmp(include_directive, "file=\"", 6) != 0) {
-
- fputs(line, stdout);
- continue;
- }
- include_directive += 6;
- end = strchr(include_directive, '\"');
- if (!end) {
- fputs(line, stdout);
- continue;
- }
-
-
- if (preceding_len) {
- line[preceding_len] = '\0';
- fputs(line, stdout);
- }
-
- *end++ = '\0';
- end = strchr(end, '>');
- if (end)
- end = strdup(end + 1);
-
- process_includes(include_directive);
-
- if (end) {
- fputs(end, stdout);
- free(end);
- }
- }
- if (curdir_fd >= 0)
- fchdir(curdir_fd);
- fclose(fp);
- }
- int main(int argc, char *argv[])
- {
- if (!argv[1])
- return 1;
-
- fputs(
-
-
-
- "Connection: close\r\n"
- "Content-Type: text/html\r\n"
- "\r\n",
- stdout
- );
- process_includes(argv[1]);
- return 0;
- }
|