]> git.ipfire.org Git - thirdparty/sarg.git/blame - userinfo.c
Keep the user name intact in the report.
[thirdparty/sarg.git] / userinfo.c
CommitLineData
f2ec8c75
FM
1/*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2010
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
24 *
25 */
26
27#include "include/conf.h"
28#include "include/defs.h"
29
30//! The number of users to group in one unit.
31#define USERS_PER_GROUP 50
32
33/*! \brief Group the users in one allocation unit.
34Structure to store a group of users and reduce the number of memory
35allocations.
36*/
37struct usergroupstruct
38{
39 //! The next group of users.
40 struct usergroupstruct *next;
41 //! A group of users.
42 struct userinfostruct list[USERS_PER_GROUP];
43 //! The number of users stored in the list.
44 int nusers;
45};
46
47//! The first group of users.
48static struct usergroupstruct *first_user_group=NULL;
49
50struct userinfostruct *userinfo_create(const char *userid)
51{
52 struct usergroupstruct *group, *last;
53 struct userinfostruct *user;
54 int i, lastuser;
55 int skip;
56 int flen;
57 int count, clen;
58 char cstr[9];
59
60 last=NULL;
61 for (group=first_user_group ; group ; group=group->next) {
62 if (group->nusers<USERS_PER_GROUP) break;
63 last=group;
64 }
65
66 if (!group) {
67 group=malloc(sizeof(*group));
68 if (!group) {
69 debuga(_("Not enough memory to store the user"));
70 exit(1);
71 }
72 memset(group,0,sizeof(*group));
73 if (last)
74 last->next=group;
75 else
76 first_user_group=group;
77 }
78 user=group->list+group->nusers++;
79
80 strncpy(user->id,userid,MAX_USER_LEN-1);
81 user->id[MAX_USER_LEN-1]='\0';
82
83 skip=0;
84 for(i=0 ; userid[i] && i<MAX_USER_FNAME_LEN-1 ; i++) {
85 if(userid[i]=='?' || userid[i]=='.' || userid[i]==':' || userid[i]=='/' || userid[i]=='\\' || userid[i]=='\'' ||
86 userid[i]=='$' || userid[i]=='@' || userid[i]=='\"' || userid[i]=='*') {
87 if (!skip) {
88 user->filename[i]='_';
89 skip=1;
90 }
91 } else {
92 user->filename[i]=userid[i];
93 skip=0;
94 }
95 }
96 user->filename[i]='\0';
97 flen=i;
98
99 count=0;
100 for (group=first_user_group ; group ; group=group->next) {
101 lastuser=(group->next) ? group->nusers : group->nusers-1;
102 for (i=0 ; i<lastuser ; i++) {
103 if (strcasecmp(user->filename,group->list[i].filename)==0) {
104 clen=sprintf(cstr,"-%04X",count++);
105 if (flen+clen<MAX_USER_FNAME_LEN)
106 strcpy(user->filename+flen,cstr);
107 else
108 strcpy(user->filename+MAX_USER_FNAME_LEN-clen,cstr);
109 }
110 }
111 }
112
113 return(user);
114}
115
116void userinfo_free(void)
117{
118 struct usergroupstruct *group, *next;
119
120 for (group=first_user_group ; group ; group=next) {
121 next=group->next;
122 free(group);
123 }
124 first_user_group=NULL;
125}
126
127struct userinfostruct *userinfo_find_from_file(const char *filename)
128{
129 struct usergroupstruct *group;
130 int i;
131
132 for (group=first_user_group ; group ; group=group->next) {
133 for (i=0 ; i<group->nusers ; i++)
134 if (strcmp(filename,group->list[i].filename)==0)
135 return(group->list+i);
136 }
137 return(NULL);
138}
139
140struct userinfostruct *userinfo_find_from_id(const char *id)
141{
142 struct usergroupstruct *group;
143 int i;
144
145 for (group=first_user_group ; group ; group=group->next) {
146 for (i=0 ; i<group->nusers ; i++)
147 if (strcmp(id,group->list[i].id)==0)
148 return(group->list+i);
149 }
150 return(NULL);
151}