Plan 9 from Bell Labs’s /usr/web/sources/contrib/mjl/wip/deluge/subr.c

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


#include "deluge.h"


char *
estrdup(char *s)
{
	char *p;
	p = strdup(s);
	if(p == nil)
		sysfatal("estrdup: %r");
	return p;
}

void *
emalloc(ulong size)
{
	void *p;
	p = malloc(size);
	if(p == nil)
		sysfatal("emalloc: %r");
	return p;
}

void *
erealloc(void *p, ulong size)
{
	p = realloc(p, size);
	if(p == nil)
		sysfatal("erealloc: %r");
	return p;
}

void *
emallocz(ulong size, int clr)
{
	void *p;
	p = mallocz(size, clr);
	if(p == nil)
		sysfatal("emallocz: %r");
	return p;
}

char *
ememdup(char *p, ulong len)
{
	char *r;
	r = emalloc(len);
	memmove(r, p, len);
	return r;
}


int
Hfmt(Fmt *f)
{
	uchar *h;
	char buf[2*20+1];
	int i;

	h = va_arg(f->args, uchar *);
	for(i = 0; i < 20; i++)
		sprint(buf+i*2, "%02x", h[i]);
	buf[40] = 0;
	return fmtprint(f, "%s", buf);
}

int
Lfmt(Fmt *f)
{
	vlong v;
	char exts[] = {'B', 'K', 'M', 'G', 'T'};
	char *ext;
	ext = exts;

	v = va_arg(f->args, vlong);
	while(v >= 10000){
		v /= 1024;
		ext++;
	}

	/* XXX use width */

	return fmtprint(f, "%lld%c", v, *ext);
}


int
Mfmt(Fmt *f)
{
	Msg *m;
	char *types[] = {
	[MChoke]			"choke",
	[MUnchoke]		"unchoke",
	[MInterested]		"interested",
	[MNotinterested]	"notinterested",
	[MHave]			"have",
	[MBitfield]			"bitfield",
	[MRequest]		"request",
	[MPiece]			"piece",
	[MCancel]			"cancel",
	};

	m = va_arg(f->args, Msg *);

	switch(m->type){
	case MKeepalive:
		return fmtprint(f, "msg(keepalive)");
	case MChoke:
	case MUnchoke:
	case MInterested:
	case MNotinterested:
		return fmtprint(f, "msg(%s)", types[m->type]);
	case MHave:
		return fmtprint(f, "msg(%s piecen=%uld)", types[m->type], m->have);
	case MBitfield:
		return fmtprint(f, "msg(%s haves=%B)", types[m->type], m->haves);
	case MRequest:
	case MCancel:
	case MPiece:
		return fmtprint(f, "msg(%s piecen=%uld offset=%uld length=%uld)", types[m->type], m->index, m->begin, m->length);
	default:
		return fmtprint(f, "msg(%d (unknown message))", m->type);
	}
}

int
Bfmt(Fmt *f)
{
	Bits *b;
	char *buf;
	char *p;
	int i, r;

	b = va_arg(f->args, Bits *);
	p = buf = emalloc(bitlen(b)+1);
	for(i = 0; i < bitlen(b); i++)
		*p++ = bitget(b, i) ? '1' : '0';
	*p = 0;
	r = fmtprint(f, "bits(n=%d nhave=%d bits=%s)", b->n, b->nhave, buf);
	free(buf);
	return r;
}

int
Pfmt(Fmt *f)
{
	Peer *p;

	p = va_arg(f->args, Peer *);
	if(!(p->status&Valid))
		return fmtprint(f, "peer(invalid)");
	return fmtprint(f, "peer(n=%d ip=%s port=%s fd=%d pieces=%B rate=%R status=%ud nlreq=%d nrreq=%d lastchange=%uld nmeta=%d ndataout=%d next=%#p)",
		p->n, p->ip, p->port, p->fd, p->pieces, p->rate,
		p->status, bitelen(p->lreq), bitelen(p->rreq), p->lastchange,
		msglen(p->meta), msglen(p->dataout), p->next);
	
}

int
Cfmt(Fmt *f)
{
	Piece *pc;

	pc = va_arg(f->args, Piece *);
	return fmtprint(f, "piece(n=%d hash=%H nids=%d length=%d bites=%B reqbites=%B)",
		pc->n,
		pc->hash,
		pc->nids,
		pc->length,
		pc->bites,
		pc->reqbites);
}

int
Ifmt(Fmt *f)
{
	Bite *b;

	b = va_arg(f->args, Bite *);
	return fmtprint(f, "bite(n=%d piecen=%d offset=%d length=%d next=%#p)",
		b->n,
		b->piecen,
		b->offset,
		b->length,
		b->next);
}

int
Ffmt(Fmt *f)
{
	File *fi;

	fi = va_arg(f->args, File *);
	return fmtprint(f, "file(fd=%d path=%s offset=%lld length=%lld)",
		fi->fd,
		fi->path,
		fi->offset,
		fi->length);
}

/*
int
Rfmt(Fmt *f)
{
	Rate *r;
	int rr;
	String *s;
	int i;

	r = va_arg(f->args, Rate *);

	s = s_new();
	for(i = 0; i < nticks; i++){
		p = smprint("%uld", p[(r->i+i) % r->nticks]);
		s_append(s, p);
		free(p);
	}

	rr = fmtprint(f, "rate(nticks=%d i=%d cur=%lld first=%lld p=%s)",
		r->nticks,
		r->i,
		r->cur,
		r->first,
		s_to_c(s));
	s_free(s);
	return rr;
}
*/

int
Rfmt(Fmt *f)
{
	Rate *r;

	r = va_arg(f->args, Rate *);
	return fmtprint(f, "rate(nticks=%d up=%.2f down=%.2f))", r->nticks, ratecurrent(r, 20, Upload), ratecurrent(r, 20, Download));
}

int
Tfmt(Fmt *f)
{
	Torrent *t;

	t = va_arg(f->args, Torrent *);
	return fmtprint(f, "torrent(nfiles=%d piecelen=%uld length=%lld announce=%s infohash=%H rate=%R ul=%lld dl=%lld stored=%lld interval=%d listenport=%s strategy=%d haves=%B reqpieces=%B)",
		filelen(t->files),
		t->piecelen,
		t->length,
		t->announce,
		t->infohash,
		t->rate,
		t->ul,
		t->dl,
		t->stored,
		t->interval,
		t->listenport,
		t->strategy,
		t->haves,
		t->reqpieces);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.