/************************************************************************************** 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 <ubixfs/file.h> #include <ubixfs/ubixfs.h> #include <ubixos/kmalloc.h> #include <ubixos/schedule.h> #include <ubixos/vitals.h> fileDescriptor *fdTable = 0x0, *lastFd = 0x0; //These Are Temporary int sprintf(char *buf,const char *fmt, ...); char getch(); /************************************************************************ Function: fileDescriptor *fopen(const char *file,cont char *flags) Description: This Will Open A File And Return A File Descriptor Notes: 08/05/02 - Just Started A Rewrite Of This Function Should Work Out Well ************************************************************************/ fileDescriptor *fopen(const char *file,const char *flags) { fileDescriptor *tmpFd = 0x0; //Allocate Memory For File Descriptor tmpFd = (fileDescriptor *)kmalloc(sizeof(fileDescriptor)); //Search For The File if (findFile((char *)file,tmpFd) == 1) { //If The File Is Found Then Set Up The Descriptor tmpFd->buffer = (uChar *)0x1000;//(uChar *)kmalloc(sizeof(uChar)*512); sprintf(tmpFd->fileName,"%s",file); //This Will Set Up The Descriptor Modes while (flags[0]) { switch(flags[0]) { case 'w': case 'W': tmpFd->mode = 1; break; case 'r': case 'R': tmpFd->mode = 0; break; default: break; } flags++; } //Set Its Status To Open tmpFd->status = fdOpen; //Initial File Offset Is Zero tmpFd->offset = 0x0; //Pointer To Next Descriptor Is NULL tmpFd->next = 0x0; //If This Is The First File Descriptor Then Set It Up To Be Starting Fd if (fdTable == 0x0) { fdTable = tmpFd; lastFd = tmpFd; } else { //If There Is Already A Starting FD Set This Up As The Last One lastFd->next = tmpFd; lastFd = tmpFd; } systemVitals->openFiles++; //Return The FD return(lastFd); } else { //If The File Was Not Found Free The Memory kfree(tmpFd); } //Return NULL return(0x0); } /************************************************************************ 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,*prevFd; prevFd = fdTable; //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; // kfree(fd->buffer); kfree(fd); systemVitals->openFiles--; return(1); } //If FD Is The lastFd Then Reset lastFd else if (tmpFd == lastFd) { prevFd->next = 0x0; lastFd = prevFd; // kfree(fd->buffer); kfree(fd); systemVitals->openFiles--; return(1); } //This One Is Easy We Just Free The Fd And Adjust Pointers else { prevFd->next = tmpFd->next; kfree(fd); systemVitals->openFiles--; return(1); } } //Set Previous File Descriptor Pointer prevFd = tmpFd; } //Return NULL If Descriptor Was 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) { ch = getFileByte(tmpFd,tmpFd->offset); //kprintf("[%i]",ch); tmpFd->offset++; return(ch); } } //Return NULL If FD Is Not Found return(0x0); } /************************************************************************ Function: void sysFopen(); Description: Opens A File Descriptor For A User Task Notes: ************************************************************************/ void sysFopen() { char *file = 0x0,*flags = 0x0; userFileDescriptor *userFd = 0x0; asm("":"=b" (file),"=c" (flags),"=d" (userFd)); asm("sti"); userFd->fd = fopen(file,flags); //Return return; } /************************************************************************ Function: void sysFread(); Description: Reads SIZE Bytes From The userFd Into DATA Notes: ************************************************************************/ void sysFread() { int i = 0x0; char *data = 0x0; uLong *size = 0x0; userFileDescriptor *userFd = 0x0; fileDescriptor *tmpFd = 0x0; asm("":"=b" (data),"=c" (size),"=d" (userFd)); asm("sti"); tmpFd = userFd->fd; for (i=0;i<*size;i++) { data[i] = fgetc(tmpFd); } //Return return; } /************************************************************************ Function: void sysFclse(); Description: Closes A File Descriptor For A User Task Notes: ************************************************************************/ void sysFclose() { int *status = 0x0; userFileDescriptor *userFd = 0x0; asm("":"=b" (userFd),"=c" (status)); *status = fclose(userFd->fd); //Return return; } void sysFgetc() { int *ptr = 0x0; userFileDescriptor *userFd = 0x0; fileDescriptor *tmpFd = 0x0; asm("": "=b" (ptr),"=c" (userFd)); asm("sti"); tmpFd = userFd->fd; if (userFd->fd == 0) { ptr[0] = getch(); } else { ptr[0] = fgetc(tmpFd); } }