/*****************************************************************************
 *
 * VERSION:
 *   1.00
 *
 * NOTE:
 *   This file and all accompanying source files are copyrighted (c) 1991 by
 *   Louis Zirkel III.  This source code is include so the individual can
 *   modify this utility for his/her private use.
 *
 *   This code was compiled using BC++ v2.00
 *
 *   See NMSGS.DOC for details on using this utility.
 *
 *****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include "nmsgs.h"

/*
  The line below controls whether the program behaves 'quietly' or not.
  Comment this line out to display the number of messages.
*/
/**
#define	QUIET
 **/

/*****************************************************************************
 * int main(int argc, char **argv)
 * Program entry point.
 *****************************************************************************/
int	main(int argc, char **argv)
{
	register		i;
	int				File,
					NumberMessages = 0,
					TargetNumber = 0;
	msgBaseHeader	mBHdr;
	msgHeader		mHdr;

	for (i = 1; i < argc; i++) {
		if (argv[i][0] == '-') {
			switch (argv[i][1]) {
				case 'n' :
						if (argv[i][2]) {
							TargetNumber = atol(argv[i] + 2);
						} else if (argv[i + 1][0] != '-') {
							TargetNumber = atol(argv[++i]);
						} else {
							fprintf(stderr, "Invalid number of messages\n");
							exit(EXIT_FAILURE);
						}
					break;
				case 't' :
						fprintf(stderr, "Base Header     %u\n", sizeof(msgBaseHeader));
						fprintf(stderr, "Message Header  %u\n", sizeof(msgHeader));
						exit(EXIT_FAILURE);
					break;
				default :
					break;
			}
		} else {
			if ((File = msgOpen(strupr(argv[i]))) == -1) {
				perror(argv[i]);
				exit(EXIT_FAILURE);
			}
			msgRead(File, &mBHdr);
			while (msgRead(File, &mHdr)) {
				if (mHdr.msgActive == MSG_ACTIVE) {
					NumberMessages++;
				} else if (mHdr.msgActive == MSG_INACTIVE) {
				} else {
				}
				msgSeek(File, mHdr.msgBlocks);
			}
			msgClose(File);
			break;
		}
	}

#ifndef	QUIET
	printf("Number of messages: %d\n", NumberMessages);
#endif

	return (NumberMessages >= TargetNumber) ? EXIT_SUCCESS : EXIT_FAILURE;
}


/*****************************************************************************
 * int msgOpen(char *filename)
 * Open a messge base for reading with file sharing.
 *****************************************************************************/
int	msgOpen(char *filename)
{
	return open(filename, O_BINARY | O_RDONLY | O_DENYNONE);
}


/*****************************************************************************
 * int msgClose(int fd)
 * Close a message file.
 *****************************************************************************/
int	msgClose(int fd)
{
	return close(fd);
}


/*****************************************************************************
 * int msgRead(int fd, void *buf)
 * Read a standard block (128 bytes) from a message file.
 *****************************************************************************/
int	msgRead(int fd, void *buf)
{
	return read(fd, buf, MSG_BUFSIZE);
}


/*****************************************************************************
 * int msgWrite(int fd, void *buf)
 * Write a standard block (128 bytes) to a message file.
 *****************************************************************************/
int	msgWrite(int fd, void *buf)
{
	return write(fd, buf, MSG_BUFSIZE);
}


/*****************************************************************************
 * long msgSeek(int fd, byte blocks)
 * Seek to the next message in a message file.
 *****************************************************************************/
long	msgSeek(int fd, byte blocks)
{
	return lseek(fd, (long)((blocks - 1) * MSG_BUFSIZE), SEEK_CUR);
}
