1 /*
2     webalizer - a web server log analysis program
3 
4     Copyright (C) 1997-2013  Bradford L. Barrett
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version, and provided that the above
10     copyright and permission notice is included with all distributed
11     copies of this or derived software.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 
22 */
23 
24 /*********************************************/
25 /* STANDARD INCLUDES                         */
26 /*********************************************/
27 
28 #include <time.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>                           /* normal stuff             */
33 #include <ctype.h>
34 #include <errno.h>
35 #include <sys/stat.h>
36 #include <sys/utsname.h>
37 #ifdef USE_DNS
38 #include <db.h>
39 #endif
40 
41 /* ensure sys/types */
42 #ifndef _SYS_TYPES_H
43 #include <sys/types.h>
44 #endif
45 
46 /* need socket header? */
47 #ifdef HAVE_SYS_SOCKET_H
48 #include <sys/socket.h>
49 #endif
50 
51 /* some systems need this */
52 #ifdef HAVE_MATH_H
53 #include <math.h>
54 #endif
55 
56 #ifdef USE_GEOIP
57 #include <maxminddb.h>
58 #endif
59 
60 #include "webalizer.h"                        /* main header              */
61 #include "lang.h"
62 #include "hashtab.h"
63 #include "preserve.h"
64 #include "linklist.h"
65 #include "graphs.h"
66 #include "output.h"
67 
68 /* internal function prototypes */
69 void    write_html_head(char *, FILE *);            /* head of html page   */
70 void    write_html_tail(FILE *);                    /* tail of html page   */
71 void    month_links();                              /* Page links          */
72 void    month_total_table();                        /* monthly total table */
73 void    daily_total_table();                        /* daily total table   */
74 void    hourly_total_table();                       /* hourly total table  */
75 void    top_sites_table(int);                       /* top n sites table   */
76 void    top_urls_table(int);                        /* top n URLs table    */
77 void    top_entry_table(int);                       /* top n entry/exits   */
78 void    top_refs_table();                           /* top n referrers ""  */
79 void    top_agents_table();                         /* top n u-agents  ""  */
80 void    top_ctry_table();                           /* top n countries ""  */
81 void    top_search_table();                         /* top n search strs   */
82 void    top_users_table();                          /* top n ident table   */
83 u_int64_t load_url_array(  UNODEPTR *);             /* load URL array      */
84 u_int64_t load_site_array( HNODEPTR *);             /* load Site array     */
85 u_int64_t load_ref_array(  RNODEPTR *);             /* load Refs array     */
86 u_int64_t load_agent_array(ANODEPTR *);             /* load Agents array   */
87 u_int64_t load_srch_array( SNODEPTR *);             /* load srch str array */
88 u_int64_t load_ident_array(INODEPTR *);             /* load ident array    */
89 int	qs_url_cmph( const void*, const void*);     /* compare by hits     */
90 int	qs_url_cmpk( const void*, const void*);     /* compare by kbytes   */
91 int	qs_url_cmpn( const void*, const void*);     /* compare by entrys   */
92 int	qs_url_cmpx( const void*, const void*);     /* compare by exits    */
93 int	qs_site_cmph(const void*, const void*);     /* compare by hits     */
94 int	qs_site_cmpk(const void*, const void*);     /* compare by kbytes   */
95 int	qs_ref_cmph( const void*, const void*);     /* compare by hits     */
96 int     qs_agnt_cmph(const void*, const void*);     /* compare by hits     */
97 int     qs_srch_cmph(const void*, const void*);     /* compare by hits     */
98 int     qs_ident_cmph(const void*, const void*);    /* compare by hits     */
99 int     qs_ident_cmpk(const void*, const void*);    /* compare by kbytes   */
100 
101 int     all_sites_page(u_int64_t, u_int64_t);       /* output site page    */
102 int     all_urls_page(u_int64_t, u_int64_t);        /* output urls page    */
103 int     all_refs_page(u_int64_t, u_int64_t);        /* output refs page    */
104 int     all_agents_page(u_int64_t, u_int64_t);      /* output agents page  */
105 int     all_search_page(u_int64_t, u_int64_t);      /* output search page  */
106 int     all_users_page(u_int64_t, u_int64_t);       /* output ident page   */
107 void    dump_all_sites();                           /* dump sites tab file */
108 void    dump_all_urls();                            /* dump urls tab file  */
109 void    dump_all_refs();                            /* dump refs tab file  */
110 void    dump_all_agents();                          /* dump agents file    */
111 void    dump_all_users();                           /* dump usernames file */
112 void    dump_all_search();                          /* dump search file    */
113 
114 /* define some colors for HTML */
115 #define WHITE          "#FFFFFF"
116 #define BLACK          "#000000"
117 #define RED            "#FF0000"
118 #define ORANGE         "#FF8000"
119 #define LTBLUE         "#0080FF"
120 #define BLUE           "#0000FF"
121 #define GREEN          "#00FF00"
122 #define DKGREEN        "#008040"
123 #define GREY           "#C0C0C0"
124 #define LTGREY         "#E8E8E8"
125 #define YELLOW         "#FFFF00"
126 #define PURPLE         "#FF00FF"
127 #define CYAN           "#00E0FF"
128 #define GRPCOLOR       "#D0D0E0"
129 
130 /* configurable html colors */
131 #define HITCOLOR       hit_color
132 #define FILECOLOR      file_color
133 #define SITECOLOR      site_color
134 #define KBYTECOLOR     kbyte_color
135 #define PAGECOLOR      page_color
136 #define VISITCOLOR     visit_color
137 #define MISCCOLOR      misc_color
138 
139 /* sort arrays */
140 UNODEPTR *u_array      = NULL;                /* Sort array for URLs      */
141 HNODEPTR *h_array      = NULL;                /* hostnames (sites)        */
142 RNODEPTR *r_array      = NULL;                /* referrers                */
143 ANODEPTR *a_array      = NULL;                /* user agents              */
144 SNODEPTR *s_array      = NULL;                /* search strings           */
145 INODEPTR *i_array      = NULL;                /* ident strings (username) */
146 u_int64_t a_ctr        = 0;                   /* counter for sort array   */
147 
148 FILE     *out_fp;
149 
150 /*********************************************/
151 /* WRITE_HTML_HEAD - output top of HTML page */
152 /*********************************************/
153 
write_html_head(char * period,FILE * out_fp)154 void write_html_head(char *period, FILE *out_fp)
155 {
156    NLISTPTR lptr;                          /* used for HTMLhead processing */
157 
158    /* HTMLPre code goes before all else    */
159    lptr = html_pre;
160    if (lptr==NULL)
161    {
162       /* Default 'DOCTYPE' header record if none specified */
163       fprintf(out_fp,
164       "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n\n");
165    }
166    else
167    {
168       while (lptr!=NULL)
169       {
170          fprintf(out_fp,"%s\n",lptr->string);
171          lptr=lptr->next;
172       }
173    }
174    /* Standard header comments */
175    fprintf(out_fp,"<!-- Generated by The Webalizer  Ver. %s-%s -->\n",
176       version,editlvl);
177    fprintf(out_fp,"<!--                                          -->\n");
178    fprintf(out_fp,"<!-- Copyright 1997-2013  Bradford L. Barrett -->\n");
179    fprintf(out_fp,"<!--                                          -->\n");
180    fprintf(out_fp,"<!-- Distributed under the GNU GPL  Version 2 -->\n");
181    fprintf(out_fp,"<!--        Full text may be found at:        -->\n");
182    fprintf(out_fp,"<!--         http://www.webalizer.org         -->\n");
183    fprintf(out_fp,"<!--                                          -->\n");
184    fprintf(out_fp,"<!--  Give the power back to the programmers  -->\n");
185    fprintf(out_fp,"<!--   Support the Free Software Foundation   -->\n");
186    fprintf(out_fp,"<!--           (http://www.fsf.org)           -->\n");
187    fprintf(out_fp,"<!--                                          -->\n");
188    fprintf(out_fp,"<!-- *** Generated: %s *** -->\n\n",cur_time());
189 
190    fprintf(out_fp,"<HTML lang=\"%s\">\n<HEAD>\n",langcode);
191    fprintf(out_fp," <TITLE>%s %s - %s</TITLE>\n",
192                   msg_title, hname, (period)?period:msg_main_per);
193    lptr=html_head;
194    while (lptr!=NULL)
195    {
196       fprintf(out_fp,"%s\n",lptr->string);
197       lptr=lptr->next;
198    }
199    fprintf(out_fp,"</HEAD>\n\n");
200 
201    lptr = html_body;
202    if (lptr==NULL)
203       fprintf(out_fp,"<BODY BGCOLOR=\"%s\" TEXT=\"%s\" "   \
204               "LINK=\"%s\" VLINK=\"%s\">\n",
205               LTGREY, BLACK, BLUE, RED);
206    else
207    {
208       while (lptr!=NULL)
209       {
210          fprintf(out_fp,"%s\n",lptr->string);
211          lptr=lptr->next;
212       }
213    }
214    fprintf(out_fp,"<H2>%s %s</H2>\n",msg_title, hname);
215    if (period)
216       fprintf(out_fp,"<SMALL><STRONG>\n%s: %s<BR>\n",msg_hhdr_sp,period);
217    else
218       fprintf(out_fp,"<SMALL><STRONG>\n%s<BR>\n",msg_main_per);
219    fprintf(out_fp,"%s %s<BR>\n</STRONG></SMALL>\n",msg_hhdr_gt,cur_time());
220    lptr=html_post;
221    while (lptr!=NULL)
222    {
223       fprintf(out_fp,"%s\n",lptr->string);
224       lptr=lptr->next;
225    }
226    fprintf(out_fp,"<CENTER>\n<HR>\n<P>\n");
227 }
228 
229 /*********************************************/
230 /* WRITE_HTML_TAIL - output HTML page tail   */
231 /*********************************************/
232 
write_html_tail(FILE * out_fp)233 void write_html_tail(FILE *out_fp)
234 {
235    NLISTPTR lptr;
236 
237    fprintf(out_fp,"</CENTER>\n");
238    fprintf(out_fp,"<P>\n<HR>\n");
239    fprintf(out_fp,"<TABLE WIDTH=\"100%%\" CELLPADDING=0 " \
240                   "CELLSPACING=0 BORDER=0>\n");
241    fprintf(out_fp,"<TR>\n");
242    fprintf(out_fp,"<TD ALIGN=left VALIGN=top>\n");
243    fprintf(out_fp,"<SMALL>Generated by\n");
244    fprintf(out_fp,"<A HREF=\"http://www.webalizer.org/\">");
245    fprintf(out_fp,"<STRONG>Webalizer Version %s</STRONG></A>\n",version);
246    fprintf(out_fp,"</SMALL>\n</TD>\n");
247    lptr=html_tail;
248    if (lptr)
249    {
250       fprintf(out_fp,"<TD ALIGN=\"right\" VALIGN=\"top\">\n");
251       while (lptr!=NULL)
252       {
253          fprintf(out_fp,"%s\n",lptr->string);
254          lptr=lptr->next;
255       }
256       fprintf(out_fp,"</TD>\n");
257    }
258    fprintf(out_fp,"</TR>\n</TABLE>\n");
259 
260    /* wind up, this is the end of the file */
261    fprintf(out_fp,"\n<!-- Webalizer Version %s-%s (Mod: %s) -->\n",
262            version,editlvl,moddate);
263    lptr = html_end;
264    if (lptr)
265    {
266       while (lptr!=NULL)
267       {
268          fprintf(out_fp,"%s\n",lptr->string);
269          lptr=lptr->next;
270       }
271    }
272    else fprintf(out_fp,"\n</BODY>\n</HTML>\n");
273 }
274 
275 /*********************************************/
276 /* WRITE_MONTH_HTML - does what it says...   */
277 /*********************************************/
278 
write_month_html()279 int write_month_html()
280 {
281    char html_fname[256];           /* filename storage areas...       */
282    char png1_fname[32];
283    char png2_fname[32];
284 
285    char buffer[BUFSIZE];           /* scratch buffer                  */
286    char dtitle[256];
287    char htitle[256];
288 
289    if (verbose>1)
290       printf("%s %s %d\n",msg_gen_rpt, l_month[cur_month-1], cur_year);
291 
292    /* fill in filenames */
293    snprintf(html_fname,sizeof(html_fname),"usage_%04d%02d.%s",
294             cur_year,cur_month,html_ext);
295    sprintf(png1_fname,"daily_usage_%04d%02d.png",cur_year,cur_month);
296    sprintf(png2_fname,"hourly_usage_%04d%02d.png",cur_year,cur_month);
297 
298    /* create PNG images for web page */
299    if (daily_graph)
300    {
301       snprintf(dtitle,sizeof(dtitle),"%s %s %d",
302                msg_hmth_du,l_month[cur_month-1],cur_year);
303       month_graph6 (  png1_fname,          /* filename          */
304                       dtitle,              /* graph title       */
305                       cur_month,           /* graph month       */
306                       cur_year,            /* graph year        */
307                       tm_hit,              /* data 1 (hits)     */
308                       tm_file,             /* data 2 (files)    */
309                       tm_site,             /* data 3 (sites)    */
310                       tm_xfer,             /* data 4 (kbytes)   */
311                       tm_page,             /* data 5 (pages)    */
312                       tm_visit);           /* data 6 (visits)   */
313    }
314 
315    if (hourly_graph)
316    {
317       snprintf(htitle,sizeof(htitle),"%s %s %d",
318                msg_hmth_hu,l_month[cur_month-1],cur_year);
319       day_graph3(    png2_fname,
320                      htitle,
321                      th_hit,
322                      th_file,
323                      th_page );
324    }
325 
326    /* now do html stuff... */
327    /* first, open the file */
328    if ( (out_fp=open_out_file(html_fname))==NULL ) return 1;
329 
330    snprintf(buffer,sizeof(buffer),"%s %d",l_month[cur_month-1],cur_year);
331    write_html_head(buffer, out_fp);
332    month_links();
333    month_total_table();
334    if (daily_graph || daily_stats)        /* Daily stuff */
335    {
336       fprintf(out_fp,"<A NAME=\"DAYSTATS\"></A>\n");
337       if (daily_graph) fprintf(out_fp,"<IMG SRC=\"%s\" ALT=\"%s\" " \
338                   "HEIGHT=400 WIDTH=512><P>\n",png1_fname,dtitle);
339       if (daily_stats) daily_total_table();
340    }
341 
342    if (hourly_graph || hourly_stats)      /* Hourly stuff */
343    {
344       fprintf(out_fp,"<A NAME=\"HOURSTATS\"></A>\n");
345       if (hourly_graph) fprintf(out_fp,"<IMG SRC=\"%s\" ALT=\"%s\" "  \
346                      "HEIGHT=256 WIDTH=512><P>\n",png2_fname,htitle);
347       if (hourly_stats) hourly_total_table();
348    }
349 
350    /* Do URL related stuff here, sorting appropriately                      */
351    if ( (a_ctr=load_url_array(NULL)) )
352    {
353     if ( (u_array=malloc(sizeof(UNODEPTR)*(a_ctr))) !=NULL )
354     {
355      a_ctr=load_url_array(u_array);        /* load up our sort array        */
356      if (ntop_urls || dump_urls)
357      {
358        qsort(u_array,a_ctr,sizeof(UNODEPTR),qs_url_cmph);
359        if (ntop_urls) top_urls_table(0);   /* Top URLs (by hits)            */
360        if (dump_urls) dump_all_urls();     /* Dump URLs tab file            */
361      }
362      if (ntop_urlsK)                       /* Top URLs (by kbytes)          */
363       {qsort(u_array,a_ctr,sizeof(UNODEPTR),qs_url_cmpk); top_urls_table(1); }
364      if (ntop_entry)                       /* Top Entry Pages               */
365       {qsort(u_array,a_ctr,sizeof(UNODEPTR),qs_url_cmpn); top_entry_table(0);}
366      if (ntop_exit)                        /* Top Exit Pages                */
367       {qsort(u_array,a_ctr,sizeof(UNODEPTR),qs_url_cmpx); top_entry_table(1);}
368      free(u_array);
369     }
370     else if (verbose) fprintf(stderr,"%s [u_array]\n",msg_nomem_tu); /* err */
371    }
372 
373    /* do hostname (sites) related stuff here, sorting appropriately...      */
374    if ( (a_ctr=load_site_array(NULL)) )
375    {
376     if ( (h_array=malloc(sizeof(HNODEPTR)*(a_ctr))) !=NULL )
377     {
378      a_ctr=load_site_array(h_array);       /* load up our sort array        */
379      if (ntop_sites || dump_sites)
380      {
381        qsort(h_array,a_ctr,sizeof(HNODEPTR),qs_site_cmph);
382        if (ntop_sites) top_sites_table(0); /* Top sites table (by hits)     */
383        if (dump_sites) dump_all_sites();   /* Dump sites tab file           */
384      }
385      if (ntop_sitesK)                      /* Top Sites table (by kbytes)   */
386      {
387        qsort(h_array,a_ctr,sizeof(HNODEPTR),qs_site_cmpk);
388        top_sites_table(1);
389      }
390      free(h_array);
391     }
392     else if (verbose) fprintf(stderr,"%s [h_array]\n",msg_nomem_ts); /* err */
393    }
394 
395    /* do referrer related stuff here, sorting appropriately...              */
396    if ( (a_ctr=load_ref_array(NULL)) )
397    {
398     if ( (r_array=malloc(sizeof(RNODEPTR)*(a_ctr))) != NULL)
399     {
400      a_ctr=load_ref_array(r_array);
401      if (ntop_refs || dump_refs)
402      {
403        qsort(r_array,a_ctr,sizeof(RNODEPTR),qs_ref_cmph);
404        if (ntop_refs) top_refs_table();   /* Top referrers table            */
405        if (dump_refs) dump_all_refs();    /* Dump referrers tab file        */
406      }
407      free(r_array);
408     }
409     else if (verbose) fprintf(stderr,"%s [r_array]\n",msg_nomem_tr); /* err */
410    }
411 
412    /* do search string related stuff, sorting appropriately...              */
413    if ( (a_ctr=load_srch_array(NULL)) )
414    {
415     if ( (s_array=malloc(sizeof(SNODEPTR)*(a_ctr))) != NULL)
416     {
417      a_ctr=load_srch_array(s_array);
418      if (ntop_search || dump_search)
419      {
420        qsort(s_array,a_ctr,sizeof(SNODEPTR),qs_srch_cmph);
421        if (ntop_search) top_search_table(); /* top search strings table     */
422        if (dump_search) dump_all_search();  /* dump search string tab file  */
423      }
424      free(s_array);
425     }
426     else if (verbose) fprintf(stderr,"%s [s_array]\n",msg_nomem_tsr);/* err */
427    }
428 
429    /* do ident (username) related stuff here, sorting appropriately...      */
430    if ( (a_ctr=load_ident_array(NULL)) )
431    {
432     if ( (i_array=malloc(sizeof(INODEPTR)*(a_ctr))) != NULL)
433     {
434      a_ctr=load_ident_array(i_array);
435      if (ntop_users || dump_users)
436      {
437        qsort(i_array,a_ctr,sizeof(INODEPTR),qs_ident_cmph);
438        if (ntop_users) top_users_table(); /* top usernames table            */
439        if (dump_users) dump_all_users();  /* dump usernames tab file        */
440      }
441      free(i_array);
442     }
443     else if (verbose) fprintf(stderr,"%s [i_array]\n",msg_nomem_ti); /* err */
444    }
445 
446    /* do user agent related stuff here, sorting appropriately...            */
447    if ( (a_ctr=load_agent_array(NULL)) )
448    {
449     if ( (a_array=malloc(sizeof(ANODEPTR)*(a_ctr))) != NULL)
450     {
451      a_ctr=load_agent_array(a_array);
452      if (ntop_agents || dump_agents)
453      {
454        qsort(a_array,a_ctr,sizeof(ANODEPTR),qs_agnt_cmph);
455        if (ntop_agents) top_agents_table(); /* top user agents table        */
456        if (dump_agents) dump_all_agents();  /* dump user agents tab file    */
457      }
458      free(a_array);
459     }
460     else if (verbose) fprintf(stderr,"%s [a_array]\n",msg_nomem_ta); /* err */
461    }
462 
463    if (ntop_ctrys ) top_ctry_table();     /* top countries table            */
464 
465    write_html_tail(out_fp);               /* finish up the HTML document    */
466    fclose(out_fp);                        /* close the file                 */
467    return (0);                            /* done...                        */
468 }
469 
470 /*********************************************/
471 /* MONTH_LINKS - links to other page parts   */
472 /*********************************************/
473 
month_links()474 void month_links()
475 {
476    fprintf(out_fp,"<SMALL>\n");
477    if (daily_stats || daily_graph)
478       fprintf(out_fp,"<A HREF=\"#DAYSTATS\">[%s]</A>\n",msg_hlnk_ds);
479    if (hourly_stats || hourly_graph)
480       fprintf(out_fp,"<A HREF=\"#HOURSTATS\">[%s]</A>\n",msg_hlnk_hs);
481    if (ntop_urls || ntop_urlsK)
482       fprintf(out_fp,"<A HREF=\"#TOPURLS\">[%s]</A>\n",msg_hlnk_u);
483    if (ntop_entry)
484       fprintf(out_fp,"<A HREF=\"#TOPENTRY\">[%s]</A>\n",msg_hlnk_en);
485    if (ntop_exit)
486       fprintf(out_fp,"<A HREF=\"#TOPEXIT\">[%s]</A>\n",msg_hlnk_ex);
487    if (ntop_sites || ntop_sitesK)
488       fprintf(out_fp,"<A HREF=\"#TOPSITES\">[%s]</A>\n",msg_hlnk_s);
489    if (ntop_refs && t_ref)
490       fprintf(out_fp,"<A HREF=\"#TOPREFS\">[%s]</A>\n",msg_hlnk_r);
491    if (ntop_search)
492       fprintf(out_fp,"<A HREF=\"#TOPSEARCH\">[%s]</A>\n",msg_hlnk_sr);
493    if (ntop_users && t_user)
494       fprintf(out_fp,"<A HREF=\"#TOPUSERS\">[%s]</A>\n",msg_hlnk_i);
495    if (ntop_agents && t_agent)
496       fprintf(out_fp,"<A HREF=\"#TOPAGENTS\">[%s]</A>\n",msg_hlnk_a);
497    if (ntop_ctrys)
498       fprintf(out_fp,"<A HREF=\"#TOPCTRYS\">[%s]</A>\n",msg_hlnk_c);
499    fprintf(out_fp,"</SMALL>\n<P>\n");
500 }
501 
502 /*********************************************/
503 /* MONTH_TOTAL_TABLE - monthly totals table  */
504 /*********************************************/
505 
month_total_table()506 void month_total_table()
507 {
508    int i,days_in_month;
509    u_int64_t max_files=0,max_hits=0,max_visits=0,max_pages=0,max_sites=0;
510    double max_xfer=0.0;
511 
512    days_in_month=(l_day-f_day)+1;
513    for (i=0;i<31;i++)
514    {  /* Get max/day values */
515       if (tm_hit[i]>max_hits)     max_hits  = tm_hit[i];
516       if (tm_file[i]>max_files)   max_files = tm_file[i];
517       if (tm_page[i]>max_pages)   max_pages = tm_page[i];
518       if (tm_visit[i]>max_visits) max_visits= tm_visit[i];
519       if (tm_site[i]>max_sites)   max_sites = tm_site[i];
520       if (tm_xfer[i]>max_xfer)    max_xfer  = tm_xfer[i];
521    }
522 
523    fprintf(out_fp,"<TABLE WIDTH=510 BORDER=2 CELLSPACING=1 CELLPADDING=1>\n");
524    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
525    fprintf(out_fp,"<TR><TH COLSPAN=3 ALIGN=center BGCOLOR=\"%s\">"           \
526       "%s %s %d</TH></TR>\n",GREY,msg_mtot_ms,l_month[cur_month-1],cur_year);
527    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
528    /* Total Hits */
529    fprintf(out_fp,"<TR><TD WIDTH=380><FONT SIZE=\"-1\">%s</FONT></TD>\n"     \
530       "<TD ALIGN=right COLSPAN=2><FONT SIZE=\"-1\"><B>%llu</B>"              \
531       "</FONT></TD></TR>\n",msg_mtot_th,t_hit);
532    /* Total Files */
533    fprintf(out_fp,"<TR><TD WIDTH=380><FONT SIZE=\"-1\">%s</FONT></TD>\n"     \
534       "<TD ALIGN=right COLSPAN=2><FONT SIZE=\"-1\"><B>%llu</B>"              \
535       "</FONT></TD></TR>\n",msg_mtot_tf,t_file);
536    /* Total Pages */
537    fprintf(out_fp,"<TR><TD WIDTH=380><FONT SIZE=\"-1\">%s %s</FONT></TD>\n"  \
538       "<TD ALIGN=right COLSPAN=2><FONT SIZE=\"-1\"><B>%llu</B>"              \
539       "</FONT></TD></TR>\n",msg_h_total, msg_h_pages, t_page);
540    /* Total Visits */
541    fprintf(out_fp,"<TR><TD WIDTH=380><FONT SIZE=\"-1\">%s %s</FONT></TD>\n"  \
542       "<TD ALIGN=right COLSPAN=2><FONT SIZE=\"-1\"><B>%llu</B>"              \
543       "</FONT></TD></TR>\n",msg_h_total, msg_h_visits, t_visit);
544    /* Total XFer */
545    fprintf(out_fp,"<TR><TD WIDTH=380><FONT SIZE=\"-1\">%s</FONT></TD>\n"     \
546       "<TD ALIGN=right COLSPAN=2><FONT SIZE=\"-1\"><B>%.0f</B>"              \
547       "</FONT></TD></TR>\n",msg_mtot_tx,t_xfer/1024);
548    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
549    /**********************************************/
550    /* Unique Sites */
551    fprintf(out_fp,"<TR>"                                                     \
552       "<TD WIDTH=380><FONT SIZE=\"-1\">%s</FONT></TD>\n"                     \
553       "<TD ALIGN=right COLSPAN=2><FONT SIZE=\"-1\"><B>%llu</B>"              \
554       "</FONT></TD></TR>\n",msg_mtot_us,t_site);
555    /* Unique URLs */
556    fprintf(out_fp,"<TR>"                                                     \
557       "<TD WIDTH=380><FONT SIZE=\"-1\">%s</FONT></TD>\n"                     \
558       "<TD ALIGN=right COLSPAN=2><FONT SIZE=\"-1\"><B>%llu</B>"              \
559       "</FONT></TD></TR>\n",msg_mtot_uu,t_url);
560    /* Unique Referrers */
561    if (t_ref != 0)
562    fprintf(out_fp,"<TR>"                                                     \
563       "<TD WIDTH=380><FONT SIZE=\"-1\">%s</FONT></TD>\n"                     \
564       "<TD ALIGN=right COLSPAN=2><FONT SIZE=\"-1\"><B>%llu</B>"              \
565       "</FONT></TD></TR>\n",msg_mtot_ur,t_ref);
566    /* Unique Usernames */
567    if (t_user != 0)
568    fprintf(out_fp,"<TR>"                                                     \
569       "<TD WIDTH=380><FONT SIZE=\"-1\">%s</FONT></TD>\n"                     \
570       "<TD ALIGN=right COLSPAN=2><FONT SIZE=\"-1\"><B>%llu</B>"              \
571       "</FONT></TD></TR>\n",msg_mtot_ui,t_user);
572    /* Unique Agents */
573    if (t_agent != 0)
574    fprintf(out_fp,"<TR>"                                                     \
575       "<TD WIDTH=380><FONT SIZE=\"-1\">%s</FONT></TD>\n"                     \
576       "<TD ALIGN=right COLSPAN=2><FONT SIZE=\"-1\"><B>%llu</B>"              \
577       "</FONT></TD></TR>\n",msg_mtot_ua,t_agent);
578    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
579    /**********************************************/
580    /* Hourly/Daily avg/max totals */
581    fprintf(out_fp,"<TR>"                                                     \
582       "<TH WIDTH=380 BGCOLOR=\"%s\"><FONT SIZE=-1 COLOR=\"%s\">.</FONT></TH>\n"\
583       "<TH WIDTH=65 BGCOLOR=\"%s\" ALIGN=right>"                             \
584       "<FONT SIZE=-1>%s </FONT></TH>\n"                                      \
585       "<TH WIDTH=65 BGCOLOR=\"%s\" ALIGN=right>"                             \
586       "<FONT SIZE=-1>%s </FONT></TH></TR>\n",
587       GREY,GREY,GREY,msg_h_avg,GREY,msg_h_max);
588    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
589    /* Max/Avg Hits per Hour */
590    fprintf(out_fp,"<TR>"                                                     \
591       "<TD><FONT SIZE=\"-1\">%s</FONT></TD>\n"                               \
592       "<TD ALIGN=right WIDTH=65><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n" \
593       "<TD WIDTH=65 ALIGN=right><FONT SIZE=-1><B>%llu</B>"                   \
594       "</FONT></TD></TR>\n",msg_mtot_mhh, t_hit/(24*days_in_month),mh_hit);
595    /* Max/Avg Hits per Day */
596    fprintf(out_fp,"<TR>"                                                     \
597       "<TD><FONT SIZE=\"-1\">%s</FONT></TD>\n"                               \
598       "<TD ALIGN=right WIDTH=65><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n" \
599       "<TD WIDTH=65 ALIGN=right><FONT SIZE=-1><B>%llu</B>"                   \
600       "</FONT></TD></TR>\n",msg_mtot_mhd, t_hit/days_in_month, max_hits);
601    /* Max/Avg Files per Day */
602    fprintf(out_fp,"<TR>"                                                     \
603       "<TD><FONT SIZE=\"-1\">%s</FONT></TD>\n"                               \
604       "<TD ALIGN=right WIDTH=65><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n" \
605       "<TD WIDTH=65 ALIGN=right><FONT SIZE=-1><B>%llu</B>"                   \
606       "</FONT></TD></TR>\n",msg_mtot_mfd, t_file/days_in_month,max_files);
607    /* Max/Avg Pages per Day */
608    fprintf(out_fp,"<TR>"                                                     \
609       "<TD><FONT SIZE=\"-1\">%s</FONT></TD>\n"                               \
610       "<TD ALIGN=right WIDTH=65><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n" \
611       "<TD WIDTH=65 ALIGN=right><FONT SIZE=-1><B>%llu</B>"                   \
612       "</FONT></TD></TR>\n",msg_mtot_mpd, t_page/days_in_month,max_pages);
613    /* Max/Avg Sites per Day */
614    fprintf(out_fp,"<TR>"                                                     \
615       "<TD><FONT SIZE=\"-1\">%s</FONT></TD>\n"                               \
616       "<TD ALIGN=right WIDTH=65><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n" \
617       "<TD WIDTH=65 ALIGN=right><FONT SIZE=-1><B>%llu</B>"                   \
618       "</FONT></TD></TR>\n",msg_mtot_msd, t_site/days_in_month,max_sites);
619    /* Max/Avg Visits per Day */
620    fprintf(out_fp,"<TR>"                                                     \
621       "<TD><FONT SIZE=\"-1\">%s</FONT></TD>\n"                               \
622       "<TD ALIGN=right WIDTH=65><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n" \
623       "<TD WIDTH=65 ALIGN=right><FONT SIZE=-1><B>%llu</B>"                   \
624       "</FONT></TD></TR>\n",msg_mtot_mvd, t_visit/days_in_month,max_visits);
625    /* Max/Avg KBytes per Day */
626    fprintf(out_fp,"<TR>"                                                     \
627       "<TD><FONT SIZE=\"-1\">%s</FONT></TD>\n"                               \
628       "<TD ALIGN=right WIDTH=65><FONT SIZE=\"-1\"><B>%.0f</B></FONT></TD>\n" \
629       "<TD WIDTH=65 ALIGN=right><FONT SIZE=-1><B>%.0f</B>"                   \
630       "</FONT></TD></TR>\n",msg_mtot_mkd,
631       (t_xfer/1024)/days_in_month,max_xfer/1024);
632    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
633    /**********************************************/
634    /* response code totals */
635    fprintf(out_fp,"<TR><TH COLSPAN=3 ALIGN=center BGCOLOR=\"%s\">\n"         \
636            "<FONT SIZE=\"-1\">%s</FONT></TH></TR>\n",GREY,msg_mtot_rc);
637    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
638    for (i=0;i<TOTAL_RC;i++)
639    {
640       if (response[i].count != 0)
641          fprintf(out_fp,"<TR><TD><FONT SIZE=\"-1\">%s</FONT></TD>\n"         \
642             "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"       \
643             "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD></TR>\n",
644             response[i].desc,PCENT(response[i].count,t_hit),response[i].count);
645    }
646    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
647    /**********************************************/
648 
649    fprintf(out_fp,"</TABLE>\n");
650    fprintf(out_fp,"<P>\n");
651 }
652 
653 /*********************************************/
654 /* DAILY_TOTAL_TABLE - daily totals          */
655 /*********************************************/
656 
daily_total_table()657 void daily_total_table()
658 {
659    int i,j;
660 
661    /* Daily stats */
662    fprintf(out_fp,"<TABLE WIDTH=510 BORDER=2 CELLSPACING=1 CELLPADDING=1>\n");
663    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
664    /* Daily statistics for ... */
665    fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" COLSPAN=13 ALIGN=center>"          \
666            "%s %s %d</TH></TR>\n",
667            GREY,msg_dtot_ds,l_month[cur_month-1], cur_year);
668    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
669    fprintf(out_fp,"<TR><TH ALIGN=center BGCOLOR=\"%s\">"                     \
670                   "<FONT SIZE=\"-1\">%s</FONT></TH>\n"                       \
671                   "<TH ALIGN=center BGCOLOR=\"%s\" COLSPAN=2>"               \
672                   "<FONT SIZE=\"-1\">%s</FONT></TH>\n"                       \
673                   "<TH ALIGN=center BGCOLOR=\"%s\" COLSPAN=2>"               \
674                   "<FONT SIZE=\"-1\">%s</FONT></TH>\n"                       \
675                   "<TH ALIGN=center BGCOLOR=\"%s\" COLSPAN=2>"               \
676                   "<FONT SIZE=\"-1\">%s</FONT></TH>\n"                       \
677                   "<TH ALIGN=center BGCOLOR=\"%s\" COLSPAN=2>"               \
678                   "<FONT SIZE=\"-1\">%s</FONT></TH>\n"                       \
679                   "<TH ALIGN=center BGCOLOR=\"%s\" COLSPAN=2>"               \
680                   "<FONT SIZE=\"-1\">%s</FONT></TH>\n"                       \
681                   "<TH ALIGN=center BGCOLOR=\"%s\" COLSPAN=2>"               \
682                   "<FONT SIZE=\"-1\">%s</FONT></TH></TR>\n",
683                   GREY,       msg_h_day,
684                   HITCOLOR,   msg_h_hits,
685                   FILECOLOR,  msg_h_files,
686                   PAGECOLOR,  msg_h_pages,
687                   VISITCOLOR, msg_h_visits,
688                   SITECOLOR,  msg_h_sites,
689                   KBYTECOLOR, msg_h_xfer);
690    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
691 
692    /* skip beginning blank days in a month */
693    for (i=0;i<l_day;i++) if (tm_hit[i]!=0) break;
694    if (i==l_day) i=0;
695 
696    for (;i<l_day;i++)
697    {
698       j = jdate(i+1,cur_month,cur_year);
699       if ( (j%7==6) || (j%7==0) )
700            fprintf(out_fp,"<TR BGCOLOR=\"%s\"><TD ALIGN=center>",GRPCOLOR);
701       else fprintf(out_fp,"<TR><TD ALIGN=center>");
702       fprintf(out_fp,"<FONT SIZE=\"-1\"><B>%d</B></FONT></TD>\n", i+1);
703       fprintf(out_fp,"<TD ALIGN=right>"                                      \
704               "<FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n"                  \
705               "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n",
706               tm_hit[i],PCENT(tm_hit[i],t_hit));
707       fprintf(out_fp,"<TD ALIGN=right>"                                      \
708               "<FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n"                  \
709               "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n",
710               tm_file[i],PCENT(tm_file[i],t_file));
711       fprintf(out_fp,"<TD ALIGN=right>"                                      \
712               "<FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n"                  \
713               "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n",
714               tm_page[i],PCENT(tm_page[i],t_page));
715       fprintf(out_fp,"<TD ALIGN=right>"                                      \
716               "<FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n"                  \
717               "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n",
718               tm_visit[i],PCENT(tm_visit[i],t_visit));
719       fprintf(out_fp,"<TD ALIGN=right>"                                      \
720               "<FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n"                  \
721               "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n",
722               tm_site[i],PCENT(tm_site[i],t_site));
723       fprintf(out_fp,"<TD ALIGN=right>"                                      \
724               "<FONT SIZE=\"-1\"><B>%.0f</B></FONT></TD>\n"                  \
725               "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD></TR>\n",
726               tm_xfer[i]/1024,PCENT(tm_xfer[i],t_xfer));
727    }
728    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
729    fprintf(out_fp,"</TABLE>\n");
730    fprintf(out_fp,"<P>\n");
731 }
732 
733 /*********************************************/
734 /* HOURLY_TOTAL_TABLE - hourly table         */
735 /*********************************************/
736 
hourly_total_table()737 void hourly_total_table()
738 {
739    int       i,days_in_month;
740    u_int64_t avg_file=0;
741    double    avg_xfer=0.0;
742 
743    days_in_month=(l_day-f_day)+1;
744 
745    /* Hourly stats */
746    fprintf(out_fp,"<TABLE WIDTH=510 BORDER=2 CELLSPACING=1 CELLPADDING=1>\n");
747    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
748    fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" COLSPAN=13 ALIGN=center>"\
749            "%s %s %d</TH></TR>\n",
750            GREY,msg_htot_hs,l_month[cur_month-1], cur_year);
751    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
752    fprintf(out_fp,"<TR><TH ALIGN=center ROWSPAN=2 BGCOLOR=\"%s\">" \
753                   "<FONT SIZE=\"-1\">%s</FONT></TH>\n"             \
754                   "<TH ALIGN=center BGCOLOR=\"%s\" COLSPAN=3>"     \
755                   "<FONT SIZE=\"-1\">%s</FONT></TH>\n"             \
756                   "<TH ALIGN=center BGCOLOR=\"%s\" COLSPAN=3>"     \
757                   "<FONT SIZE=\"-1\">%s</FONT></TH>\n"             \
758                   "<TH ALIGN=center BGCOLOR=\"%s\" COLSPAN=3>"     \
759                   "<FONT SIZE=\"-1\">%s</FONT></TH>\n"             \
760                   "<TH ALIGN=center BGCOLOR=\"%s\" COLSPAN=3>"     \
761                   "<FONT SIZE=\"-1\">%s</FONT></TH></TR>\n",
762                   GREY,       msg_h_hour,
763                   HITCOLOR,   msg_h_hits,
764                   FILECOLOR,  msg_h_files,
765                   PAGECOLOR,  msg_h_pages,
766                   KBYTECOLOR, msg_h_xfer);
767    fprintf(out_fp,"<TR><TH ALIGN=center BGCOLOR=\"%s\">"           \
768                   "<FONT SIZE=\"-2\">%s</FONT></TH>\n"             \
769                   "<TH ALIGN=center BGCOLOR=\"%s\" COLSPAN=2>"     \
770                   "<FONT SIZE=\"-2\">%s</FONT></TH>\n",
771                   HITCOLOR, msg_h_avg, HITCOLOR, msg_h_total);
772    fprintf(out_fp,"<TH ALIGN=center BGCOLOR=\"%s\">"               \
773                   "<FONT SIZE=\"-2\">%s</FONT></TH>\n"             \
774                   "<TH ALIGN=center BGCOLOR=\"%s\" COLSPAN=2>"     \
775                   "<FONT SIZE=\"-2\">%s</FONT></TH>\n",
776                   FILECOLOR, msg_h_avg, FILECOLOR, msg_h_total);
777    fprintf(out_fp,"<TH ALIGN=center BGCOLOR=\"%s\">"               \
778                   "<FONT SIZE=\"-2\">%s</FONT></TH>\n"             \
779                   "<TH ALIGN=center BGCOLOR=\"%s\" COLSPAN=2>"     \
780                   "<FONT SIZE=\"-2\">%s</FONT></TH>\n",
781                   PAGECOLOR, msg_h_avg, PAGECOLOR, msg_h_total);
782    fprintf(out_fp,"<TH ALIGN=center BGCOLOR=\"%s\">"               \
783                   "<FONT SIZE=\"-2\">%s</FONT></TH>\n"             \
784                   "<TH ALIGN=center BGCOLOR=\"%s\" COLSPAN=2>"     \
785                   "<FONT SIZE=\"-2\">%s</FONT></TH></TR>\n",
786                   KBYTECOLOR, msg_h_avg, KBYTECOLOR, msg_h_total);
787 
788    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
789    for (i=0;i<24;i++)
790    {
791       fprintf(out_fp,"<TR><TD ALIGN=center>"                          \
792          "<FONT SIZE=\"-1\"><B>%d</B></FONT></TD>\n",i);
793       fprintf(out_fp,
794          "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n"\
795          "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n"\
796          "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n",
797          th_hit[i]/days_in_month,th_hit[i],
798          PCENT(th_hit[i],t_hit));
799       fprintf(out_fp,
800          "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n"\
801          "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n"\
802          "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n",
803          th_file[i]/days_in_month,th_file[i],
804          PCENT(th_file[i],t_file));
805       fprintf(out_fp,
806          "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n"\
807          "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n"\
808          "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n",
809          th_page[i]/days_in_month,th_page[i],
810          PCENT(th_page[i],t_page));
811       fprintf(out_fp,
812          "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%.0f</B></FONT></TD>\n" \
813          "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%.0f</B></FONT></TD>\n" \
814          "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD></TR>\n",
815          (th_xfer[i]/days_in_month)/1024,th_xfer[i]/1024,
816          PCENT(th_xfer[i],t_xfer));
817       avg_file += th_file[i]/days_in_month;
818       avg_xfer+= (th_xfer[i]/days_in_month)/1024;
819    }
820    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
821    fprintf(out_fp,"</TABLE>\n<P>\n");
822 }
823 
824 /*********************************************/
825 /* TOP_SITES_TABLE - generate top n table    */
826 /*********************************************/
827 
top_sites_table(int flag)828 void top_sites_table(int flag)
829 {
830    u_int64_t cnt=0, h_reg=0, h_grp=0, h_hid=0, tot_num;
831    int       i;
832    HNODEPTR  hptr, *pointer;
833 
834    cnt=a_ctr; pointer=h_array;
835    while(cnt--)
836    {
837       /* calculate totals */
838       switch ( (int)((HNODEPTR)(*pointer)->flag) )
839       {
840          case OBJ_REG:   h_reg++;  break;
841          case OBJ_GRP:   h_grp++;  break;
842          case OBJ_HIDE:  h_hid++;  break;
843       }
844       pointer++;
845    }
846 
847    if ( (tot_num=h_reg+h_grp)==0 ) return;              /* split if none    */
848    i=(flag)?ntop_sitesK:ntop_sites;                     /* Hits or KBytes?? */
849    if (tot_num > i) tot_num = i;                        /* get max to do... */
850 
851    if ((!flag) || (flag&&!ntop_sites))                  /* now do <A> tag   */
852       fprintf(out_fp,"<A NAME=\"TOPSITES\"></A>\n");
853 
854    fprintf(out_fp,"<TABLE WIDTH=510 BORDER=2 CELLSPACING=1 CELLPADDING=1>\n");
855    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
856    if (flag) fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" ALIGN=CENTER COLSPAN=10>" \
857            "%s %llu %s %llu %s %s %s</TH></TR>\n",
858            GREY, msg_top_top,tot_num,msg_top_of,
859            t_site,msg_top_s,msg_h_by,msg_h_xfer);
860    else      fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" ALIGN=CENTER COLSPAN=10>" \
861            "%s %llu %s %llu %s</TH></TR>\n",
862            GREY,msg_top_top, tot_num, msg_top_of, t_site, msg_top_s);
863    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
864    fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" ALIGN=center>"                    \
865           "<FONT SIZE=\"-1\">#</FONT></TH>\n",GREY);
866    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center COLSPAN=2>"              \
867           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",HITCOLOR,msg_h_hits);
868    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center COLSPAN=2>"              \
869           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",FILECOLOR,msg_h_files);
870    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center COLSPAN=2>"              \
871           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",KBYTECOLOR,msg_h_xfer);
872    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center COLSPAN=2>"              \
873           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",VISITCOLOR,msg_h_visits);
874    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center>"                        \
875           "<FONT SIZE=\"-1\">%s</FONT></TH></TR>\n",MISCCOLOR,msg_h_hname);
876    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
877 
878    pointer=h_array; i=0;
879    while(tot_num)
880    {
881       hptr=*pointer++;
882       if (hptr->flag != OBJ_HIDE)
883       {
884          /* shade grouping? */
885          if (shade_groups && (hptr->flag==OBJ_GRP))
886             fprintf(out_fp,"<TR BGCOLOR=\"%s\">\n", GRPCOLOR);
887          else fprintf(out_fp,"<TR>\n");
888 
889          fprintf(out_fp,
890               "<TD ALIGN=center><FONT SIZE=\"-1\"><B>%d</B></FONT></TD>\n"  \
891               "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n" \
892               "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"    \
893               "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n" \
894               "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"    \
895               "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%.0f</B></FONT></TD>\n" \
896               "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"    \
897               "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n" \
898               "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"    \
899               "<TD ALIGN=left NOWRAP><FONT SIZE=\"-1\">",
900               i+1,hptr->count,
901               (t_hit==0)?0:((float)hptr->count/t_hit)*100.0,hptr->files,
902               (t_file==0)?0:((float)hptr->files/t_file)*100.0,hptr->xfer/1024,
903               (t_xfer==0)?0:((float)hptr->xfer/t_xfer)*100.0,hptr->visit,
904               (t_visit==0)?0:((float)hptr->visit/t_visit)*100.0);
905 
906          if ((hptr->flag==OBJ_GRP)&&hlite_groups)
907              fprintf(out_fp,"<STRONG>%s</STRONG></FONT></TD></TR>\n",
908                hptr->string);
909          else fprintf(out_fp,"%s</FONT></TD></TR>\n",
910                hptr->string);
911          tot_num--;
912          i++;
913       }
914    }
915 
916    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
917    if ((!flag) || (flag&&!ntop_sites))
918    {
919       if ( (all_sites) && ((h_reg+h_grp)>ntop_sites) )
920       {
921          if (all_sites_page(h_reg, h_grp))
922          {
923             fprintf(out_fp,"<TR BGCOLOR=\"%s\">",GRPCOLOR);
924             fprintf(out_fp,"<TD COLSPAN=10 ALIGN=\"center\">\n");
925             fprintf(out_fp,"<FONT SIZE=\"-1\">");
926             fprintf(out_fp,"<A HREF=\"./site_%04d%02d.%s\">",
927                     cur_year,cur_month,html_ext);
928             fprintf(out_fp,"%s</A></TD></TR>\n",msg_v_sites);
929             if (flag)   /* do we need to sort? */
930                qsort(h_array,a_ctr,sizeof(HNODEPTR),qs_site_cmph);
931          }
932       }
933    }
934    fprintf(out_fp,"</TABLE>\n<P>\n");
935 }
936 
937 /*********************************************/
938 /* ALL_SITES_PAGE - HTML page of all sites   */
939 /*********************************************/
940 
all_sites_page(u_int64_t h_reg,u_int64_t h_grp)941 int all_sites_page(u_int64_t h_reg, u_int64_t h_grp)
942 {
943    HNODEPTR hptr, *pointer;
944    char     site_fname[256], buffer[256];
945    FILE     *out_fp;
946    int      i=(h_grp)?1:0;
947 
948    /* generate file name */
949    snprintf(site_fname,sizeof(site_fname),"site_%04d%02d.%s",
950             cur_year,cur_month,html_ext);
951 
952    /* open file */
953    if ( (out_fp=open_out_file(site_fname))==NULL ) return 0;
954 
955    snprintf(buffer,sizeof(buffer),"%s %d - %s",
956             l_month[cur_month-1],cur_year,msg_h_sites);
957    write_html_head(buffer, out_fp);
958 
959    fprintf(out_fp,"<FONT SIZE=\"-1\"></CENTER><PRE>\n");
960 
961    fprintf(out_fp," %12s      %12s      %12s      %12s      %s\n",
962            msg_h_hits, msg_h_files, msg_h_xfer, msg_h_visits, msg_h_hname);
963    fprintf(out_fp,"----------------  ----------------  ----------------  " \
964                   "----------------  --------------------\n\n");
965 
966    /* Do groups first (if any) */
967    pointer=h_array;
968    while(h_grp)
969    {
970       hptr=*pointer++;
971       if (hptr->flag == OBJ_GRP)
972       {
973          fprintf(out_fp,
974             "%-8llu %6.02f%%  %8llu %6.02f%%  %8.0f %6.02f%%  "            \
975             "%8llu %6.02f%%  %s\n",
976             hptr->count,
977             (t_hit==0)?0:((float)hptr->count/t_hit)*100.0,hptr->files,
978             (t_file==0)?0:((float)hptr->files/t_file)*100.0,hptr->xfer/1024,
979             (t_xfer==0)?0:((float)hptr->xfer/t_xfer)*100.0,hptr->visit,
980             (t_visit==0)?0:((float)hptr->visit/t_visit)*100.0,
981             hptr->string);
982          h_grp--;
983       }
984    }
985 
986    if (i) fprintf(out_fp,"\n");
987 
988    /* Now do individual sites (if any) */
989    pointer=h_array;
990    if (!hide_sites) while(h_reg)
991    {
992       hptr=*pointer++;
993       if (hptr->flag == OBJ_REG)
994       {
995 #ifdef USE_CLICKABLE_REFERER
996          if (strstr(hptr->string,"://")!=NULL)
997             fprintf(out_fp,
998                "%-8llu %6.02f%%  %8llu %6.02f%%  %8.0f %6.02f%%  "            \
999                "%8llu %6.02f%%  <A HREF=\"%s\">%s</A>\n",
1000                hptr->count,
1001                (t_hit==0)?0:((float)hptr->count/t_hit)*100.0,hptr->files,
1002                (t_file==0)?0:((float)hptr->files/t_file)*100.0,hptr->xfer/1024,
1003                (t_xfer==0)?0:((float)hptr->xfer/t_xfer)*100.0,hptr->visit,
1004                (t_visit==0)?0:((float)hptr->visit/t_visit)*100.0,
1005                hptr->string,
1006                hptr->string);
1007          else
1008 #endif
1009          fprintf(out_fp,
1010             "%-8llu %6.02f%%  %8llu %6.02f%%  %8.0f %6.02f%%  "            \
1011             "%8llu %6.02f%%  %s\n",
1012             hptr->count,
1013             (t_hit==0)?0:((float)hptr->count/t_hit)*100.0,hptr->files,
1014             (t_file==0)?0:((float)hptr->files/t_file)*100.0,hptr->xfer/1024,
1015             (t_xfer==0)?0:((float)hptr->xfer/t_xfer)*100.0,hptr->visit,
1016             (t_visit==0)?0:((float)hptr->visit/t_visit)*100.0,
1017             hptr->string);
1018          h_reg--;
1019       }
1020    }
1021 
1022    fprintf(out_fp,"</PRE></FONT>\n");
1023    write_html_tail(out_fp);
1024    fclose(out_fp);
1025    return 1;
1026 }
1027 
1028 /*********************************************/
1029 /* TOP_URLS_TABLE - generate top n table     */
1030 /*********************************************/
1031 
top_urls_table(int flag)1032 void top_urls_table(int flag)
1033 {
1034    u_int64_t cnt=0,u_reg=0,u_grp=0,u_hid=0, tot_num;
1035    int       i;
1036    UNODEPTR  uptr, *pointer;
1037 
1038    cnt=a_ctr; pointer=u_array;
1039    while (cnt--)
1040    {
1041       /* calculate totals */
1042       switch ( (int)((UNODEPTR)(*pointer)->flag) )
1043       {
1044          case OBJ_REG:  u_reg++;  break;
1045          case OBJ_GRP:  u_grp++;  break;
1046          case OBJ_HIDE: u_hid++;  break;
1047       }
1048       pointer++;
1049    }
1050 
1051    if ( (tot_num=u_reg+u_grp)==0 ) return;              /* split if none    */
1052    i=(flag)?ntop_urlsK:ntop_urls;                       /* Hits or KBytes?? */
1053    if (tot_num > i) tot_num = i;                        /* get max to do... */
1054    if ((!flag) || (flag&&!ntop_urls))                   /* now do <A> tag   */
1055       fprintf(out_fp,"<A NAME=\"TOPURLS\"></A>\n");
1056 
1057    fprintf(out_fp,"<TABLE WIDTH=510 BORDER=2 CELLSPACING=1 CELLPADDING=1>\n");
1058    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1059    if (flag) fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" ALIGN=CENTER COLSPAN=6>"  \
1060            "%s %llu %s %llu %s %s %s</TH></TR>\n",
1061            GREY,msg_top_top,tot_num,msg_top_of,
1062            t_url,msg_top_u,msg_h_by,msg_h_xfer);
1063    else fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" ALIGN=CENTER COLSPAN=6>"   \
1064            "%s %llu %s %llu %s</TH></TR>\n",
1065            GREY,msg_top_top,tot_num,msg_top_of,t_url,msg_top_u);
1066    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1067    fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" ALIGN=center>"                  \
1068                   "<FONT SIZE=\"-1\">#</FONT></TH>\n",GREY);
1069    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center COLSPAN=2>"            \
1070                   "<FONT SIZE=\"-1\">%s</FONT></TH>\n",
1071                   HITCOLOR,msg_h_hits);
1072    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center COLSPAN=2>"            \
1073                   "<FONT SIZE=\"-1\">%s</FONT></TH>\n",
1074                   KBYTECOLOR,msg_h_xfer);
1075    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center>"                      \
1076                   "<FONT SIZE=\"-1\">%s</FONT></TH></TR>\n",
1077                   MISCCOLOR,msg_h_url);
1078    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1079 
1080    pointer=u_array; i=0;
1081    while (tot_num)
1082    {
1083       uptr=*pointer++;             /* point to the URL node */
1084       if (uptr->flag != OBJ_HIDE)
1085       {
1086          /* shade grouping? */
1087          if (shade_groups && (uptr->flag==OBJ_GRP))
1088             fprintf(out_fp,"<TR BGCOLOR=\"%s\">\n", GRPCOLOR);
1089          else fprintf(out_fp,"<TR>\n");
1090 
1091          fprintf(out_fp,
1092             "<TD ALIGN=center><FONT SIZE=\"-1\"><B>%d</B></FONT></TD>\n" \
1093             "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n"\
1094             "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"   \
1095             "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%.0f</B></FONT></TD>\n"\
1096             "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"   \
1097             "<TD ALIGN=left NOWRAP><FONT SIZE=\"-1\">",
1098             i+1,uptr->count,
1099             (t_hit==0)?0:((float)uptr->count/t_hit)*100.0,
1100             uptr->xfer/1024,
1101             (t_xfer==0)?0:((float)uptr->xfer/t_xfer)*100.0);
1102 
1103          if (uptr->flag==OBJ_GRP)
1104          {
1105             if (hlite_groups)
1106                fprintf(out_fp,"<STRONG>%s</STRONG></FONT></TD></TR>\n",
1107                 uptr->string);
1108             else fprintf(out_fp,"%s</FONT></TD></TR>\n",uptr->string);
1109          }
1110          else
1111 	 {
1112             /* check for a service prefix (ie: http://) */
1113             if (strstr(uptr->string,"://")!=NULL)
1114                fprintf(out_fp,"<A HREF=\"%s\">%s</A></FONT></TD></TR>\n",
1115                  uptr->string,uptr->string);
1116 	    else
1117             {
1118                if (log_type == LOG_FTP) /* FTP log? */
1119                    fprintf(out_fp,"%s</FONT></TD></TR>\n",uptr->string);
1120                else
1121                {             /* Web log  */
1122                   if (use_https)
1123                      /* secure server mode, use https:// */
1124                      fprintf(out_fp,
1125                      "<A HREF=\"https://%s%s\">%s</A></FONT></TD></TR>\n",
1126                       hname,uptr->string,uptr->string);
1127                    else
1128                       /* otherwise use standard 'http://' */
1129                       fprintf(out_fp,
1130                       "<A HREF=\"http://%s%s\">%s</A></FONT></TD></TR>\n",
1131                       hname,uptr->string,uptr->string);
1132                }
1133             }
1134 	 }
1135          tot_num--;
1136          i++;
1137       }
1138    }
1139    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1140    if ((!flag) || (flag&&!ntop_urls))
1141    {
1142       if ( (all_urls) && ((u_reg+u_grp)>ntop_urls) )
1143       {
1144          if (all_urls_page(u_reg, u_grp))
1145          {
1146             fprintf(out_fp,"<TR BGCOLOR=\"%s\">",GRPCOLOR);
1147             fprintf(out_fp,"<TD COLSPAN=6 ALIGN=\"center\">\n");
1148             fprintf(out_fp,"<FONT SIZE=\"-1\">");
1149             fprintf(out_fp,"<A HREF=\"./url_%04d%02d.%s\">",
1150                     cur_year,cur_month,html_ext);
1151             fprintf(out_fp,"%s</A></TD></TR>\n",msg_v_urls);
1152             if (flag)   /* do we need to sort first? */
1153                qsort(u_array,a_ctr,sizeof(UNODEPTR),qs_url_cmph);
1154          }
1155       }
1156    }
1157    fprintf(out_fp,"</TABLE>\n<P>\n");
1158 }
1159 
1160 /*********************************************/
1161 /* ALL_URLS_PAGE - HTML page of all urls     */
1162 /*********************************************/
1163 
all_urls_page(u_int64_t u_reg,u_int64_t u_grp)1164 int all_urls_page(u_int64_t u_reg, u_int64_t u_grp)
1165 {
1166    UNODEPTR uptr, *pointer;
1167    char     url_fname[256], buffer[256];
1168    FILE     *out_fp;
1169    int      i=(u_grp)?1:0;
1170 
1171    /* generate file name */
1172    snprintf(url_fname,sizeof(url_fname),"url_%04d%02d.%s",
1173             cur_year,cur_month,html_ext);
1174 
1175    /* open file */
1176    if ( (out_fp=open_out_file(url_fname))==NULL ) return 0;
1177 
1178    snprintf(buffer,sizeof(buffer),"%s %d - %s",
1179             l_month[cur_month-1],cur_year,msg_h_url);
1180    write_html_head(buffer, out_fp);
1181 
1182    fprintf(out_fp,"<FONT SIZE=\"-1\"></CENTER><PRE>\n");
1183 
1184    fprintf(out_fp," %12s      %12s      %s\n",
1185            msg_h_hits,msg_h_xfer,msg_h_url);
1186    fprintf(out_fp,"----------------  ----------------  " \
1187                   "--------------------\n\n");
1188 
1189    /* do groups first (if any) */
1190    pointer=u_array;
1191    while (u_grp)
1192    {
1193       uptr=*pointer++;
1194       if (uptr->flag == OBJ_GRP)
1195       {
1196          fprintf(out_fp,"%-8llu %6.02f%%  %8.0f %6.02f%%  %s\n",
1197             uptr->count,
1198             (t_hit==0)?0:((float)uptr->count/t_hit)*100.0,
1199             uptr->xfer/1024,
1200             (t_xfer==0)?0:((float)uptr->xfer/t_xfer)*100.0,
1201             uptr->string);
1202          u_grp--;
1203       }
1204    }
1205 
1206    if (i) fprintf(out_fp,"\n");
1207 
1208    /* now do invididual sites (if any) */
1209    pointer=u_array;
1210    while (u_reg)
1211    {
1212       uptr=*pointer++;
1213       if (uptr->flag == OBJ_REG)
1214       {
1215          fprintf(out_fp,"%-8llu %6.02f%%  %8.0f %6.02f%%  %s\n",
1216             uptr->count,
1217             (t_hit==0)?0:((float)uptr->count/t_hit)*100.0,
1218             uptr->xfer/1024,
1219             (t_xfer==0)?0:((float)uptr->xfer/t_xfer)*100.0,
1220             uptr->string);
1221          u_reg--;
1222       }
1223    }
1224 
1225    fprintf(out_fp,"</PRE></FONT>\n");
1226    write_html_tail(out_fp);
1227    fclose(out_fp);
1228    return 1;
1229 }
1230 
1231 /*********************************************/
1232 /* TOP_ENTRY_TABLE - top n entry/exit urls   */
1233 /*********************************************/
1234 
top_entry_table(int flag)1235 void top_entry_table(int flag)
1236 {
1237    u_int64_t cnt=0, u_entry=0, u_exit=0, tot_num;
1238    u_int64_t t_entry=0, t_exit=0;
1239    int       i;
1240    UNODEPTR  uptr, *pointer;
1241 
1242    cnt=a_ctr; pointer=u_array;
1243    while (cnt--)
1244    {
1245       if ( (int)((UNODEPTR)(*pointer)->flag) == OBJ_REG )
1246       {
1247          if ( (u_int64_t)(((UNODEPTR)(*pointer))->entry) )
1248             {  u_entry++; t_entry+=(u_int64_t)(((UNODEPTR)(*pointer))->entry); }
1249          if ( (u_int64_t)(((UNODEPTR)(*pointer))->exit)  )
1250             { u_exit++;   t_exit +=(u_int64_t)(((UNODEPTR)(*pointer))->exit);  }
1251       }
1252       pointer++;
1253    }
1254 
1255    /* calculate how many we have */
1256    tot_num=(flag)?u_exit:u_entry;
1257    if (flag) { if (tot_num > ntop_exit ) tot_num=ntop_exit;  }
1258    else      { if (tot_num > ntop_entry) tot_num=ntop_entry; }
1259 
1260    /* return if none to do */
1261    if (!tot_num) return;
1262 
1263    if (flag) fprintf(out_fp,"<A NAME=\"TOPEXIT\"></A>\n"); /* do anchor tag */
1264    else      fprintf(out_fp,"<A NAME=\"TOPENTRY\"></A>\n");
1265 
1266    fprintf(out_fp,"<TABLE WIDTH=510 BORDER=2 CELLSPACING=1 CELLPADDING=1>\n");
1267    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1268    fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" ALIGN=CENTER COLSPAN=6>"        \
1269            "%s %llu %s %llu %s</TH></TR>\n",
1270            GREY,msg_top_top,tot_num,msg_top_of,
1271            (flag)?u_exit:u_entry,(flag)?msg_top_ex:msg_top_en);
1272    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1273    fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" ALIGN=center>"                  \
1274                   "<FONT SIZE=\"-1\">#</FONT></TH>\n",
1275                   GREY);
1276    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center COLSPAN=2>"            \
1277                   "<FONT SIZE=\"-1\">%s</FONT></TH>\n",
1278                   HITCOLOR,msg_h_hits);
1279    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center COLSPAN=2>"            \
1280                   "<FONT SIZE=\"-1\">%s</FONT></TH>\n",
1281                   VISITCOLOR,msg_h_visits);
1282    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center>"                      \
1283                   "<FONT SIZE=\"-1\">%s</FONT></TH></TR>\n",
1284                   MISCCOLOR,msg_h_url);
1285    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1286 
1287    pointer=u_array; i=0;
1288    while (tot_num)
1289    {
1290       uptr=*pointer++;
1291       if (uptr->flag != OBJ_HIDE)
1292       {
1293          fprintf(out_fp,"<TR>\n");
1294          fprintf(out_fp,
1295              "<TD ALIGN=center><FONT SIZE=\"-1\"><B>%d</B></FONT></TD>\n" \
1296              "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n"\
1297              "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"   \
1298              "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n"\
1299              "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"   \
1300              "<TD ALIGN=left NOWRAP><FONT SIZE=\"-1\">",
1301              i+1,uptr->count,
1302              (t_hit==0)?0:((float)uptr->count/t_hit)*100.0,
1303              (flag)?uptr->exit:uptr->entry,
1304              (flag)?((t_exit==0)?0:((float)uptr->exit/t_exit)*100.0)
1305                    :((t_entry==0)?0:((float)uptr->entry/t_entry)*100.0));
1306 
1307          /* check for a service prefix (ie: http://) */
1308          if (strstr(uptr->string,"://")!=NULL)
1309           fprintf(out_fp,
1310              "<A HREF=\"%s\">%s</A></FONT></TD></TR>\n",
1311               uptr->string,uptr->string);
1312 	 else
1313          {
1314             if (use_https)
1315             /* secure server mode, use https:// */
1316              fprintf(out_fp,
1317                 "<A HREF=\"https://%s%s\">%s</A></FONT></TD></TR>\n",
1318                  hname,uptr->string,uptr->string);
1319             else
1320             /* otherwise use standard 'http://' */
1321              fprintf(out_fp,
1322                 "<A HREF=\"http://%s%s\">%s</A></FONT></TD></TR>\n",
1323                  hname,uptr->string,uptr->string);
1324 	 }
1325          tot_num--;
1326          i++;
1327       }
1328    }
1329    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1330    fprintf(out_fp,"</TABLE>\n<P>\n");
1331 }
1332 
1333 /*********************************************/
1334 /* TOP_REFS_TABLE - generate top n table     */
1335 /*********************************************/
1336 
top_refs_table()1337 void top_refs_table()
1338 {
1339    u_int64_t cnt=0, r_reg=0, r_grp=0, r_hid=0, tot_num;
1340    int       i;
1341    RNODEPTR  rptr, *pointer;
1342 
1343    if (t_ref==0) return;        /* return if none to process */
1344 
1345    cnt=a_ctr; pointer=r_array;
1346    while(cnt--)
1347    {
1348       /* calculate totals */
1349       switch ( (int)((RNODEPTR)(*pointer)->flag) )
1350       {
1351          case OBJ_REG:  r_reg++;  break;
1352          case OBJ_HIDE: r_hid++;  break;
1353          case OBJ_GRP:  r_grp++;  break;
1354       }
1355       pointer++;
1356    }
1357 
1358    if ( (tot_num=r_reg+r_grp)==0 ) return;              /* split if none    */
1359    if (tot_num > ntop_refs) tot_num=ntop_refs;          /* get max to do... */
1360 
1361    fprintf(out_fp,"<A NAME=\"TOPREFS\"></A>\n");
1362    fprintf(out_fp,"<TABLE WIDTH=510 BORDER=2 CELLSPACING=1 CELLPADDING=1>\n");
1363    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1364    fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" ALIGN=CENTER COLSPAN=4>"         \
1365            "%s %llu %s %llu %s</TH></TR>\n",
1366            GREY, msg_top_top, tot_num, msg_top_of, t_ref, msg_top_r);
1367    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1368    fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" ALIGN=center>"                   \
1369           "<FONT SIZE=\"-1\">#</FONT></TH>\n",
1370           GREY);
1371    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center COLSPAN=2>"             \
1372           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",
1373           HITCOLOR,msg_h_hits);
1374    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center>"                       \
1375           "<FONT SIZE=\"-1\">%s</FONT></TH></TR>\n",
1376           MISCCOLOR,msg_h_ref);
1377    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1378 
1379    pointer=r_array; i=0;
1380    while(tot_num)
1381    {
1382       rptr=*pointer++;
1383       if (rptr->flag != OBJ_HIDE)
1384       {
1385          /* shade grouping? */
1386          if (shade_groups && (rptr->flag==OBJ_GRP))
1387             fprintf(out_fp,"<TR BGCOLOR=\"%s\">\n", GRPCOLOR);
1388          else fprintf(out_fp,"<TR>\n");
1389 
1390          fprintf(out_fp,
1391              "<TD ALIGN=center><FONT SIZE=\"-1\"><B>%d</B></FONT></TD>\n"  \
1392              "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n" \
1393              "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"    \
1394              "<TD ALIGN=left NOWRAP><FONT SIZE=\"-1\">",
1395              i+1,rptr->count,
1396              (t_hit==0)?0:((float)rptr->count/t_hit)*100.0);
1397 
1398          if (rptr->flag==OBJ_GRP)
1399          {
1400             if (hlite_groups)
1401                fprintf(out_fp,"<STRONG>%s</STRONG>",rptr->string);
1402             else fprintf(out_fp,"%s",rptr->string);
1403          }
1404          else
1405          {
1406             /* only link if enabled and has a service prefix */
1407             if ( (strstr(rptr->string,"://")!=NULL) && link_referrer )
1408                fprintf(out_fp,"<A HREF=\"%s\" rel=\"nofollow\">%s</A>",
1409                        rptr->string, rptr->string);
1410             else
1411                fprintf(out_fp,"%s", rptr->string);
1412          }
1413          fprintf(out_fp,"</FONT></TD></TR>\n");
1414          tot_num--;
1415          i++;
1416       }
1417    }
1418    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1419    if ( (all_refs) && ((r_reg+r_grp)>ntop_refs) )
1420    {
1421       if (all_refs_page(r_reg, r_grp))
1422       {
1423          fprintf(out_fp,"<TR BGCOLOR=\"%s\">",GRPCOLOR);
1424          fprintf(out_fp,"<TD COLSPAN=4 ALIGN=\"center\">\n");
1425          fprintf(out_fp,"<FONT SIZE=\"-1\">");
1426          fprintf(out_fp,"<A HREF=\"./ref_%04d%02d.%s\">",
1427                  cur_year,cur_month,html_ext);
1428          fprintf(out_fp,"%s</A></TD></TR>\n",msg_v_refs);
1429       }
1430    }
1431    fprintf(out_fp,"</TABLE>\n<P>\n");
1432 }
1433 
1434 /*********************************************/
1435 /* ALL_REFS_PAGE - HTML page of all refs     */
1436 /*********************************************/
1437 
all_refs_page(u_int64_t r_reg,u_int64_t r_grp)1438 int all_refs_page(u_int64_t r_reg, u_int64_t r_grp)
1439 {
1440    RNODEPTR rptr, *pointer;
1441    char     ref_fname[256], buffer[256];
1442    FILE     *out_fp;
1443    int      i=(r_grp)?1:0;
1444 
1445    /* generate file name */
1446    snprintf(ref_fname,sizeof(ref_fname),"ref_%04d%02d.%s",
1447             cur_year,cur_month,html_ext);
1448 
1449    /* open file */
1450    if ( (out_fp=open_out_file(ref_fname))==NULL ) return 0;
1451 
1452    snprintf(buffer,sizeof(buffer),"%s %d - %s",
1453             l_month[cur_month-1],cur_year,msg_h_ref);
1454    write_html_head(buffer, out_fp);
1455 
1456    fprintf(out_fp,"<FONT SIZE=\"-1\"></CENTER><PRE>\n");
1457 
1458    fprintf(out_fp," %12s      %s\n",msg_h_hits,msg_h_ref);
1459    fprintf(out_fp,"----------------  --------------------\n\n");
1460 
1461    /* do groups first (if any) */
1462    pointer=r_array;
1463    while(r_grp)
1464    {
1465       rptr=*pointer++;
1466       if (rptr->flag == OBJ_GRP)
1467       {
1468          fprintf(out_fp,"%-8llu %6.02f%%  %s\n",
1469             rptr->count,
1470             (t_hit==0)?0:((float)rptr->count/t_hit)*100.0,
1471             rptr->string);
1472          r_grp--;
1473       }
1474    }
1475 
1476    if (i) fprintf(out_fp,"\n");
1477 
1478    pointer=r_array;
1479    while(r_reg)
1480    {
1481       rptr=*pointer++;
1482       if (rptr->flag == OBJ_REG)
1483       {
1484          fprintf(out_fp,"%-8llu %6.02f%%  %s\n",
1485             rptr->count,
1486             (t_hit==0)?0:((float)rptr->count/t_hit)*100.0,
1487             rptr->string);
1488          r_reg--;
1489       }
1490    }
1491 
1492    fprintf(out_fp,"</PRE></FONT>\n");
1493    write_html_tail(out_fp);
1494    fclose(out_fp);
1495    return 1;
1496 }
1497 
1498 /*********************************************/
1499 /* TOP_AGENTS_TABLE - generate top n table   */
1500 /*********************************************/
1501 
top_agents_table()1502 void top_agents_table()
1503 {
1504    u_int64_t cnt, a_reg=0, a_grp=0, a_hid=0, tot_num;
1505    int       i;
1506    ANODEPTR  aptr, *pointer;
1507 
1508    if (t_agent == 0) return;    /* don't bother if we don't have any */
1509 
1510    cnt=a_ctr; pointer=a_array;
1511    while(cnt--)
1512    {
1513       /* calculate totals */
1514       switch ( (int)((ANODEPTR)(*pointer)->flag) )
1515       {
1516          case OBJ_REG:   a_reg++;  break;
1517          case OBJ_GRP:   a_grp++;  break;
1518          case OBJ_HIDE:  a_hid++;  break;
1519       }
1520       pointer++;
1521    }
1522 
1523    if ( (tot_num=a_reg+a_grp)==0 ) return;              /* split if none    */
1524    if (tot_num > ntop_agents) tot_num=ntop_agents;      /* get max to do... */
1525 
1526    fprintf(out_fp,"<A NAME=\"TOPAGENTS\"></A>\n");
1527    fprintf(out_fp,"<TABLE WIDTH=510 BORDER=2 CELLSPACING=1 CELLPADDING=1>\n");
1528    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1529    fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" ALIGN=CENTER COLSPAN=4>"        \
1530           "%s %llu %s %llu %s</TH></TR>\n",
1531           GREY, msg_top_top, tot_num, msg_top_of, t_agent, msg_top_a);
1532    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1533    fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" ALIGN=center>"                  \
1534           "<FONT SIZE=\"-1\">#</FONT></TH>\n",
1535           GREY);
1536    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center COLSPAN=2>"            \
1537           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",
1538           HITCOLOR,msg_h_hits);
1539    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center>"                      \
1540           "<FONT SIZE=\"-1\">%s</FONT></TH></TR>\n",
1541           MISCCOLOR,msg_h_agent);
1542    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1543 
1544    pointer=a_array; i=0;
1545    while(tot_num)
1546    {
1547       aptr=*pointer++;
1548       if (aptr->flag != OBJ_HIDE)
1549       {
1550          /* shade grouping? */
1551          if (shade_groups && (aptr->flag==OBJ_GRP))
1552             fprintf(out_fp,"<TR BGCOLOR=\"%s\">\n", GRPCOLOR);
1553          else fprintf(out_fp,"<TR>\n");
1554 
1555          fprintf(out_fp,
1556              "<TD ALIGN=center><FONT SIZE=\"-1\"><B>%d</B></FONT></TD>\n" \
1557              "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n"\
1558              "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"   \
1559              "<TD ALIGN=left NOWRAP><FONT SIZE=\"-1\">",
1560              i+1,aptr->count,
1561              (t_hit==0)?0:((float)aptr->count/t_hit)*100.0);
1562 
1563          if ((aptr->flag==OBJ_GRP)&&hlite_groups)
1564             fprintf(out_fp,"<STRONG>%s</STRONG></FONT></TD></TR>\n",
1565                aptr->string);
1566          else fprintf(out_fp,"%s</FONT></TD></TR>\n",
1567                aptr->string);
1568          tot_num--;
1569          i++;
1570       }
1571    }
1572    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1573    if ( (all_agents) && ((a_reg+a_grp)>ntop_agents) )
1574    {
1575       if (all_agents_page(a_reg, a_grp))
1576       {
1577          fprintf(out_fp,"<TR BGCOLOR=\"%s\">",GRPCOLOR);
1578          fprintf(out_fp,"<TD COLSPAN=4 ALIGN=\"center\">\n");
1579          fprintf(out_fp,"<FONT SIZE=\"-1\">");
1580          fprintf(out_fp,"<A HREF=\"./agent_%04d%02d.%s\">",
1581                  cur_year,cur_month,html_ext);
1582          fprintf(out_fp,"%s</A></TD></TR>\n",msg_v_agents);
1583       }
1584    }
1585    fprintf(out_fp,"</TABLE>\n<P>\n");
1586 }
1587 
1588 /*********************************************/
1589 /* ALL_AGENTS_PAGE - HTML user agent page    */
1590 /*********************************************/
1591 
all_agents_page(u_int64_t a_reg,u_int64_t a_grp)1592 int all_agents_page(u_int64_t a_reg, u_int64_t a_grp)
1593 {
1594    ANODEPTR aptr, *pointer;
1595    char     agent_fname[256], buffer[256];
1596    FILE     *out_fp;
1597    int      i=(a_grp)?1:0;
1598 
1599    /* generate file name */
1600    snprintf(agent_fname,sizeof(agent_fname),"agent_%04d%02d.%s",
1601             cur_year,cur_month,html_ext);
1602 
1603    /* open file */
1604    if ( (out_fp=open_out_file(agent_fname))==NULL ) return 0;
1605 
1606    snprintf(buffer,sizeof(buffer),"%s %d - %s",
1607             l_month[cur_month-1],cur_year,msg_h_agent);
1608    write_html_head(buffer, out_fp);
1609 
1610    fprintf(out_fp,"<FONT SIZE=\"-1\"></CENTER><PRE>\n");
1611 
1612    fprintf(out_fp," %12s      %s\n",msg_h_hits,msg_h_agent);
1613    fprintf(out_fp,"----------------  ----------------------\n\n");
1614 
1615    /* do groups first (if any) */
1616    pointer=a_array;
1617    while(a_grp)
1618    {
1619       aptr=*pointer++;
1620       if (aptr->flag == OBJ_GRP)
1621       {
1622          fprintf(out_fp,"%-8llu %6.02f%%  %s\n",
1623              aptr->count,
1624              (t_hit==0)?0:((float)aptr->count/t_hit)*100.0,
1625              aptr->string);
1626          a_grp--;
1627       }
1628    }
1629 
1630    if (i) fprintf(out_fp,"\n");
1631 
1632    pointer=a_array;
1633    while(a_reg)
1634    {
1635       aptr=*pointer++;
1636       if (aptr->flag == OBJ_REG)
1637       {
1638          fprintf(out_fp,"%-8llu %6.02f%%  %s\n",
1639              aptr->count,
1640              (t_hit==0)?0:((float)aptr->count/t_hit)*100.0,
1641              aptr->string);
1642          a_reg--;
1643       }
1644    }
1645 
1646    fprintf(out_fp,"</PRE></FONT>\n");
1647    write_html_tail(out_fp);
1648    fclose(out_fp);
1649    return 1;
1650 }
1651 
1652 /*********************************************/
1653 /* TOP_SEARCH_TABLE - generate top n table   */
1654 /*********************************************/
1655 
top_search_table()1656 void top_search_table()
1657 {
1658    u_int64_t cnt,t_val=0, tot_num;
1659    int       i;
1660    SNODEPTR  sptr, *pointer;
1661 
1662    if (a_ctr==0) return;                  /* don't bother if none to do    */
1663 
1664    cnt=tot_num=a_ctr; pointer=s_array;
1665    while(cnt--)
1666    {
1667       t_val+=(u_int64_t)(((SNODEPTR)(*pointer))->count);
1668       pointer++;
1669    }
1670 
1671    if ( tot_num > ntop_search) tot_num=ntop_search;
1672 
1673    fprintf(out_fp,"<A NAME=\"TOPSEARCH\"></A>\n");
1674    fprintf(out_fp,"<TABLE WIDTH=510 BORDER=2 CELLSPACING=1 CELLPADDING=1>\n");
1675    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1676    fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" ALIGN=CENTER COLSPAN=4>"        \
1677           "%s %llu %s %llu %s</TH></TR>\n",
1678           GREY, msg_top_top, tot_num, msg_top_of, a_ctr, msg_top_sr);
1679    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1680    fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" ALIGN=center>"                  \
1681           "<FONT SIZE=\"-1\">#</FONT></TH>\n",
1682           GREY);
1683    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center COLSPAN=2>"            \
1684           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",
1685           HITCOLOR,msg_h_hits);
1686    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center>"                      \
1687           "<FONT SIZE=\"-1\">%s</FONT></TH></TR>\n",
1688           MISCCOLOR,msg_h_search);
1689    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1690 
1691    pointer=s_array; i=0;
1692    while(tot_num)
1693    {
1694       sptr=*pointer++;
1695       fprintf(out_fp,
1696          "<TR>\n"                                                     \
1697          "<TD ALIGN=center><FONT SIZE=\"-1\"><B>%d</B></FONT></TD>\n" \
1698          "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n"\
1699          "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"   \
1700          "<TD ALIGN=left NOWRAP><FONT SIZE=\"-1\">",
1701          i+1,sptr->count,
1702          (t_val==0)?0:((float)sptr->count/t_val)*100.0);
1703       fprintf(out_fp,"%s</FONT></TD></TR>\n",sptr->string);
1704       tot_num--;
1705       i++;
1706    }
1707    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1708    if ( (all_search) && (a_ctr>ntop_search) )
1709    {
1710       if (all_search_page(a_ctr, t_val))
1711       {
1712          fprintf(out_fp,"<TR BGCOLOR=\"%s\">",GRPCOLOR);
1713          fprintf(out_fp,"<TD COLSPAN=4 ALIGN=\"center\">\n");
1714          fprintf(out_fp,"<FONT SIZE=\"-1\">");
1715          fprintf(out_fp,"<A HREF=\"./search_%04d%02d.%s\">",
1716                  cur_year,cur_month,html_ext);
1717          fprintf(out_fp,"%s</A></TD></TR>\n",msg_v_search);
1718       }
1719    }
1720    fprintf(out_fp,"</TABLE>\n<P>\n");
1721 }
1722 
1723 /*********************************************/
1724 /* ALL_SEARCH_PAGE - HTML for search strings */
1725 /*********************************************/
1726 
all_search_page(u_int64_t tot_num,u_int64_t t_val)1727 int all_search_page(u_int64_t tot_num, u_int64_t t_val)
1728 {
1729    SNODEPTR sptr, *pointer;
1730    char     search_fname[256], buffer[256];
1731    FILE     *out_fp;
1732 
1733    if (!tot_num) return 0;
1734 
1735    /* generate file name */
1736    snprintf(search_fname,sizeof(search_fname),"search_%04d%02d.%s",
1737             cur_year,cur_month,html_ext);
1738 
1739    /* open file */
1740    if ( (out_fp=open_out_file(search_fname))==NULL ) return 0;
1741 
1742    snprintf(buffer,sizeof(buffer),"%s %d - %s",
1743             l_month[cur_month-1],cur_year,msg_h_search);
1744    write_html_head(buffer, out_fp);
1745 
1746    fprintf(out_fp,"<FONT SIZE=\"-1\"></CENTER><PRE>\n");
1747 
1748    fprintf(out_fp," %12s      %s\n",msg_h_hits,msg_h_search);
1749    fprintf(out_fp,"----------------  ----------------------\n\n");
1750 
1751    pointer=s_array;
1752    while(tot_num)
1753    {
1754       sptr=*pointer++;
1755       fprintf(out_fp,"%-8llu %6.02f%%  %s\n",
1756          sptr->count,
1757          (t_val==0)?0:((float)sptr->count/t_val)*100.0,
1758          sptr->string);
1759       tot_num--;
1760    }
1761    fprintf(out_fp,"</PRE></FONT>\n");
1762    write_html_tail(out_fp);
1763    fclose(out_fp);
1764    return 1;
1765 }
1766 
1767 /*********************************************/
1768 /* TOP_USERS_TABLE - generate top n table    */
1769 /*********************************************/
1770 
top_users_table()1771 void top_users_table()
1772 {
1773    u_int64_t cnt=0, i_reg=0, i_grp=0, i_hid=0, tot_num;
1774    int       i;
1775    INODEPTR  iptr, *pointer;
1776 
1777    cnt=a_ctr; pointer=i_array;
1778    while(cnt--)
1779    {
1780       /* calculate totals */
1781       switch ( (int)((INODEPTR)(*pointer)->flag) )
1782       {
1783          case OBJ_REG:   i_reg++;  break;
1784          case OBJ_GRP:   i_grp++;  break;
1785          case OBJ_HIDE:  i_hid++;  break;
1786       }
1787       pointer++;
1788    }
1789 
1790    if ( (tot_num=i_reg+i_grp)==0 ) return;              /* split if none    */
1791    if (tot_num > ntop_users) tot_num = ntop_users;
1792 
1793    fprintf(out_fp,"<A NAME=\"TOPUSERS\"></A>\n");       /* now do <A> tag   */
1794 
1795    fprintf(out_fp,"<TABLE WIDTH=510 BORDER=2 CELLSPACING=1 CELLPADDING=1>\n");
1796    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1797    fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" ALIGN=CENTER COLSPAN=10>" \
1798            "%s %llu %s %llu %s</TH></TR>\n",
1799            GREY,msg_top_top, tot_num, msg_top_of, t_user, msg_top_i);
1800    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1801    fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" ALIGN=center>"                   \
1802           "<FONT SIZE=\"-1\">#</FONT></TH>\n",GREY);
1803    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center COLSPAN=2>"             \
1804           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",HITCOLOR,msg_h_hits);
1805    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center COLSPAN=2>"             \
1806           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",FILECOLOR,msg_h_files);
1807    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center COLSPAN=2>"             \
1808           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",KBYTECOLOR,msg_h_xfer);
1809    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center COLSPAN=2>"             \
1810           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",VISITCOLOR,msg_h_visits);
1811    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center>"                       \
1812           "<FONT SIZE=\"-1\">%s</FONT></TH></TR>\n",MISCCOLOR,msg_h_uname);
1813    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1814 
1815    pointer=i_array; i=0;
1816    while(tot_num)
1817    {
1818       iptr=*pointer++;
1819       if (iptr->flag != OBJ_HIDE)
1820       {
1821          /* shade grouping? */
1822          if (shade_groups && (iptr->flag==OBJ_GRP))
1823             fprintf(out_fp,"<TR BGCOLOR=\"%s\">\n", GRPCOLOR);
1824          else fprintf(out_fp,"<TR>\n");
1825 
1826          fprintf(out_fp,
1827               "<TD ALIGN=center><FONT SIZE=\"-1\"><B>%d</B></FONT></TD>\n"  \
1828               "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n" \
1829               "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"    \
1830               "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n" \
1831               "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"    \
1832               "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%.0f</B></FONT></TD>\n" \
1833               "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"    \
1834               "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n" \
1835               "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"    \
1836               "<TD ALIGN=left NOWRAP><FONT SIZE=\"-1\">",
1837               i+1,iptr->count,
1838               (t_hit==0)?0:((float)iptr->count/t_hit)*100.0,iptr->files,
1839               (t_file==0)?0:((float)iptr->files/t_file)*100.0,iptr->xfer/1024,
1840               (t_xfer==0)?0:((float)iptr->xfer/t_xfer)*100.0,iptr->visit,
1841               (t_visit==0)?0:((float)iptr->visit/t_visit)*100.0);
1842 
1843          if ((iptr->flag==OBJ_GRP)&&hlite_groups)
1844              fprintf(out_fp,"<STRONG>%s</STRONG></FONT></TD></TR>\n",
1845                iptr->string);
1846          else fprintf(out_fp,"%s</FONT></TD></TR>\n",
1847                iptr->string);
1848          tot_num--;
1849          i++;
1850       }
1851    }
1852 
1853    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
1854    if ( (all_users) && ((i_reg+i_grp)>ntop_users) )
1855    {
1856       if (all_users_page(i_reg, i_grp))
1857       {
1858          fprintf(out_fp,"<TR BGCOLOR=\"%s\">",GRPCOLOR);
1859          fprintf(out_fp,"<TD COLSPAN=10 ALIGN=\"center\">\n");
1860          fprintf(out_fp,"<FONT SIZE=\"-1\">");
1861          fprintf(out_fp,"<A HREF=\"./user_%04d%02d.%s\">",
1862             cur_year,cur_month,html_ext);
1863          fprintf(out_fp,"%s</A></TD></TR>\n",msg_v_users);
1864       }
1865    }
1866    fprintf(out_fp,"</TABLE>\n<P>\n");
1867 }
1868 
1869 /*********************************************/
1870 /* ALL_USERS_PAGE - HTML of all usernames    */
1871 /*********************************************/
1872 
all_users_page(u_int64_t i_reg,u_int64_t i_grp)1873 int all_users_page(u_int64_t i_reg, u_int64_t i_grp)
1874 {
1875    INODEPTR iptr, *pointer;
1876    char     user_fname[256], buffer[256];
1877    FILE     *out_fp;
1878    int      i=(i_grp)?1:0;
1879 
1880    /* generate file name */
1881    snprintf(user_fname,sizeof(user_fname),"user_%04d%02d.%s",
1882             cur_year,cur_month,html_ext);
1883 
1884    /* open file */
1885    if ( (out_fp=open_out_file(user_fname))==NULL ) return 0;
1886 
1887    snprintf(buffer,sizeof(buffer),"%s %d - %s",
1888             l_month[cur_month-1],cur_year,msg_h_uname);
1889    write_html_head(buffer, out_fp);
1890 
1891    fprintf(out_fp,"<FONT SIZE=\"-1\"></CENTER><PRE>\n");
1892 
1893    fprintf(out_fp," %12s      %12s      %12s      %12s      %s\n",
1894            msg_h_hits, msg_h_files, msg_h_xfer, msg_h_visits, msg_h_uname);
1895    fprintf(out_fp,"----------------  ----------------  ----------------  " \
1896                   "----------------  --------------------\n\n");
1897 
1898    /* Do groups first (if any) */
1899    pointer=i_array;
1900    while(i_grp)
1901    {
1902       iptr=*pointer++;
1903       if (iptr->flag == OBJ_GRP)
1904       {
1905          fprintf(out_fp,
1906       "%-8llu %6.02f%%  %8llu %6.02f%%  %8.0f %6.02f%%  %8llu %6.02f%%  %s\n",
1907             iptr->count,
1908             (t_hit==0)?0:((float)iptr->count/t_hit)*100.0,iptr->files,
1909             (t_file==0)?0:((float)iptr->files/t_file)*100.0,iptr->xfer/1024,
1910             (t_xfer==0)?0:((float)iptr->xfer/t_xfer)*100.0,iptr->visit,
1911             (t_visit==0)?0:((float)iptr->visit/t_visit)*100.0,
1912             iptr->string);
1913          i_grp--;
1914       }
1915    }
1916 
1917    if (i) fprintf(out_fp,"\n");
1918 
1919    /* Now do individual users (if any) */
1920    pointer=i_array;
1921    while(i_reg)
1922    {
1923       iptr=*pointer++;
1924       if (iptr->flag == OBJ_REG)
1925       {
1926          fprintf(out_fp,
1927       "%-8llu %6.02f%%  %8llu %6.02f%%  %8.0f %6.02f%%  %8llu %6.02f%%  %s\n",
1928             iptr->count,
1929             (t_hit==0)?0:((float)iptr->count/t_hit)*100.0,iptr->files,
1930             (t_file==0)?0:((float)iptr->files/t_file)*100.0,iptr->xfer/1024,
1931             (t_xfer==0)?0:((float)iptr->xfer/t_xfer)*100.0,iptr->visit,
1932             (t_visit==0)?0:((float)iptr->visit/t_visit)*100.0,
1933             iptr->string);
1934          i_reg--;
1935       }
1936    }
1937 
1938    fprintf(out_fp,"</PRE></FONT>\n");
1939    write_html_tail(out_fp);
1940    fclose(out_fp);
1941    return 1;
1942 }
1943 
1944 /*********************************************/
1945 /* TOP_CTRY_TABLE - top countries table      */
1946 /*********************************************/
1947 
top_ctry_table()1948 void top_ctry_table()
1949 {
1950    int       i,j,x,tot_num=0,tot_ctry=0;
1951    int       ctry_fnd=0;
1952    u_int64_t idx;
1953    HNODEPTR  hptr;
1954    char      *domain;
1955    u_int64_t pie_data[10];
1956    char      *pie_legend[10];
1957    char      pie_title[48];
1958    char      pie_fname[48];
1959    char      flag_buf[256];
1960 
1961    extern int ctry_graph;  /* include external flag */
1962 
1963 #ifdef USE_GEOIP
1964    extern int    geoip;
1965    extern MMDB_s mmdb;
1966    const  char   *geo_rc=NULL;
1967 #endif
1968    char          geo_ctry[3]="--";
1969 
1970    /* scan hash table adding up domain totals */
1971    for (i=0;i<MAXHASH;i++)
1972    {
1973       hptr=sm_htab[i];
1974       while (hptr!=NULL)
1975       {
1976          if (hptr->flag != OBJ_GRP)   /* ignore group totals */
1977          {
1978             if (isipaddr(hptr->string)>0)
1979             {
1980                idx=0;                 /* unresolved/unknown  */
1981 #ifdef USE_DNS
1982                if (geodb)
1983                {
1984                   /* Lookup IP address here, turn into idx   */
1985                   geodb_get_cc(geo_db, hptr->string, geo_ctry);
1986                   if (geo_ctry[0]=='-')
1987                   {
1988                      if (debug_mode)
1989                         fprintf(stderr,"GeoDB: %s unknown!\n",hptr->string);
1990                   }
1991                   else idx=ctry_idx(geo_ctry);
1992                }
1993 #endif
1994 #ifdef USE_GEOIP
1995                if (geoip)
1996                {
1997                   /* Lookup IP address here,  turn into idx  */
1998                   MMDB_lookup_result_s result;
1999                   MMDB_entry_data_s entry_data;
2000                   int gai_error, mmdb_error, rc;
2001                   result=MMDB_lookup_string(&mmdb, hptr->string, &gai_error, &mmdb_error);
2002                   if (gai_error!=0||mmdb_error!=MMDB_SUCCESS||!result.found_entry)
2003                   {
2004                      if (debug_mode)
2005                         fprintf(stderr,"GeoIP: %s unknown (returns '%s')\n",
2006                                 hptr->string,MMDB_strerror(mmdb_error));
2007                   }
2008                   else
2009                   {
2010                      rc = MMDB_get_value(&result.entry, &entry_data, "country", "iso_code", NULL);
2011                      if (rc==MMDB_SUCCESS&&entry_data.has_data)
2012                      {
2013                        /* index returned geo_ctry */
2014                        geo_ctry[0]=tolower(entry_data.utf8_string[0]);
2015                        geo_ctry[1]=tolower(entry_data.utf8_string[1]);
2016                        idx=ctry_idx(geo_ctry);
2017                      }
2018                   }
2019                }
2020 #endif /* USE_GEOIP */
2021             }
2022             else
2023             {
2024                /* resolved hostname.. try to get TLD */
2025                domain = hptr->string+strlen(hptr->string)-1;
2026                while ( (*domain!='.')&&(domain!=hptr->string)) domain--;
2027                if (domain++==hptr->string) idx=0;
2028                else idx=ctry_idx(domain);
2029             }
2030             if (idx!=0)
2031             {
2032                ctry_fnd=0;
2033                for (j=0;ctry[j].desc;j++)
2034                {
2035                   if (idx==ctry[j].idx)
2036                   {
2037                      ctry[j].count+=hptr->count;
2038                      ctry[j].files+=hptr->files;
2039                      ctry[j].xfer +=hptr->xfer;
2040                      ctry_fnd=1;
2041                      break;
2042                   }
2043                }
2044             }
2045             if (!ctry_fnd || idx==0)
2046             {
2047                ctry[0].count+=hptr->count;
2048                ctry[0].files+=hptr->files;
2049                ctry[0].xfer +=hptr->xfer;
2050             }
2051          }
2052          hptr=hptr->next;
2053       }
2054    }
2055 
2056    for (i=0;ctry[i].desc;i++)
2057    {
2058       if (ctry[i].count!=0) tot_ctry++;
2059       for (j=0;j<ntop_ctrys;j++)
2060       {
2061          if (top_ctrys[j]==NULL) { top_ctrys[j]=&ctry[i]; break; }
2062          else
2063          {
2064             if (ctry[i].count > top_ctrys[j]->count)
2065             {
2066                for (x=ntop_ctrys-1;x>j;x--)
2067                   top_ctrys[x]=top_ctrys[x-1];
2068                top_ctrys[x]=&ctry[i];
2069                break;
2070             }
2071          }
2072       }
2073    }
2074 
2075    /* put our anchor tag first... */
2076    fprintf(out_fp,"<A NAME=\"TOPCTRYS\"></A>\n");
2077 
2078    /* generate pie chart if needed */
2079    if (ctry_graph)
2080    {
2081       for (i=0;i<10;i++) pie_data[i]=0;             /* init data array      */
2082       if (ntop_ctrys<10) j=ntop_ctrys; else j=10;   /* ensure data size     */
2083 
2084       for (i=0;i<j;i++)
2085       {
2086          pie_data[i]=top_ctrys[i]->count;           /* load the array       */
2087          pie_legend[i]=top_ctrys[i]->desc;
2088       }
2089       snprintf(pie_title,sizeof(pie_title),"%s %s %d",
2090                msg_ctry_use,l_month[cur_month-1],cur_year);
2091       sprintf(pie_fname,"ctry_usage_%04d%02d.png",cur_year,cur_month);
2092 
2093       pie_chart(pie_fname,pie_title,t_hit,pie_data,pie_legend);  /* do it   */
2094 
2095       /* put the image tag in the page */
2096       fprintf(out_fp,"<IMG SRC=\"%s\" ALT=\"%s\" " \
2097                   "HEIGHT=300 WIDTH=512><P>\n",pie_fname,pie_title);
2098    }
2099 
2100    /* Now do the table */
2101 
2102    for (i=0;i<ntop_ctrys;i++) if (top_ctrys[i]->count!=0) tot_num++;
2103    fprintf(out_fp,"<TABLE WIDTH=510 BORDER=2 CELLSPACING=1 CELLPADDING=1>\n");
2104    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
2105    fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" ALIGN=CENTER COLSPAN=8>"         \
2106            "%s %d %s %d %s</TH></TR>\n",
2107            GREY,msg_top_top,tot_num,msg_top_of,tot_ctry,msg_top_c);
2108    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
2109    fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" ALIGN=center>"                   \
2110           "<FONT SIZE=\"-1\">#</FONT></TH>\n",GREY);
2111    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center COLSPAN=2>"             \
2112           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",HITCOLOR,msg_h_hits);
2113    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center COLSPAN=2>"             \
2114           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",FILECOLOR,msg_h_files);
2115    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center COLSPAN=2>"             \
2116           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",KBYTECOLOR,msg_h_xfer);
2117    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=center>"                       \
2118           "<FONT SIZE=\"-1\">%s</FONT></TH></TR>\n",MISCCOLOR,msg_h_ctry);
2119    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
2120    for (i=0;i<ntop_ctrys;i++)
2121    {
2122       flag_buf[0]=0;
2123       if (use_flags)
2124       {
2125          domain=un_idx((idx=top_ctrys[i]->idx));
2126          if (strlen(domain)<3 && idx!=0) /* only to ccTLDs */
2127          {
2128             if ( domain[0]!='a'||domain[1]!='p' )  /* all but 'ap' */
2129             snprintf(flag_buf,sizeof(flag_buf),
2130             "<IMG SRC=\"%s/%s.png\" ALT=\"%s\" WIDTH=18 HEIGHT=12> ",
2131             flag_dir,domain,top_ctrys[i]->desc);
2132          }
2133       }
2134 
2135       if (top_ctrys[i]->count!=0)
2136       fprintf(out_fp,"<TR>"                                                \
2137               "<TD ALIGN=center><FONT SIZE=\"-1\"><B>%d</B></FONT></TD>\n" \
2138               "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n"\
2139               "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"   \
2140               "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%llu</B></FONT></TD>\n"\
2141               "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"   \
2142               "<TD ALIGN=right><FONT SIZE=\"-1\"><B>%.0f</B></FONT></TD>\n"\
2143               "<TD ALIGN=right><FONT SIZE=\"-2\">%3.02f%%</FONT></TD>\n"   \
2144               "<TD ALIGN=left NOWRAP>%s<FONT SIZE=\"-1\">%s</FONT>"        \
2145               "</TD></TR>\n",
2146               i+1,top_ctrys[i]->count,
2147               (t_hit==0)?0:((float)top_ctrys[i]->count/t_hit)*100.0,
2148               top_ctrys[i]->files,
2149               (t_file==0)?0:((float)top_ctrys[i]->files/t_file)*100.0,
2150               top_ctrys[i]->xfer/1024,
2151               (t_xfer==0)?0:((float)top_ctrys[i]->xfer/t_xfer)*100.0,
2152               flag_buf,top_ctrys[i]->desc);
2153    }
2154    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
2155    fprintf(out_fp,"</TABLE>\n<P>\n");
2156 }
2157 
2158 /*********************************************/
2159 /* DUMP_ALL_SITES - dump sites to tab file   */
2160 /*********************************************/
2161 
dump_all_sites()2162 void dump_all_sites()
2163 {
2164    HNODEPTR  hptr, *pointer;
2165    FILE      *out_fp;
2166    char      filename[256];
2167    u_int64_t cnt=a_ctr;
2168 
2169    /* generate file name */
2170    snprintf(filename,sizeof(filename),"%s/site_%04d%02d.%s",
2171       (dump_path)?dump_path:".",cur_year,cur_month,dump_ext);
2172 
2173    /* open file */
2174    if ( (out_fp=open_out_file(filename))==NULL ) return;
2175 
2176    /* need a header? */
2177    if (dump_header)
2178    {
2179       fprintf(out_fp,"%s\t%s\t%s\t%s\t%s\n",
2180        msg_h_hits,msg_h_files,msg_h_xfer,msg_h_visits,msg_h_hname);
2181    }
2182 
2183    /* dump 'em */
2184    pointer=h_array;
2185    while (cnt)
2186    {
2187       hptr=*pointer++;
2188       if (hptr->flag != OBJ_GRP)
2189       {
2190          fprintf(out_fp,
2191          "%llu\t%llu\t%.0f\t%llu\t%s\n",
2192             hptr->count,hptr->files,hptr->xfer/1024,
2193             hptr->visit,hptr->string);
2194       }
2195       cnt--;
2196    }
2197    fclose(out_fp);
2198    return;
2199 }
2200 
2201 /*********************************************/
2202 /* DUMP_ALL_URLS - dump all urls to tab file */
2203 /*********************************************/
2204 
dump_all_urls()2205 void dump_all_urls()
2206 {
2207    UNODEPTR  uptr, *pointer;
2208    FILE      *out_fp;
2209    char      filename[256];
2210    u_int64_t cnt=a_ctr;
2211 
2212    /* generate file name */
2213    snprintf(filename,sizeof(filename),"%s/url_%04d%02d.%s",
2214       (dump_path)?dump_path:".",cur_year,cur_month,dump_ext);
2215 
2216    /* open file */
2217    if ( (out_fp=open_out_file(filename))==NULL ) return;
2218 
2219    /* need a header? */
2220    if (dump_header)
2221    {
2222       fprintf(out_fp,"%s\t%s\t%s\n",msg_h_hits,msg_h_xfer,msg_h_url);
2223    }
2224 
2225    /* dump 'em */
2226    pointer=u_array;
2227    while (cnt)
2228    {
2229       uptr=*pointer++;
2230       if (uptr->flag != OBJ_GRP)
2231       {
2232          fprintf(out_fp,"%llu\t%.0f\t%s\n",
2233             uptr->count,uptr->xfer/1024,uptr->string);
2234       }
2235       cnt--;
2236    }
2237    fclose(out_fp);
2238    return;
2239 }
2240 
2241 /*********************************************/
2242 /* DUMP_ALL_REFS - dump all refs to tab file */
2243 /*********************************************/
2244 
dump_all_refs()2245 void dump_all_refs()
2246 {
2247    RNODEPTR  rptr, *pointer;
2248    FILE      *out_fp;
2249    char      filename[256];
2250    u_int64_t cnt=a_ctr;
2251 
2252    /* generate file name */
2253    snprintf(filename,sizeof(filename),"%s/ref_%04d%02d.%s",
2254       (dump_path)?dump_path:".",cur_year,cur_month,dump_ext);
2255 
2256    /* open file */
2257    if ( (out_fp=open_out_file(filename))==NULL ) return;
2258 
2259    /* need a header? */
2260    if (dump_header)
2261    {
2262       fprintf(out_fp,"%s\t%s\n",msg_h_hits,msg_h_ref);
2263    }
2264 
2265    /* dump 'em */
2266    pointer=r_array;
2267    while(cnt)
2268    {
2269       rptr=*pointer++;
2270       if (rptr->flag != OBJ_GRP)
2271       {
2272          fprintf(out_fp,"%llu\t%s\n",rptr->count, rptr->string);
2273       }
2274       cnt--;
2275    }
2276    fclose(out_fp);
2277    return;
2278 }
2279 
2280 /*********************************************/
2281 /* DUMP_ALL_AGENTS - dump agents htab file   */
2282 /*********************************************/
2283 
dump_all_agents()2284 void dump_all_agents()
2285 {
2286    ANODEPTR  aptr, *pointer;
2287    FILE      *out_fp;
2288    char      filename[256];
2289    u_int64_t cnt=a_ctr;
2290 
2291    /* generate file name */
2292    snprintf(filename,sizeof(filename),"%s/agent_%04d%02d.%s",
2293       (dump_path)?dump_path:".",cur_year,cur_month,dump_ext);
2294 
2295    /* open file */
2296    if ( (out_fp=open_out_file(filename))==NULL ) return;
2297 
2298    /* need a header? */
2299    if (dump_header)
2300    {
2301       fprintf(out_fp,"%s\t%s\n",msg_h_hits,msg_h_agent);
2302    }
2303 
2304    /* dump 'em */
2305    pointer=a_array;
2306    while(cnt)
2307    {
2308       aptr=*pointer++;
2309       if (aptr->flag != OBJ_GRP)
2310       {
2311          fprintf(out_fp,"%llu\t%s\n",aptr->count,aptr->string);
2312       }
2313       cnt--;
2314    }
2315    fclose(out_fp);
2316    return;
2317 }
2318 
2319 /*********************************************/
2320 /* DUMP_ALL_USERS - dump username tab file   */
2321 /*********************************************/
2322 
dump_all_users()2323 void dump_all_users()
2324 {
2325    INODEPTR  iptr, *pointer;
2326    FILE      *out_fp;
2327    char      filename[256];
2328    u_int64_t cnt=a_ctr;
2329 
2330    /* generate file name */
2331    snprintf(filename,sizeof(filename),"%s/user_%04d%02d.%s",
2332       (dump_path)?dump_path:".",cur_year,cur_month,dump_ext);
2333 
2334    /* open file */
2335    if ( (out_fp=open_out_file(filename))==NULL ) return;
2336 
2337    /* need a header? */
2338    if (dump_header)
2339    {
2340       fprintf(out_fp,"%s\t%s\t%s\t%s\t%s\n",
2341          msg_h_hits,msg_h_files,msg_h_xfer,msg_h_visits,msg_h_uname);
2342    }
2343 
2344    /* dump 'em */
2345    pointer=i_array;
2346    while(cnt)
2347    {
2348       iptr=*pointer++;
2349       if (iptr->flag != OBJ_GRP)
2350       {
2351          fprintf(out_fp,
2352          "%llu\t%llu\t%.0f\t%llu\t%s\n",
2353             iptr->count,iptr->files,iptr->xfer/1024,
2354             iptr->visit,iptr->string);
2355       }
2356       cnt--;
2357    }
2358    fclose(out_fp);
2359    return;
2360 }
2361 
2362 /*********************************************/
2363 /* DUMP_ALL_SEARCH - dump search htab file   */
2364 /*********************************************/
2365 
dump_all_search()2366 void dump_all_search()
2367 {
2368    SNODEPTR  sptr, *pointer;
2369    FILE      *out_fp;
2370    char      filename[256];
2371    u_int64_t cnt=a_ctr;
2372 
2373    /* generate file name */
2374    snprintf(filename,sizeof(filename),"%s/search_%04d%02d.%s",
2375       (dump_path)?dump_path:".",cur_year,cur_month,dump_ext);
2376 
2377    /* open file */
2378    if ( (out_fp=open_out_file(filename))==NULL ) return;
2379 
2380    /* need a header? */
2381    if (dump_header)
2382    {
2383       fprintf(out_fp,"%s\t%s\n",msg_h_hits,msg_h_search);
2384    }
2385 
2386    /* dump 'em */
2387    pointer=s_array;
2388    while(cnt)
2389    {
2390       sptr=*pointer++;
2391       fprintf(out_fp,"%llu\t%s\n",sptr->count,sptr->string);
2392       cnt--;
2393    }
2394    fclose(out_fp);
2395    return;
2396 }
2397 
2398 /*********************************************/
2399 /* WRITE_MAIN_INDEX - main index.html file   */
2400 /*********************************************/
2401 
write_main_index()2402 int write_main_index()
2403 {
2404    /* create main index file */
2405 
2406    int     i,j,days_in_month;
2407    int     s_year=hist[HISTSIZE-1].year;
2408    char    index_fname[256];
2409    char    buffer[BUFSIZE];
2410    u_int64_t m_hit=0;
2411    u_int64_t m_files=0;
2412    u_int64_t m_pages=0;
2413    u_int64_t m_visits=0;
2414    double    m_xfer=0.0;
2415    double  gt_hit=0.0;
2416    double  gt_files=0.0;
2417    double  gt_pages=0.0;
2418    double  gt_xfer=0.0;
2419    double  gt_visits=0.0;
2420 
2421    if (verbose>1) printf("%s\n",msg_gen_sum);
2422 
2423    snprintf(buffer,sizeof(buffer),"%s %s",msg_main_us,hname);
2424    year_graph6x("usage.png", buffer, hist);
2425 
2426    /* now do html stuff... */
2427    snprintf(index_fname,sizeof(index_fname),"index.%s",html_ext);
2428 
2429    /* .htaccess file needed? */
2430    if (htaccess)
2431    {
2432       if ((out_fp=fopen(".htaccess","wx")) != NULL)
2433       {
2434          fprintf(out_fp,"DirectoryIndex %s\n",index_fname);
2435          fclose(out_fp);
2436       }
2437       else
2438       {
2439          if (errno!=EEXIST && verbose)
2440             fprintf(stderr,"Error: Failed to create .htaccess file: %s\n",
2441                     strerror(errno));
2442       }
2443    }
2444 
2445    if ( (out_fp=open_out_file(index_fname)) == NULL)
2446    {
2447       if (verbose)
2448       fprintf(stderr,"%s %s!\n",msg_no_open,index_fname);
2449       return 1;
2450    }
2451    write_html_head(NULL, out_fp);
2452 
2453    /* year graph */
2454    fprintf(out_fp,"<IMG SRC=\"usage.png\" ALT=\"%s\" "    \
2455                   "HEIGHT=256 WIDTH=512><P>\n",buffer);
2456    /* month table */
2457    fprintf(out_fp,"<TABLE WIDTH=600 BORDER=2 CELLSPACING=1 CELLPADDING=1>\n");
2458    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
2459    fprintf(out_fp,"<TR><TH COLSPAN=11 BGCOLOR=\"%s\" ALIGN=center>",GREY);
2460    fprintf(out_fp,"%s</TH></TR>\n",msg_main_sum);
2461    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
2462    fprintf(out_fp,"<TR><TH ALIGN=left ROWSPAN=2 BGCOLOR=\"%s\">"          \
2463           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",GREY,msg_h_mth);
2464    fprintf(out_fp,"<TH ALIGN=center COLSPAN=4 BGCOLOR=\"%s\">"            \
2465           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",GREY,msg_main_da);
2466    fprintf(out_fp,"<TH ALIGN=center COLSPAN=6 BGCOLOR=\"%s\">"            \
2467           "<FONT SIZE=\"-1\">%s</FONT></TH></TR>\n",GREY,msg_main_mt);
2468    fprintf(out_fp,"<TR><TH ALIGN=center BGCOLOR=\"%s\">"                  \
2469           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",HITCOLOR,msg_h_hits);
2470    fprintf(out_fp,"<TH ALIGN=center BGCOLOR=\"%s\">"                      \
2471           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",FILECOLOR,msg_h_files);
2472    fprintf(out_fp,"<TH ALIGN=center BGCOLOR=\"%s\">"                      \
2473           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",PAGECOLOR,msg_h_pages);
2474    fprintf(out_fp,"<TH ALIGN=center BGCOLOR=\"%s\">"                      \
2475           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",VISITCOLOR,msg_h_visits);
2476    fprintf(out_fp,"<TH ALIGN=center BGCOLOR=\"%s\">"                      \
2477           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",SITECOLOR,msg_h_sites);
2478    fprintf(out_fp,"<TH ALIGN=center BGCOLOR=\"%s\">"                      \
2479           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",KBYTECOLOR,msg_h_xfer);
2480    fprintf(out_fp,"<TH ALIGN=center BGCOLOR=\"%s\">"                      \
2481           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",VISITCOLOR,msg_h_visits);
2482    fprintf(out_fp,"<TH ALIGN=center BGCOLOR=\"%s\">"                      \
2483           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",PAGECOLOR,msg_h_pages);
2484    fprintf(out_fp,"<TH ALIGN=center BGCOLOR=\"%s\">"                      \
2485           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",FILECOLOR,msg_h_files);
2486    fprintf(out_fp,"<TH ALIGN=center BGCOLOR=\"%s\">"                      \
2487           "<FONT SIZE=\"-1\">%s</FONT></TH></TR>\n",HITCOLOR,msg_h_hits);
2488    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
2489    for (i=HISTSIZE-1;i>=HISTSIZE-index_mths;i--)
2490    {
2491       if (hist[i].hit==0)
2492       {
2493          days_in_month=1;
2494          for (j=i;j>=0;j--) if (hist[j].hit!=0) break;
2495          if (j<0) break;
2496       }
2497       else days_in_month=(hist[i].lday-hist[i].fday)+1;
2498 
2499       /* Check for year change */
2500       if (s_year!=hist[i].year)
2501       {
2502          /* Year Totals */
2503          if (index_mths>16 && year_totals)
2504          {
2505             fprintf(out_fp,"<TR><TH COLSPAN=6 BGCOLOR=\"%s\" "         \
2506                 "ALIGN=left><FONT SIZE=\"-1\"><STRONG>%04d</TH>\n",
2507                 GRPCOLOR,s_year);
2508             fprintf(out_fp,"<TH ALIGN=\"right\" BGCOLOR=\"%s\">"       \
2509                   "<FONT SIZE=\"-1\">%.0f</TH>", GRPCOLOR, m_xfer);
2510             fprintf(out_fp,"<TH ALIGN=\"right\" BGCOLOR=\"%s\">"       \
2511                   "<FONT SIZE=\"-1\">%0llu</TH>", GRPCOLOR, m_visits);
2512             fprintf(out_fp,"<TH ALIGN=\"right\" BGCOLOR=\"%s\">"       \
2513                   "<FONT SIZE=\"-1\">%0llu</TH>", GRPCOLOR, m_pages);
2514             fprintf(out_fp,"<TH ALIGN=\"right\" BGCOLOR=\"%s\">"       \
2515                   "<FONT SIZE=\"-1\">%0llu</TH>", GRPCOLOR, m_files);
2516             fprintf(out_fp,"<TH ALIGN=\"right\" BGCOLOR=\"%s\">"       \
2517                   "<FONT SIZE=\"-1\">%0llu</TH>", GRPCOLOR, m_hit);
2518             m_xfer=0; m_visits=0; m_pages=0; m_files=0; m_hit=0;
2519          }
2520 
2521          /* Year Header */
2522          s_year=hist[i].year;
2523          if (index_mths>16 && year_hdrs)
2524             fprintf(out_fp,"<TR><TH COLSPAN=11 BGCOLOR=\"%s\" "           \
2525                "ALIGN=center>%04d</TH></TR>\n", GREY, s_year);
2526       }
2527 
2528       fprintf(out_fp,"<TR><TD NOWRAP>");
2529       if (hist[i].hit!=0)
2530          fprintf(out_fp,"<A HREF=\"usage_%04d%02d.%s\">"                  \
2531                         "<FONT SIZE=\"-1\">%s %d</FONT></A></TD>\n",
2532                          hist[i].year, hist[i].month, html_ext,
2533                          s_month[hist[i].month-1], hist[i].year);
2534       else
2535          fprintf(out_fp,"<FONT SIZE=\"-1\">%s %d</FONT></A></TD>\n",      \
2536                          s_month[hist[i].month-1], hist[i].year);
2537 
2538       fprintf(out_fp,"<TD ALIGN=right><FONT SIZE=\"-1\">%llu</FONT></TD>\n",
2539                       hist[i].hit/days_in_month);
2540       fprintf(out_fp,"<TD ALIGN=right><FONT SIZE=\"-1\">%llu</FONT></TD>\n",
2541                       hist[i].files/days_in_month);
2542       fprintf(out_fp,"<TD ALIGN=right><FONT SIZE=\"-1\">%llu</FONT></TD>\n",
2543                       hist[i].page/days_in_month);
2544       fprintf(out_fp,"<TD ALIGN=right><FONT SIZE=\"-1\">%llu</FONT></TD>\n",
2545                       hist[i].visit/days_in_month);
2546       fprintf(out_fp,"<TD ALIGN=right><FONT SIZE=\"-1\">%llu</FONT></TD>\n",
2547                       hist[i].site);
2548       fprintf(out_fp,"<TD ALIGN=right><FONT SIZE=\"-1\">%.0f</FONT></TD>\n",
2549                       hist[i].xfer);
2550       fprintf(out_fp,"<TD ALIGN=right><FONT SIZE=\"-1\">%llu</FONT></TD>\n",
2551                       hist[i].visit);
2552       fprintf(out_fp,"<TD ALIGN=right><FONT SIZE=\"-1\">%llu</FONT></TD>\n",
2553                       hist[i].page);
2554       fprintf(out_fp,"<TD ALIGN=right><FONT SIZE=\"-1\">%llu</FONT></TD>\n",
2555                       hist[i].files);
2556       fprintf(out_fp,"<TD ALIGN=right><FONT SIZE=\"-1\">%llu</FONT></TD>",
2557                       hist[i].hit);
2558       fprintf(out_fp,"</TR>\n");
2559       gt_hit   += hist[i].hit;
2560       gt_files += hist[i].files;
2561       gt_pages += hist[i].page;
2562       gt_xfer  += hist[i].xfer;
2563       gt_visits+= hist[i].visit;
2564        m_hit   += hist[i].hit;
2565        m_files += hist[i].files;
2566        m_pages += hist[i].page;
2567        m_visits+= hist[i].visit;
2568        m_xfer  += hist[i].xfer;
2569    }
2570 
2571    if (index_mths>16 && year_totals)
2572    {
2573       fprintf(out_fp,"<TR><TH COLSPAN=6 BGCOLOR=\"%s\" "         \
2574                      "ALIGN=left><FONT SIZE=\"-1\"><STRONG>%04d</TH>\n",
2575                      GRPCOLOR,s_year);
2576       fprintf(out_fp,"<TH ALIGN=\"right\" BGCOLOR=\"%s\">"       \
2577                      "<FONT SIZE=\"-1\">%.0f</TH>", GRPCOLOR, m_xfer);
2578       fprintf(out_fp,"<TH ALIGN=\"right\" BGCOLOR=\"%s\">"       \
2579                      "<FONT SIZE=\"-1\">%0llu</TH>", GRPCOLOR, m_visits);
2580       fprintf(out_fp,"<TH ALIGN=\"right\" BGCOLOR=\"%s\">"       \
2581                      "<FONT SIZE=\"-1\">%0llu</TH>", GRPCOLOR, m_pages);
2582       fprintf(out_fp,"<TH ALIGN=\"right\" BGCOLOR=\"%s\">"       \
2583                      "<FONT SIZE=\"-1\">%0llu</TH>", GRPCOLOR, m_files);
2584       fprintf(out_fp,"<TH ALIGN=\"right\" BGCOLOR=\"%s\">"       \
2585                      "<FONT SIZE=\"-1\">%0llu</TH>", GRPCOLOR, m_hit);
2586    }
2587    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
2588    fprintf(out_fp,"<TR><TH BGCOLOR=\"%s\" COLSPAN=6 ALIGN=left>"          \
2589           "<FONT SIZE=\"-1\">%s</FONT></TH>\n",GREY,msg_h_totals);
2590    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=right>"                       \
2591           "<FONT SIZE=\"-1\">%.0f</FONT></TH>\n",GREY,gt_xfer);
2592    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=right>"                       \
2593           "<FONT SIZE=\"-1\">%.0f</FONT></TH>\n",GREY,gt_visits);
2594    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=right>"                       \
2595           "<FONT SIZE=\"-1\">%.0f</FONT></TH>\n",GREY,gt_pages);
2596    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=right>"                       \
2597           "<FONT SIZE=\"-1\">%.0f</FONT></TH>\n",GREY,gt_files);
2598    fprintf(out_fp,"<TH BGCOLOR=\"%s\" ALIGN=right>"                       \
2599           "<FONT SIZE=\"-1\">%.0f</FONT></TH></TR>\n",GREY,gt_hit);
2600    fprintf(out_fp,"<TR><TH HEIGHT=4></TH></TR>\n");
2601    fprintf(out_fp,"</TABLE>\n");
2602    write_html_tail(out_fp);
2603    fclose(out_fp);
2604    return 0;
2605 }
2606 
2607 /*********************************************/
2608 /* QS_SITE_CMPH - QSort compare site by hits */
2609 /*********************************************/
2610 
qs_site_cmph(const void * cp1,const void * cp2)2611 int qs_site_cmph(const void *cp1, const void *cp2)
2612 {
2613    u_int64_t t1, t2;
2614    t1=(*(HNODEPTR *)cp1)->count;
2615    t2=(*(HNODEPTR *)cp2)->count;
2616    if (t1!=t2) return (t2<t1)?-1:1;
2617    /* if hits are the same, we sort by hostname instead */
2618    return strcmp( (*(HNODEPTR *)cp1)->string,
2619                   (*(HNODEPTR *)cp2)->string );
2620 }
2621 
2622 /*********************************************/
2623 /* QS_SITE_CMPK - QSort cmp site by bytes    */
2624 /*********************************************/
2625 
qs_site_cmpk(const void * cp1,const void * cp2)2626 int qs_site_cmpk(const void *cp1, const void *cp2)
2627 {
2628    double t1, t2;
2629    t1=(*(HNODEPTR *)cp1)->xfer;
2630    t2=(*(HNODEPTR *)cp2)->xfer;
2631    if (t1!=t2) return (t2<t1)?-1:1;
2632    /* if xfer bytes are the same, we sort by hostname instead */
2633    return strcmp( (*(HNODEPTR *)cp1)->string,
2634                   (*(HNODEPTR *)cp2)->string );
2635 }
2636 
2637 /*********************************************/
2638 /* QS_URL_CMPH - QSort compare URL by hits   */
2639 /*********************************************/
2640 
qs_url_cmph(const void * cp1,const void * cp2)2641 int qs_url_cmph(const void *cp1, const void *cp2)
2642 {
2643    u_int64_t t1, t2;
2644    t1=(*(UNODEPTR *)cp1)->count;
2645    t2=(*(UNODEPTR *)cp2)->count;
2646    if (t1!=t2) return (t2<t1)?-1:1;
2647    /* if hits are the same, we sort by url instead */
2648    return strcmp( (*(UNODEPTR *)cp1)->string,
2649                   (*(UNODEPTR *)cp2)->string );
2650 }
2651 
2652 /*********************************************/
2653 /* QS_URL_CMPK - QSort compare URL by bytes  */
2654 /*********************************************/
2655 
qs_url_cmpk(const void * cp1,const void * cp2)2656 int qs_url_cmpk(const void *cp1, const void *cp2)
2657 {
2658    double t1, t2;
2659    t1=(*(UNODEPTR *)cp1)->xfer;
2660    t2=(*(UNODEPTR *)cp2)->xfer;
2661    if (t1!=t2) return (t2<t1)?-1:1;
2662    /* if xfer bytes are the same, we sort by url instead */
2663    return strcmp( (*(UNODEPTR *)cp1)->string,
2664                   (*(UNODEPTR *)cp2)->string );
2665 }
2666 
2667 /*********************************************/
2668 /* QS_URL_CMPN - QSort compare URL by entry  */
2669 /*********************************************/
2670 
qs_url_cmpn(const void * cp1,const void * cp2)2671 int qs_url_cmpn(const void *cp1, const void *cp2)
2672 {
2673    double t1, t2;
2674    t1=(*(UNODEPTR *)cp1)->entry;
2675    t2=(*(UNODEPTR *)cp2)->entry;
2676    if (t1!=t2) return (t2<t1)?-1:1;
2677    /* if xfer bytes are the same, we sort by url instead */
2678    return strcmp( (*(UNODEPTR *)cp1)->string,
2679                   (*(UNODEPTR *)cp2)->string );
2680 }
2681 
2682 /*********************************************/
2683 /* QS_URL_CMPX - QSort compare URL by exit   */
2684 /*********************************************/
2685 
qs_url_cmpx(const void * cp1,const void * cp2)2686 int qs_url_cmpx(const void *cp1, const void *cp2)
2687 {
2688    double t1, t2;
2689    t1=(*(UNODEPTR *)cp1)->exit;
2690    t2=(*(UNODEPTR *)cp2)->exit;
2691    if (t1!=t2) return (t2<t1)?-1:1;
2692    /* if xfer bytes are the same, we sort by url instead */
2693    return strcmp( (*(UNODEPTR *)cp1)->string,
2694                   (*(UNODEPTR *)cp2)->string );
2695 }
2696 
2697 /*********************************************/
2698 /* QS_REF_CMPH - QSort compare Refs by hits  */
2699 /*********************************************/
2700 
qs_ref_cmph(const void * cp1,const void * cp2)2701 int qs_ref_cmph(const void *cp1, const void *cp2)
2702 {
2703    u_int64_t t1, t2;
2704    t1=(*(RNODEPTR *)cp1)->count;
2705    t2=(*(RNODEPTR *)cp2)->count;
2706    if (t1!=t2) return (t2<t1)?-1:1;
2707    /* if hits are the same, we sort by referrer URL instead */
2708    return strcmp( (*(RNODEPTR *)cp1)->string,
2709                   (*(RNODEPTR *)cp2)->string );
2710 }
2711 
2712 /*********************************************/
2713 /* QS_AGNT_CMPH - QSort cmp Agents by hits   */
2714 /*********************************************/
2715 
qs_agnt_cmph(const void * cp1,const void * cp2)2716 int qs_agnt_cmph(const void *cp1, const void *cp2)
2717 {
2718    u_int64_t t1, t2;
2719    t1=(*(ANODEPTR *)cp1)->count;
2720    t2=(*(ANODEPTR *)cp2)->count;
2721    if (t1!=t2) return (t2<t1)?-1:1;
2722    /* if hits are the same, we sort by agent string instead */
2723    return strcmp( (*(ANODEPTR *)cp1)->string,
2724                   (*(ANODEPTR *)cp2)->string );
2725 }
2726 
2727 /*********************************************/
2728 /* QS_SRCH_CMPH - QSort cmp srch str by hits */
2729 /*********************************************/
2730 
qs_srch_cmph(const void * cp1,const void * cp2)2731 int qs_srch_cmph(const void *cp1, const void *cp2)
2732 {
2733    u_int64_t t1, t2;
2734    t1=(*(SNODEPTR *)cp1)->count;
2735    t2=(*(SNODEPTR *)cp2)->count;
2736    if (t1!=t2) return (t2<t1)?-1:1;
2737    /* if hits are the same, we sort by search string instead */
2738    return strcmp( (*(SNODEPTR *)cp1)->string,
2739                   (*(SNODEPTR *)cp2)->string );
2740 }
2741 
2742 /*********************************************/
2743 /* QS_IDENT_CMPH - QSort cmp ident by hits   */
2744 /*********************************************/
2745 
qs_ident_cmph(const void * cp1,const void * cp2)2746 int qs_ident_cmph(const void *cp1, const void *cp2)
2747 {
2748    u_int64_t t1, t2;
2749    t1=(*(INODEPTR *)cp1)->count;
2750    t2=(*(INODEPTR *)cp2)->count;
2751    if (t1!=t2) return (t2<t1)?-1:1;
2752    /* if hits are the same, sort by ident (username) string instead */
2753    return strcmp( (*(INODEPTR *)cp1)->string,
2754                   (*(INODEPTR *)cp2)->string );
2755 }
2756 
2757 /*********************************************/
2758 /* LOAD_SITE_ARRAY - load up the sort array  */
2759 /*********************************************/
2760 
load_site_array(HNODEPTR * pointer)2761 u_int64_t load_site_array(HNODEPTR *pointer)
2762 {
2763    HNODEPTR  hptr;
2764    int       i;
2765    u_int64_t ctr = 0;
2766 
2767    /* load the array */
2768    for (i=0;i<MAXHASH;i++)
2769    {
2770       hptr=sm_htab[i];
2771       while (hptr!=NULL)
2772       {
2773          if (pointer==NULL) ctr++;       /* fancy way to just count 'em    */
2774          else *(pointer+ctr++)=hptr;     /* otherwise, really do the load  */
2775          hptr=hptr->next;
2776       }
2777    }
2778    return ctr;   /* return number loaded */
2779 }
2780 
2781 /*********************************************/
2782 /* LOAD_URL_ARRAY - load up the sort array   */
2783 /*********************************************/
2784 
load_url_array(UNODEPTR * pointer)2785 u_int64_t load_url_array(UNODEPTR *pointer)
2786 {
2787    UNODEPTR  uptr;
2788    int       i;
2789    u_int64_t ctr = 0;
2790 
2791    /* load the array */
2792    for (i=0;i<MAXHASH;i++)
2793    {
2794       uptr=um_htab[i];
2795       while (uptr!=NULL)
2796       {
2797          if (pointer==NULL) ctr++;       /* fancy way to just count 'em    */
2798          else *(pointer+ctr++)=uptr;     /* otherwise, really do the load  */
2799          uptr=uptr->next;
2800       }
2801    }
2802    return ctr;   /* return number loaded */
2803 }
2804 
2805 /*********************************************/
2806 /* LOAD_REF_ARRAY - load up the sort array   */
2807 /*********************************************/
2808 
load_ref_array(RNODEPTR * pointer)2809 u_int64_t load_ref_array(RNODEPTR *pointer)
2810 {
2811    RNODEPTR  rptr;
2812    int       i;
2813    u_int64_t ctr = 0;
2814 
2815    /* load the array */
2816    for (i=0;i<MAXHASH;i++)
2817    {
2818       rptr=rm_htab[i];
2819       while (rptr!=NULL)
2820       {
2821          if (pointer==NULL) ctr++;       /* fancy way to just count 'em    */
2822          else *(pointer+ctr++)=rptr;     /* otherwise, really do the load  */
2823          rptr=rptr->next;
2824       }
2825    }
2826    return ctr;   /* return number loaded */
2827 }
2828 
2829 /*********************************************/
2830 /* LOAD_AGENT_ARRAY - load up the sort array */
2831 /*********************************************/
2832 
load_agent_array(ANODEPTR * pointer)2833 u_int64_t load_agent_array(ANODEPTR *pointer)
2834 {
2835    ANODEPTR  aptr;
2836    int       i;
2837    u_int64_t ctr = 0;
2838 
2839    /* load the array */
2840    for (i=0;i<MAXHASH;i++)
2841    {
2842       aptr=am_htab[i];
2843       while (aptr!=NULL)
2844       {
2845          if (pointer==NULL) ctr++;       /* fancy way to just count 'em    */
2846          else *(pointer+ctr++)=aptr;     /* otherwise, really do the load  */
2847          aptr=aptr->next;
2848       }
2849    }
2850    return ctr;   /* return number loaded */
2851 }
2852 
2853 /*********************************************/
2854 /* LOAD_SRCH_ARRAY - load up the sort array  */
2855 /*********************************************/
2856 
load_srch_array(SNODEPTR * pointer)2857 u_int64_t load_srch_array(SNODEPTR *pointer)
2858 {
2859    SNODEPTR  sptr;
2860    int       i;
2861    u_int64_t ctr = 0;
2862 
2863    /* load the array */
2864    for (i=0;i<MAXHASH;i++)
2865    {
2866       sptr=sr_htab[i];
2867       while (sptr!=NULL)
2868       {
2869          if (pointer==NULL) ctr++;       /* fancy way to just count 'em    */
2870          else *(pointer+ctr++)=sptr;     /* otherwise, really do the load  */
2871          sptr=sptr->next;
2872       }
2873    }
2874    return ctr;   /* return number loaded */
2875 }
2876 
2877 /*********************************************/
2878 /* LOAD_IDENT_ARRAY - load up the sort array */
2879 /*********************************************/
2880 
load_ident_array(INODEPTR * pointer)2881 u_int64_t load_ident_array(INODEPTR *pointer)
2882 {
2883    INODEPTR  iptr;
2884    int       i;
2885    u_int64_t ctr = 0;
2886 
2887    /* load the array */
2888    for (i=0;i<MAXHASH;i++)
2889    {
2890       iptr=im_htab[i];
2891       while (iptr!=NULL)
2892       {
2893          if (pointer==NULL) ctr++;       /* fancy way to just count 'em    */
2894          else *(pointer+ctr++)=iptr;     /* otherwise, really do the load  */
2895          iptr=iptr->next;
2896       }
2897    }
2898    return ctr;   /* return number loaded */
2899 }
2900 
2901 /*********************************************/
2902 /* OPEN_OUT_FILE - Open file for output      */
2903 /*********************************************/
2904 
open_out_file(char * filename)2905 FILE *open_out_file(char *filename)
2906 {
2907    struct stat out_stat;
2908    FILE *out_fp;
2909 
2910    /* stat the file */
2911    if ( !(lstat(filename, &out_stat)) )
2912    {
2913       /* check if the file a symlink */
2914       if ( S_ISLNK(out_stat.st_mode) )
2915       {
2916          if (verbose)
2917          fprintf(stderr,"%s %s (symlink)\n",msg_no_open,filename);
2918          return NULL;
2919       }
2920    }
2921 
2922    /* open the file... */
2923    if ( (out_fp=fopen(filename,"w")) == NULL)
2924    {
2925       if (verbose)
2926       fprintf(stderr,"%s %s!\n",msg_no_open,filename);
2927       return NULL;
2928    }
2929    return out_fp;
2930 }
2931 
2932