Adding upstream version 3.30~pre4.
[syslinux-debian/hramrach.git] / libfat / searchdir.c
blob733bf5ec5576c72b68679a5c009742f82e593596
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2004 H. Peter Anvin - All Rights Reserved
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.
11 * ----------------------------------------------------------------------- */
14 * searchdir.c
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.
21 #include <string.h>
22 #include "libfatint.h"
24 int32_t libfat_searchdir(struct libfat_filesystem *fs, int32_t dirclust,
25 const void *name, struct libfat_direntry *direntry)
27 struct fat_dirent *dep;
28 int nent;
29 libfat_sector_t s = libfat_clustertosector(fs, dirclust);
31 while ( 1 ) {
32 if ( s == 0 )
33 return -2; /* Not found */
34 else if ( s == (libfat_sector_t)-1 )
35 return -1; /* Error */
37 dep = libfat_get_sector(fs, s);
38 if ( !dep )
39 return -1; /* Read error */
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;
49 if ( read32(&dep->size) == 0 )
50 return 0; /* An empty file has no clusters */
51 else
52 return read16(&dep->clustlo) + (read16(&dep->clusthi) << 16);
55 if ( dep->name[0] == 0 )
56 return -2; /* Hit high water mark */
58 dep++;
61 s = libfat_nextsector(fs, s);