#include #include #include #include #include #include #include #include #include #define LOG_DIRECTORY "/usr/local/apache/share/kanada-lab/staff/kanada/log2/" int main(int argc,char *argv[]) { int i,j; FILE *file; char *addr,*http_referer,*http_user_agent; char *hostname; char filename[256],strTime[256],account[256]; unsigned char byte[4]; time_t ct; struct hostent *host; if( argc!=2 ){ printf("Usage: wwwlog filename\n"); exit(0); } addr=getenv("REMOTE_ADDR"); http_referer=getenv("HTTP_REFERER"); http_user_agent=getenv("HTTP_USER_AGENT"); if ( addr==NULL ){ printf("Unexpected error!\n"); exit(0); } for(i=j=0;i<3;i++){ byte[i]=addr[j++]-'0'; while(addr[j]!='.') byte[i] = byte[i]*10 + addr[j++]-'0'; j++; } byte[3]=addr[j++]-'0'; while(addr[j]!='\0') byte[3] = byte[3]*10 + addr[j++]-'0'; host=gethostbyaddr(byte,4,2); if( host==NULL ) hostname=addr; else hostname=host->h_name; strcpy(filename,LOG_DIRECTORY); strcat(filename,argv[1]); ct=time(NULL); strftime(strTime,(size_t)256,"%Y/%m/%d %H:%M:%S",localtime(&ct)); file=fopen(filename,"a"); if( http_referer==NULL ){ fprintf(file,"%s %s - %s\n",strTime,hostname,http_user_agent); }else{ fprintf(file,"%s %s %s %s\n",strTime,hostname,http_referer, http_user_agent); } fclose(file); printf("",argv[1]); }