[alt.sources] laston.c - determine last time user has logged on

davek@lakesys.lakesys.com (Dave Kraft) (01/09/90)

Here's a little something I wrote to help me determine when I've last logged
on.  It gets the current time/date from /dev/clock.  If you aren't
familiar on how /dev/clock is broken down, here it is:

	0108182790

Positions Meaning
1-2       month
3-4       day
5-6       hour (24-hour format)
7-8       minute
9-10      year

It gets the last time on from a file in the same format as /dev/clock in your
home directory.

If you have any questions, improvements, etc., please leave email.

Dave
---- cut here --
#include <stdio.h>

main()
{
	struct {
		char month[3];
		char day[3];
		char hour[3];
		char minute[3];
		char year[3];
	} now, last;
	int flag = 0;
	char *fn;
	FILE *f1, *fopen();

	fn = getenv("HOME");
	strcat(fn,"/.laston");
	putchar('\n');
	f1 = fopen("/dev/clock","r");
	fread(now.month,2,1,f1);
	fread(now.day,2,1,f1);
	fread(now.hour,2,1,f1);
	fread(now.minute,2,1,f1);
	fread(now.year,2,1,f1);
	fclose(f1);
	add_null(now.month,2);
	add_null(now.day,2);
	add_null(now.hour,2);
	add_null(now.minute,2);
	add_null(now.year,2);
	f1 = fopen(fn,"r");
	if(f1 == NULL){
		flag = 1;
		printf("Could not determine last time on.\n");
	}
	if(flag == 0){
		fread(last.month,2,1,f1);
		fread(last.day,2,1,f1);
		fread(last.hour,2,1,f1);
		fread(last.minute,2,1,f1);
		fread(last.year,2,1,f1);
		add_null(last.month,2);
		add_null(last.day,2);
		add_null(last.hour,2);
		add_null(last.minute,2);
		add_null(last.year,2);
	}
	fclose(f1);
	printf("Last on:  ");
	if(flag == 0)
		printf("%s:%s\t%s/%s/%s\n",last.hour,last.minute,last.month,
			last.day,last.year);
	else
		printf("%s:%s\t%s/%s/%s\n",now.hour,now.minute,now.month,
			now.day,now.year);
	f1 = fopen(fn,"w");
	fwrite(now.month,2,1,f1);
	fwrite(now.day,2,1,f1);
	fwrite(now.hour,2,1,f1);
	fwrite(now.minute,2,1,f1);
	fwrite(now.year,2,1,f1);
	fclose(f1);
	putchar('\n');
}
add_null(s,pos)
char s[];
int pos;
{
	s[pos] = '\0';
}
-- 
davek@lakesys.lakesys.com <OR> uunet!marque!lakesys!davek
-------------------------------------------------------------------------------
"Empathy is sort of like telepathy's kid brother"
  -- taken from "Stardance" by Spider and Jeanne Robinson

tneff@bfmny0.UU.NET (Tom Neff) (01/10/90)

In article <1540@lakesys.lakesys.com> davek@lakesys.lakesys.com (Dave Kraft) writes:
>Here's a little something I wrote to help me determine when I've last logged
>on...

Perhaps a bit overdone?  How about

	#! /bin/sh
	lof=$HOME/.laston
	test -f $lof && echo "Last on: "`cat $lof`
	date > $lof

-- 
'We have luck only with women -- not spacecraft!'     \\  Tom Neff
 -- R. Kremnev, builder of failed Soviet FOBOS probes //  tneff@bfmny0.UU.NET