
/************************************************************************/
/*  Kd.c     (K)ilobyte (d)irectory                        July 2, 1987 */
/*                                   Compiled with                      */
/* cc Kd.c +L                        Manx Aztec C                       */
/* ln Kd.o -lm32 -lc32               version 3.4  patch 3               */
/*                                                                      */
/* Usage: Kd [path]                                                     */
/*   Will return a directory, five filenames per line, sorted by size,  */
/* (biggest at top-left), with filesizes to the nearest tenth of a      */
/* kilobyte. In addition, Kd counts total bytes blocks files and dirs.  */
/*                                                                      */
/*     Kd is copyright 1987 Richard Lee Stockton and released to the    */
/* public domain. Copy it, use it, pass it around... No Charge!         */
/*                                                                      */
/*     HOWEVER! You May Not Sell Kd, In Whole Or In Part, Without       */
/* The Proverbial Written Permission from me, Richard Lee Stockton.     */
/*      NO quarantee of any sort is herein expressed or implied.        */
/*  I will not be responsible for warts or other problems caused by Kd  */
/*                                                                      */
/* Thanks: Andry Rachmat, Tom Smythe, John Thomas Berry,                */
/*         and especially Leo Schwab, for the proper attitude.          */
/*                                                                      */
/*                                       Comments and Contributions to: */
/*                                                                      */
/*                                                 Richard Lee Stockton */
/*  "Fast & Compact"    Under Consideration:       21305 60th Ave West  */
/*                        Edible Software,         Mountlake Terrace,   */
/*    "Yum, yum!"     a concept for the 1990's     Washington  98043    */
/*                       while(Parse(DNA))         (206)  776-1253      */
/*                                              PLINKorGENIE: RLStockton*/
/************************************************************************/

#include <exec/types.h>
#include "exec/memory.h"
#include "libraries/dos.h"
#include "libraries/dosextens.h"

#define USAGE "\n\
   Kd  Copyright 1987\n\
  Richard Lee Stockton\n\
  21305 60th Ave. West\n\
  MtLkTerr, Wash 98043\n\
   (206)  776-1253\n\n\
Kd [path] returns k-byte dir.\n\n"

extern long Output(), branch();
struct unit                                       /* create a structure */
   {
   char u_title[108];
   long u_bytes;
   };

char OutLine[108] = "";
char Temp[60] = "";
struct unit sortfile[200];
int sortindx[200];
int indx, count, tnames=0, tblocks=0, tfiles=0, comma=1;
long tbytes = 0L;

main(argc,argv)
   int argc;
   char *argv[];
{
   struct FileLock *lock,*Lock();
   struct FileInfoBlock *f_info;
   
   if(!(strcmp(argv[1],"?")))
   {
       Write(Output(),USAGE,(long)strlen(USAGE));
       exit();
   }
   
   if((f_info=(struct FileInfoBlock *)
       AllocMem(sizeof(struct FileInfoBlock),MEMF_CHIP|MEMF_CLEAR))==0)
           goto bombout;
    
   if((lock=Lock(argv[1],ACCESS_READ))==0)
       goto bombout;
       
   if((Examine(lock,f_info))==0)
       goto bombout;
   
   Write(Output(),"\033[41m\033[32m ",12L);           /* DIR name stuff */
   if(*(f_info->fib_FileName))
       Write(Output(),f_info->fib_FileName,(long)strlen(f_info->fib_FileName));
   else
       Write(Output(),"Ram Disk",9L);
   Write(Output()," \033[m = \033[30m\033[41m",18L);
                           
   if(f_info->fib_Size>0)                  /* Takes care of single file */
   {
       Write(Output(),itos(f_info->fib_Size),
           (long)strlen(itos(f_info->fib_Size)));
       Write(Output(),"\033[32mk\033[m",11L);
       goto bombout;
   }
                                         /* fill sortfile & count stuff */
   while ((ExNext(lock,f_info))!=0)
   {
       strcpy(sortfile[tnames].u_title,f_info->fib_FileName);
       sortfile[tnames].u_bytes = f_info->fib_Size;
       if(f_info->fib_Size) tfiles++;
       tbytes += f_info->fib_Size;
       tblocks += f_info->fib_NumBlocks;
                                                     /* adjust sortindx */
       indx = tnames-1; 
       while((f_info->fib_Size>sortfile[sortindx[indx]].u_bytes)&&indx>-1)
       {
          sortindx[indx+1] = sortindx[indx];
          indx--;
       }
       sortindx[indx+1] = tnames;
       tnames++;
   }
   if(IoErr()==ERROR_NO_MORE_ENTRIES)
       Write(Output(),"\033m",3L);
   else
   {
       Write(Output(),"\nDirectory Read Error!",23L);
       goto bombout;
   }                                           /* send counts to screen */
   strcpy(OutLine,itos(tbytes));
   strcat(OutLine," bytes \033[m-\033[41m\033[30m");
   strcat(OutLine,itos(tblocks));
   strcat(OutLine," blocks \033[m-\033[41m\033[30m");
   Write(Output(),OutLine,(long)strlen(OutLine));
   strcpy(OutLine,"");
   strcat(OutLine,itos(tfiles));
   strcat(OutLine," files \033[m + \033[41m\033[30m");
   strcat(OutLine,itos(tnames-tfiles));
   strcat(OutLine," dirs \033[m");
   Write(Output(),OutLine,(long)strlen(OutLine));
   comma = 0;                         /* flag to turn off comma in itos */
   
                        /* format filenames + Kbytes and send to screen */

   for(indx=0; indx<(tnames); indx++, count++)
   {
       if(count>4)
           count = 0;
       if(count == 0)
           Write(Output(),"\n\033[32m k\033[m",12L);
       strcpy(OutLine,"");
       strcpy(Temp,"");

       if(sortfile[sortindx[indx]].u_bytes) 
       {
           strcat(Temp,sortfile[sortindx[indx]].u_title);
           strcat(Temp,"              ");
           strncat(OutLine,Temp,14L - (long)
               strlen(itok(sortfile[sortindx[indx]].u_bytes)));
           strcat(OutLine,itok(sortfile[sortindx[indx]].u_bytes));
           strcat(OutLine,"\033[32mk\033[m");
       }    
       else
       {
           strcat(OutLine,"\033[33m");
           strcat(Temp,sortfile[sortindx[indx]].u_title);
           strcat(Temp,"..............");
           strncat(OutLine,Temp,14);
           strcat(OutLine,"\033[32mk\033[m");
       }
       Write(Output(),OutLine,(long)strlen(OutLine));
   }
   
 bombout:
   Write(Output(),"\033[m\n",5L);
   UnLock(lock);
   FreeMem(f_info,sizeof(struct FileInfoBlock));
}


                                   /* long int to char string w/decimal */
itok(ord)
   long   ord;
{
   static char  kstring[12];
   int    next = 3;

   ord = (ord + 50)/10;
   
   if (ord > 9)
   {
   strcpy(kstring,itos(ord));
   kstring[(long)(strlen(kstring))-1L] = kstring[(long)(strlen(kstring))-2L];
   kstring[(long)(strlen(kstring))-2L] = '.';
   }
       else
           strcpy(kstring,".0");
   return(kstring);
}

                                    /* long int to char string w/commas */
itos(ord)
   long   ord;
{
   static char ostring[12];
   int         next = 11;
   int         shift = 0;
   
   if (!ord) ostring[next--] = '0';
   while ((ord+9)/10)
   {
      if ( ((next==8) || (next==4)) && (comma) )
           ostring[next--] = ',';
      ostring[next--] = ord % 10 + '0';
      ord /= 10;
   }
   ostring[next] = ' ';
   while(next<12)
       ostring[shift++] = ostring[next++];
   while(shift<12)
       ostring[shift++] = '\0';
   return(ostring);
}

/************************** end of Kd.c ********************************/