/* File date routines.
 * Filename:	FileDates.c
 * History:		(most recent change first)
 *
 * 01/01/88 -MRR- Happy new year!  Added a new function, CompareFileDates.
 * 09/24/87 -MRR- SetFileDate was allocating a BSTR of 68 bytes, rather
 *                than using the length of the file name string.  I don't
 *                have the foggiest notion why (that was DUMB!).
 */

#include "exec/types.h"
#include "exec/ports.h"
#include "exec/io.h"
#include "exec/memory.h"
#include "libraries/dosextens.h"
#include <stdio.h>
#define AZTEC 1
#ifdef AZTEC
#include "functions.h"				/* aztec C include */
#endif

#define ACTION_SETDATE_MODE 34L		/* Set creation date on file */
#define DOSTRUE 	    -1L			/* AmigaDos TRUE */
#define MAXARGS          7L			/* limit in packet structure
									   (dosextens.h) */
#define NARGS		     4L			/* Number of args for setdate */

long    sendpkt();

/* Function:
 *		CompareFileDates
 *
 * Called with:
 *		name1, name2: filenames for which dates are to be compared
 *
 * Returns:
 *		0 => dates are equal or both files don't exist
 *     -1 => date of <name1> < date of <name2> or <name1> doesn't exist
 *      1 => date of <name1> > date of <name2> or <name2> doesn't exist
 *
 * Description:
 *		CompareFileDates attempts to compare the file creation/modification
 *		dates of two files.
 */
int
CompareFileDates(name1, name2)
	char *name1, *name2;
{
	struct DateStamp d1, d2;

	GetFileDate(name1, &d1);
	GetFileDate(name2, &d2);
	return CompareDS(&d1, &d2);
}
^L
/* Function:
 *		GetFileDate
 * 
 * Called with:
 *		name:	file name
 *		date:	pointer to DateStamp structure
 *
 * Returns:
 *		result:	1 => got a date, 0 => didn't
 *
 * Description:
 *		GetFileDate attempts to get the creation/modification date
 *		of a file (unfortunately, they're one and the same) and stores
 *		it into the location pointed to by <date>.  If the file doesn't
 *		exist or for some reason the date can't be obtained, <date>
 *      is set to zeros and a zero is returned.  Otherwise, <date> is set
 *		to the file date and a 1 is returned.
 */

BOOL 
GetFileDate(name, date)
	char   *name; struct DateStamp *date;
{
	struct FileInfoBlock *Fib;
	ULONG   FLock;
	int     result = FALSE;
	register struct DateStamp  *d;

	if ((FLock = (ULONG) Lock(name,(long)(ACCESS_READ)))== NULL) 
		goto exit1;

	Fib = (struct FileInfoBlock *)
		AllocMem((long)sizeof(struct FileInfoBlock),
					(long)(MEMF_CHIP|MEMF_PUBLIC));

	if (Fib == NULL )
		result = FALSE;
	else{
		if (!Examine(FLock, Fib )) {
			result = FALSE;
		}
		else 
			if (Fib->fib_DirEntryType > 0 )
				result = FALSE;		/* It's a directory */
			else{
				d = &Fib->fib_Date;
				date->ds_Days = d->ds_Days;
				date->ds_Minute = d->ds_Minute;
				date->ds_Tick = d->ds_Tick;
				result = TRUE;
			}
		FreeMem((void *)Fib,(long)sizeof(struct FileInfoBlock));
	}

	UnLock(FLock);
exit1:
	if (! result ) {
		date->ds_Days = 0;
		date->ds_Minute = 0;
		date->ds_Tick = 0;
	}
	return result;
}
^L
/* Function:
 *		IsDir
 *
 * Called with:
 *		name:		pathname specification
 *
 * Returns:
 *		0 =>		no
 *		1 => 		yes
 *	   <0 =>		-<system error number>
 *
 * Description:
 *
 * 		Examine a pathname to determine if it is a device or directory name.
 */
int
IsDir(name)
	char *name;
{
	struct FileInfoBlock   *FIB = NULL;
    struct Lock *lock = NULL;	
	int status;


	if (!(FIB =
		AllocMem((long)sizeof(struct FileInfoBlock),
				 MEMF_CHIP|MEMF_CLEAR))) {
		return -ERROR_NO_FREE_STORE;
	}

	if (!(lock = (struct Lock *) Lock(name,SHARED_LOCK))) {
		status = -IoErr();
		goto done;
	}

	if ((Examine(lock,FIB))==0){
		status = -IoErr();
		goto done;
	}

	status = (FIB->fib_DirEntryType > 0);
done:
	if (FIB)
		FreeMem(FIB, (long) sizeof(struct FileInfoBlock));
	UnLock(lock);
	return status;
}
^L
/*---------------------------------------------------------------------*/
/*  SetFileDate: datestamp the given file with the given date.	       */
/*---------------------------------------------------------------------*/

BOOL SetFileDate(name,date )
char   *name;
struct DateStamp   *date;
{
	struct MsgPort     *task;		/* for process id handler */
	ULONG   arg[4];					/* array of arguments     */
	int nameleng;
	char   *bstr, *strcpy();		/* of file to be set      */
	long    rc;
	char   *strchr();
	int     strlen();

	rc = 0;

	nameleng = strlen(name);
	if (!(bstr = (char *)AllocMem((long) (nameleng + 2),MEMF_PUBLIC)))
		goto exit2;

	if (!(task = (struct MsgPort *)DeviceProc(name )))
		goto exit1;

 /* Dos Packet needs the filename in Bstring format */

	(void) strcpy(bstr+1, name );
	*bstr = nameleng;

	arg[0]= (ULONG)NULL;
	arg[1]= (ULONG)IoErr();			/* lock on parent director set by
									   DeviceProc() */
	arg[2]= (ULONG)bstr >> 2;
	arg[3]= (ULONG)date;
	rc = sendpkt(task,ACTION_SETDATE_MODE,arg,4L );

exit1: if (bstr )
		FreeMem((void *)bstr, (long) (nameleng + 2));
exit2: if (rc == DOSTRUE )
		return TRUE;
	else
		return FALSE;
}
