Plan 9 from Bell Labs’s /usr/web/sources/contrib/steve/misc/activity.c

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


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

enum {
	secperday = 60 * 60 * 24
};

void
logevent(char *msg, long t)
{
	int fd;
	char *fn, *s, *p;

	s = ctime(t);
	if((p = strchr(s, '\n')) != nil)
		*p = 0;

	fn = smprint("/usr/%s/lib/activity", getuser());
	fd = open(fn, OWRITE);
	free(fn);
	if(fd == -1)
		return;
	seek(fd, 0, 2);				// should be append anyway
	fprint(fd, "%s %s\n", s, msg);
	close(fd);
}

void
main(void)
{
	Dir *d;
	long mday, mouse, day, now;

	day = 0;
	mday = 0;
	while(1){
		now = time(nil);
		if((d = dirstat("#m/mouse")) == nil)
			sysfatal("#m/mouse - cannot stat - %r\n");
		mouse = d->atime;

		if(day != now/secperday){
			if(day && day == mday)
				logevent("logout", mouse);
			day = now/secperday;
		}

		if(mday != mouse/secperday){
			if(mday)
				logevent("login", mouse);
			else
				logevent("restart", mouse);
			mday = mouse/secperday;
		}
		sleep(60*5*1000);
	}
}

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