1 #ident "$Id: searchdir.c,v 1.2 2005/01/04 03:04:54 hpa Exp $"
2 /* ----------------------------------------------------------------------- *
4 * Copyright 2004 H. Peter Anvin - All Rights Reserved
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 * Boston MA 02111-1307, USA; either version 2 of the License, or
10 * (at your option) any later version; incorporated herein by reference.
12 * ----------------------------------------------------------------------- */
17 * Search a FAT directory for a particular pre-mangled filename.
18 * Copies the directory entry into direntry and returns the starting cluster
19 * if found; returns -2 on not found, -1 on error, 0 on empty file.
23 #include "libfatint.h"
25 int32_t libfat_searchdir(struct libfat_filesystem *fs, int32_t dirclust,
26 const void *name, struct libfat_direntry *direntry)
28 struct fat_dirent *dep;
30 libfat_sector_t s = libfat_clustertosector(fs, dirclust);
34 return -2; /* Not found */
35 else if ( s == (libfat_sector_t)-1 )
36 return -1; /* Error */
38 dep = libfat_get_sector(fs, s);
40 return -1; /* Read error */
42 for ( nent = 0 ; nent < LIBFAT_SECTOR_SIZE ;
43 nent += sizeof(struct fat_dirent) ) {
44 if ( !memcmp(dep->name, name, 11) ) {
46 memcpy(direntry->entry, dep, sizeof (*dep));
48 direntry->offset = nent;
50 if ( read32(&dep->size) == 0 )
51 return 0; /* An empty file has no clusters */
53 return read16(&dep->clustlo) + (read16(&dep->clusthi) << 16);
56 if ( dep->name[0] == 0 )
57 return -2; /* Hit high water mark */
62 s = libfat_nextsector(fs, s);