📄 fatfs.c
字号:
i = *(fat_tab + j); do { *(fat_tab + j) = VACANT_FAT_ITEM; if (i == EOF_FAT_ITEM) break; j = i; i = *(fat_tab + j); } while (i != EOF_FAT_ITEM); *(fat_tab + j) = VACANT_FAT_ITEM;}/*int freedir(char *fname) release the directory item owned by fname*/int freedir(char *fname){ int i; for (i = 0; i < ROOTSIZE && strcmp(fname, (dir_tab + i)->filename); i++) { ; } if (i < ROOTSIZE) { (dir_tab + i)->filename[0] = (char) ERASED; return 1; /* got it */ } else { return 0; /* no such file fnames */ }}void delFile(char *fname, direntry * dirs){ if (readdir(fname, dirs)) { printf("%s\n", fname); printf("%s,%d,%d\n", dirs->filename, dirs->filesize, dirs->firstfat); freefat(dirs->firstfat); freedir(fname); } else { printf("No such file \"%s\"\n", fname); }}void listFat(){ int i; for (i = 0; i < BLKCOUNT / 10; i++) printf("<i=%d %d>%s", i, fat_tab[i], (i + 1) % 6 ? " " : "\n"); printf("\n");}void fsver(){ printf("\n"); printf("RAM disk FAT cloned FileSystems Ver 1.0\n"); printf("Copyright jockeyson <snallie@tom.com> 2003,2004\n"); printf("\n");}/* ################################################ *//* for open, read, write, lseek, close system call *//* ################################################ */#define ERROR -1#define OK 1#define TRUE 1#define FALSE 0/* perrstr(), print error information indicated by the global variable errno */void perrstr(){ printf("ERR:%d,%s", errno, errstring[errno]);}/* seek the file descriptor table for the freed entry *//* return: ERROR, no vacant entry in file descriptor table *//* the vacant entry index in the file descriptor table , if found */int seekDesc(){ int i; fileDescriptor *fd = fileDescriptors; for (i = 0; i < OPEN_MAX && (fd + i)->status != FREED; i++); if (i >= OPEN_MAX) return ERROR; /* no vacant entry in fd table */ else return i; /* found vacant entry */}/* Mopen , if succeeded , return the lowest available positive integer as the file descriptor, *//* if failed return -1 and global variable errno is set; *//* oflags = READONLY("r"), WRITEONLY("w"), READWRITE, CREATEWRITE("a") */int Mopen(char *pathname, openMode oflags){ direntry dirsrcs; direntry *dirsrc = &dirsrcs; fileDescriptor *fd = fileDescriptors; int fdpos; int newCreate = 0; if (strlen(pathname) < 1) { errno = ENAMEVACANT; /* file name is too short */ return ERROR; } if (strlen(pathname) > FILE_NAME_LENGTH) { errno = ENAMETOOLONG; /* file name is too long */ return ERROR; } if (!readdir(pathname, dirsrc)) { /* no such file in dir table , create it first */ int ret; ret = wrfile(pathname, (char *) &fdpos, 1); /* if ret>0 , a new file pathname created */ if (ret < 0) { /* failed of file creation */ return ERROR; } else { readdir(pathname, dirsrc); /* retrieve directory for pathname */ newCreate = 1; /* a new file just created */ } } if ((fdpos = seekDesc()) == ERROR) { /* failure of no vacant file descriptor, else fdpos is FD */ direntry dirs; if (newCreate) delFile(pathname, &dirs); /* maybe the file pathname have been created above, remove it while no available FD */ errno = ENOFILEDESC; return ERROR; } (fd + fdpos)->fileHdl = fdpos; /* fill with fdpos, the file descriptor */ strcpy((fd + fdpos)->fileName, pathname); /* copy the file name to be opened to file structure */ (fd + fdpos)->curPosition = 0; /* file r/w pointer set to the beginning of the file */ (fd + fdpos)->status = OCCUPIED; /* make this file structure occupied */ (fd + fdpos)->fileLen = dirsrc->filesize; /* fill filed fileLen with filesize from directory entry */ (fd + fdpos)->eof = 0; /* end of file flag set to 0, meaning that it hasn't got the bottom of file */ (fd + fdpos)->IOMode = oflags; /* open mode */ /* allocating new memory for IObuffer */ if (((fd + fdpos)->IObuffer = (char *) malloc(dirsrc->filesize * sizeof(char))) == NULL) { printf("Malloc failed for %d bytes\n", dirsrc->filesize * sizeof(char)); errno = EMALLOC; /* memory allocating error */ return ERROR; } rdfile((fd + fdpos)->fileName, (fd + fdpos)->IObuffer); /* read file from disk to IObuffer */ return fdpos; /* return the file descriptor */}/* int seekFileHdl(int fds) *//* seek the specific file descriptor named fds in file descriptor table *//* return ERROR, if no such file descriptor fds in table *//* the desired file descriptor which matches the argument fds if found */int seekFileHdl(int fds){ int i; fileDescriptor *fd = fileDescriptors; for (i = 0; i < OPEN_MAX && (fd + i)->fileHdl != fds; i++) { ; } if (i >= OPEN_MAX) return ERROR; /* no such fd in fd table */ else return i; /* found */}/* int Mclose(int fds) *//* close the opened file indicated by argument fds *//* return: ERROR, if the file descriptor fds not existed in table *//* OK, if the close operation on fds proceeded ok, it released the file descriptor entry indicated *//* by the fds, free the memory that acquired in the Mopen() */int Mclose(int fds){ int pos; fileDescriptor *fd = fileDescriptors; if ((pos = seekFileHdl(fds)) == -1) return ERROR; /* not found this file descriptor */ else { (fd + pos)->status = FREED; (fd + pos)->fileHdl = -1; /* set fileHdl to -1, means a released file descriptor recalled */ (fd + pos)->eof = 1; /* copy IObuffer to diskspace while file is not opened for READONLY */ if ((fd + pos)->IOMode == WRITEONLY || (fd + pos)->IOMode == READWRITE || (fd + pos)->IOMode == CREATEWRITE) { direntry dirsrcs; direntry *dirsrc = &dirsrcs; int wrbytes; readdir((fd + pos)->fileName, dirsrc); freefat(dirsrc->firstfat); freedir((fd + pos)->fileName); wrbytes = wrfile((fd + pos)->fileName, (fd + pos)->IObuffer, (fd + pos)->fileLen); if (wrbytes < 0) { printf("close file failed"); return ERROR; } } free((fd + pos)->IObuffer); return OK; /* found this fds */ }}/* int Mread(int fds, char *buff, int nbytes) *//* extracted totally nbytes bytes from fds and save it to buff *//* return: ERROR, if no descriptor fds in table *//* the bytes count that have read from fds */int Mread(int fds, char *buff, int nbytes){ int i, fdpos, bytesRead; fileDescriptor *fd = fileDescriptors; fdpos = seekFileHdl(fds); if (fdpos < 0) { errno = ENOFILEDESC; return ERROR; } memcpy(buff, (fd + fdpos)->IObuffer + (fd + fdpos)->curPosition, bytesRead = (fd + fdpos)->curPosition + nbytes > (fd + fdpos)->fileLen ? (fd + fdpos)->fileLen - (fd + fdpos)-> curPosition : nbytes); (fd + fdpos)->curPosition += bytesRead; (fd + fdpos)->eof = (fd + fdpos)->curPosition >= (fd + fdpos)->fileLen ? 1 : 0; return bytesRead;}/* int Mseek(int fds, int offset, int whence) *//* adjust the file r/w pointer and set the appropriate field in file descriptor such as eof, curPosition for *//* fds, *//* return: ERROR, if fds not exists in file descriptor table *//* the current value of file pointer if operated ok */int Mseek(int fds, int offset, int whence){ int i, fdpos, bytesRead; fileDescriptor *fd = fileDescriptors; fdpos = seekFileHdl(fds); if (fdpos < 0) { printf("NO such file descriptor %d", fds); return -1; } /* whence, the position from where I can seek */ switch (whence) { case SEEK_SET1: /* the beginning of the file */ (fd + fdpos)->curPosition = 0; break; case SEEK_CUR1: /* current position */ (fd + fdpos)->curPosition = (fd + fdpos)->curPosition; break; case SEEK_END1: /* the end of the file */ (fd + fdpos)->curPosition = (fd + fdpos)->fileLen; break; } (fd + fdpos)->curPosition = (fd + fdpos)->curPosition + offset >= (fd + fdpos)->fileLen ? (fd + fdpos)->fileLen : (fd + fdpos)-> curPosition + offset; (fd + fdpos)->curPosition = (fd + fdpos)->curPosition < 0 ? 0 : (fd + fdpos)->curPosition; (fd + fdpos)->eof = (fd + fdpos)->curPosition >= (fd + fdpos)->fileLen ? 1 : 0; /* set the eof=1 if reach the bottom of the file , otherwise 0 */ return (fd + fdpos)->curPosition;}/* int Mwrite(int fds, char *buff, int nbytes) *//* write the spcific nbytes data indexed by buff to fds *//* return: ERROR if no matchment with fds in file descriptor table; *//* the bytes number if successfully written to fd's IObuffer */int Mwrite(int fds, char *buff, int nbytes){ int i, fdpos, bytesWrite; fileDescriptor *fd = fileDescriptors; fdpos = seekFileHdl(fds); if (fdpos < 0) { printf("NO such file descriptor %d\n", fds); return ERROR; } if ((fd + fdpos)->curPosition + nbytes > (fd + fdpos)->fileLen) { (fd + fdpos)->IObuffer = (char *) realloc((fd + fdpos)->IObuffer, (fd + fdpos)->curPosition + nbytes); if ((fd + fdpos)->IObuffer == NULL) { printf("realloc failed in mwrite\n"); errno = EMALLOC; return ERROR; } (fd + fdpos)->fileLen = (fd + fdpos)->curPosition + nbytes; } memcpy((fd + fdpos)->IObuffer + (fd + fdpos)->curPosition, buff, bytesWrite = nbytes); (fd + fdpos)->curPosition += bytesWrite; (fd + fdpos)->eof = (fd + fdpos)->curPosition >= (fd + fdpos)->fileLen ? 1 : 0; return bytesWrite;}/* int ifEof(int fds) *//* if EOF occured in file operation for fds *//* return: ERROR if error happened *//* TRUE if the bottom of the file arrived *//* FALSE if not */int ifEof(int fds){ int fdpos; fileDescriptor *fd = fileDescriptors; fdpos = seekFileHdl(fds); if (fdpos == ERROR) { return ERROR; } else return (fd + fdpos)->eof;}/* end of open read write close *//* ################################################ *//* standard file IO function based above ones *//* fopen fclose fgetc fputc feof *//* ################################################ *//* MFILE *Mfopen(char *fileName, char *mode) *//* open fileName with mode , return FILE* pointer *//*r Open text file for reading. The stream is positioned at the beginning of the file.r+ Open for reading and writing. The stream is positioned at the beginning of the file.w Truncate file to zero length or create text file for writing. The stream is posi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -