From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: To: 9fans@cse.psu.edu Subject: Re: [9fans] i need someone to pinpoint a sample program for me : ) From: "Sascha Retzki" Date: Tue, 5 Sep 2006 22:25:30 +0200 In-Reply-To: <44F699AA.7010802@enerla.net> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Topicbox-Message-UUID: ae6f9108-ead1-11e9-9d60-3106f5b1d025 > Ahoi all... > Im like lets starts something basic (basic might be a relative term), > like a server that exports a ctl and a data file, > every line written to ctl can be read from data with the letters sorted. I did something like this some long time ago, I was told that using 9p(2)/9pfile(2) for such tiny things, which file structure does not change much is not suggested - I actually did not get that. Anyway, this may not even compile, I copied out of a source-tree. You want to read it and understand every line, anyway, do you? ;) #include #include #include #include #include <9p.h> Srv *daefs; /* our file layout. Note that the enum Qstatus is used in daefsfiles_create() to determine the size of the array. Either fix the function or add files (in any) before Qstatus. */ enum { Qctl, Qout, Qstatus, }; struct { char name[20]; /* name of the file */ ulong mode; /* permission mode */ File *file; /* File-struct used by createfile() */ char *data; /* The data this file contains */ } daefsfiles[] = { "ctl", 0666, nil, nil, "output", 0444, nil, nil, "status", 0444, nil, nil, }; void daefs_write(Req *r) { int i, myQid = (int)r->fid->file->qid.path; if(myQid > Qstatus) sysfatal("unknown Qid %d in write-call\n",myQid); for(i=0;i+r->ifcall.offset < FILESIZE && i < r->ifcall.count;i++) { daefsfiles[myQid].data[i+r->ifcall.offset] = r->ifcall.data[i]; r->ofcall.count++; } daefsfiles[myQid].file->length = r->ifcall.offset + r->ifcall.count; if(myQid == Qctl) { char *msg = daefsfiles[Qctl].data; // Do your processing... // Copy the string strcpy(daefsfiles[Qstatus].data,msg); // Do not forget to set the new length of the file, for stat(5) etc. daefsfiles[Qstatus].file->length = strlen(msg); } respond(r,nil); } void daefs_read(Req *r) { readstr(r,daefsfiles[(int)r->fid->file->qid.path].data); respond(r,nil); } void daefsfiles_create(void) { int i; if((daefs->tree = alloctree("daefs","daefs",DMDIR|0777,nil)) == nil) sysfatal("daefs: daefsfiles_create(): alloctree() == nil"); for(i=0;i<=Qstatus;i++) { daefsfiles[i].data = emalloc9p(FILESIZE); if((daefsfiles[i].file = createfile(daefs->tree->root, daefsfiles[i].name,"daefs", daefsfiles[i].mode,(void *)daefsfiles[i].data)) == nil) sysfatal("daefs: daefsfiles_create() for file \"%s\": createfile() == nil\n",daefsfiles[i].name); } daefs_status("ready\n"); } void main(int argc, char **argv) { char *mtpt = "/n/daefs/"; daefs = emalloc9p(sizeof(Srv)); daefs->write = daefs_write; daefs->read = daefs_read; daefsfiles_create(); threadpostmountsrv(daefs,"daefs",mtpt,0); }