/**************************************************************************************
 Copyright (c) 2002 The UbixOS Project
 All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions, the following disclaimer and the list of authors.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions, the following disclaimer and the list of authors
in the documentation and/or other materials provided with the distribution. Neither the name of the UbixOS Project nor the names of its
contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 $Id: main.c,v 1.4 2003/04/24 05:16:28 reddawg Exp $

**************************************************************************************/

#include <stdio.h>
#include <stdlib.h>

#define permRead    0x8
#define permWrite   0x4
#define permExecute 0x2
#define permHidden  0x1


//UbixFS Directory Entry
struct directoryEntry {
  uLong  startCluster;   //Starting Cluster Of File
  uLong  size;           //Size Of File
  uLong  creationDate;  //Date Created
  uLong  lastModified;  //Date Last Modified
  uLong  uid;           //UID Of Owner
  uLong  gid;           //GID Of Owner
  uShort attributes;    //Files Attributes
  uShort permissions;   //Files Permissions
  char   fileName[256]; //File Name
  };

int main(int argc,char **argv) {
  int i = 0x0,x = 0x0,tmpPerms = 0x0;
  char *pwd = (char *)malloc(256);
  char *permsData = (char *)malloc(13);
  FILE *fd;
  struct directoryEntry *dirEntry = 0x0;
  if (argv[1] == 0x0) {
    if ((fd = fopen(":","rb")) == 0x0) {
      printf("Error Reading Directory\n");
      exit(1);
      }
    }
  else {
    if ((fd = fopen(argv[1],"rb")) == 0x0) {
      printf("Error Reading Directory\n");
      exit(1);
      }    
    }
  dirEntry = (struct directoryEntry *)malloc(fd->size);
  fread(dirEntry,fd->size,1,fd);
  if (!fclose(fd)) {
    printf("Error Closing Directory\n");
    }
  pwd[0] = '/';
  for (i=0;i<(fd->size/sizeof(struct directoryEntry));i++) {
    if ((dirEntry[i].fileName[0] > 0) && (dirEntry[i].fileName[0] != ':')) {
      for (x=0;x<12;x++) {
        permsData[x] = '-';
        }
      if (dirEntry[i].attributes == 0x800) {
        permsData[0] = 'F';
        }
      if (dirEntry[i].attributes == 0x8000) {
        permsData[0] = 'D';
        }
      tmpPerms = ((dirEntry[i].permissions & 0xF00) >> 8);
      if ((tmpPerms & permRead) == permRead) permsData[1] = 'R';
      if ((tmpPerms & permWrite) == permWrite) permsData[2] = 'W';
      if ((tmpPerms & permExecute) == permExecute) permsData[3] = 'E';
      if ((tmpPerms & permHidden) == permHidden) permsData[4] = 'H';
      tmpPerms = ((dirEntry[i].permissions & 0x0F0) >> 4);
      if ((tmpPerms & permRead) == permRead) permsData[5] = 'R';
      if ((tmpPerms & permWrite) == permWrite) permsData[6] = 'W';
      if ((tmpPerms & permExecute) == permExecute) permsData[7] = 'E';
      if ((tmpPerms & permHidden) == permHidden) permsData[8] = 'H';
      tmpPerms = ((dirEntry[i].permissions & 0x00F) >> 0);
      if ((tmpPerms & permRead) == permRead) permsData[9] = 'R';
      if ((tmpPerms & permWrite) == permWrite) permsData[10] = 'W';
      if ((tmpPerms & permExecute) == permExecute) permsData[11] = 'E';
      if ((tmpPerms & permHidden) == permHidden) permsData[12] = 'H';       
      printf("%s %i %i %i %s\n",permsData,(int)dirEntry[i].uid,(int)dirEntry[i].gid,(int)dirEntry[i].size,dirEntry[i].fileName);
      }
    }
  for (i=0;i<argc;i++) {
    printf("[%s]\n",argv[i]);
    }
  return(0);
  }
