/************************************************************************
 * env.c -- print all the environment                                  *
 *                                                                     *
 *             Copyright 1990 Peter Chubb                              *
 *               All rights reserved                                   *
 *                                                                     *
 ************************************************************************/

static char RCSID[] = "$Id: env.c,v 1.1 90/05/26 12:46:48 peterc Exp $";

/*
 *
 * $Log:       env.c,v $
 * Revision 1.1  90/05/26  12:46:48  peterc
 * Initial revision
 * 
 *
 */

# include <exec/types.h>
# include <proto/dos.h>
# include <proto/exec.h>
# include <string.h>

# define ENVDIR "ENV:"
# define BUFSIZ 512

void
_main()
{
    BPTR lck;
    BPTR Op;
    BPTR Ep;
    struct FileInfoBlock *FB = AllocMem(sizeof(struct FileInfoBlock), 0);
    long Status = 0;
    register char *p;
    register char *q;
    int                  len;
    struct DosLibrary *DOSBase;
    char *buf = AllocMem(BUFSIZ, 0);

    if (!(DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 0)))
       return;

    Op  = Output();
  
    lck = Lock(ENVDIR, SHARED_LOCK);
    if (lck)
    {
       Status = Examine(lck, FB);
       if (Status)
           while (ExNext(lck, FB))
           {
               if (FB->fib_EntryType >= 0)
                   continue;
               Write(Op, FB->fib_FileName, strlen(FB->fib_FileName));
               Write(Op, "=", 1);
               for (p = ENVDIR, q = buf; *p; *q++ = *p++)
                       ;
               for (p = FB->fib_FileName; *p; *q++ = *p++)
                       ;
               *q = '\0';

       if ((Ep = Open(buf, MODE_OLDFILE)) != (BPTR)0)
               {
                   if ((len = Read(Ep, buf, BUFSIZ)) > 0)
                   {
                       Write(Op, buf, len);
                   }
                   Close(Ep);
               }
               Write(Op, "\n", 1);
           }
       UnLock(lck);
    }

    CloseLibrary((struct Library *)DOSBase);
    FreeMem(buf, BUFSIZ);
    FreeMem(FB, sizeof (*FB));
}
