1 /* ----------------------------------------------------------------------- * 2 * 3 * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, Inc., 53 Temple Place Ste 330, 8 * Boston MA 02111-1307, USA; either version 2 of the License, or 9 * (at your option) any later version; incorporated herein by reference. 10 * 11 * ----------------------------------------------------------------------- */ 12 13 /* 14 * searchdir.c 15 * 16 * Search a FAT directory for a particular pre-mangled filename. 17 * Copies the directory entry into direntry and returns the starting cluster 18 * if found; returns -2 on not found, -1 on error, 0 on empty file. 19 */ 20 21 #include <string.h> 22 #include "libfatint.h" 23 24 int32_t libfat_searchdir(struct libfat_filesystem *fs, int32_t dirclust, 25 const void *name, struct libfat_direntry *direntry) 26 { 27 struct fat_dirent *dep; 28 int nent; 29 libfat_sector_t s = libfat_clustertosector(fs, dirclust); 30 31 while (1) { 32 if (s == 0) 33 return -2; /* Not found */ 34 else if (s == (libfat_sector_t) - 1) 35 return -1; /* Error */ 36 37 dep = libfat_get_sector(fs, s); 38 if (!dep) 39 return -1; /* Read error */ 40 41 for (nent = 0; nent < LIBFAT_SECTOR_SIZE; 42 nent += sizeof(struct fat_dirent)) { 43 if (!memcmp(dep->name, name, 11)) { 44 if (direntry) { 45 memcpy(direntry->entry, dep, sizeof(*dep)); 46 direntry->sector = s; 47 direntry->offset = nent; 48 } 49 if (read32(&dep->size) == 0) 50 return 0; /* An empty file has no clusters */ 51 else 52 return read16(&dep->clustlo) + 53 (read16(&dep->clusthi) << 16); 54 } 55 56 if (dep->name[0] == 0) 57 return -2; /* Hit high water mark */ 58 59 dep++; 60 } 61 62 s = libfat_nextsector(fs, s); 63 } 64 } 65