/* Program: GetFileInfo <device> [TYPE|NAME|PRO|SIZE|BLOCKS|COMMENT]
   Desc: Program that gets info about a file and returns the
   result in a global env. variable: File.
   Author: P Hutchison 3/12/93
   Modifications:
   1. Use proto headers (25/6/95)
   2. Use PutStr() and improve prototyping (28/9/95)
*/

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

struct FileInfoBlock fileinfo =
{0, 0, (char)NULL, 0, 0, 0, 0, NULL, (char)NULL, (char)NULL };

char buf[108];

void FetchInfo(char *opt);

main(int argc, char *argv[])
{
   struct DOSBase *DOSBase;
   long __oslibversion = 37;
   char name[5];
   BPTR lock;
   
   strcpy(name, "File");
   buf[0] = (char)NULL;
   
   if (DOSBase = (struct DOSBase *)OpenLibrary("dos.library", __oslibversion))
   {      
    if (argc == 3) /* NB: Command name counts as 1 also! */
    {
      if (lock = Lock(argv[1], ACCESS_READ))
      {
         if (Examine(lock, &fileinfo))
         {
          FetchInfo(argv[2]);
          
          SetVar(name, buf, -1, GVF_GLOBAL_ONLY); /* Set env. variable */
         } 
         UnLock(lock); 
      }
    } else
      PutStr("GetFileInfo <file> [TYPE|NAME|PRO|SIZE|BLOCKS|COMMENT]\n");
    CloseLibrary((struct Library *)DOSBase);
   } 
}
      
void FetchInfo(char *opt)
{
   long  result;
   char  *prot_ptr;

   if (stricmp(opt, "TYPE") == 0)
   {
      result = fileinfo.fib_DirEntryType;
      if (result < 0) strcpy(buf, "FILE");
      if (result > 0) strcpy(buf, "DIRECTORY");
   }
   if (stricmp(opt, "NAME") == 0)
      strcpy(buf, fileinfo.fib_FileName);
            
   if (stricmp(opt, "PRO") == 0)     /* Get Information */
   {  
      prot_ptr = &buf[0];
      result = fileinfo.fib_Protection;
      if (result & FIBF_SCRIPT) *prot_ptr++ = 'S';    
      if (result & FIBF_PURE) *prot_ptr++ = 'P';    
      if (result & FIBF_ARCHIVE) *prot_ptr++ = 'A';    
      if (result & FIBF_READ) *prot_ptr++ = 'R';    
      if (result & FIBF_WRITE) *prot_ptr++ = 'W';    
      if (result & FIBF_EXECUTE) *prot_ptr++ = 'E';    
      if (result & FIBF_DELETE) *prot_ptr++ = 'D';    
      *prot_ptr = (char)NULL;
   }
   if (stricmp(opt, "SIZE") == 0) 
   {
      result = fileinfo.fib_Size;
      stcl_d(buf, result);
   }
   if (stricmp(opt, "BLOCKS") == 0)
   {
      result = fileinfo.fib_NumBlocks;
      stcl_d(buf, result);
   }
   if (stricmp(opt, "COMMENT") == 0)
   {
      strcpy(buf, fileinfo.fib_Comment);
    }
}
        
