/* name: homes.c purpose: example use of Chunks of HTML from C author: george j. carrette gjc@delphi.com */ #include #include #include #include #include #ifndef WIN32 #include #include #endif #include "chtml.h" static char *homes_id = "$Id: homes.c,v 1.14 1998/05/20 19:55:11 gjc Exp $"; static void failure(char *msg) {printf("Status: 500 Server Error (Application)\n"); printf("Content-type: text/plain\n\n"); printf("%s\n",msg);} CHTML_STRITEM table = NULL; CHTML_STRITEM base_table = NULL; #ifndef WIN32 void get_homes_loop(void) {struct passwd *u; char tmp[1024]; if (!gethostname(tmp,sizeof(tmp))) chtml_stritem_kinsert(&table,"sitename",tmp,NULL); setpwent(); while((u = getpwent())) {sprintf(tmp,"%s/public_html",u->pw_dir); if (access(tmp,R_OK) == 0) {chtml_stritem_kinsert(&table,"username",u->pw_name,NULL); chtml_stritem_kinsert(&table, "fullname", u->pw_gecos, strchr(u->pw_gecos,','));}} endpwent();} #endif #ifdef WIN32 void get_homes_loop(void) {chtml_stritem_kinsert(&table,"username","gjc",NULL); chtml_stritem_kinsert(&table,"fullname","george c",NULL); chtml_stritem_kinsert(&table,"username","sam",NULL); chtml_stritem_kinsert(&table,"fullname","sam iam",NULL);} #endif void get_homes(void) {char tmp[1024]; get_homes_loop(); sprintf(tmp,"%d",chtml_stritem_len(chtml_stritem_getem("username",table))); chtml_stritem_kinsert(&table,"usercount",tmp,NULL); sprintf(tmp,"%.3f",((double)clock()) / ((double)CLOCKS_PER_SEC)); chtml_stritem_kinsert(&table,"querytime",tmp,NULL);} int main(int argc,char **argv) {struct chtml *form; int len; if (!(form = chtml_load("homes.html-bin",NULL))) {failure("Could not load application template file"); return(EXIT_FAILURE);} chtml_stritem_kinsert(&base_table,"SRC",homes_id,NULL); chtml_stritem_copy(&table,base_table,NULL); chtml_stritem_copy(&table,base_table,"EXTRA:"); get_homes(); if ((getenv("HTTP_DEBUG") && atol(getenv("HTTP_DEBUG"))) || (getenv("QUERY_STRING") && (strcmp(getenv("QUERY_STRING"),"debug") == 0))) {printf("Content-type: text/plain\n\n"); chtml_stritem_debug_print(table);} len = chtml_size_write((char *(*)(char *,void *)) chtml_stritem_eval, table, form); if (len < 0) failure("Could not format result screen"); else {chtml_stritem_rewind(table); printf("Content-type: text/html\nContent-length: %d\n\n",len); chtml_do_write((char *(*)(char *,void *)) chtml_stritem_eval, table, form, (void (*)(char *,void *))fputs, stdout);} chtml_stritem_free(&table); chtml_stritem_free(&base_table); chtml_free(form); return((len >= 0) ? EXIT_SUCCESS : EXIT_FAILURE);}