/* File date routines.
 * Filename:	FileDates.c
 * History:		(most recent change first)
 *
 * 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();


/*---------------------------------------------------------------------*/
/*  GetFileDate: get the datestamp the given file.			       	   */
/*---------------------------------------------------------------------*/

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

	if ((FLock = (ULONG)Lock(name,(long)(ACCESS_READ)))== NULL)
		return FALSE;

	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);
	return result;
}

/* Examine a pathname to determine if it is a device or directory name.
 * Called with:
 *		name:		pathname specification
 * Returns:
 *		0 =>		no
 *		1 => 		yes
 *	   <0 =>		-<system error number>
 */
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=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;
}

/*---------------------------------------------------------------------*/
/*  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;
}
