/* Programm zum Ausgeben der Filelaenge eines Files */

/* Geschrieben auf einem Amiga von Andre Willms (01/1992) */


#include <exec/types.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <ctype.h>
#include <math.h>

void *OpenLibrary();

struct DosBase *DosBase;
struct FileLock *DupLock(),*Lock();


long listfiles();

main(arganz,argfeld)

int arganz;
char **argfeld;

{
int chpos;
char dirname[160];

  printf("\nAList V1.00 written 02/1992 by Andre Willms\n\n");

  if (!(arganz==2))
    {
      printf("Bad Arguments!\n\n");
      printf("Usage : AList <Path>\n\n");
    }
  else
    {
      libopen();

        chpos=strlen(argfeld[1])-1;
        if ((argfeld[1][chpos]==':')|(argfeld[1][chpos]=='/'))
          strcpy(&dirname,argfeld[1]);
        else
          sprintf(&dirname,"%s/",argfeld[1]);

        listfiles(&dirname);

      libclose();

    }
}

libopen()
{
if(!(DosBase= (struct DosBase *)
     OpenLibrary("dos.library",0L)))
  {
    printf("Keine Dos-Library");
    libclose();
    exit(FALSE);
  }

}

libclose()
{
if (DosBase) CloseLibrary(DosBase);
}




/* Funktion listet alle Files eines Directories  (incl. Sub-Dirs)*/

listfiles(name)

char *name;

{
BOOL ok;
struct FileLock *mylock;
struct FileInfoBlock *myfib;
UBYTE data[264];
char newname[160],dirname[31];
long laenge=0;

  myfib= (struct FileInfoBlock *) (((ULONG)(data) % 4) ? (data+2) : data);

  if(!(mylock=Lock(name,ACCESS_READ)))                /* Lock holen */
    {
      printf("ERROR while locking \"%s\". (Perhaps not existent.)\n\n",name);
      libclose();
      exit(FALSE);
    }
   else
    {
      if(!(ok=Examine(mylock,myfib)))                /* FIB holen */
        {
          printf("ERROR while getting FIB of \"%s\" !",name);
        }
      else
        {
          if(myfib->fib_DirEntryType<0)
            {
              printf("    %s\r\t\t\t\t%d \n",
                      myfib->fib_FileName,myfib->fib_Size);
              laenge+=myfib->fib_Size;
            }
          else
            {
              printf("\n%s\n",myfib->fib_FileName);
              strcpy(&dirname,myfib->fib_FileName);

              while ((ok=ExNext(mylock,myfib)))
                {
                  if (myfib->fib_DirEntryType<0)
                    {
                      printf("    %s\r\t\t\t\t%d \n",
                              myfib->fib_FileName,myfib->fib_Size);
                      laenge+=myfib->fib_Size;
                    }
                  else
                    {
                      sprintf(&newname,"%s%s/",name,myfib->fib_FileName);

                      laenge+=listfiles(&newname);    /* Rekursion */
                    }

                } /* while exnext */
  
              printf("-----------------------------------------------------------------------------\n");
              printf("%s\r\t\t\t\t%d Bytes (%.1f KBytes, %.1f MBytes) \n\n",
                      &dirname,laenge,(float)(laenge)/1024,
                      (float)(laenge)/1024/1024);
    
            } /* else examine */

        } /* else direntrytype */

      UnLock(mylock);
      
    } /* else lock */

  return(laenge);

} /* listfiles */



