3 * Stäubli Faverges - <www.staubli.com>
4 * Pierre AUBERT p.aubert@staubli.com
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 #include <linux/ctype.h>
32 static int dir_read (Fs_t *fs,
36 struct vfat_state *v);
38 static int unicode_read (char *in, char *out, int num);
39 static int match (const char *s, const char *p);
40 static unsigned char sum_shortname (char *name);
41 static int check_vfat (struct vfat_state *v, Directory_t *dir);
42 static char *conv_name (char *name, char *ext, char Case, char *ans);
45 /*-----------------------------------------------------------------------------
47 *-----------------------------------------------------------------------------
49 static void clear_vfat (struct vfat_state *v)
55 /*-----------------------------------------------------------------------------
57 *-----------------------------------------------------------------------------
59 int vfat_lookup (Slot_t *dir,
70 struct vfat_state vfat;
71 char newfile [VSE_NAMELEN];
81 if (dir_read (fs, dir, dirent, *entry, &vfat) < 0) {
90 if (dirent -> name[0] == '\0'){
91 if (vfat_start == 0) {
97 if (dirent -> attr == ATTR_VSE) {
98 /* VSE entry, continue */
101 if ( (dirent -> name [0] == DELMARK) ||
102 ((dirent -> attr & ATTR_DIRECTORY) != 0 &&
103 (flags & ACCEPT_DIR) == 0) ||
104 ((dirent -> attr & ATTR_VOLUME) != 0 &&
105 (flags & ACCEPT_LABEL) == 0) ||
106 (((dirent -> attr & (ATTR_DIRECTORY | ATTR_VOLUME)) == 0) &&
107 (flags & ACCEPT_PLAIN) == 0)) {
112 vfat_present = check_vfat (&vfat, dirent);
114 *vfat_start = *entry - 1;
116 *vfat_start -= vfat.subentries;
120 if (dirent -> attr & ATTR_VOLUME) {
121 strncpy (newfile, dirent -> name, 8);
123 strncat (newfile, dirent -> ext, 3);
127 conv_name (dirent -> name, dirent -> ext, dirent -> Case, newfile);
130 if (flags & MATCH_ANY) {
135 if ((vfat_present && match (vfat.name, filename)) ||
136 (match (newfile, filename))) {
144 if ((flags & DO_OPEN) && file) {
145 if (open_file (file, dirent) < 0) {
151 strcpy (outname, vfat.name);
154 strcpy (outname, newfile);
157 return (0); /* File found */
160 return -1; /* File not found */
164 /*-----------------------------------------------------------------------------
165 * dir_read -- Read one directory entry
166 *-----------------------------------------------------------------------------
168 static int dir_read (Fs_t *fs,
172 struct vfat_state *v)
175 /* read the directory entry */
180 MDIR_SIZE) != MDIR_SIZE) {
184 if (v && (dirent -> attr == ATTR_VSE)) {
185 struct vfat_subentry *vse;
186 unsigned char id, last_flag;
189 vse = (struct vfat_subentry *) dirent;
190 id = vse -> id & VSE_MASK;
191 last_flag = (vse -> id & VSE_LAST);
192 if (id > MAX_VFAT_SUBENTRIES) {
193 /* Invalid VSE entry */
199 if(v -> sum != vse -> sum) {
201 v -> sum = vse -> sum;
205 v -> status |= 1 << (id - 1);
207 v -> subentries = id;
210 c = &(v -> name [VSE_NAMELEN * (id - 1)]);
211 c += unicode_read (vse->text1, c, VSE1SIZE);
212 c += unicode_read (vse->text2, c, VSE2SIZE);
213 c += unicode_read (vse->text3, c, VSE3SIZE);
216 *c = '\0'; /* Null terminate long name */
223 /*-----------------------------------------------------------------------------
225 *-----------------------------------------------------------------------------
227 static int unicode_read (char *in, char *out, int num)
231 for (j = 0; j < num; ++j) {
242 /*-----------------------------------------------------------------------------
244 *-----------------------------------------------------------------------------
246 static int match (const char *s, const char *p)
249 for (; *p != '\0'; ) {
250 if (toupper (*s) != toupper (*p)) {
264 /*-----------------------------------------------------------------------------
266 *-----------------------------------------------------------------------------
268 static unsigned char sum_shortname (char *name)
273 for (j = sum = 0; j < 11; ++j) {
274 sum = ((sum & 1) ? 0x80 : 0) + (sum >> 1) +
275 (name [j] ? name [j] : ' ');
279 /*-----------------------------------------------------------------------------
281 * Return 1 if long name is valid, 0 else
282 *-----------------------------------------------------------------------------
284 static int check_vfat (struct vfat_state *v, Directory_t *dir)
288 if (v -> subentries == 0) {
292 strncpy (name, dir -> name, 8);
293 strncpy (name + 8, dir -> ext, 3);
296 if (v -> sum != sum_shortname (name)) {
300 if( (v -> status & ((1 << v -> subentries) - 1)) !=
301 (1 << v -> subentries) - 1) {
304 v->name [VSE_NAMELEN * v -> subentries] = 0;
308 /*-----------------------------------------------------------------------------
310 *-----------------------------------------------------------------------------
312 static char *conv_name (char *name, char *ext, char Case, char *ans)
314 char tname [9], text [4];
318 while (i < 8 && name [i] != ' ' && name [i] != '\0') {
319 tname [i] = name [i];
324 if (Case & BASECASE) {
325 for (i = 0; i < 8 && tname [i]; i++) {
326 tname [i] = tolower (tname [i]);
331 while (i < 3 && ext [i] != ' ' && ext [i] != '\0') {
338 for (i = 0; i < 3 && text [i]; i++) {
339 text [i] = tolower (text [i]);