/* hunkpad.c -- written by Aaron Avery -- released 6/12/87 */

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

extern struct FileInfoBlock *AllocMem();
extern struct FileLock *Lock();

main(argc, argv)
int argc;
char *argv[];
{
int i, size;
char *pad;
long test, file;
struct FileInfoBlock *FBlock;
struct FileLock      *FLock;

   if(argc != 2)
      { printf("Usage: %s <file>\n",argv[0]); exit(0); }

   pad = "\0\0\003\362"; /* hunk_end to pad with */

   if(!(FLock = Lock(argv[1],ACCESS_READ)))
      { printf("Couldn't find file named: %s\n",argv[1]); exit(0L); }

   file = Open(argv[1],MODE_OLDFILE);
   if(FBlock = AllocMem(sizeof(*FBlock), MEMF_CHIP))
      {
      if(Examine(FLock,FBlock)) /* fill FBlock */
         {
         size = (int)(FBlock->fib_Size & 127); /* size mod 128 */
         if(size) /* needs padding */
            {
            Read(file,&test,4L);
            if(test == 0x000003f3) /* first longword identifies tools */
               {
               if(Seek(file,0L,OFFSET_END) != -1) /* go to end of file */
                  {
                  for(i=0;i < ((128 - size) >> 2);i++)
                     Write(file,pad,4L); /* assume we can write */
                  }                     /* if we get this far  */
               else
                  printf("Dos error! (Seek())\n");
               }
            else
               printf("%s is not a tool.\n",argv[1]);
            }
         else
            printf("File length is already divisible by 128.\n");
         }
      else
         printf("Dos Error! (Examine())\n");
      FreeMem(FBlock,sizeof(*FBlock));
      }
   else
      printf("Not enough memory!\n");

   Close(file);
   UnLock(FLock);
}
