/*------------------------------------------------------------------------ * lflRead -- read from a previously opened local file *------------------------------------------------------------------------ */ devcall lflRead ( struct dentry *devptr, /* entry in device switch table */ char *buff, /* buffer to hold bytes */ int32 count /* max bytes to read */ ) { uint32 numread; /* number of bytes read */ // int32 nxtbyte; /* character or SYSERR/EOF */ kprintf("In lflRead\r\n"); if (count < 0) { kprintf("Exiting lflRead: count < 0\r\n"); return SYSERR; } /*for (numread=0 ; numread < count ; numread++) { nxtbyte = lflGetc(devptr); if (nxtbyte == SYSERR) { return SYSERR; } else if (nxtbyte == EOF) { if (numread == 0) { return EOF; } else { return numread; } } else { *buff++ = (char) (0xff & nxtbyte); } }*/ struct lflcblk *lfptr = &lfltab[devptr->dvminor]; if(lfptr->lfstate != LF_USED) { signal(lfptr->lfmutex); kprintf("Exiting lflRead: lfstate != LF_USED\r\n"); return SYSERR; } if(lfptr->lfpos >= lfptr->size) { signal(lfptr->lfmutex); kprintf("Exiting lflRead\r\n"); return EOF; } numread = 0; do { if(lfptr->lfbyte >= &lfptr->lfdblock[LF_BLKSIZ]) { lfsetup(lfptr); } *buff++ = (char)(0XFF & *lfptr->lfbyte++); lfptr->lfpos++; }while(++numread < count && lfptr->lfpos < lfptr->size); kprintf("Exiting lflRead\r\n"); return numread; }
/*------------------------------------------------------------------------ * lflPutc - write a single byte to an open local file *------------------------------------------------------------------------ */ devcall lflPutc ( struct dentry *devptr, /* entry in device switch table */ char ch /* character (byte) to write */ ) { struct lflcblk *lfptr; /* ptr to open file table entry */ //struct ldentry *ldptr; /* ptr to file's entry in the */ /* in-memory directory */ /* Obtain exclusive use of the file */ lfptr = &lfltab[devptr->dvminor]; wait(lfptr->lfmutex); /* If file is not open, return an error */ if (lfptr->lfstate != LF_USED) { signal(lfptr->lfmutex); return SYSERR; } /* Return SYSERR for an attempt to skip bytes beyond the */ /* current end of the file */ //ldptr = lfptr->lfdirptr; if (lfptr->lfpos > lfptr->fileSize) { signal(lfptr->lfmutex); return SYSERR; } /* If pointer is outside current block, set up new block */ if (lfptr->lfbyte >= &lfptr->lfdblock[LF_BLKSIZ]) { /* Set up block for current file position */ lfsetup(lfptr); } /* If appending a byte to the file, increment the file size. */ /* Note: comparison might be equal, but should not be greater. */ if (lfptr->lfpos >= lfptr->fileSize ) { lfptr->fileSize++; } /* Place byte in buffer and mark buffer "dirty" */ *lfptr->lfbyte++ = ch; lfptr->lfpos++; lfptr->lfdbdirty = TRUE; if(DEBUG_1) { // kprintf("lflPutc Writing Character %c\r\n",ch); // kprintf("lflPutc Writing Character %d\r\n",ch); } signal(lfptr->lfmutex); return OK; }
/*------------------------------------------------------------------------ * lflRead -- read from a previously opened local file *------------------------------------------------------------------------ */ devcall lflRead ( struct dentry *devptr, /* entry in device switch table */ char *buff, /* buffer to hold bytes */ int32 count /* max bytes to read */ ) { uint32 numread; /* number of bytes read */ int32 nxtbyte; /* character or SYSERR/EOF */ struct lflcblk *lfptr; /* ptr to open file table entry */ struct ldentry *ldptr; /* ptr to file's entry in the */ /* in-memory directory */ //kprintf("Read begins!\r\n"); if (count < 0) { signal(lfptr->lfmutex); return SYSERR; } for (numread=0 ; numread < count ; numread++) { lfptr = &lfltab[devptr->dvminor]; wait(lfptr->lfmutex); if (lfptr->lfstate != LF_USED) { signal(lfptr->lfmutex); nxtbyte = SYSERR; } ldptr = lfptr->lfdirptr; if (lfptr->lfpos >= ldptr->ld_size) { signal(lfptr->lfmutex); nxtbyte = EOF; } if (lfptr->lfbyte >= &lfptr->lfdblock[LF_BLKSIZ]) { lfsetup(lfptr); } nxtbyte = 0xff & *lfptr->lfbyte++; lfptr->lfpos++; signal(lfptr->lfmutex); if (nxtbyte == SYSERR) { //kprintf("nxtbyte == SYSERR\r\n"); return SYSERR; } else if (nxtbyte == EOF) { /* EOF before finished */ if (numread == 0) { //kprintf("nxtbyte = EOF and numread = 0\r\n"); return EOF; } else { //kprintf("nxtbyte = EOF and numread = %d\r\n", numread); return numread; } } else { *buff++ = (char) (0xff & nxtbyte); } } //kprintf("Read returns %d\r\n", numread); return numread; }
/*------------------------------------------------------------------------ * lflGetc -- Read the next byte from an open local file *------------------------------------------------------------------------ */ devcall lflGetc ( struct dentry *devptr /* entry in device switch table */ ) { struct lflcblk *lfptr; /* ptr to open file table entry */ struct ldentry *ldptr; /* ptr to file's entry in the */ /* in-memory directory */ int32 onebyte; /* next data byte in the file */ /* Obtain exclusive use of the file */ lfptr = &lfltab[devptr->dvminor]; wait(lfptr->lfmutex); /* If file is not open, return an error */ if (lfptr->lfstate != LF_USED) { signal(lfptr->lfmutex); return SYSERR; } /* Return EOF for any attempt to read beyond the end-of-file */ ldptr = lfptr->lfdirptr; if (lfptr->lfpos >= ldptr->ld_size) { signal(lfptr->lfmutex); return EOF; } /* If byte pointer is beyond the current data block, */ /* set up a new data block */ if (lfptr->lfbyte >= &lfptr->lfdblock[LF_BLKSIZ]) { lfsetup(lfptr); } /* Extract the next byte from block, update file position, and */ /* return the byte to the caller */ onebyte = 0xff & *lfptr->lfbyte++; lfptr->lfpos++; signal(lfptr->lfmutex); return onebyte; }
//------------------------------------------------------------------------ // lfseek -- seek to a specified position of a file //------------------------------------------------------------------------ int lfseek(struct devsw *devptr, long offset) { struct flblk *flptr; int ps; ps = disable(); flptr = (struct flblk *)devptr->iobuf; if (flptr->fl_mode & FLWRITE) { if (flptr->fl_dch) lfsflush(flptr); } else if (offset > (flptr->fl_dent)->fdlen) { restore(ps); return SYSERR; } flptr->fl_pos = offset; lfsetup(flptr->fl_dev, flptr); restore(ps); return OK; }