📄 tfsapi.c
字号:
/* tfsapi.c: * This file contains the portion of TFS that provides the function-level * API to the application. If this is not being used by the application * then it can be omitted from the monitor build. * Note that not all of the api-specific code is here; some of it is in * tfs.c. This is because the the MicroMonitor uses some of the api itself, * so it cannot be omitted from the TFS package without screwing up some * other monitor functionality that needs it. * * General notice: * This code is part of a boot-monitor package developed as a generic base * platform for embedded system designs. As such, it is likely to be * distributed to various projects beyond the control of the original * author. Please notify the author of any enhancements made or bugs found * so that all may benefit from the changes. In addition, notification back * to the author will allow the new user to pick up changes that may have * been made by other users after this version of the code was distributed. * * Note1: the majority of this code was edited with 4-space tabs. * Note2: as more and more contributions are accepted, the term "author" * is becoming a mis-representation of credit. * * Original author: Ed Sutter * Email: esutter@lucent.com * Phone: 908-582-2351 */#include "config.h"#include "cpu.h"#include "stddefs.h"#include "genlib.h"#include "tfs.h"#include "tfsprivate.h"#if INCLUDE_TFSAPI/* tfstruncate(): * To support the ability to truncate a file (make it smaller); this * function allows the user to adjust the high-water point of the currently * opened (and assumed to be opened for modification) file and replaces * that with the incoming argument. This replacement is only done if the * current high-water point is higher than the incoming length. * MONLIB NOTICE: this function is accessible through monlib.c. */inttfstruncate(int fd, long len){ struct tfsdat *tdat; /* Verify valid range of incoming file descriptor. */ if ((fd < 0) || (fd >= TFS_MAXOPEN)) return(TFSERR_BADFD); tdat = &tfsSlots[fd]; /* Make sure the file pointed to by the incoming descriptor is active * and that the incoming length is greater than the current high-water * point... */ if (tdat->offset == -1) return(TFSERR_BADFD); if (len > tdat->hwp) return(TFSERR_BADARG); /* Make the adjustment... */ tdat->hwp = len; return(TFS_OKAY);}/* tfseof(): * Return 1 if at the end of the file, else 0 if not at end; else negative * if error. * MONLIB NOTICE: this function is accessible through monlib.c. */inttfseof(int fd){ struct tfsdat *tdat; /* Verify valid range of incoming file descriptor. */ if ((fd < 0) || (fd >= TFS_MAXOPEN)) return(TFSERR_BADARG); tdat = &tfsSlots[fd]; /* Make sure the file pointed to by the incoming descriptor is active. */ if (tdat->offset == -1) return(TFSERR_BADFD); if (tdat->offset >= tdat->hdr.filsize) return(1); else return(0);}/* tfsread(): * Similar to a standard read call to a file. * MONLIB NOTICE: this function is accessible through monlib.c. */inttfsread(int fd, char *buf, int cnt){ struct tfsdat *tdat; uchar *from; if (tfsTrace > 1) printf("tfsread(%d,0x%lx,%d)\n",fd,(ulong)buf,cnt); /* Verify valid range of incoming file descriptor. */ if ((cnt < 1) || (fd < 0) || (fd >= TFS_MAXOPEN)) return(TFSERR_BADARG); tdat = &tfsSlots[fd]; /* Make sure the file pointed to by the incoming descriptor is active. */ if (tdat->offset == -1) return(TFSERR_BADFD); if (tdat->offset >= tdat->hdr.filsize) return(TFSERR_EOF); from = (uchar *) tdat->base + tdat->offset; /* If request size is within the range of the file and current * then copy the data to the requestors buffer, increment offset * and return the count. */ if ((tdat->offset + cnt) <= tdat->hdr.filsize) { if (tfsmemcpy(buf, from, cnt,0,0) != 0) return(TFSERR_MEMFAIL); } /* If request size goes beyond the size of the file, then copy * to the end of the file and return that smaller count. */ else { cnt = tdat->hdr.filsize - tdat->offset; if (tfsmemcpy(buf, from, cnt, 0, 0) != 0) return(TFSERR_MEMFAIL); } tdat->offset += cnt; return(cnt);}/* tfswrite(): * Similar to a standard write call to a file. * MONLIB NOTICE: this function is accessible through monlib.c. */inttfswrite(int fd, char *buf, int cnt){ struct tfsdat *tdat; if (tfsTrace > 1) printf("tfswrite(%d,0x%lx,%d)\n", fd,(ulong)buf,cnt); /* Verify valid range of incoming file descriptor. */ if ((cnt < 1) || (fd < 0) || (fd >= TFS_MAXOPEN)) return(TFSERR_BADARG); /* Make sure the file pointed to by the incoming descriptor is active. */ if (tfsSlots[fd].offset == -1) return(TFSERR_BADARG); tdat = &tfsSlots[fd]; /* Make sure file is not opened as read-only */ if (tdat->flagmode & TFS_RDONLY) return(TFSERR_RDONLY); if (tfsmemcpy(tdat->base+tdat->offset,buf,cnt,0,0) != 0) return(TFSERR_MEMFAIL); tdat->offset += cnt; /* If new offset is greater than current high-water point, then * adjust the high water point so that it is always reflecting the * highest offset into which the file has had some data written. */ if (tdat->offset > tdat->hwp) tdat->hwp = tdat->offset; return(TFS_OKAY);}/* tfsseek(): * Adjust the current pointer into the specified file. * If file is read-only, then the offset cannot exceed the file size; * otherwise, the only check made to the offset is that it is positive. * MONLIB NOTICE: this function is accessible through monlib.c. */inttfsseek(int fd, int offset, int whence){ int o_offset; struct tfsdat *tdat; if ((fd < 0) || (fd >= TFS_MAXOPEN)) return(TFSERR_BADARG); tdat = &tfsSlots[fd]; o_offset = tdat->offset; switch (whence) { case TFS_BEGIN: tdat->offset = offset; break; case TFS_CURRENT: tdat->offset += offset; break; default: return(TFSERR_BADARG); } /* If new offset is less than zero or if the file is read-only and the * new offset is greater than the file size, return EOF... */ if ((tdat->offset < 0) || ((tdat->flagmode & TFS_RDONLY) && (tdat->offset > tdat->hdr.filsize))){ tdat->offset = o_offset; return(TFSERR_EOF); } return(tdat->offset);}/* tfsgetline(): * Read into the buffer a block of characters upto the next CR or LF in * the file. After the CR/LF, or after max-1 chars are loaded, terminate * with a NULL. Return the number of characters loaded. * At end of file return 0. * MONLIB NOTICE: this function is accessible through monlib.c. */inttfsgetline(int fd,char *buf,int max){ int cnt; uchar *from, *fromtmp; struct tfsdat *tdat; max--; if (tfsTrace > 1) printf("tfsgetline(%d,0x%lx,%d)\n",fd,(ulong)buf,max); /* Verify valid range of incoming file descriptor. */ if ((max < 1) || (fd < 0) || (fd >= TFS_MAXOPEN)) return(TFSERR_BADARG); /* Make sure the file pointed to by the incoming descriptor is active. */ if (tfsSlots[fd].offset == -1) return(TFSERR_BADARG); tdat = &tfsSlots[fd]; if (tdat->offset == -1) return(TFSERR_BADFD); if (tdat->offset >= tdat->hdr.filsize) return(0); from = (uchar *) tdat->base + tdat->offset; /* Determine the count based on the presence (or lack thereof) of a * carriage return or line feed... */ for(fromtmp=from,cnt=0;cnt<max && *fromtmp;cnt++,fromtmp++) { if ((*fromtmp == '\r') || (*fromtmp == '\n')) { cnt++; break; } } /* If request size is within the range of the file and current * then copy the data to the requestors buffer, increment offset * and return the count. */ if ((tdat->offset + cnt) <= tdat->hdr.filsize) { if (tfsmemcpy(buf, from, cnt,0,0) != 0) return(TFSERR_MEMFAIL); } /* If request size goes beyond the size of the file, then copy * to the end of the file and return that smaller count. */ else { cnt = tdat->hdr.filsize - tdat->offset; if (tfsmemcpy(buf, from, cnt, 0, 0) != 0) return(TFSERR_MEMFAIL); } buf[cnt] = 0; tdat->offset += cnt; return(cnt);}/* tfsipmod():
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -