ifcpu64.c32: clean up the sources
[syslinux.git] / libfat / searchdir.c
blob6fcde139ddd2dcea28f3bac8064a68d5f1b00adb
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2004-2008 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);