Newer
Older
ubix / src / bin / ls / main.c
@reddawg reddawg on 15 Sep 2002 4 KB Cleaning Lots Of It
/**************************************************************************************
 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$

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

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