/* MRBackup:	Listing support routines
 * Filename:	List.c
 * Author:		Mark R. Rinfret
 * Date:		11/27/87
 *
 * History:		(most recent changes first)
 *
 * 11/27/87 -MRR- This module was created for version 2.0.
 *
 */

#define MAIN

#include "MRBackup.h"

static char listDate[20] = "??/??/?? ??:??:??";

^L
/* Output a new header to the listing file. */

Header()
{
	if (doListing) {
		if (doFormat) {				/* A new disk? */
			fprintf(listing,
				"\f    MRBackup Listing of Volume %s created on %s\n\n", 
				destVol, listDate);
		}
		else {
			fprintf(listing,
			"\f    MRBackup Partial Listing of Volume %s updated on %s\n\n",
				destVol, listDate);
		}
		lineCount = 2;
	}
}
^L
/* List information about the file or directory we just created.
 * Called with:
 *		name:		destination filename string
 */

void
ListFileInfo(name)
	char *name;
{
	struct Lock *lock;
	char dateStr[20], infoLine[255];

	if (!doListing) return;

	if (! (lock = (struct Lock *) Lock(name, SHARED_LOCK))) {
		sprintf(infoLine, "ListFileInfo: could not lock %s", name);
		ListLine(infoLine);
		return;
	}

	if (Examine(lock, fibWork)) {
		if (fibWork->fib_DirEntryType >=0) {
			NewLine(2);
			sprintf(infoLine,"Directory:  %s", name);
			NewLine(1);
		}
		else {
			DS2Str(dateStr, "%02m/%02d/%02y %02h:%02n:%02s", 
					&fibWork->fib_Date);
			/* It is my understanding that the maximum filename length for
			 * a particular node is 30 characters.  The following code will
			 * probably never execute but it makes me feel warm and safe.
			 */

			if (strlen(fibWork->fib_FileName) > 40) {
				fibWork->fib_FileName[40] = '*';
				fibWork->fib_FileName[41] = '\0';
			}
			sprintf(infoLine,"  %s %8ld %5ld %s",
					dateStr, fibWork->fib_Size, fibWork->fib_NumBlocks,
					fibWork->fib_FileName);
		}
	}
	else {
		sprintf(infoLine,"ListFileInfo: can't examine %s", name);
	}
	UnLock(lock);
	ListLine(infoLine);
}
^L
/* Output a line to the listing file.  Start a new line if necessary.
 * The output string is assumed to have no form control characters.
 * Called with:
 *		str:		string to be output
 */
ListLine(str)
	char *str;
{	
	if (doListing) {
		if (lineCount >= LINES_PER_PAGE) Header();
		fprintf(listing,"  %s\n",str);
		fflush(listing);
		++lineCount;
	}
}
^L
/* Skip 'n' lines in the listing file.
 * Called with:
 *		n:		number of lines to skip
 */

NewLine(n)
	int n;
{
	if (doListing) {
		if (n + lineCount >= LINES_PER_PAGE)
			Header();
		else while (n--) {
			fputc('\n', listing);
			++lineCount;
		}
	}
}
^L
/* Open the listing file.
 * Called with:
 *		name:		file or device pathname
 * Returns:
 *		status (0 => success)
 */
int
OpenList(name)
	char *name;
{
	int status = 0;

	if (listing) fclose(listing);		/* prior listing file open? */
	if (!(listing = fopen(name, "w"))) {
		status = errno;
		sprintf(conmsg,
				"I can't open the listing file \"%s\", error %d.\n",
				name, status);
		TypeAndSpeak(conmsg);
	}
	else {
		DS2Str(listDate, "%02m/%02d/%02y %02h:%02n:%02s",now); 
		lineCount = LINES_PER_PAGE;
	}
	return status;
}
