/*----------------------------------------------------------------------*\ | Ignore hits by robots and spiders; use key text in the agent string | | to recognize these users. | \*----------------------------------------------------------------------*/ #include #include #include #include #include #include static char *read_text (char *name) { long n; FILE *fp; char *dst,*end; long size; char *number; if (!name) return (NULL); dst = NULL; if (fp = fopen (name,"rb")) { if (fseek (fp,0L,SEEK_END) == 0) { size = 1L + ftell (fp); if (size > 1L) { if (dst = (char *) malloc (size)) { rewind (fp); n = fread (dst,1,size,fp); end = dst + n; *end = 0; } else fprintf (stderr,"Error: could not allocate space for file\n"); } else fprintf (stderr,"Error: Input file is empty.\n"); } fclose (fp); } else fprintf (stderr,"Error: could not open input file %s\n",name); return (dst); } static char **spider_name = NULL; static int spider_name_count = 0; static char *spider_name_buffer = NULL; static int compare (const void *e1, const void *e2) { const char *s1 = *(const char **) e1; const char *s2 = *(const char **) e2; return (strcmp (s1,s2)); } int read_spider_names (char *spider_name_file) { char *ptr, *eol; if (spider_name_buffer = read_text (spider_name_file)) { int n = 1; for (ptr = spider_name_buffer; *ptr; *ptr++) if (*ptr == '\n') n++; if (spider_name = (char **) malloc (n * sizeof (char *))) { char **p = spider_name; ptr = spider_name_buffer; while (*ptr) { for (eol=ptr; *eol; eol++) { if (*eol == '\r') *eol++ = 0; if (*eol == '\n') { *eol++ = 0; break; } } /*------------------------------------------------------*\ | Ignore blank lines and comments (any line beginning | | with #) in the list of spiders. | \*------------------------------------------------------*/ if (strlen(ptr) > 0 && *ptr != '#') *p++ = ptr; ptr = eol; } spider_name_count = p - spider_name; qsort (spider_name,spider_name_count,sizeof(char *),compare); } } return (spider_name_count); } int is_spider (char *agent, char *host) { int i; if (agent && *agent) for (i=0; i < spider_name_count; i++) if (strstr (agent,spider_name[i])) return (1); if (host && *host) for (i=0; i < spider_name_count; i++) if (strstr (host,spider_name[i])) return (1); return (0); } #ifdef TEST int main (int argc, char *argv[]) { int i,n; n = read_spider_names (spider_name_file); if (n > 0) { printf ("%2d spider names from %s\n",spider_name_count,spider_name_file); if (argc > 1) for (i=1; i < argc; i++) if (is_spider (argv[i])) printf ("Yes: %s\n",argv[i]); else printf ("No: %s\n",argv[i]); } else printf ("No spider names found in %s\n",spider_name_file); } #endif /*----------------------------------------------------------------------*\ \*----------------------------------------------------------------------*/