/* * ------------------------------------------------------------------- * "THE BEER-WARE LICENSE" (Revision 42 - bzerk) * wrote this file. As long as you retain this notice * you can do whatever you want with this stuff. If we meet some day, * and you think this stuff is worth it, you can buy me a beer in * return. Ruben de Groot * ------------------------------------------------------------------- */ /* splitlog.c, RdG 2002 */ #include #include #define VHOST_ROOT "/home/www/" #define MAX_LINE 255 #define LOG_FILE "access.log" #define ELOG_FILE "/var/log/www/split-errors.log" #define ALOG_FILE "/var/log/www/vhosts-access.log" main() { char vhost[MAX_LINE]; char line[MAX_LINE], logf[MAX_LINE]; char *lp; size_t lsize; FILE *lfd, *elfd, *alfd; if ( (elfd = fopen(ELOG_FILE, "a")) == NULL ) { perror("fopen"); exit(1); } if ( (alfd = fopen(ALOG_FILE, "a")) == NULL ) { perror("fopen"); exit(1); } while ( lp = fgetln(stdin, &lsize) ) { strncpy(line, lp, lsize < MAX_LINE ? lsize : MAX_LINE -1 ); if (lsize >= MAX_LINE) { line[MAX_LINE-1] = '\0'; line[MAX_LINE-2] = '\n'; } else { line[lsize] = '\0'; } sscanf(line, "%s%*", vhost); strncpy(logf, VHOST_ROOT, MAX_LINE -1); strncat(logf, vhost, MAX_LINE -1); strncat(logf, "/logs/", MAX_LINE -1); strncat(logf, LOG_FILE, MAX_LINE -1); if (lfd=fopen(logf, "a")) { fprintf(lfd, "%s", line); fclose(lfd); } else { fprintf(elfd, "%s", line); fflush(elfd); } fprintf(alfd, "%s", line); fflush(alfd); } }