<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf8">
<title>/usr/web/sources/contrib/arpunk/crap.c - Plan 9 from Bell Labs</title>
<!-- THIS FILE IS AUTOMATICALLY GENERATED. -->
<!-- EDIT sources.tr INSTEAD. -->
</meta>
</head>
<body>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<p style="line-height: 1.2em; margin-left: 1.00in; text-indent: 0.00in; margin-right: 1.00in; margin-top: 0; margin-bottom: 0; text-align: center;">
<span style="font-size: 10pt"><a href="/plan9/">Plan 9 from Bell Labs</a>’s /usr/web/sources/contrib/arpunk/crap.c</span></p>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<center><font size=-1>
Copyright © 2009 Alcatel-Lucent.<br />
Distributed under the
<a href="/plan9/license.html">Lucent Public License version 1.02</a>.
<br />
<a href="/plan9/download.html">Download the Plan 9 distribution.</a>
</font>
</center>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<table width="100%" cellspacing=0 border=0><tr><td align="center">
<table cellspacing=0 cellpadding=5 bgcolor="#eeeeff"><tr><td align="left">
<pre>
<!-- END HEADER -->
#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);
}
<!-- BEGIN TAIL -->
</pre>
</td></tr></table>
</td></tr></table>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<p style="line-height: 1.2em; margin-left: 1.00in; text-indent: 0.00in; margin-right: 1.00in; margin-top: 0; margin-bottom: 0; text-align: center;">
<span style="font-size: 10pt"></span></p>
<p style="margin-top: 0; margin-bottom: 0.50in"></p>
<p style="margin-top: 0; margin-bottom: 0.33in"></p>
<center><table border="0"><tr>
<td valign="middle"><a href="
http://www.alcatel-lucent.com/"><img border="0" src="/plan9/img/logo_ft.gif" alt="Bell Labs" />
</a></td>
<td valign="middle"><a href="
http://www.opensource.org"><img border="0" alt="OSI certified" src="/plan9/img/osi-certified-60x50.gif" />
</a></td>
<td><img style="padding-right: 45px;" alt="Powered by Plan 9" src="/plan9/img/power36.gif" />
</td>
</tr></table></center>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<center>
<span style="font-size: 10pt">(<a href="/plan9/">Return to Plan 9 Home Page</a>)</span>
</center>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<center><font size=-1>
<span style="font-size: 10pt"><a href="
http://www.lucent.com/copyright.html">Copyright</a></span>
<span style="font-size: 10pt">© 2009 Alcatel-Lucent.</span>
<span style="font-size: 10pt">All Rights Reserved.</span>
<br />
<span style="font-size: 10pt">Comments to</span>
<span style="font-size: 10pt"><a href="mailto:
[email protected]">
[email protected]</a>.</span>
</font></center>
</body>
</html>