//
//  LD.C
//
//  List Date - OS/2 directory lister, lists all 3 dates in an
//  HPFS volume.
//
//  This program requires the MS Presentation Manager toolkit, version
//  1.1 or later, to compile. It could easily be made to compile without
//  this toolkit simply by supplying your own prototypes for DosFindFirst()
//  and DosFindNext(). You would also need to recreate the _FILEFINDBUF
//  structure.
//
//  Chris Boaro   18-Jan-90   Initial Coding
//

#define  INCL_DOSERRORS
#include <os2.h>

#include <stdio.h>


char *addcomma(long, char *, int);


main(int argc, char *argv[])
{
   int rc, a;
   char     temp[80], temp2[30];
   long     dirsize;
   struct   _FILEFINDBUF findbuff;
   unsigned short handle = 0xffff;
   unsigned short count = 1;

   if( argc > 1 )
      strcpy(temp,argv[1]);
   else
      strcpy(temp,"*.*");

   count = 1; a=0; dirsize = 0L;
   handle = 0xffff;
   rc = DosFindFirst(temp,&handle,0,&findbuff,sizeof(findbuff),&count,0L);
   if( rc == ERROR_NO_MORE_FILES )
      printf("\n\rNo files found\n\r");
   else if( rc == 0 ) 
      printf("\n\rDirectory for %s\n\r\n\r",temp);
   while( rc == 0 ) {
      a++;      /* file count */
      dirsize += findbuff.cbFile;
      #define FDLW  findbuff.fdateLastWrite
      #define TMLW  findbuff.ftimeLastWrite
      #define FDCR  findbuff.fdateCreation
      #define TMCR  findbuff.ftimeCreation
      #define FDAC  findbuff.fdateLastAccess
      #define TMAC  findbuff.ftimeLastAccess
      if( a == 1 ) 
         printf("                          Created         Last Write      Last Access\n");
      addcomma(findbuff.cbFile,temp,10);
      printf("%-12s  %10s  %2d-%02d-%2d %2d:%02d  %2d-%02d-%2d %2d:%02d  %2d-%02d-%2d %2d:%02d\n\r",findbuff.achName,temp,
             FDCR.month,FDCR.day,FDCR.year+80,
             TMCR.hours,TMCR.minutes,
             FDLW.month,FDLW.day,FDLW.year+80,
             TMLW.hours,TMLW.minutes,
             FDAC.month,FDAC.day,FDAC.year+80,
             TMAC.hours,TMAC.minutes);
      rc = DosFindNext(handle,&findbuff,sizeof(findbuff),&count);
   }
   if( a ) {
      addcomma((long)a,temp,20);
      addcomma(dirsize,temp2,20);
      printf("\n\r   %s file%s found, using %s bytes of disk space\n\r",temp,a==1?"":"s",temp2);
   }
   else
      printf("\n\rNo matching files found\n\r");
   return(0);
}


//
//  addcomma()
//
//  Operates similar to ltoa() except that commas are inserted in the
//  correct places in the number. If the resultant string is greater
//  then 'maxlen', an 'E' appears in the last digit.
//
char *addcomma(long inb, char *outst, int maxlen)
{
   char tmp[20], tmp2[25], *p;
   int  numc, len, chsf=0, i;

   ltoa(inb,tmp,10);
   len = strlen(tmp);
   numc = (len - 1) / 3;                 // get number of commas
   if( (inb < 0) && ((len) % 3 == 1) )   // check for negative numbers
      numc--;
   p = &tmp2[24];
   *(p--) = '\0';
   while( len ) {                        // while there are chars to add
      if( (chsf == 3) && numc ) {        // if chars added is 3 and still need more commas
         *(p--) = ',';
         chsf = 0;
         numc--;
      }
      else {                             // otherwise, just add the char
         *(p--) = tmp[(len--)-1];
         chsf++;
      }
   }
   p++;
   len = strlen(p);                      // check the end string len
   if( len > maxlen ) {
      *(p+maxlen) = '\0';
      *(p+maxlen-1) = 'E';
   }
   strcpy(outst,p);                      // copy to the dest string
   return(outst);
}
