/*****************************************************************************************
 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 <vfs/file.h>
#include <ubixos/sched.h>
#include <ubixos/vitals.h>
#include <lib/kmalloc.h>
#include <lib/kprintf.h>
#include <lib/string.h>
#include <sys/video.h>

fileDescriptor *fdTable = 0x0;

/* These Are Temporary */
int sprintf(char *buf,const char *fmt, ...);
char getch();



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

Function: int feof(fileDescriptor *fd)
Description: Check A File Descriptor For EOF And Return Result
Notes:

************************************************************************/  
int feof(fileDescriptor *fd) {
  if (fd->status == fdEof) {
    return(-1);
    }
  return(0);
  }

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

Function: int fclose(fileDescriptor *fd);
Description: This Will Close And Free A File Descriptor
Notes:

************************************************************************/
int fclose(fileDescriptor *fd) {
  fileDescriptor *tmpFd = 0x0;
  /* Search For File Descriptor */
  for (tmpFd=fdTable;tmpFd;tmpFd=tmpFd->next) {
    if (tmpFd == fd) {
      /* If Fd Is The First FD Then Reset fdTable */
      if (tmpFd == fdTable) {
        fdTable = tmpFd->next;
        if (fdTable != 0x0) {
          fdTable->prev = 0x0;
          }
        kfree(fd->buffer);
        kfree(fd);
        systemVitals->openFiles--;
        return(1);
        }
      else {
        tmpFd->prev->next = tmpFd->next;
        tmpFd->next->prev = tmpFd->prev;
        kfree(fd->buffer);
        kfree(fd);
        systemVitals->openFiles--;
        return(1);
        }
      }    
    }
  /* Return NULL If Descriptor Was Not Found */
  return(0x0);
  }

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

Function: int fputc(int ch,fileDescriptor *fd)
Description: This Will Write Character To FD
Notes:

************************************************************************/
int fputc(int ch,fileDescriptor *fd) {
  fileDescriptor *tmpFd = 0x0;
  /* Search For File Descriptor */
  for (tmpFd=fdTable;tmpFd;tmpFd=tmpFd->next) {
    /* If Found Return Next Char */
    if (tmpFd == fd) {
      ch = tmpFd->mp->fs->vfsWrite(tmpFd,(char *)ch,tmpFd->offset,1);
      tmpFd->offset++;
      return(ch);
      }
    }
  /* Return NULL If FD Is Not Found */
  return(0x0);
  }
  
/************************************************************************

Function: int fgetc(fileDescriptor *fd)
Description: This Will Return The Next Character In A FD Stream
Notes:

************************************************************************/  
int fgetc(fileDescriptor *fd) {
  int ch = 0x0;
  fileDescriptor *tmpFd = 0x0;
  /* Search For File Descriptor */
  for (tmpFd=fdTable;tmpFd;tmpFd=tmpFd->next) {
    /* If Found Return Next Char */
    if (tmpFd == fd) {
      tmpFd->mp->fs->vfsRead(tmpFd,(char *)&ch,tmpFd->offset,1);
      tmpFd->offset++;
      return(ch);
      }
    }
  /* Return NULL If FD Is Not Found */
  return(0x0);
  }

size_t fread(void *ptr,int size,int nmemb,fileDescriptor *fd) {
  fileDescriptor *tmpFd = 0x0;
  /* Search For File Descriptor */
  for (tmpFd=fdTable;tmpFd;tmpFd=tmpFd->next) {
    /* If Found Return Next Char */
    if (tmpFd == fd) {
      if (nmemb == 0x0) nmemb = 1; //Temp Fix
      tmpFd->mp->fs->vfsRead(tmpFd,ptr,tmpFd->offset,size * nmemb);
      tmpFd->offset += size * nmemb;
      }
    }
  return(0x0);
  }

size_t fwrite(void *ptr,int size,int nmemb,fileDescriptor *fd) {
  fileDescriptor *tmpFd = 0x0;
  /* Search For File Descriptor */
  for (tmpFd=fdTable;tmpFd;tmpFd=tmpFd->next) {
    /* If Found Return Next Char */
    if (tmpFd == fd) {
      tmpFd->mp->fs->vfsWrite(tmpFd,ptr,tmpFd->offset,size * nmemb);
      tmpFd->offset += size * nmemb;
      }
    }
  return(0x0);
  }

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

Function: void sysFopen();
Description: Opens A File Descriptor For A User Task
Notes:

************************************************************************/
void sysFopen(char *file,char *flags,userFileDescriptor *userFd) {
  userFd->fd = fopen(file,flags);
  if (userFd->fd != 0x0) {
    userFd->fdSize = userFd->fd->size;
    }
  /* Return */
  return;
  }

#if 0
size_t fread(void *ptr, int size, int nmemb,fileDescriptor *fd) {
  
  /* YO! what about EOF's in the middle of reading?? */

  int i = 0x0;
  char *data = (char *)ptr;
    for (i=0;i<size;i++) {
      data[i] = (char) fgetc(fd);
      }
  /* Return */
  return(size);
  }
#endif
/************************************************************************

Function: void sysFread();
Description: Reads SIZE Bytes From The userFd Into DATA
Notes:

************************************************************************/  
void sysFread(void *data,long size,userFileDescriptor *userFd) {
  fread(data,size,1,userFd->fd);
  /* Return */
  return;
  }

void sysFwrite(char *ptr,int size,userFileDescriptor *userFd) {
  if (userFd->fd == 0x0) {
      kprintf(ptr);
    }
  else {
    fwrite(ptr,size,1,userFd->fd);
    }
  return;
  }
  
/************************************************************************

Function: void sysFclse();
Description: Closes A File Descriptor For A User Task
Notes:

************************************************************************/
void sysFclose(userFileDescriptor *userFd,int *status) {
  *status = fclose(userFd->fd);
  /* Return */
  return;
  }

void sysFgetc(int *ptr,userFileDescriptor *userFd) {
  fileDescriptor *tmpFd = 0x0;
  asm("sti");
  tmpFd = userFd->fd;
  if (userFd->fd == 0) {
    ptr[0] = (int) getch();
    }
  else {
    ptr[0] = (int) fgetc(tmpFd);
    }
  }

void sysRmDir() {
  return;
  }

int fseek(fileDescriptor *tmpFd,long offset,int whence) {
  tmpFd->offset = offset+whence;
  return(tmpFd->offset);
  }

void sysFseek(userFileDescriptor *userFd,long offset,int whence) {
  kprintf("sysFseek: [0x%X][0x%X]\n",userFd,userFd->fd);
  userFd->fd->offset = offset+whence;
  }

void sysChDir(const char *path) {
  sprintf(_current->oInfo.cwd,path);
  }

void sysUnlink(const char *path,int *retVal) {
  *retVal = unlink(path);
  }

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

Function: void chDir(const char *path);
Description: Changes Current Working Dir
Notes:

************************************************************************/
void chDir(const char *path) {
  if (_current->oInfo.fileInfo.cwd != 0x0) {
    kfree(_current->oInfo.fileInfo.cwd);
    }
  _current->oInfo.fileInfo.cwd = verifyDir(path);
  return;
  }

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

Function: char *verifyDir(const char *path);
Description: Changes Current Working Dir
Notes:

************************************************************************/
char *verifyDir(const char *path) {
  return(0x0);
  }

/***
 $Log$

 END
 ***/

