Plan 9 from Bell Labs’s /usr/web/sources/contrib/gabidiaz/wip/mboxfs/eutil.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include "a.h"

/* memory utils */
void*
emalloc(ulong n)
{
	void *v;

	v = malloc(n);
	if(v == nil)
		sysfatal("out of memory allocating %lud", n);
	memset(v, 0, n);
	setmalloctag(v, getcallerpc(&n));
	return v;
}

void*
erealloc(void *v, ulong n)
{
	v = realloc(v, n);
	if(v == nil)
		sysfatal("out of memory reallocating %lud", n);
	setmalloctag(v, getcallerpc(&v));
	return v;
}

char*
estrdup(char *s) 
{  
	char *ns;
	int len;

	len=strlen(s)+1;
	ns = malloc(len);
	if(ns == 0)
		return nil;

	memcpy(ns,s,len);
	setmalloctag(ns,getcallerpc(&s));

	return ns;
}


/* debugging utils */
int
error(int level, char *format, ... )
{
	va_list args;
	char lasterror[ERRMAX];

	va_start(args, format);
	vsnprint(lasterror,ERRMAX, format, args);
	va_end(args);

	werrstr(lasterror);
	switch(level) {
		case DEBUG:
			fprint(2,"Debug:%r");
			return 0;
			break;
		case FATAL:
			sysfatal("Error: %r");
			break;
		case HOLD:
			fprint(2,"%r");
			abort();
			break;
	}

	return 0;
}
int
printmsg(Message *m, int t)
{

	for(int i=0;i<t;i++)
		print("\t");
	print("M 0x%luX id %d s %lld e %lld H s %lld e %lld B s %lld e %lld Clen %d\n",
					m,
					m->id,
					m->start,
					m->end,
					m->hstart,
					m->hend,
					m->bstart,
					m->bend,
					m->cache->l);

	return 1;
}

int
printm(Message *m,int t) 
{
	Message *p;


	for(;m!=nil;m=m->next) {
		printmsg(m,t);
		for(p=m->part;p!=nil;p=p->next) {
			printmsg(p,t+1);
			if ( p->part != nil )
				printm(p->part,t+2);
		}
	}
	return 1;
}

int
printbox(Mailbox *b) 
{
	Message *m;

	m=b->msgs;
	printm(m,1);

	return 1;
}

/* message lists management */


Mailbox*
newbox(char *f)
{
	Mailbox *b;
	char *pt;

	b=emalloc(sizeof *b);

	pt=utfrrune(f, '/');
	if ( pt == nil )
		pt=f;
	else
		pt++;

	b->name=smprint("%s",pt);
	b->file=smprint("%s",f);

	return b;

}

Mailbox*
blookup(Mailbox *list, char *name)
{
	for(;list!=nil;list=list->next)
		if(strncmp(list->name,name,strlen(name))==0)
			return list;
	return nil;

}

Mailbox*
btofront(Mailbox *list, Mailbox *new)
{
	new->next=list;
	return new;
}

Mailbox*
btoend(Mailbox *list, Mailbox *new)
{
	Mailbox *p;
	if ( list == nil)
		return new;
	for(p=list;p->next !=nil;p=p->next)
		;
	p->next=new;
	return list;
}

Message*
newmsg(vlong start)
{
	Message *m;

	m=emalloc(sizeof *m);
	m->start=start;
	m->next=nil;
	m->part=nil;
	m->root=nil;

	setmalloctag(m,getcallerpc(&start));
	return m;

}


Message*
mtofront(Message *list, Message *new)
{
	new->next=list;
	return new;

}

Message*
mtoend(Message *list, Message *new)
{
	Message *p;
	if ( list == nil)
		return new;
	for(p=list;p->next !=nil;p=p->next)
		;
	p->next=new;
	return list;
}

Message*
mlookup(Message *list, int id)
{
	for(;list!=nil;list=list->next)
		if(list->id == id)
			return list;
	return nil;

}

Message*
ptofront(Message *list, Message *new)
{
	new->part=list;
	return new;

}

Message*
ptoend(Message *list, Message *new)
{
	Message *p;
	if ( list == nil)
		return new;
	for(p=list;p->part !=nil;p=p->part)
		;
	p->part=new;
	return list;
}

Message*
plookup(Message *list, int id)
{
	for(;list!=nil;list=list->part)
		if(list->id == id)
			return list;
	return nil;

}

void
freemime(Mime *m)
{
	free(m->boundary);
	free(m->type);
	free(m->charset);
	free(m->disposition);
	free(m->filename);
	free(m->encoding);

}

void
freem(Message *m)
{
	freemime(&m->mime);
	free(m->cache);
	free(m);

}

void
freemlist(Message *list)
{
	Message *next;

	for(;list!=nil;list=next) {
		next=list->next;
		freem(list);
	}

}

void
freeplist(Message *list)
{
	Message *part;

	for(;list!=nil;list=part) {
		part=list->part;
		freem(list);
	}

}

Message*
delm(Message *list, int id)
{
	Message *p, *prev;

	prev=nil;

	for(p=list;p!=nil;p=p->next) {
		if(id==p->id) {
			error(DEBUG,"delm(): delete message %d\n",id);
			if(prev==nil)
				list=p->next;
			else
				prev->next=p->next;
			freem(p);
			return list;
		}
		prev=p;

	}

	return list;

}


Message*
delp(Message *list, int id)
{
	Message *p, *prev;

	prev=nil;
	for(p=list;p!=nil;p=p->part) {
		if(id==p->id) {
			if(prev==nil)
				list=p->next;
			else
				prev->part=p->part;
			freem(p);
			return list;
		}
		prev=p;

	}

	return list;
}


Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].