#include "libc.c"

#define DATA_SIZE 3000
#define FILE_NAME_SIZE 100

extern void readfile();

short
strcmp(arg1,arg2)
char * arg1;
char * arg2;
{
   short ret,i,ch1,ch2;
   ret = 0;
   for(i = 0; arg1[i] && arg2[i]; i++) {
      ch1 = arg1[i];
      ch2 = arg2[i];
      if(ch1 == ch2) {

      }
      else if(ch1 < ch2) {
         ret = -1;
         break;
      }
      else if(ch1 > ch2) {
         ret = 1;
         break;
      }
   }
   return ret;
}

void
bzero(ptr,size)
char * ptr;
short size;
{
   short i;
   for(i = 0; i < size; i++) {
      *ptr = '\0';
      ptr++;
   }
}


void
gets(ptr)
char * ptr;
{
   short ch;
   short i;

   i = 0;
   ch = getch();
   while (ch != '\r' && ch != '\n') {
      if (ch == '\b' && i > 0) {
         putc(ch);
         ch = getch();
         i--;
         ptr[i] = '\0';
         continue;
      }
      else if(ch == '\b') {
         ch = getch();
         continue;
      }
      putc(ch);
      ptr[i] = ch;
      i++;
      ch = getch();
   }
   ptr[i] = '\0';
}

short
findsector2(cmd)
char * cmd;
{
   short i,j,ch;
   char buffer[1024];
   char name[100];
   char sector[100];

   bzero(buffer, 1024);
   bzero(name, 100);
   bzero(sector, 100);

   for(i = 0; get_fat(i) != '}'; i++) {
      ch = get_fat(i);
      buffer[i] = ch;
   }

   buffer[i] = '}';

   j = 0;
   for(i = 1; buffer[i] != '}'; i++) {
      if(buffer[i] == '-') {
         name[j] = '\0';
         if(strcmp(name, cmd) == 0) {
            i++;
            j = 0;
            while(buffer[i] != ',') {
               sector[j] = buffer[i];
               i++;
               j++;
            }
            sector[i] = '\0';
            break;
         }
         else {
            j = 0;
            i++;
            while(buffer[i] != ',') {
               sector[j] = buffer[i];
               i++;
               j++;
            }
            j = 0;
            i++;
            continue;
         }
      }
      if(buffer[i] != '-') {
         name[j] = buffer[i];
         j++;
      }
   }

   return atoi(sector);
}



short
main()
{
   char data[DATA_SIZE];
   char file_name[FILE_NAME_SIZE];
   short sector;

   printf("PLEASE ENTER FILE NAME: ");

   bzero(file_name, FILE_NAME_SIZE);

   bzero(data, DATA_SIZE);

   gets(file_name);

   printf("\r\n");

   sector = findsector2(file_name);

   readfile(sector, data);

   printf("%s\r\n", data);

   return 0;
}
