
/************************************************************************/
/*			      Touch V1.10				*/
/* Written and Copyright ©1993 by Dave Schreiber.  All Rights Reserved. */
/*									*/
/* This is an Amigaized version of the Unix utility with the same name. */
/* Usage:								*/
/*    Touch FILE/A/M,NOCREATE=-V/S					*/
/*									*/
/* Touch sets the date/time of all specified files to the current date	*/
/* and time.  This uses Workbench 2.0 wildcard pattern-matching, so all */
/* 2.0 wildcards are valid.						*/
/*									*/
/* To compile (SAS/C V6.0):                                             */
/*    smake								*/
/*									*/
/* Version history:							*/
/*    1.10 - If a file is specified but does not exist, touch now	*/
/*	     creates it (unless the NOCREATE flag is specified).        */
/*	     July 28, 1993						*/
/*    1.01 - Fixed an enforcer hit					*/
/*	     July 17, 1993						*/
/*    1.00 - Initial Release.						*/
/*	     July 17, 1993						*/
/************************************************************************/

#include <exec/types.h>
#include <dos/dos.h>
#include <exec/memory.h>

#include <clib/dos_protos.h>
#include <clib/exec_protos.h>

char trashBuf[512];

struct RDArgs ra=
{
   {NULL,0,0},
   NULL,
   trashBuf,
   512,
   "FILE/A/M,NOCREATE=-V/S"
};

char *version="$VER: Touch V1.10 (28.7.93)";

char *copyright="Copyright 1993 by Dave Schreiber.  All Rights Reserved";

int __main(void);
void touchFiles(char *pattern,BOOL);

int __main(void)
{
   ULONG args[3];
   char **filenames;
   int c;
   BOOL createFiles=TRUE;

   args[0]=args[1]=NULL;
   /*Get the list of filenames/patterns*/
   ReadArgs("FILE/A/M,NOCREATE=-V/S",args,&ra);
   filenames=(char **)args[0];
   if(args[1])
      createFiles=FALSE;

   /*Update the date and time of each name matching any given pattern*/
   if(filenames!=NULL)
      for(c=0;filenames[c]!=NULL;c++)
	 touchFiles(filenames[c],createFiles);

   /*Free resources allocated by ReadArgs()*/
   FreeArgs(&ra);

   return 0;
}

void touchFiles(char *pattern,BOOL createFiles)
{
   struct AnchorPath *anchor;
   struct DateStamp currentDate;
   BPTR origDir;
   char temp[514];

   /*Allocate a structure required by MatchFirst()*/
   anchor=(struct AnchorPath *)AllocMem(sizeof(struct AnchorPath),MEMF_CLEAR);
   if(anchor==NULL)
      return;

   /*Get first file*/
   if(MatchFirst(pattern,anchor)==0)
   {
      /*Update date/time*/
      origDir=CurrentDir(anchor->ap_Current->an_Lock);
      DateStamp(&currentDate);
      SetFileDate(anchor->ap_Info.fib_FileName,&currentDate);
      CurrentDir(origDir);

      /*Update the rest of the files*/
      while(MatchNext(anchor)==0)
      {
	 origDir=CurrentDir(anchor->ap_Current->an_Lock);
	 DateStamp(&currentDate);
	 SetFileDate(anchor->ap_Info.fib_FileName,&currentDate);
	 CurrentDir(origDir);
      }
   }
   else
      /*If no files were found matching the pattern, check to see*/
      /*if it actually a pattern, or just a regular filename*/
      if(createFiles && ParsePatternNoCase(pattern,temp,514)==0)
      {
	 /*If its a filename, and the user hasn't disallowed the creation*/
	 /*of unfound files, then create a file of the given name.*/
	 BPTR file;
	 file=Open(pattern,MODE_NEWFILE);
	 if(file!=NULL)
	    Close(file);
      }

   /*All done*/
   MatchEnd(anchor);
   FreeMem(anchor,sizeof(struct AnchorPath));

   return;
}

