;/* touch - compiles with SAS/C 5.10a by executing this file
lc -cfis -v -b0 -O -j73 touch
blink touch.o to touch
quit ;*/
/*
*	touch.c - touches files (makes their date NOW) - v1.0
*
*	Usage:	touch <file-list>
*
*	Handles standard AmigaDOS wildcards. If a given file doesn't exist (and
*	it's not a pattern) the file is created with size zero.
*
*	Martin W. Scott, 4/92.
*
*	v1.01 - added notification of file creation (12/5/92).
*/
#include <exec/types.h>
#include <libraries/dos.h>
#include <dos/dosasl.h>
#include <dos/rdargs.h>

#include <proto/exec.h>		/* use PRAGMAS */
#include <proto/dos.h>

void	createfile(char *), touch(char *),
	touchall(char *), main(void);

char version_str[] = "$VER: touch v1.01";

struct DosLibrary *DOSBase;
struct DateStamp now;			/* indeed */
struct AnchorPath __aligned ap;		/* for Match(First|Next|End) */


void main(void)		/* entry: touch files (set their date to NOW) */
{
	struct RDArgs *readargs;
	LONG rargs[1];
	char **strings;

	if (DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37L))
	{
		if (readargs = ReadArgs("FILES/A/M", rargs, NULL))
		{
			strings = (char **)(rargs[0]);

			DateStamp(&now);
			while (*strings)
			{
				touchall(*strings);
				strings++;
			}
		}
		else PrintFault(IoErr(), "touch");

		CloseLibrary(DOSBase);
	}

} /* main */


void createfile(char *file)		/* touch named file in current directory */
{
	BPTR fh;

	if (fh = Open(file, MODE_NEWFILE))
	{
		Close(fh);
		PutStr("Creating file ");
		PutStr(file);
		PutStr("\n");
	}
	else PrintFault(IoErr(), "touch");
}

void touch(char *file)		/* touch named file in current directory */
{
	if (!SetFileDate(file, &now))
		PrintFault(IoErr(), "touch");
}

void touchall(char *pat)	/* touch all files matching pattern */
{
	BPTR oldcd;
	LONG err;

	if ((err = MatchFirst(pat, &ap)) == 0)	/* success */
	{
		do {
			oldcd = CurrentDir(ap.ap_Current->an_Lock);
			touch(ap.ap_Info.fib_FileName);
			CurrentDir(oldcd);
			err = MatchNext(&ap);
		} while (!err);
	}

	if (!(ap.ap_Flags & APF_ITSWILD) && (err == ERROR_OBJECT_NOT_FOUND))
		createfile(pat);
	else if (err != ERROR_NO_MORE_ENTRIES)	/* abnormal error */
		PrintFault(err, "touch");

	MatchEnd(&ap);
}

