Newer
Older
ubixos / src / sys / kernel / file.c
/*****************************************************************************************
 Copyright (c) 2002-2004 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 <ubixos/kpanic.h>
#include <ubixos/tty.h>
#include <lib/kmalloc.h>
#include <lib/kprintf.h>
#include <lib/string.h>
#include <sys/video.h>
#include <assert.h>

/* 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 fputc(int ch,fileDescriptor *fd)
Description: This Will Write Character To FD
Notes:

************************************************************************/
int fputc(int ch,fileDescriptor *fd) {
  if (fd != 0x0) {
    ch = fd->mp->fs->vfsWrite(fd,(char *)ch,fd->offset,1);
    fd->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;
  /* If Found Return Next Char */
  if (fd != 0x0) {
    fd->mp->fs->vfsRead(fd,(char *)&ch,fd->offset,1);
    fd->offset++;
    return(ch);
    }

  /* Return NULL If FD Is Not Found */
  return(0x0);
  }

size_t fread(void *ptr,int size,int nmemb,fileDescriptor *fd) {
 
  if (fd == 0x0)
    return(0x0);
    
 if (nmemb == 0x0) nmemb = 1; //Temp Fix
 assert(fd);
 //kprintf("fd->fileName: %s:%i\n",fd->fileName,_current->id);
 assert(fd->mp);
 assert(fd->mp->fs);
 fd->mp->fs->vfsRead(fd,ptr,fd->offset,size * nmemb);
 fd->offset += size * nmemb;
 return(size * nmemb);
  }

size_t fwrite(void *ptr,int size,int nmemb,fileDescriptor *fd) {
  if (fd != 0x0) {
    fd->mp->fs->vfsWrite(fd,ptr,fd->offset,size * nmemb);
    fd->offset += size * nmemb;
    }
  return(0x0);
  }

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

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

************************************************************************/
void sysFopen(const char *file,char *flags,userFileDescriptor *userFd) {
  if (userFd == 0x0)
    kpanic("Error: userFd == NULL, File: %s, Line: %i\n",__FILE__,__LINE__);
    
  userFd->fd = fopen(file,flags);
  if (userFd->fd != 0x0) {
    userFd->fdSize = userFd->fd->size;
    }
  /* Return */
  return;
  }
  
/************************************************************************

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

************************************************************************/  
void sysFread(void *data,long size,userFileDescriptor *userFd) {
  /* TODO : coredump? */
  if (userFd == NULL)
  	return;
  if (userFd->fd == NULL)
  	return;
	
  fread(data,size,1,userFd->fd);
  /* Return */
  return;
  }

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

void sysFgetc(int *ptr,userFileDescriptor *userFd) {
  fileDescriptor *tmpFd = 0x0;
  tmpFd = userFd->fd;
  if (userFd->fd == 0x0) {
    while (1) {
      if (_current->term == tty_foreground) {
        if ((*ptr = getch()) != 0x0)
          return;
        sched_yield();
        }
      else {
        sched_yield();
        }
 /*
       else {
         kprintf("Waking Task: %i\n",tty_foreground->owner);
         sched_setStatus(tty_foreground->owner,READY);
         kprintf("Sleeping Task: %i\n",_current->id);
         sched_setStatus(_current->id,WAIT);
         sched_yield();
         }
*/
      }
    }
  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) {
  // TODO : coredump?
  if (userFd == NULL)
  	return;
  if (userFd->fd == NULL)
  	return;

  userFd->fd->offset = offset+whence;
  }

void sysChDir(const char *path) {
  if (strstr(path,":") == 0x0) {
    sprintf(_current->oInfo.cwd,"%s%s",_current->oInfo.cwd,path);
    }
  else {
    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$
 Revision 1.17  2004/09/11 12:11:11  reddawg
 Cleaning up the VFS more changes to follow...

 Revision 1.16  2004/09/08 23:19:58  reddawg
 hmm

 Revision 1.15  2004/08/09 12:58:05  reddawg
 let me know when you got the surce

 Revision 1.14  2004/08/09 05:40:31  reddawg
 tty: removed current and made a foreground

 Revision 1.13  2004/08/06 22:32:16  reddawg
 Ubix Works Again

 Revision 1.11  2004/08/04 08:17:57  reddawg
 tty: we have primative ttys try f1-f5 so it is easier to use and debug
      ubixos

 Revision 1.10  2004/07/28 17:07:25  reddawg
 MPI: moved the syscalls

 Revision 1.9  2004/07/28 15:05:43  reddawg
 Major:
   Pages now have strict security enforcement.
   Many null dereferences have been resolved.
   When apps loaded permissions set for pages rw and ro

 Revision 1.8  2004/07/28 00:17:05  reddawg
 Major:
   Disconnected page 0x0 from the system... Unfortunately this broke many things
   all of which have been fixed. This was good because nothing deferences NULL
   any more.

 Things affected:
   malloc,kmalloc,getfreepage,getfreevirtualpage,pagefault,fork,exec,ld,ld.so,exec,file

 Revision 1.7  2004/07/05 23:06:32  reddawg
 Fixens

 Revision 1.6  2004/06/29 00:18:49  reddawg
 Sub Dirs

 Revision 1.5  2004/06/26 01:24:44  reddawg
 Fixens

 Revision 1.4  2004/06/16 19:56:35  reddawg
 Moved fclose

 Revision 1.3  2004/06/16 16:31:58  reddawg
 ld.so: the dynamic linker works and has been tested

 Revision 1.2  2004/05/15 02:30:28  reddawg
 Lots of changes


 END
 ***/