
/*
 *	Function	FullSpoolInfo
 *	Programmer	N.d'Alterio
 *	Date		13/09/95
 *
 *  Synopsis:	Builds up a array of strings containing all the 
 *		information about the contents of the spool directory.
 *		Includes date, destination and size. Use FreeFullInfo
 *		to return memory allocated by this function.
 *
 *  Arguments:	struct SWArgs *swa		Prog arguments structure.
 *		char type			Char to determine if news
 *						or mail spool dir. 
 *
 *  Returns:	struct FullInfo *fi		FullInfo structure pointer.
 *		NULL				If error.
 *
 *  Variables:	 exit_code			Loop error exit flag.
 *		 num_days			Number of days since writing.
 *		 num_hours			Number of hours since writing.
 *		 num_mins			Number of minutes since writing.
 *		 mem_failed			Flag for of mem alloc fails.
 *		*dest				Array to place destination string in.
 *		*spooldir			Name of spool directory.
 *		*lock				Lock on spool dir.
 *		*old_lock			Lock on current directory.
 * 		*fib				FileInfoBlock pointer
 *		*fi				FullInfo structure.
 *		*cur_fi				Pointer to current FullInfo
 *		 ds				Date stamp structure.
 *
 *  Functions:	AllocMem			Allocate memory (EXEC)
 *		FreeMem				Free memory (EXEC)
 *		SWError				Display error message (SW)
 *		strcpy				Copy one string to another (ANSI)
 *		Examine				Get info on file/dir (DOS)
 *		ExNext				Get info on next file/dir (DOS)
 *		AllocDosObject			Allocate dos object (DOS)
 *		FreeDosObject			Free dos object (DOS)
 *		DateDiff			Difference between 2 dates (SW)
 * 		Lock				Lock file or dir (DOS)
 *		UnLock				Unlock file or dir (DOS)
 *		CurrentDir			Change directory (DOS)
 *		GetDest				Get destination of msg (SW)
 *		StopStrAt			Stop string at char (SW)
 *
 *  $Id: FullSpoolInfo.c 2.1 1995/09/23 20:11:15 daltern Exp $
 *
 */

#include "SpoolWatch.h"

struct FullInfo *FullSpoolInfo( struct SWArgs *swa, char type  )

{

  int exit_code;

  int num_days;
  int num_hours;
  int num_mins;

  char dest[MAX_DEST_LEN];

  char *spooldir;

  BPTR lock;
  BPTR old_lock;

  struct FileInfoBlock *fib;

  struct FullInfo      *fi     = NULL;
  struct FullInfo      *cur_fi = NULL;

  struct DateStamp      ds;


/* 
 *  Get current date stamp to compare with file datestamp.
 */

  DateStamp( &ds );

/*
 *   Set spooldir.
 */

  switch ( type ) {

	case MAIL_TYPE:
		
		spooldir  = swa->mail_spooldir;
		break;

	case NEWS_TYPE:

		spooldir  = swa->news_spooldir;
		break;

  }   /* end switch type */

/*
 *   Do a full scan of spool directory.
 */

  if ( lock = Lock( spooldir, ACCESS_READ ) ) {

	old_lock = CurrentDir( lock );
	fib = (struct FileInfoBlock *)AllocDosObject( DOS_FIB, NULL );

	fi        = NULL;
	exit_code = SUCCESS;

	Examine( lock, fib );
	while ( ExNext( lock, fib ) && !exit_code ) {

		if ( fib->fib_DirEntryType < 0 ) {


/*
 *  Get destination. Only allocate structure if successfully get
 *  the destination from file otherwise it is probably one
 *  of the support file made by the mailer or newsreader so
 *  it can be ignored.
 */

			if ( !GetDest( fib->fib_FileName, type, dest ) ) {

/*
 *   Allocate memory for this file.
 */

				if ( !fi ) {

					cur_fi = (struct FullInfo *)AllocMem( sizeof( struct FullInfo ), MEMF_ANY | MEMF_CLEAR );
					if ( cur_fi ) fi = cur_fi;
				
				} else {

					cur_fi->fi_next = (struct FullInfo *)AllocMem( sizeof( struct FullInfo ), MEMF_ANY | MEMF_CLEAR );
			
					if ( cur_fi->fi_next ) cur_fi = cur_fi->fi_next;

				}    /*  end if !fi */

/*
 *   Process destination string.
 */

				if ( cur_fi ) {

					StopStrAt( dest, ',' );

/*
 *  Calculate time in queue.
 */

					DateDiff( ds, fib->fib_Date, &num_days, &num_hours, &num_mins ); 

/*
 *   Fill in the structure.
 */
			
					
					cur_fi->fi_size  = fib->fib_Size;

					cur_fi->fi_days  = num_days;
					cur_fi->fi_hours = num_hours;
					cur_fi->fi_mins  = num_mins;

					strcpy( cur_fi->fi_dest, dest );

				} else {

					exit_code = FAIL;
					SWError( swa, NULL, "Not enough memory" );
					FreeFullInfo( fi );
					fi = NULL;

				}   /* end if AllocMem */
				
			}   /* end if found dest */

		}   /* end if file */

	}   /* end while */

	FreeDosObject( DOS_FIB, fib );
	CurrentDir( old_lock );
	UnLock( lock );

  } else {

	SWError( swa, NULL, "Could not lock spool directory" );

  }   /* end if lock */

  return fi;

}   /* end function FullSpoolInfo */

/*========================================================================*
		    END FUNCTION FullSpoolInfo
 *========================================================================*/