]>
git.ipfire.org Git - thirdparty/sarg.git/blob - userinfo.c
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
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.
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.
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.
27 #include "include/conf.h"
28 #include "include/defs.h"
30 //! The number of users to group in one unit.
31 #define USERS_PER_GROUP 50
33 /*! \brief Group the users in one allocation unit.
34 Structure to store a group of users and reduce the number of memory
37 struct usergroupstruct
39 //! The next group of users.
40 struct usergroupstruct
*next
;
42 struct userinfostruct list
[USERS_PER_GROUP
];
43 //! The number of users stored in the list.
47 /*! \brief Hold pointer to scan through the user list.
51 //! The group containing the user.
52 struct usergroupstruct
*group
;
53 //! The index of the user in the group.
57 //! The first group of users.
58 static struct usergroupstruct
*first_user_group
=NULL
;
59 //! The counter to generate unique user number when ::AnonymousOutputFiles is set.
60 static int AnonymousCounter
=0;
62 struct userinfostruct
*userinfo_create(const char *userid
)
64 struct usergroupstruct
*group
, *last
;
65 struct userinfostruct
*user
;
73 for (group
=first_user_group
; group
; group
=group
->next
) {
74 if (group
->nusers
<USERS_PER_GROUP
) break;
79 group
=malloc(sizeof(*group
));
81 debuga(_("Not enough memory to store the user\n"));
84 memset(group
,0,sizeof(*group
));
88 first_user_group
=group
;
90 user
=group
->list
+group
->nusers
++;
92 safe_strcpy(user
->id
,userid
,sizeof(user
->id
));
94 if (AnonymousOutputFiles
) {
95 snprintf(user
->filename
,sizeof(user
->filename
),"%d",AnonymousCounter
++);
99 for (i
=0 ; userid
[i
] && j
<MAX_USER_FNAME_LEN
-1 ; i
++) {
100 if (isalnum(userid
[i
]) || userid
[i
]=='-' || userid
[i
]=='_') {
101 user
->filename
[j
++]=userid
[i
];
105 user
->filename
[j
++]='_';
110 user
->filename
[j
]='\0';
114 for (group
=first_user_group
; group
; group
=group
->next
) {
115 lastuser
=(group
->next
) ? group
->nusers
: group
->nusers
-1;
116 for (i
=0 ; i
<lastuser
; i
++) {
117 if (strcasecmp(user
->filename
,group
->list
[i
].filename
)==0) {
118 clen
=sprintf(cstr
,"-%04X",count
++);
119 if (flen
+clen
<MAX_USER_FNAME_LEN
)
120 strcpy(user
->filename
+flen
,cstr
);
122 strcpy(user
->filename
+MAX_USER_FNAME_LEN
-clen
,cstr
);
131 void userinfo_free(void)
133 struct usergroupstruct
*group
, *next
;
135 for (group
=first_user_group
; group
; group
=next
) {
139 first_user_group
=NULL
;
142 struct userinfostruct
*userinfo_find_from_file(const char *filename
)
144 struct usergroupstruct
*group
;
147 for (group
=first_user_group
; group
; group
=group
->next
) {
148 for (i
=0 ; i
<group
->nusers
; i
++)
149 if (strcmp(filename
,group
->list
[i
].filename
)==0)
150 return(group
->list
+i
);
155 struct userinfostruct
*userinfo_find_from_id(const char *id
)
157 struct usergroupstruct
*group
;
160 for (group
=first_user_group
; group
; group
=group
->next
) {
161 for (i
=0 ; i
<group
->nusers
; i
++)
162 if (strcmp(id
,group
->list
[i
].id
)==0)
163 return(group
->list
+i
);
169 Start the scanning of the user list.
171 \return The object to pass to subsequent scanning functions or NULL
172 if it failed. The object must be freed with a call to userinfo_stop().
174 userscan
userinfo_startscan(void)
178 uscan
=malloc(sizeof(*uscan
));
179 if (!uscan
) return(NULL
);
180 uscan
->group
=first_user_group
;
186 Free the memory allocated by userinfo_start().
188 \param uscan The object created by userinfo_start().
190 void userinfo_stopscan(userscan uscan
)
196 Get the user pointed to by the object and advance the object
199 \param uscan The object created by userinfo_start().
201 \return The user in the list or NULL if the end of the list
204 struct userinfostruct
*userinfo_advancescan(userscan uscan
)
206 struct userinfostruct
*uinfo
;
208 if (!uscan
) return(NULL
);
209 if (!uscan
->group
) return(NULL
);
210 if (uscan
->index
<0 || uscan
->index
>=uscan
->group
->nusers
) return(NULL
);
212 uinfo
=uscan
->group
->list
+uscan
->index
;
215 if (uscan
->index
>=uscan
->group
->nusers
) {
216 uscan
->group
=uscan
->group
->next
;
223 Clear the general purpose flag from all the user's info.
225 void userinfo_clearflag(void)
227 struct usergroupstruct
*group
;
230 for (group
=first_user_group
; group
; group
=group
->next
) {
231 for (i
=0 ; i
<group
->nusers
; i
++)
232 group
->list
[i
].flag
=0;