2 * DOS file system functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996 Alexandre Julliard
16 #include <sys/statfs.h>
27 /* Chars we don't want to see in DOS file names */
28 #define INVALID_DOS_CHARS "*?<>|\"+=,;[] \345"
30 static const char *DOSFS_Devices[][2] =
34 { "NUL", "/dev/null" },
46 #define GET_DRIVE(path) \
47 (((path)[1] == ':') ? toupper((path)[0]) - 'A' : DOSFS_CurDrive)
49 /* DOS extended error status */
50 WORD DOS_ExtendedError;
56 /***********************************************************************
59 * Return 1 if Unix file 'name' is also a valid MS-DOS name
60 * (i.e. contains only valid DOS chars, lower-case only, fits in 8.3 format).
61 * File name can be terminated by '\0', '\\' or '/'.
63 static int DOSFS_ValidDOSName( const char *name )
65 static const char invalid_chars[] = INVALID_DOS_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
71 /* Check for "." and ".." */
74 /* All other names beginning with '.' are invalid */
75 return (IS_END_OF_NAME(*p));
77 while (!IS_END_OF_NAME(*p))
79 if (strchr( invalid_chars, *p )) return 0; /* Invalid char */
80 if (*p == '.') break; /* Start of the extension */
81 if (++len > 8) return 0; /* Name too long */
84 if (*p != '.') return 1; /* End of name */
86 if (IS_END_OF_NAME(*p)) return 0; /* Empty extension not allowed */
88 while (!IS_END_OF_NAME(*p))
90 if (strchr( invalid_chars, *p )) return 0; /* Invalid char */
91 if (*p == '.') return 0; /* Second extension not allowed */
92 if (++len > 3) return 0; /* Extension too long */
99 /***********************************************************************
102 * Remove all '.' and '..' at the beginning of 'name'.
104 static const char * DOSFS_CheckDotDot( const char *name, char *buffer,
105 char sep , int *len )
107 char *p = buffer + strlen(buffer);
111 if (IS_END_OF_NAME(name[1]))
114 while ((*name == '\\') || (*name == '/')) name++;
116 else if ((name[1] == '.') && IS_END_OF_NAME(name[2]))
119 while ((*name == '\\') || (*name == '/')) name++;
120 while ((p > buffer) && (*p != sep)) { p--; (*len)++; }
121 *p = '\0'; /* Remove trailing separator */
129 /***********************************************************************
130 * DOSFS_ToDosFCBFormat
132 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
133 * expanding wild cards and converting to upper-case in the process.
134 * File name can be terminated by '\0', '\\' or '/'.
135 * Return NULL if the name is not a valid DOS name.
137 const char *DOSFS_ToDosFCBFormat( const char *name )
139 static const char invalid_chars[] = INVALID_DOS_CHARS;
140 static char buffer[12];
141 const char *p = name;
144 /* Check for "." and ".." */
148 strcpy( buffer, ". " );
150 return (!*p || (*p == '/') || (*p == '\\')) ? buffer : NULL;
153 for (i = 0; i < 8; i++)
170 if (strchr( invalid_chars, *p )) return NULL;
171 buffer[i] = toupper(*p);
179 /* Skip all chars after wildcard up to first dot */
180 while (*p && (*p != '/') && (*p != '\\') && (*p != '.')) p++;
184 /* Check if name too long */
185 if (*p && (*p != '/') && (*p != '\\') && (*p != '.')) return NULL;
187 if (*p == '.') p++; /* Skip dot */
189 for (i = 8; i < 11; i++)
199 return NULL; /* Second extension not allowed */
207 if (strchr( invalid_chars, *p )) return NULL;
208 buffer[i] = toupper(*p);
218 /***********************************************************************
219 * DOSFS_ToDosDTAFormat
221 * Convert a file name from FCB to DTA format (name.ext, null-terminated)
222 * converting to upper-case in the process.
223 * File name can be terminated by '\0', '\\' or '/'.
224 * Return NULL if the name is not a valid DOS name.
226 const char *DOSFS_ToDosDTAFormat( const char *name )
228 static char buffer[13];
231 memcpy( buffer, name, 8 );
232 for (p = buffer + 8; (p > buffer) && (p[-1] == ' '); p--);
234 memcpy( p, name + 8, 3 );
235 for (p += 3; p[-1] == ' '; p--);
236 if (p[-1] == '.') p--;
242 /***********************************************************************
245 * Check a DOS file name against a mask (both in FCB format).
247 static int DOSFS_Match( const char *mask, const char *name )
250 for (i = 11; i > 0; i--, mask++, name++)
251 if ((*mask != '?') && (*mask != *name)) return 0;
256 /***********************************************************************
257 * DOSFS_ToDosDateTime
259 * Convert a Unix time in the DOS date/time format.
261 void DOSFS_ToDosDateTime( time_t *unixtime, WORD *pDate, WORD *pTime )
263 struct tm *tm = localtime( unixtime );
265 *pTime = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
267 *pDate = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5)
272 /***********************************************************************
275 * Transform a Unix file name into a hashed DOS name. If the name is a valid
276 * DOS name, it is converted to upper-case; otherwise it is replaced by a
277 * hashed version that fits in 8.3 format.
278 * File name can be terminated by '\0', '\\' or '/'.
280 static const char *DOSFS_Hash( const char *name, int dir_format )
282 static const char invalid_chars[] = INVALID_DOS_CHARS "~.";
283 static const char hash_chars[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
285 static char buffer[13];
291 if (dir_format) strcpy( buffer, " " );
293 if (DOSFS_ValidDOSName( name ))
295 /* Check for '.' and '..' */
299 if (!dir_format) buffer[1] = buffer[2] = '\0';
300 if (name[1] == '.') buffer[1] = '.';
304 /* Simply copy the name, converting to uppercase */
306 for (dst = buffer; !IS_END_OF_NAME(*name) && (*name != '.'); name++)
307 *dst++ = toupper(*name);
310 if (dir_format) dst = buffer + 8;
312 for (name++; !IS_END_OF_NAME(*name); name++)
313 *dst++ = toupper(*name);
315 if (!dir_format) *dst = '\0';
319 /* Compute the hash code of the file name */
320 /* If you know something about hash functions, feel free to */
321 /* insert a better algorithm here... */
322 for (p = name, hash = 0xbeef; !IS_END_OF_NAME(p[1]); p++)
323 hash = (hash << 3) ^ (hash >> 5) ^ *p ^ (p[1] << 8);
324 hash = (hash << 3) ^ (hash >> 5) ^ *p; /* Last character */
326 /* Find last dot for start of the extension */
327 for (p = name+1, ext = NULL; !IS_END_OF_NAME(*p); p++)
328 if (*p == '.') ext = p;
329 if (ext && IS_END_OF_NAME(ext[1]))
330 ext = NULL; /* Empty extension ignored */
332 /* Copy first 4 chars, replacing invalid chars with '_' */
333 for (i = 4, p = name, dst = buffer; i > 0; i--, p++)
335 if (IS_END_OF_NAME(*p) || (p == ext)) break;
336 *dst++ = strchr( invalid_chars, *p ) ? '_' : toupper(*p);
338 /* Pad to 5 chars with '~' */
339 while (i-- >= 0) *dst++ = '~';
341 /* Insert hash code converted to 3 ASCII chars */
342 *dst++ = hash_chars[(hash >> 10) & 0x1f];
343 *dst++ = hash_chars[(hash >> 5) & 0x1f];
344 *dst++ = hash_chars[hash & 0x1f];
346 /* Copy the first 3 chars of the extension (if any) */
349 if (!dir_format) *dst++ = '.';
350 for (i = 3, ext++; (i > 0) && !IS_END_OF_NAME(*ext); i--, ext++)
351 *dst++ = toupper(*ext);
353 if (!dir_format) *dst = '\0';
359 /***********************************************************************
362 * Find the Unix file name in a given directory that corresponds to
363 * a file name (either in Unix or DOS format).
364 * File name can be terminated by '\0', '\\' or '/'.
365 * Return 1 if OK, 0 if no file name matches.
367 static int DOSFS_FindUnixName( const char *path, const char *name,
368 char *buffer, int maxlen )
371 struct dirent *dirent;
373 const char *dos_name = DOSFS_ToDosFCBFormat( name );
374 const char *p = strchr( name, '/' );
375 int len = p ? (int)(p - name) : strlen(name);
377 dprintf_dosfs( stddeb, "DOSFS_FindUnixName: %s %s\n", path, name );
379 if ((p = strchr( name, '\\' ))) len = MIN( (int)(p - name), len );
381 if (!(dir = opendir( path )))
383 dprintf_dosfs( stddeb, "DOSFS_FindUnixName(%s,%s): can't open dir\n",
387 while ((dirent = readdir( dir )) != NULL)
389 /* Check against Unix name */
390 if ((len == strlen(dirent->d_name) &&
391 !memcmp( dirent->d_name, name, len ))) break;
394 /* Check against hashed DOS name */
395 const char *hash_name = DOSFS_Hash( dirent->d_name, TRUE );
396 if (!strcmp( dos_name, hash_name )) break;
399 if (dirent) lstrcpyn( buffer, dirent->d_name, maxlen );
401 dprintf_dosfs( stddeb, "DOSFS_FindUnixName(%s,%s) -> %s\n",
402 path, name, dirent ? buffer : "** Not found **" );
403 return (dirent != NULL);
407 /***********************************************************************
410 * Check if a DOS file name represents a DOS device. Returns the name
411 * of the associated Unix device, or NULL if not found.
413 const char *DOSFS_IsDevice( const char *name )
418 if (name[1] == ':') name += 2;
419 if ((p = strrchr( name, '/' ))) name = p + 1;
420 if ((p = strrchr( name, '\\' ))) name = p + 1;
421 for (i = 0; i < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0]); i++)
423 const char *dev = DOSFS_Devices[i][0];
424 if (!lstrncmpi( dev, name, strlen(dev) ))
426 p = name + strlen( dev );
428 if (!*p || (*p == '.')) return DOSFS_Devices[i][1];
435 /***********************************************************************
436 * DOSFS_GetUnixFileName
438 * Convert a file name (DOS or mixed DOS/Unix format) to a valid Unix name.
439 * Return NULL if one of the path components does not exist. The last path
440 * component is only checked if 'check_last' is non-zero.
442 const char * DOSFS_GetUnixFileName( const char * name, int check_last )
444 static char buffer[MAX_PATHNAME_LEN];
445 int drive, len, found;
448 dprintf_dosfs( stddeb, "DOSFS_GetUnixFileName: %s\n", name );
451 drive = toupper(name[0]) - 'A';
454 else if (name[0] == '/') /* Absolute Unix path? */
456 if ((drive = DRIVE_FindDriveRoot( &name )) == -1)
458 fprintf( stderr, "Warning: %s not accessible from a DOS drive\n",
460 /* Assume it really was a DOS name */
461 drive = DRIVE_GetCurrentDrive();
464 else drive = DRIVE_GetCurrentDrive();
466 if (!DRIVE_IsValid(drive))
468 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
471 lstrcpyn( buffer, DRIVE_GetRoot(drive), MAX_PATHNAME_LEN );
472 if (buffer[1]) root = buffer + strlen(buffer);
473 else root = buffer; /* root directory */
475 if ((*name == '\\') || (*name == '/'))
477 while ((*name == '\\') || (*name == '/')) name++;
481 lstrcpyn( root + 1, DRIVE_GetUnixCwd(drive),
482 MAX_PATHNAME_LEN - (int)(root - buffer) - 1 );
483 if (root[1]) *root = '/';
486 p = buffer[1] ? buffer + strlen(buffer) : buffer;
487 len = MAX_PATHNAME_LEN - strlen(buffer);
489 while (*name && found)
491 const char *newname = DOSFS_CheckDotDot( name, root, '/', &len );
494 p = root + strlen(root);
500 DOS_ERROR( ER_PathNotFound, EC_NotFound, SA_Abort, EL_Disk );
503 if ((found = DOSFS_FindUnixName( buffer, name, p+1, len-1 )))
508 while (!IS_END_OF_NAME(*name)) name++;
510 else if (!check_last)
513 for (len--; !IS_END_OF_NAME(*name) && (len > 1); name++, len--)
514 *p++ = tolower(*name);
517 while ((*name == '\\') || (*name == '/')) name++;
523 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
526 if (*name) /* Not last */
528 DOS_ERROR( ER_PathNotFound, EC_NotFound, SA_Abort, EL_Disk );
532 if (!buffer[0]) strcpy( buffer, "/" );
533 dprintf_dosfs( stddeb, "DOSFS_GetUnixFileName: returning %s\n", buffer );
538 /***********************************************************************
539 * DOSFS_GetDosTrueName
541 * Convert a file name (DOS or Unix format) to a complete DOS name.
542 * Return NULL if the path name is invalid or too long.
543 * The unix_format flag is a hint that the file name is in Unix format.
545 const char * DOSFS_GetDosTrueName( const char *name, int unix_format )
547 static char buffer[MAX_PATHNAME_LEN];
551 dprintf_dosfs( stddeb, "DOSFS_GetDosTrueName(%s,%d)\n", name, unix_format);
554 drive = toupper(name[0]) - 'A';
557 else if (name[0] == '/') /* Absolute Unix path? */
559 if ((drive = DRIVE_FindDriveRoot( &name )) == -1)
561 fprintf( stderr, "Warning: %s not accessible from a DOS drive\n",
563 /* Assume it really was a DOS name */
564 drive = DRIVE_GetCurrentDrive();
567 else drive = DRIVE_GetCurrentDrive();
569 if (!DRIVE_IsValid(drive))
571 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
578 if (IS_END_OF_NAME(*name))
580 while ((*name == '\\') || (*name == '/')) name++;
585 lstrcpyn( p, DRIVE_GetDosCwd(drive), sizeof(buffer) - 3 );
586 if (*p) p += strlen(p); else p--;
589 len = MAX_PATHNAME_LEN - (int)(p - buffer);
593 const char *newname = DOSFS_CheckDotDot( name, buffer+2, '\\', &len );
596 p = buffer + strlen(buffer);
602 DOS_ERROR( ER_PathNotFound, EC_NotFound, SA_Abort, EL_Disk );
606 if (unix_format) /* Hash it into a DOS name */
608 lstrcpyn( p, DOSFS_Hash( name, FALSE ), len );
611 while (!IS_END_OF_NAME(*name)) name++;
613 else /* Already DOS format, simply upper-case it */
615 while (!IS_END_OF_NAME(*name) && (len > 1))
617 *p++ = toupper(*name);
623 while ((*name == '\\') || (*name == '/')) name++;
630 dprintf_dosfs( stddeb, "DOSFS_GetDosTrueName: returning %s\n", buffer );
635 /***********************************************************************
638 * Find the next matching file. Return the number of entries read to find
639 * the matching one, or 0 if no more entries.
641 int DOSFS_FindNext( const char *path, const char *mask, int drive,
642 BYTE attr, int skip, DOS_DIRENT *entry )
644 static DIR *dir = NULL;
645 struct dirent *dirent;
647 static char buffer[MAX_PATHNAME_LEN];
648 static int cur_pos = 0;
650 const char *hash_name;
652 if ((attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
654 time_t now = time(NULL);
656 strcpy( entry->name, DRIVE_GetLabel( drive ) );
657 entry->attr = FA_LABEL;
659 DOSFS_ToDosDateTime( &now, &entry->date, &entry->time );
663 /* Check the cached directory */
664 if (dir && !strcmp( buffer, path ) && (cur_pos <= skip)) skip -= cur_pos;
665 else /* Not in the cache, open it anew */
667 dprintf_dosfs( stddeb, "DOSFS_FindNext: cache miss, path=%s skip=%d buf=%s cur=%d\n",
668 path, skip, buffer, cur_pos );
670 if (dir) closedir(dir);
671 if (!(dir = opendir( path ))) return 0;
672 lstrcpyn( buffer, path, sizeof(buffer) - 1 );
674 strcat( buffer, "/" );
675 p = buffer + strlen(buffer);
676 attr |= FA_UNUSED | FA_ARCHIVE | FA_RDONLY;
678 while ((dirent = readdir( dir )) != NULL)
680 if (skip-- > 0) continue;
682 hash_name = DOSFS_Hash( dirent->d_name, TRUE );
683 if (!DOSFS_Match( mask, hash_name )) continue;
684 lstrcpyn( p, dirent->d_name, sizeof(buffer) - (int)(p - buffer) );
686 if (!FILE_Stat( buffer, &entry->attr, &entry->size,
687 &entry->date, &entry->time ))
689 fprintf( stderr, "DOSFS_FindNext: can't stat %s\n", buffer );
692 if (entry->attr & ~attr) continue;
693 strcpy( entry->name, hash_name );
694 lstrcpyn( entry->unixname, dirent->d_name, sizeof(entry->unixname) );
695 dprintf_dosfs( stddeb, "DOSFS_FindNext: returning %s %02x %ld\n",
696 entry->name, entry->attr, entry->size );
698 p[-1] = '\0'; /* Remove trailing slash in buffer */
703 return 0; /* End of directory */