forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commons.c
233 lines (199 loc) · 4.55 KB
/
commons.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* commons.c -- holds different data types
* Copyright (C) 2009-2014 by Gerardo Orellana <[email protected]>
* GoAccess - An Ncurses apache weblog analyzer & interactive viewer
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* A copy of the GNU General Public License is attached to this
* source distribution for its full text.
*
* Visit http://goaccess.prosoftcorp.com for new releases.
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "commons.h"
#include "error.h"
#include "util.h"
#include "xmalloc.h"
/* processing time */
time_t end_proc;
time_t timestamp;
time_t start_proc;
/* resizing/scheme */
size_t real_size_y = 0;
size_t term_h = 0;
size_t term_w = 0;
static GEnum MODULES[] = {
{"VISITORS", VISITORS},
{"REQUESTS", REQUESTS},
{"REQUESTS_STATIC", REQUESTS_STATIC},
{"NOT_FOUND", NOT_FOUND},
{"HOSTS", HOSTS},
{"OS", OS},
{"BROWSERS", BROWSERS},
{"VISIT_TIMES", VISIT_TIMES},
{"REFERRERS", REFERRERS},
{"REFERRING_SITES", REFERRING_SITES},
{"KEYPHRASES", KEYPHRASES},
#ifdef HAVE_LIBGEOIP
{"GEO_LOCATION", GEO_LOCATION},
#endif
{"STATUS_CODES", STATUS_CODES},
};
/* calculate hits percentage */
float
get_percentage (unsigned long long total, unsigned long long hit)
{
return ((float) (hit * 100) / (total));
}
void
display_storage (void)
{
#ifdef TCB_BTREE
fprintf (stdout, "Built using Tokyo Cabinet On-Disk B+ Tree.\n");
#elif TCB_MEMHASH
fprintf (stdout, "Built using Tokyo Cabinet On-Memory Hash database.\n");
#else
fprintf (stdout, "Built using GLib On-Memory Hash database.\n");
#endif
}
void
display_version (void)
{
fprintf (stdout, "GoAccess - %s.\n", GO_VERSION);
fprintf (stdout, "For more details visit: http://goaccess.io\n");
fprintf (stdout, "Copyright (C) 2009-2015 GNU GPL'd, by Gerardo Orellana\n");
}
int
get_module_enum (const char *str)
{
return str2enum (MODULES, ARRAY_SIZE (MODULES), str);
}
int
str2enum (const GEnum map[], int len, const char *str)
{
int i;
for (i = 0; i < len; ++i) {
if (!strcmp (str, map[i].str))
return map[i].idx;
}
return -1;
}
GSLList *
list_create (void *data)
{
GSLList *node = xmalloc (sizeof (GSLList));
node->data = data;
node->next = NULL;
return node;
}
GSLList *
list_insert_append (GSLList * node, void *data)
{
GSLList *newnode;
newnode = list_create (data);
newnode->next = node->next;
node->next = newnode;
return newnode;
}
GSLList *
list_insert_prepend (GSLList * list, void *data)
{
GSLList *newnode;
newnode = list_create (data);
newnode->next = list;
return newnode;
}
GSLList *
list_find (GSLList * node, int (*func) (void *, void *), void *data)
{
while (node) {
if (func (node->data, data) > 0)
return node;
node = node->next;
}
return NULL;
}
int
list_remove_nodes (GSLList * list)
{
GSLList *tmp;
while (list != NULL) {
tmp = list->next;
if (list->data)
free (list->data);
free (list);
list = tmp;
}
return 0;
}
int
list_foreach (GSLList * node, int (*func) (void *, void *), void *user_data)
{
while (node) {
if (func (node->data, user_data) != 0)
return -1;
node = node->next;
}
return 0;
}
int
list_count (GSLList * node)
{
int count = 0;
while (node != 0) {
count++;
node = node->next;
}
return count;
}
GAgents *
new_gagents (void)
{
GAgents *agents = xmalloc (sizeof (GAgents));
memset (agents, 0, sizeof *agents);
return agents;
}
GAgentItem *
new_gagent_item (uint32_t size)
{
GAgentItem *item = xcalloc (size, sizeof (GAgentItem));
return item;
}
void
format_date_visitors (GMetrics * metrics)
{
char date[DATE_LEN] = ""; /* Ymd */
char *datum = metrics->data;
memset (date, 0, sizeof *date);
/* verify we have a valid date conversion */
if (convert_date (date, datum, "%Y%m%d", "%d/%b/%Y", DATE_LEN) == 0) {
free (datum);
metrics->data = xstrdup (date);
return;
}
LOG_DEBUG (("invalid date: %s", datum));
free (datum);
metrics->data = xstrdup ("---");
}
int
has_timestamp (const char *fmt)
{
if (strcmp ("%s", fmt) == 0)
return 1;
return 0;
}