9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: "Sascha Retzki" <sretzki@gmx.de>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] i need someone to pinpoint a sample program for me : )
Date: Tue,  5 Sep 2006 22:25:30 +0200	[thread overview]
Message-ID: <f51978c6a85f48d5386bc77ed61297d9@mail.gmx.net> (raw)
In-Reply-To: <44F699AA.7010802@enerla.net>

> 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 <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#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);
}



      parent reply	other threads:[~2006-09-05 20:25 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-08-25  2:30 [9fans] replica Richard Bilson
2006-08-25  3:38 ` Lyndon Nerenberg
2006-08-25  4:08   ` geoff
2006-08-25  8:19     ` Martin Neubauer
2006-08-27 23:23       ` geoff
2006-08-28  2:46         ` Richard Bilson
2006-08-25 14:22 ` [9fans] Pegasus 2.2 is released arisawa
2006-08-26  1:56   ` Skip Tavakkolian
2006-08-26  4:36     ` arisawa
2006-08-30  1:32       ` Lyndon Nerenberg
2006-08-26 19:29   ` Skip Tavakkolian
2006-08-26 21:51     ` arisawa
2006-08-29  4:10       ` [9fans] " Dave Eckhardt
2006-08-29  8:16         ` arisawa
2006-08-29 19:57           ` erik quanstrom
2006-08-30  5:39             ` Dave Eckhardt
2006-08-30 23:59               ` erik quanstrom
2006-08-31  8:11                 ` [9fans] i need someone to pinpoint a sample program for me : ) bituman
2006-08-31 10:18                   ` Gabriel Diaz
2006-08-31 12:52                     ` rog
2006-08-31 13:38                       ` Gabriel Diaz
2006-08-31 13:52                         ` Russ Cox
2006-08-31 14:04                           ` rog
2006-09-07 23:54                             ` Russ Cox
2006-08-31 14:05                         ` rog
2006-08-31 15:30                     ` David Leimbach
2006-08-31 15:32                       ` David Leimbach
2006-08-31 15:05                   ` Sergey Zhilkin
2006-09-05 20:25                   ` Sascha Retzki [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f51978c6a85f48d5386bc77ed61297d9@mail.gmx.net \
    --to=sretzki@gmx.de \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).