Plan 9 from Bell Labs’s /usr/web/sources/contrib/anothy/src/cmd/notify/awin.c

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


/* Acme notifications */

#include <u.h>
#include <libc.h>

int bfd, cfd, tfd;
char srvf[1024];
Dir *dir;
void newwin(void);
int rmservice(void *, char *);

void
main(int argc, char *argv[])
{
	int f, p[2];
	long n, m;
	char buf[1024];

	USED(argc);
	USED(argv);

	sprint(srvf, "/srv/notify.%s", getuser());
	dir = dirstat(srvf);
	if (dir)
		exits("srv file exists");
	free(dir);

	pipe(p);

	newwin();

	atnotify(rmservice, 1);
	f = create(srvf, OWRITE, 0222);
	if(f < 0)
		exits(smprint("%r [create]"));
	snprint(buf, sizeof buf, "%d", p[1]);
	if(write(f, buf, strlen(buf)) != strlen(buf))
		exits(smprint("%r [write]"));
	close(f);

	for(;;){
		m = read(p[0], buf, sizeof buf);

		n = write(cfd, "cleartag\n", 9);
		if(n != 9){
			close(cfd);
			newwin();
		}

		write(tfd, smprint(" %s", buf), m+1);
		write(bfd, buf, m);
	}
}

void
newwin(void)
{
	int n, wid;
	char buf[1024];

	n = open("/mnt/acme/new/ctl", ORDWR);
	read(n, buf, 11);
	wid = atoi(buf);
	if(wid <= 0)
		exits(smprint("%r [wid]"));
	if((bfd = open(smprint("/mnt/acme/%d/body", wid), OWRITE)) < 0)
		exits(smprint("%r [bfd]"));
	if((cfd = open(smprint("/mnt/acme/%d/ctl", wid), OWRITE)) < 0)
		exits(smprint("%r [cfd]"));
	if((tfd = open(smprint("/mnt/acme/%d/tag", wid), OWRITE)) < 0)
		exits(smprint("%r [tfd]"));
	write(cfd, "name +notify\n", 13);
	close(n);
}

int
rmservice(void *, char *)
{
	remove(srvf);
	return 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 webmaster@9p.io.