]> git.ipfire.org Git - thirdparty/sarg.git/blob - userinfo.c
Tag the released 2.3 version.
[thirdparty/sarg.git] / userinfo.c
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.
34 Structure to store a group of users and reduce the number of memory
35 allocations.
36 */
37 struct 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.
48 static struct usergroupstruct *first_user_group=NULL;
49
50 struct 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\n"));
70 exit(EXIT_FAILURE);
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(isalnum(userid[i]) || userid[i]=='-' || userid[i]=='_') {
86 user->filename[i]=userid[i];
87 skip=0;
88 } else {
89 if (!skip) {
90 user->filename[i]='_';
91 skip=1;
92 }
93 }
94 }
95 user->filename[i]='\0';
96 flen=i;
97
98 count=0;
99 for (group=first_user_group ; group ; group=group->next) {
100 lastuser=(group->next) ? group->nusers : group->nusers-1;
101 for (i=0 ; i<lastuser ; i++) {
102 if (strcasecmp(user->filename,group->list[i].filename)==0) {
103 clen=sprintf(cstr,"-%04X",count++);
104 if (flen+clen<MAX_USER_FNAME_LEN)
105 strcpy(user->filename+flen,cstr);
106 else
107 strcpy(user->filename+MAX_USER_FNAME_LEN-clen,cstr);
108 }
109 }
110 }
111
112 return(user);
113 }
114
115 void userinfo_free(void)
116 {
117 struct usergroupstruct *group, *next;
118
119 for (group=first_user_group ; group ; group=next) {
120 next=group->next;
121 free(group);
122 }
123 first_user_group=NULL;
124 }
125
126 struct userinfostruct *userinfo_find_from_file(const char *filename)
127 {
128 struct usergroupstruct *group;
129 int i;
130
131 for (group=first_user_group ; group ; group=group->next) {
132 for (i=0 ; i<group->nusers ; i++)
133 if (strcmp(filename,group->list[i].filename)==0)
134 return(group->list+i);
135 }
136 return(NULL);
137 }
138
139 struct userinfostruct *userinfo_find_from_id(const char *id)
140 {
141 struct usergroupstruct *group;
142 int i;
143
144 for (group=first_user_group ; group ; group=group->next) {
145 for (i=0 ; i<group->nusers ; i++)
146 if (strcmp(id,group->list[i].id)==0)
147 return(group->list+i);
148 }
149 return(NULL);
150 }