#include #include #include #include #include #include "common.c" /* Feed info. */ struct feed { char *name; /* feed name */ unsigned long new; /* amount of new items per feed */ unsigned long total; /* total items */ struct feed *next; /* linked list */ }; static int showsidebar = 1; /* show sidebar ? */ void /* print error message to stderr */ die(const char *s) { fputs("sfeed_html: ", stderr); fputs(s, stderr); fputc('\n', stderr); exit(EXIT_FAILURE); } struct feed * feednew(void) { struct feed *f; if(!(f = calloc(1, sizeof(struct feed)))) die("can't allocate enough memory"); return f; } void feedsfree(struct feed *f) { struct feed *next; while(f) { next = f->next; free(f->name); free(f); f = next; } } /* print feed name for id; spaces and tabs in string as "-" (spaces in anchors are not valid). */ void printfeednameid(const char *s) { for(; *s; s++) putchar(isspace(*s) ? '-' : *s); } void printhtmlencoded(const char *s) { for(; *s; s++) { switch(*s) { case '<': fputs("<", stdout); break; case '>': fputs(">", stdout); break; case '&': fputs("&", stdout); break; default: putchar(*s); } } } int main(void) { char *line = NULL, *fields[FieldLast]; unsigned long totalfeeds = 0, totalnew = 0; unsigned int islink, isnew; struct feed *feedcurrent = NULL, *feeds = NULL; /* start of feeds linked-list. */ time_t parsedtime, comparetime; size_t size = 0; tzset(); comparetime = time(NULL) - (3600 * 24); /* 1 day is old news */ fputs( "\n" "\n" " \n" " \n" " \n" " \n" " \n" "
\n", stdout); while(parseline(&line, &size, fields, FieldLast, stdin, FieldSeparator) > 0) { /* first of feed section or new feed section. */ if(!totalfeeds || strcmp(feedcurrent->name, fields[FieldFeedName])) { if(totalfeeds) { /* end previous one. */ fputs("\n", stdout); feedcurrent->next = feednew(); feedcurrent = feedcurrent->next; } else { feedcurrent = feednew(); feeds = feedcurrent; /* first item. */ fputs("\t\t
\n", stdout); } if(!(feedcurrent->name = strdup(fields[FieldFeedName]))) die("can't allocate enough memory"); if(fields[FieldFeedName][0] != '\0') { fputs("

name); fputs("\">name); fputs("\">", stdout); fputs(feedcurrent->name, stdout); fputs("

\n", stdout); } fputs("", stdout); totalfeeds++; } parsedtime = (time_t)strtol(fields[FieldUnixTimestamp], NULL, 10); isnew = (parsedtime >= comparetime); islink = (strlen(fields[FieldLink]) > 0); totalnew += isnew; feedcurrent->new += isnew; feedcurrent->total++; fputs("\n", stdout); } if(totalfeeds) { fputs("
", stdout); printtime(parsedtime); fputs("", stdout); if(isnew) fputs("", stdout); if(islink) { fputs("", stdout); } printhtmlencoded(fields[FieldTitle]); if(islink) fputs("", stdout); if(isnew) fputs("", stdout); fputs("
\n", stdout); fputs("\t\t
\n", stdout); /* div items */ } if(showsidebar) { fputs("\t\t
\n\t\t\t\n\t\t
\n", stdout); } fputs( "
\n" " \n" " Newsfeeds (", stdout); fprintf(stdout, "%lu", totalnew); fputs(")\n", stdout); free(line); /* free line */ feedsfree(feeds); /* free feeds linked-list */ return EXIT_SUCCESS; }