Plan 9 from Bell Labs’s /usr/web/sources/contrib/arpunk/crap.c

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


#include <u.h>
#include <libc.h>
#include <thread.h>
#include <fcall.h>
#include <9p.h>
#include <ctype.h>

#define CRAPMAXLEN 140

typedef struct Record Record;

Record* currec;
Record* lastrec;
long unsigned int writecount;

void crapwrite(Req *req);
void crapcreate(Req *req);
void crapread(Req *req);
void crapdestroy(File *f);
void usage(void);
void lstcrpcmd(char* cmd, uint len);
void printstats(void);

struct Record {
	char* usr;
	char* name;
	uint index;
	char data[CRAPMAXLEN];
	File* file;
};

static char Ewrong[] = "something wrong just happened";
static char Enomem[] = "no available memory";

void
lstcrpcmd(char* cmd, uint len)
{
	char str[140];
	char* c;

	if(len > 140) {
		fprint(2, "crapfs: ctl write too long\n");
		return;
	}

	fprint(2, "%s\n", cmd);
	return;
}

void
crapcreate(Req *req)
{
	Record *r;
	File *f;

	if(f = createfile(req->fid->file, req->ifcall.name, req->fid->uid, req->ifcall.perm, nil)) {
		if(strncmp(req->ifcall.name, "ctl", 3) == 0) {
			respond(req, "you cant create the ctl file\n");
			return;
		}

		r = emalloc9p(sizeof *r);
		memset(r->data, 0, CRAPMAXLEN);
		f->aux = r;
		req->fid->file = f;
		req->ofcall.qid = f->qid;
		r->index = 0;
		r->file = f;

		respond(req, nil);
		return;
	}
	respond(req, Ewrong);
}

void
crapwrite(Req *req)
{
	Record *r;
	long count;
	char str[256];

	r = req->fid->file->aux;

	if(strncmp(r->name, "ctl", 3) == 0) {
		fprint(2, "Hello!\n");
		lstcrpcmd(req->ifcall.data, req->ifcall.count);
		respond(req, nil);
		return;
	}
}

void
crapread(Req *req)
{
	Record *r;
	char ctlread[1024];
	long count;
	vlong offset;

	r = req->fid->file->aux;
	count = req->ifcall.count;
	offset = req->ifcall.offset;

	if(strncmp(r->name, "ctl", 3) == 0) {
		if((offset > 0) || (writecount == 0) || (currec == nil)) {
			req->ofcall.count = 0;
			respond(req, nil);
			return;
		}
		memset(ctlread, 0, 1024);
		snprint(ctlread, 1024, "Rec:%s index:%d data:%sd\n", currec->name, currec->index, currec->data);
		count = strlen(ctlread);
		if(req->ifcall.count < count) {
			respond(req, "crap: read too small\n");
			return;
		}
		memmove(req->ofcall.data, ctlread, count);
		req->ofcall.count = count;
		respond(req, nil);
		return;
	}

	respond(req, nil);
}

Srv crapsrv = {
    .read = crapread,
	.write = crapwrite,
    .create = crapcreate,
};

void
usage(void)
{
	fprint(2, "usage: crap [-D] [-s srvname] [-m mntpt]\n");
	exits("usage");
}

void
printstats(void)
{

}

void
crapdestroy(File *f)
{
	Record *rf;
	rf = f->aux;
	if(rf) {
		free(rf->data);
		free(rf);
	}
}

void
main(int argc, char **argv)
{
	char *addr = nil;
	char *srvname = nil;
	char *mntpt = nil;

	Qid q;
	File* ctl;
	Record* ctlrec;

	crapsrv.tree = alloctree(nil, nil, DMDIR|0777, crapdestroy);
	q = crapsrv.tree->root->qid;

	if(ctl = createfile(crapsrv.tree->root, "ctl", getenv("user"), 0664, nil)) {
		ctlrec = emalloc9p(sizeof *ctlrec);
		ctl->aux = ctlrec;
		ctlrec->index = 0;
		ctlrec->name = strdup("ctl");
		ctlrec->file = ctl;
	} else {
		sysfatal("could not create control file\n");
	}

	if(ctl = createfile(crapsrv.tree->root, "status", getenv("user"), 0664, nil)) {
		ctlrec = emalloc9p(sizeof *ctlrec);
		ctl->aux = ctlrec;
		ctlrec->index = 0;
		ctlrec->name = strdup("ctl");
		ctlrec->file = ctl;
	} else {
		sysfatal("could not create status file\n");
	}

	ARGBEGIN{
	case 'D':
		chatty9p++;
		break;
	case 'a':
		addr = EARGF(usage());
		break;
	case 's':
		srvname = EARGF(usage());
		break;
	case 'm':
		mntpt = EARGF(usage());
		break;
	default:
		usage();
	}ARGEND;

	if(argc)
		usage();
	if(chatty9p)
		fprint(2, "datasrv.nopipe %d srvname %s mntpt %s\n", crapsrv.nopipe, srvname, mntpt);
	if(addr == nil && srvname == nil && mntpt == nil)
		sysfatal("must specify -a, -s or -m");
	if(addr)
		listensrv(&crapsrv, addr);
	if(srvname || mntpt)
		postmountsrv(&crapsrv, srvname, mntpt, MREPL|MCREATE);
	exits(0);
}

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].