Commit 8a80971b authored by Per Cederqvist's avatar Per Cederqvist
Browse files

Cleanup. Added init_* functions.

parent 010f9a77
/*
* $Id: memory.c,v 0.14 1993/10/18 12:29:44 ceder Exp $
* $Id: memory.c,v 0.15 1994/02/19 03:56:45 ceder Exp $
* Copyright (C) 1991 Lysator Academic Computer Association.
*
* This file is part of the LysKOM server.
......@@ -23,12 +23,12 @@
* Please mail bug reports to bug-lyskom@lysator.liu.se.
*/
/*
* Memory allocators/deallocators.
* Memory allocators/deallocators/initializers.
*
* These functions should be used instead of smalloc/srealloc.
*/
static char *rcsid = "$Id: memory.c,v 0.14 1993/10/18 12:29:44 ceder Exp $";
static char *rcsid = "$Id: memory.c,v 0.15 1994/02/19 03:56:45 ceder Exp $";
#include "rcs.h"
USE(rcsid);
......@@ -42,7 +42,6 @@ USE(rcsid);
#include <sys/types.h>
#include <time.h>
#include "exp.h"
#include "s-string.h"
#include "kom-types.h"
#include "kom-memory.h"
......@@ -54,21 +53,34 @@ static int person_cnt = 0;
static int conference_cnt = 0;
static int text_stat_cnt = 0;
/* Forward declarations */
EXPORT Person *
alloc_person(void)
{
Person *p;
static void clear_mark_list (Mark_list *mark_list);
static Mark_list copy_mark_list (Mark_list ml);
static void clear_member_list (Member_list *m);
static Member_list copy_member_list (Member_list ml);
static void clear_membership (Membership *mship);
static Membership copy_membership (Membership m);
static void clear_membership_list (Membership_list *mlist);
static Membership_list copy_membership_list (Membership_list ml);
static void clear_text_list (Text_list *text_list);
static Text_list copy_text_list (Text_list tl);
person_cnt++;
p = smalloc(sizeof(Person));
*p = EMPTY_PERSON;
return p;
/* Conf_type */
void
init_conf_type(Conf_type *ct)
{
ct->rd_prot = 0;
ct->original = 0;
ct->secret = 0;
ct->letter_box = 0;
}
/* Conference */
EXPORT Conference *
Conference *
alloc_conference(void)
{
Conference *c;
......@@ -76,36 +88,65 @@ alloc_conference(void)
conference_cnt++;
c = smalloc(sizeof(Conference));
*c = EMPTY_CONFERENCE;
init_conference(c);
return c;
}
EXPORT Text_stat *
alloc_text_stat(void)
void
free_conference(Conference *confp)
{
Text_stat *t;
if ( confp == NULL )
return;
text_stat_cnt++;
t = smalloc(sizeof(Text_stat));
*t = EMPTY_TEXT_STAT;
return t;
conference_cnt--;
clear_conference(confp);
sfree(confp);
}
static void
clear_text_list(Text_list *text_list)
void
clear_conference(Conference *confp)
{
if ( text_list == NULL )
{
log("clear_text_list(): text_list == NULL.\n");
return;
}
s_clear(&confp->name);
clear_member_list(&confp->members);
clear_text_list(&confp->texts);
init_conference(confp);
}
sfree(text_list->texts);
*text_list = EMPTY_TEXT_LIST;
Conference *
copy_conference (Conference *o)
{
Conference *c;
c = alloc_conference();
*c = *o;
c->name = EMPTY_STRING;
s_strcpy(&c->name, o->name);
c->members = copy_member_list(o->members);
c->texts = copy_text_list(o->texts);
return c;
}
void
init_conference (Conference *c)
{
init_conf_type(&c->type);
c->creator = 0;
c->supervisor = 0;
c->permitted_submitters = 0;
c->super_conf = 0;
c->creation_time = NO_TIME;
c->presentation = 0;
c->last_written = NO_TIME;
c->msg_of_day = 0;
c->nice = 0;
c->name = EMPTY_STRING;
init_member_list(&c->members);
init_text_list(&c->texts);
}
/* Mark_list */
static void
clear_mark_list(Mark_list *mark_list)
{
......@@ -116,9 +157,58 @@ clear_mark_list(Mark_list *mark_list)
}
sfree(mark_list->marks);
*mark_list = EMPTY_MARK_LIST;
init_mark_list(mark_list);
}
static Mark_list
copy_mark_list(Mark_list ml)
{
Mark_list r;
r.no_of_marks = ml.no_of_marks;
r.marks = smalloc(r.no_of_marks * sizeof(Mark));
memcpy(r.marks, ml.marks, r.no_of_marks * sizeof(Mark));
return r;
}
void
init_mark_list (Mark_list *ml)
{
ml->no_of_marks = 0;
ml->marks = NULL;
}
/* Member_list */
static void
clear_member_list(Member_list *m)
{
if ( m == NULL )
return;
sfree(m->members);
init_member_list(m);
}
static Member_list
copy_member_list(Member_list ml)
{
Member_list res;
res.no_of_members = ml.no_of_members;
res.members = smalloc(res.no_of_members * sizeof ( Member ));
memcpy(res.members, ml.members, res.no_of_members * sizeof ( Member ));
return res;
}
void
init_member_list (Member_list *ml)
{
ml->no_of_members = 0;
ml->members = NULL;
}
/* Membership */
static void
clear_membership(Membership *mship)
......@@ -130,9 +220,33 @@ clear_membership(Membership *mship)
}
sfree(mship->read_texts);
*mship = EMPTY_MEMBERSHIP;
init_membership(mship);
}
static Membership
copy_membership(Membership m)
{
Membership res;
res = m;
res.read_texts = smalloc(m.no_of_read * sizeof(Local_text_no));
memcpy(res.read_texts, m.read_texts, m.no_of_read * sizeof(Local_text_no));
return res;
}
void
init_membership(Membership *m)
{
m->conf_no = 0;
m->priority = 0;
m->last_text_read = 0;
m->no_of_read = 0;
m->read_texts = NULL;
m->last_time_read = NO_TIME;
}
/* Membership_list */
static void
clear_membership_list(Membership_list *mlist)
{
......@@ -150,21 +264,47 @@ clear_membership_list(Membership_list *mlist)
}
sfree(mlist->confs);
*mlist = EMPTY_MEMBERSHIP_LIST;
init_membership_list(mlist);
}
static Membership_list
copy_membership_list(Membership_list ml)
{
Membership_list r;
int i;
r.no_of_confs = ml.no_of_confs;
r.confs = smalloc(ml.no_of_confs * sizeof(Membership));
EXPORT void
clear_person(Person *person)
for ( i = 0; i < r.no_of_confs; i++ )
{
r.confs[i] = copy_membership(ml.confs[i]);
}
return r;
}
void
init_membership_list (Membership_list *m)
{
s_clear(&person->username);
clear_text_list(&person->created_texts);
clear_mark_list(&person->marks);
clear_membership_list(&person->conferences);
*person = EMPTY_PERSON;
m->no_of_confs = 0;
m->confs = NULL;
}
/* Person */
Person *
alloc_person(void)
{
Person *p;
person_cnt++;
p = smalloc(sizeof(Person));
init_person(p);
return p;
}
EXPORT void
void
free_person(Person *person)
{
if ( person == NULL )
......@@ -175,78 +315,137 @@ free_person(Person *person)
sfree(person);
}
void
clear_person(Person *person)
{
s_clear(&person->username);
clear_text_list(&person->created_texts);
clear_mark_list(&person->marks);
clear_membership_list(&person->conferences);
init_person(person);
}
static void
clear_member_list(Member_list *m)
Person *
copy_person(Person *p)
{
if ( m == NULL )
return;
Person *c;
sfree(m->members);
*m = EMPTY_MEMBER_LIST;
c = alloc_person();
*c = *p;
c->username = EMPTY_STRING;
s_strcpy(&c->username, p->username);
c->created_texts = copy_text_list(p->created_texts);
c->marks = copy_mark_list(p->marks);
c->conferences = copy_membership_list(p->conferences);
return c;
}
EXPORT void
clear_conference(Conference *confp)
void
init_person (Person *p)
{
s_clear(&confp->name);
clear_member_list(&confp->members);
clear_text_list(&confp->texts);
*confp = EMPTY_CONFERENCE;
p->user_area = 0;
p->total_time_present = 0;
p->sessions = 0;
p->created_lines = 0;
p->created_bytes = 0;
p->read_texts = 0;
p->no_of_text_fetches = 0;
p->created_persons = 0;
p->created_confs = 0;
p->username = EMPTY_STRING;
p->last_login = NO_TIME;
init_priv_bits(&p->privileges);
init_personal_flags(&p->flags);
init_text_list(&p->created_texts);
init_mark_list(&p->marks);
init_membership_list(&p->conferences);
/* No initialization of pwd is done (or needed) */
}
EXPORT void
free_conference(Conference *confp)
{
if ( confp == NULL )
return;
/* Personal_flags */
conference_cnt--;
clear_conference(confp);
sfree(confp);
void
init_personal_flags (Personal_flags *p)
{
p->unread_is_secret = 0;
p->flg2 = 0;
p->flg3 = 0;
p->flg4 = 0;
p->flg5 = 0;
p->flg6 = 0;
p->flg7 = 0;
p->flg8 = 0;
}
EXPORT void
clear_text_stat(Text_stat *t)
/* Priv_bits */
void
init_priv_bits (Priv_bits *pb)
{
int i;
pb->wheel = 0;
pb->admin = 0;
pb->statistic = 0;
pb->create_pers = 0;
pb->create_conf = 0;
pb->change_name = 0;
pb->extern_gw = 0;
pb->flg8 = 0;
pb->flg9 = 0;
pb->flg10 = 0;
pb->flg11 = 0;
pb->flg12 = 0;
pb->flg13 = 0;
pb->flg14 = 0;
pb->flg15 = 0;
pb->flg16 = 0;
}
for ( i = 0; i < t->no_of_misc; i++ )
{
switch ( t->misc_items[ i ].type )
{
case recpt:
case cc_recpt:
case comm_to:
case comm_in:
case footn_to:
case footn_in:
case loc_no:
case rec_time:
case sent_by:
case sent_at:
/* No need to free anything for these. */
break;
/* Session_info */
default:
restart_kom("%s: free_text_stat: unknown Info_type %d.",
__FILE__, t->misc_items[ i ].type );
}
}
void
init_session_info (Session_info *s)
{
s->person = 0;
s->what_am_i_doing = EMPTY_STRING;
s->username = EMPTY_STRING;
s->working_conference = 0;
s->session = 0;
s->connection_time = NO_TIME;
s->idle_time = 0;
}
sfree(t->misc_items);
*t = EMPTY_TEXT_STAT;
/* Session_info_ident */
void
init_session_info_ident (Session_info_ident *s)
{
s->person = 0;
s->what_am_i_doing = EMPTY_STRING;
s->username = EMPTY_STRING;
s->ident_user = EMPTY_STRING;
s->hostname = EMPTY_STRING;
s->working_conference = 0;
s->session = 0;
s->connection_time = NO_TIME;
s->idle_time = 0;
}
EXPORT void
free_text_stat(Text_stat *t)
/* Text_list */
static void
clear_text_list(Text_list *text_list)
{
if ( t == NULL )
if ( text_list == NULL )
{
log("clear_text_list(): text_list == NULL.\n");
return;
}
text_stat_cnt--;
clear_text_stat(t);
sfree(t);
sfree(text_list->texts);
init_text_list(text_list);
}
static Text_list
......@@ -261,103 +460,152 @@ copy_text_list(Text_list tl)
return r;
}
static Mark_list
copy_mark_list(Mark_list ml)
void
init_text_list (Text_list *tl)
{
Mark_list r;
r.no_of_marks = ml.no_of_marks;
r.marks = smalloc(r.no_of_marks * sizeof(Mark));
memcpy(r.marks, ml.marks, r.no_of_marks * sizeof(Mark));
return r;
tl->first_local_no = 1; /* sic */
tl->no_of_texts = 0;
tl->texts = NULL;
}
/* Text_stat */
static Membership
copy_membership(Membership m)
Text_stat *
alloc_text_stat(void)
{
Membership res;
Text_stat *t;
res = m;
res.read_texts = smalloc(m.no_of_read * sizeof(Local_text_no));
memcpy(res.read_texts, m.read_texts, m.no_of_read * sizeof(Local_text_no));
return res;
text_stat_cnt++;
t = smalloc(sizeof(Text_stat));
init_text_stat(t);
return t;
}
void
free_text_stat(Text_stat *t)
{
if ( t == NULL )
return;
static Membership_list
copy_membership_list(Membership_list ml)
text_stat_cnt--;
clear_text_stat(t);
sfree(t);
}
void
clear_text_stat(Text_stat *t)
{
Membership_list r;
int i;
r.no_of_confs = ml.no_of_confs;
r.confs = smalloc(ml.no_of_confs * sizeof(Membership));
for ( i = 0; i < r.no_of_confs; i++ )
for ( i = 0; i < t->no_of_misc; i++ )
{
r.confs[i] = copy_membership(ml.confs[i]);
switch ( t->misc_items[ i ].type )
{
case recpt:
case cc_recpt:
case comm_to:
case comm_in:
case footn_to:
case footn_in:
case loc_no:
case rec_time:
case sent_by:
case sent_at:
/* No need to free anything for these. */
break;
default:
restart_kom("%s: free_text_stat: unknown Info_type %d.",
__FILE__, t->misc_items[ i ].type );
}
}
return r;
sfree(t->misc_items);
init_text_stat(t);
}
EXPORT Person *
copy_person(Person *p)
Text_stat *
copy_text_stat(Text_stat *t)
{
Person *c;
c = alloc_person();
*c = *p;
c->username = EMPTY_STRING;
s_strcpy(&c->username, p->username);
Text_stat *c;
c->created_texts = copy_text_list(p->created_texts);
c->marks = copy_mark_list(p->marks);
c->conferences = copy_membership_list(p->conferences);
c = alloc_text_stat();
*c = *t;
c->misc_items = smalloc(c->no_of_misc * sizeof(Misc_info));
memcpy(c->misc_items, t->misc_items, c->no_of_misc * sizeof(Misc_info));
return c;
}
static Member_list
copy_member_list(Member_list ml)
void
init_text_stat (Text_stat *t)
{
Member_list res;
res.no_of_members = ml.no_of_members;
res.members = smalloc(res.no_of_members * sizeof ( Member ));
memcpy(res.members, ml.members, res.no_of_members * sizeof ( Member ));
return res;
t->creation_time = NO_TIME;
t->file_pos = 0;
t->author = 0;
t->no_of_lines = 0;
t->no_of_chars = 0;
t->no_of_marks = 0;
t->no_of_misc = 0;
t->misc_items = NULL;
}