# HG changeset patch # User Jonathan Pevarnek # Date 1302234303 14400 # Node ID 3acc1f944c7b6c2ec6d144b9d40959c501495723 # Parent f806eec33c454c0f1ca72c877bd652b7682a56a1 Added more functionality to the filesystem file printFname is a nice wrapper for putline that handles filenames, it is really quite useless getFname is a nice wrapper for getline to read in a filename assuming there is an array of (really at least but...) 28 characters fnameCmp comparies two filenames fnameLookup find a filename within an array of Direntrys diff -r f806eec33c45 -r 3acc1f944c7b src/fs.c --- a/src/fs.c Thu Apr 07 23:07:46 2011 -0400 +++ b/src/fs.c Thu Apr 07 23:45:03 2011 -0400 @@ -26,6 +26,8 @@ }; typedef struct DIRENTRY Direntry; +//REQUIRES: fsBlock points to an object with size at least 1024 +//EFFECTS: Sets fsBlock to be equal int readFSBlock(u32 dev, u32 blkno, void *fsBlock) { if(fba_read_blk(dev, blkno*2, fsBlock)) return 1; @@ -33,19 +35,53 @@ return 0; } +void printFname(char *name) +{ + putline(name, 28); +} + +//Sets name to contain a filename entered by the user +//REQUIRES: name is 28 characters long +void getFname(char *fname) +{ + int chars = getline(fname, 28); + for(;chars < 28; chars++) { + fname[chars] = ' '; + } +} + +//Checks whether two file names are equal +int fnameCmp(const char *a, const char *b) +{ + int i; + for(i = 0; i < 28; i++, a++, b++) { + if(*a - *b) return *a - *b; + } + return 0; +} + +//Finds the item within an array of filenames with the same name as fname +int fnameLookup(char *fname, const Direntry array[], int last) +{ + int i; + for(i = 0; i < last; i++) + if(!fnameCmp(fname, array[i].fname)) return i; + return last; +} + void start(u64 __memsize) { sPrint(""); //I have no idea why I would possibly need this... -// char buffer[256]; + char buffer[256]; u32 dev = find_dev(0x100); Superblock sb; - if(readFSBlock(dev, 1, &sb)) { + if(readFSBlock(dev, 1, &sb)) { //get the super block sPrint("ERROR\n"); goto END; } Inode root; - if(readFSBlock(dev, sb.root_inode, &root)) { + if(readFSBlock(dev, sb.root_inode, &root)) { //get the root inode sPrint("ERROR\n"); goto END; } @@ -53,7 +89,7 @@ //calculate how many files (assume less than 32 for the moment) //TODO get rid of assumption Direntry direntries[32]; - if(readFSBlock(dev, root.blocks[0], direntries)) { + if(readFSBlock(dev, root.blocks[0], direntries)) { //get the "first 32" files sPrint("ERROR\n"); goto END; } @@ -61,8 +97,13 @@ //Prints off the name of each file Direntry *dp; for(dp = direntries; dp - direntries < nFiles; dp++) { - putline(dp->fname, 28), sPrint("\n"); + printFname(dp->fname), sPrint("\n"); } + char fname[28]; + sPrint("Please enter the file to read: "); + getFname(fname); + printFname(fname), sPrint("\n"); + sPrint(itoa(fnameLookup(fname, direntries, nFiles), buffer, 10)), sPrint("\n"); END: sPrint("DONE\n");