//---------------------------------------------------------------------------
// LOGOUT										(c) Laurence Vanhelsuwe 1994
// ------
// This program is part of a group of 3 complimentary programs to log the
// usage of a stand-alone machine. (LOGIN, LOGOUT, PASSWORD)
//
// This program has been written in ANSI C to make it fully portable across
// different hardware platforms.
//
// Original development and testing was carried out on a Commodore Amiga 3000.
//
// History
// -------
// 01-JAN-94: Design and initial file creation.
//
//
//---------------------------------------------------------------------------

//#define		DEBUG			1

#include	"stdio.h"
#include	"stdlib.h"
#include	"string.h"
#include	"fcntl.h"
#include	"time.h"

#define		MAX_NAME_LEN	(10)
#define		ENTRY_LEN		(65)

#ifdef	AMIGA
#define		LFILE			"DEVS:USERLOG.DAT"
#define		PFILE			"DEVS:PASSWORDS.DAT"
#define		HELP			"?"
#define		OPEN_MODE		(O_RDWR)

#else

#define		LFILE			"C:/DOS/USERLOG.DAT"
#define		PFILE			"C:/DOS/PASSWOR.DAT"
#define		HELP			"/?"
#define		OPEN_MODE		(O_RDWR | O_BINARY)

#endif

//---------------------------------------------------------------------------
// Function Prototypes
// -------------------
//---------------------------------------------------------------------------

void get_log_entry(void);
void change_log_entry(void);

//---------------------------------------------------------------------------
// Globals
// -------
//---------------------------------------------------------------------------

char *entry_buffer="X AAAAAAAAAA DDD MMM dd hh:mm:ss YYYY - DDD MMM dd hh:mm:ss YYYY\r";
char name[MAX_NAME_LEN+2];

long int file_size, log_pos;
int error, num_entries;

int logfile, passwfile;			// LEVEL 1 ANSI C File Handles

time_t	systime;
struct tm *tim;

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

main(int argc, char **argv) {

char *ptr, ch;
int len;

	if(argc > 1 ) {
		printf("LOGIN V1.0  (Copyright (c) Jan 1994 L. Vanhelsuwe)\n\n");
		printf("Programmed by Laurence Vanhelsuwe in C on an Amiga 3000.\n");
		printf("Ported to IBM PC environment by L. Vanhelsuwe.\n\n");
		printf("USAGE: LOGOUT [%s]\n\n", HELP);
	}

	if ((passwfile = open(PFILE, OPEN_MODE)) == -1L) {
		fprintf(stderr, "Passwords file does not exist !\n");
		exit(10);
	}
	close(passwfile);

	if ((  logfile = open(LFILE, OPEN_MODE)) == -1L) {
		fprintf(stderr, "Log file does not exist !\n");
		exit(10);
	}

	get_log_entry();					// get last log entry in entry_buffer

	if (entry_buffer[0] == 'I') {
		entry_buffer[0]  = 'O';			// set entry to correctly logged off

		time(&systime);						// get machine local time
		tim = localtime(&systime);			// convert to universal time format
		strncpy(&entry_buffer[40], asctime(tim), 24);	// copy to log file line

		ptr = &entry_buffer[3];			// find length of user's name
		len = 1;
		while (len < MAX_NAME_LEN) {
			if (*ptr++ != ' ') len++;
			else break;
		}

		strncpy(name, &entry_buffer[2], len);	// maximum MAX_NAME_LEN chars

		printf("\n");
		printf("User '%s' Logging out...\n\n", name);

		printf("Session duration:\n");	ch = entry_buffer[37]; entry_buffer[37]=0;
		printf("LOGIN  : %s\n", &entry_buffer[13]);
		printf("LOGOUT : %s\n", &entry_buffer[40]);
		printf("\n"); entry_buffer[37] = ch;

		change_log_entry();

	} else {
		printf("\n");
		printf("  -- WARNING --\n\n");
		printf("No user currently logged in ! (Please use LOGIN)\n");
	}
}

//---------------------------------------------------------------------------
// Seek to the last entry and read it in our fixed buffer.
//---------------------------------------------------------------------------
void get_log_entry(void) {
int chars,i;

	lseek(logfile, 0L, SEEK_END); file_size = tell(logfile);

	num_entries = file_size/ENTRY_LEN;
	log_pos = (num_entries-1)*ENTRY_LEN;

#ifdef DEBUG
	printf("File size = %ld, Num Entries = %d, LOG Position = %d, ENTRY_LEN = %d\n",
			 file_size, num_entries, log_pos, ENTRY_LEN);
#endif

	lseek(logfile, log_pos, SEEK_SET);
	chars = read (logfile, (void*) entry_buffer, ENTRY_LEN);

#ifdef DEBUG
	for (i=0; i< 16; i++)
	printf("buf[i] = '%c'	buf[i+16] = '%c'	buf[i+32] = '%c'	buf[i+48] = '%c'\n",
	 entry_buffer[i],
	 entry_buffer[i+16],
	 entry_buffer[i+32],
	 entry_buffer[i+48]);
	printf("BUF[64]='%c'	BUF[65]='%c'\n", entry_buffer[64], entry_buffer[65]);

	if (chars != ENTRY_LEN) {
		printf("ERROR: get_log_entry() read didn't read %d chars (but %d!)\n", ENTRY_LEN, chars);
		exit(10);
	}
#endif

	entry_buffer[12] = 0;			// zero-terminate user field
}
//---------------------------------------------------------------------------
// Seek back to the start of the last entry and overwrite it with the updated
// log entry.
//---------------------------------------------------------------------------
void change_log_entry(void) {

	entry_buffer[12] = ' ';			// restore space after name

	lseek(logfile, log_pos, SEEK_SET);
	write(logfile, (void*) entry_buffer, ENTRY_LEN);
	close(logfile);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
