2 * DOS file system functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996 Alexandre Julliard
16 #if defined(__svr4__) || defined(_SCO_DS)
17 #include <sys/statfs.h>
30 /* Chars we don't want to see in DOS file names */
31 #define INVALID_DOS_CHARS "*?<>|\"+=,;[] \345"
33 static const char *DOSFS_Devices[][2] =
37 { "NUL", "/dev/null" },
49 #define GET_DRIVE(path) \
50 (((path)[1] == ':') ? toupper((path)[0]) - 'A' : DOSFS_CurDrive)
52 /* DOS extended error status */
53 WORD DOS_ExtendedError;
59 /***********************************************************************
62 * Return 1 if Unix file 'name' is also a valid MS-DOS name
63 * (i.e. contains only valid DOS chars, lower-case only, fits in 8.3 format).
64 * File name can be terminated by '\0', '\\' or '/'.
66 static int DOSFS_ValidDOSName( const char *name )
68 static const char invalid_chars[] = INVALID_DOS_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
74 /* Check for "." and ".." */
77 /* All other names beginning with '.' are invalid */
78 return (IS_END_OF_NAME(*p));
80 while (!IS_END_OF_NAME(*p))
82 if (strchr( invalid_chars, *p )) return 0; /* Invalid char */
83 if (*p == '.') break; /* Start of the extension */
84 if (++len > 8) return 0; /* Name too long */
87 if (*p != '.') return 1; /* End of name */
89 if (IS_END_OF_NAME(*p)) return 0; /* Empty extension not allowed */
91 while (!IS_END_OF_NAME(*p))
93 if (strchr( invalid_chars, *p )) return 0; /* Invalid char */
94 if (*p == '.') return 0; /* Second extension not allowed */
95 if (++len > 3) return 0; /* Extension too long */
102 /***********************************************************************
105 * Remove all '.' and '..' at the beginning of 'name'.
107 static const char * DOSFS_CheckDotDot( const char *name, char *buffer,
108 char sep , int *len )
110 char *p = buffer + strlen(buffer);
114 if (IS_END_OF_NAME(name[1]))
117 while ((*name == '\\') || (*name == '/')) name++;
119 else if ((name[1] == '.') && IS_END_OF_NAME(name[2]))
122 while ((*name == '\\') || (*name == '/')) name++;
123 while ((p > buffer) && (*p != sep)) { p--; (*len)++; }
124 *p = '\0'; /* Remove trailing separator */
132 /***********************************************************************
133 * DOSFS_ToDosFCBFormat
135 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
136 * expanding wild cards and converting to upper-case in the process.
137 * File name can be terminated by '\0', '\\' or '/'.
138 * Return NULL if the name is not a valid DOS name.
140 const char *DOSFS_ToDosFCBFormat( const char *name )
142 static const char invalid_chars[] = INVALID_DOS_CHARS;
143 static char buffer[12];
144 const char *p = name;
147 /* Check for "." and ".." */
151 strcpy( buffer, ". " );
153 return (!*p || (*p == '/') || (*p == '\\')) ? buffer : NULL;
156 for (i = 0; i < 8; i++)
173 if (strchr( invalid_chars, *p )) return NULL;
174 buffer[i] = toupper(*p);
182 /* Skip all chars after wildcard up to first dot */
183 while (*p && (*p != '/') && (*p != '\\') && (*p != '.')) p++;
187 /* Check if name too long */
188 if (*p && (*p != '/') && (*p != '\\') && (*p != '.')) return NULL;
190 if (*p == '.') p++; /* Skip dot */
192 for (i = 8; i < 11; i++)
202 return NULL; /* Second extension not allowed */
210 if (strchr( invalid_chars, *p )) return NULL;
211 buffer[i] = toupper(*p);
221 /***********************************************************************
222 * DOSFS_ToDosDTAFormat
224 * Convert a file name from FCB to DTA format (name.ext, null-terminated)
225 * converting to upper-case in the process.
226 * File name can be terminated by '\0', '\\' or '/'.
227 * Return NULL if the name is not a valid DOS name.
229 const char *DOSFS_ToDosDTAFormat( const char *name )
231 static char buffer[13];
234 memcpy( buffer, name, 8 );
235 for (p = buffer + 8; (p > buffer) && (p[-1] == ' '); p--);
237 memcpy( p, name + 8, 3 );
238 for (p += 3; p[-1] == ' '; p--);
239 if (p[-1] == '.') p--;
245 /***********************************************************************
248 * Check a DOS file name against a mask (both in FCB format).
250 int DOSFS_Match( const char *mask, const char *name )
253 for (i = 11; i > 0; i--, mask++, name++)
254 if ((*mask != '?') && (*mask != *name)) return 0;
259 /***********************************************************************
260 * DOSFS_ToDosDateTime
262 * Convert a Unix time in the DOS date/time format.
264 void DOSFS_ToDosDateTime( time_t unixtime, WORD *pDate, WORD *pTime )
266 struct tm *tm = localtime( &unixtime );
268 *pTime = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
270 *pDate = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5)
275 /***********************************************************************
278 * Transform a Unix file name into a hashed DOS name. If the name is a valid
279 * DOS name, it is converted to upper-case; otherwise it is replaced by a
280 * hashed version that fits in 8.3 format.
281 * File name can be terminated by '\0', '\\' or '/'.
283 const char *DOSFS_Hash( const char *name, int dir_format )
285 static const char invalid_chars[] = INVALID_DOS_CHARS "~.";
286 static const char hash_chars[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
288 static char buffer[13];
294 if (dir_format) strcpy( buffer, " " );
296 if (DOSFS_ValidDOSName( name ))
298 /* Check for '.' and '..' */
302 if (!dir_format) buffer[1] = buffer[2] = '\0';
303 if (name[1] == '.') buffer[1] = '.';
307 /* Simply copy the name, converting to uppercase */
309 for (dst = buffer; !IS_END_OF_NAME(*name) && (*name != '.'); name++)
310 *dst++ = toupper(*name);
313 if (dir_format) dst = buffer + 8;
315 for (name++; !IS_END_OF_NAME(*name); name++)
316 *dst++ = toupper(*name);
318 if (!dir_format) *dst = '\0';
322 /* Compute the hash code of the file name */
323 /* If you know something about hash functions, feel free to */
324 /* insert a better algorithm here... */
325 for (p = name, hash = 0xbeef; !IS_END_OF_NAME(p[1]); p++)
326 hash = (hash << 3) ^ (hash >> 5) ^ *p ^ (p[1] << 8);
327 hash = (hash << 3) ^ (hash >> 5) ^ *p; /* Last character */
329 /* Find last dot for start of the extension */
330 for (p = name+1, ext = NULL; !IS_END_OF_NAME(*p); p++)
331 if (*p == '.') ext = p;
332 if (ext && IS_END_OF_NAME(ext[1]))
333 ext = NULL; /* Empty extension ignored */
335 /* Copy first 4 chars, replacing invalid chars with '_' */
336 for (i = 4, p = name, dst = buffer; i > 0; i--, p++)
338 if (IS_END_OF_NAME(*p) || (p == ext)) break;
339 *dst++ = strchr( invalid_chars, *p ) ? '_' : toupper(*p);
341 /* Pad to 5 chars with '~' */
342 while (i-- >= 0) *dst++ = '~';
344 /* Insert hash code converted to 3 ASCII chars */
345 *dst++ = hash_chars[(hash >> 10) & 0x1f];
346 *dst++ = hash_chars[(hash >> 5) & 0x1f];
347 *dst++ = hash_chars[hash & 0x1f];
349 /* Copy the first 3 chars of the extension (if any) */
352 if (!dir_format) *dst++ = '.';
353 for (i = 3, ext++; (i > 0) && !IS_END_OF_NAME(*ext); i--, ext++)
354 *dst++ = toupper(*ext);
356 if (!dir_format) *dst = '\0';
362 /***********************************************************************
365 * Find the Unix file name in a given directory that corresponds to
366 * a file name (either in Unix or DOS format).
367 * File name can be terminated by '\0', '\\' or '/'.
368 * Return 1 if OK, 0 if no file name matches.
370 static int DOSFS_FindUnixName( const char *path, const char *name,
371 char *buffer, int maxlen )
374 struct dirent *dirent;
376 const char *dos_name = DOSFS_ToDosFCBFormat( name );
377 const char *p = strchr( name, '/' );
378 int len = p ? (int)(p - name) : strlen(name);
380 dprintf_dosfs( stddeb, "DOSFS_FindUnixName: %s %s\n", path, name );
382 if ((p = strchr( name, '\\' ))) len = MIN( (int)(p - name), len );
384 if (!(dir = opendir( path )))
386 dprintf_dosfs( stddeb, "DOSFS_FindUnixName(%s,%s): can't open dir\n",
390 while ((dirent = readdir( dir )) != NULL)
392 /* Check against Unix name */
393 if ((len == strlen(dirent->d_name) &&
394 !memcmp( dirent->d_name, name, len ))) break;
397 /* Check against hashed DOS name */
398 const char *hash_name = DOSFS_Hash( dirent->d_name, TRUE );
399 if (!strcmp( dos_name, hash_name )) break;
402 if (dirent) lstrcpyn32A( buffer, dirent->d_name, maxlen );
404 dprintf_dosfs( stddeb, "DOSFS_FindUnixName(%s,%s) -> %s\n",
405 path, name, dirent ? buffer : "** Not found **" );
406 return (dirent != NULL);
410 /***********************************************************************
413 * Check if a DOS file name represents a DOS device. Returns the name
414 * of the associated Unix device, or NULL if not found.
416 const char *DOSFS_IsDevice( const char *name )
421 if (name[0] && (name[1] == ':')) name += 2;
422 if ((p = strrchr( name, '/' ))) name = p + 1;
423 if ((p = strrchr( name, '\\' ))) name = p + 1;
424 for (i = 0; i < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0]); i++)
426 const char *dev = DOSFS_Devices[i][0];
427 if (!lstrncmpi32A( dev, name, strlen(dev) ))
429 p = name + strlen( dev );
430 if (!*p || (*p == '.')) return DOSFS_Devices[i][1];
437 /***********************************************************************
438 * DOSFS_GetUnixFileName
440 * Convert a file name (DOS or mixed DOS/Unix format) to a valid Unix name.
441 * Return NULL if one of the path components does not exist. The last path
442 * component is only checked if 'check_last' is non-zero.
444 const char * DOSFS_GetUnixFileName( const char * name, int check_last )
446 static char buffer[MAX_PATHNAME_LEN];
447 int drive, len, found;
450 dprintf_dosfs( stddeb, "DOSFS_GetUnixFileName: %s\n", name );
451 if (name[0] && (name[1] == ':'))
453 drive = toupper(name[0]) - 'A';
456 else if (name[0] == '/') /* Absolute Unix path? */
458 if ((drive = DRIVE_FindDriveRoot( &name )) == -1)
460 fprintf( stderr, "Warning: %s not accessible from a DOS drive\n",
462 /* Assume it really was a DOS name */
463 drive = DRIVE_GetCurrentDrive();
466 else drive = DRIVE_GetCurrentDrive();
468 if (!DRIVE_IsValid(drive))
470 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
473 lstrcpyn32A( buffer, DRIVE_GetRoot(drive), MAX_PATHNAME_LEN );
474 if (buffer[1]) root = buffer + strlen(buffer);
475 else root = buffer; /* root directory */
477 if ((*name == '\\') || (*name == '/'))
479 while ((*name == '\\') || (*name == '/')) name++;
483 lstrcpyn32A( root + 1, DRIVE_GetUnixCwd(drive),
484 MAX_PATHNAME_LEN - (int)(root - buffer) - 1 );
485 if (root[1]) *root = '/';
488 p = buffer[1] ? buffer + strlen(buffer) : buffer;
489 len = MAX_PATHNAME_LEN - strlen(buffer);
491 while (*name && found)
493 const char *newname = DOSFS_CheckDotDot( name, root, '/', &len );
496 p = root + strlen(root);
502 DOS_ERROR( ER_PathNotFound, EC_NotFound, SA_Abort, EL_Disk );
505 if ((found = DOSFS_FindUnixName( buffer, name, p+1, len-1 )))
510 while (!IS_END_OF_NAME(*name)) name++;
512 else if (!check_last)
515 for (len--; !IS_END_OF_NAME(*name) && (len > 1); name++, len--)
516 *p++ = tolower(*name);
519 while ((*name == '\\') || (*name == '/')) name++;
525 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
528 if (*name) /* Not last */
530 DOS_ERROR( ER_PathNotFound, EC_NotFound, SA_Abort, EL_Disk );
534 if (!buffer[0]) strcpy( buffer, "/" );
535 dprintf_dosfs( stddeb, "DOSFS_GetUnixFileName: returning %s\n", buffer );
540 /***********************************************************************
541 * DOSFS_GetDosTrueName
543 * Convert a file name (DOS or Unix format) to a complete DOS name.
544 * Return NULL if the path name is invalid or too long.
545 * The unix_format flag is a hint that the file name is in Unix format.
547 const char * DOSFS_GetDosTrueName( const char *name, int unix_format )
549 static char buffer[MAX_PATHNAME_LEN];
553 dprintf_dosfs( stddeb, "DOSFS_GetDosTrueName(%s,%d)\n", name, unix_format);
554 if (name[0] && (name[1] == ':'))
556 drive = toupper(name[0]) - 'A';
559 else if (name[0] == '/') /* Absolute Unix path? */
561 if ((drive = DRIVE_FindDriveRoot( &name )) == -1)
563 fprintf( stderr, "Warning: %s not accessible from a DOS drive\n",
565 /* Assume it really was a DOS name */
566 drive = DRIVE_GetCurrentDrive();
569 else drive = DRIVE_GetCurrentDrive();
571 if (!DRIVE_IsValid(drive))
573 DOS_ERROR( ER_InvalidDrive, EC_MediaError, SA_Abort, EL_Disk );
580 if (IS_END_OF_NAME(*name))
582 while ((*name == '\\') || (*name == '/')) name++;
587 lstrcpyn32A( p, DRIVE_GetDosCwd(drive), sizeof(buffer) - 3 );
588 if (*p) p += strlen(p); else p--;
591 len = MAX_PATHNAME_LEN - (int)(p - buffer);
595 const char *newname = DOSFS_CheckDotDot( name, buffer+2, '\\', &len );
598 p = buffer + strlen(buffer);
604 DOS_ERROR( ER_PathNotFound, EC_NotFound, SA_Abort, EL_Disk );
608 if (unix_format) /* Hash it into a DOS name */
610 lstrcpyn32A( p, DOSFS_Hash( name, FALSE ), len );
613 while (!IS_END_OF_NAME(*name)) name++;
615 else /* Already DOS format, simply upper-case it */
617 while (!IS_END_OF_NAME(*name) && (len > 1))
619 *p++ = toupper(*name);
625 while ((*name == '\\') || (*name == '/')) name++;
632 dprintf_dosfs( stddeb, "DOSFS_GetDosTrueName: returning %s\n", buffer );
637 /***********************************************************************
640 * Find the next matching file. Return the number of entries read to find
641 * the matching one, or 0 if no more entries.
643 int DOSFS_FindNext( const char *path, const char *mask, int drive,
644 BYTE attr, int skip, DOS_DIRENT *entry )
646 static DIR *dir = NULL;
647 struct dirent *dirent;
649 static char buffer[MAX_PATHNAME_LEN];
650 static int cur_pos = 0;
651 static int drive_root = 0;
653 const char *hash_name;
655 if ((attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
658 strcpy( entry->name, DRIVE_GetLabel( drive ) );
659 entry->attr = FA_LABEL;
661 DOSFS_ToDosDateTime( time(NULL), &entry->date, &entry->time );
665 /* Check the cached directory */
666 if (dir && !strcmp( buffer, path ) && (cur_pos <= skip)) skip -= cur_pos;
667 else /* Not in the cache, open it anew */
669 const char *drive_path;
670 dprintf_dosfs( stddeb, "DOSFS_FindNext: cache miss, path=%s skip=%d buf=%s cur=%d\n",
671 path, skip, buffer, cur_pos );
673 if (dir) closedir(dir);
674 if (!(dir = opendir( path ))) return 0;
677 if (DRIVE_FindDriveRoot( &drive_path ) != -1)
679 while ((*drive_path == '/') || (*drive_path == '\\')) drive_path++;
680 if (!*drive_path) drive_root = 1;
682 dprintf_dosfs(stddeb, "DOSFS_FindNext: drive_root = %d\n", drive_root);
683 lstrcpyn32A( buffer, path, sizeof(buffer) - 1 );
686 strcat( buffer, "/" );
687 p = buffer + strlen(buffer);
688 attr |= FA_UNUSED | FA_ARCHIVE | FA_RDONLY;
690 while ((dirent = readdir( dir )) != NULL)
692 if (skip-- > 0) continue;
694 hash_name = DOSFS_Hash( dirent->d_name, TRUE );
695 if (!DOSFS_Match( mask, hash_name )) continue;
696 /* Don't return '.' and '..' in the root of the drive */
697 if (drive_root && (dirent->d_name[0] == '.') &&
698 (!dirent->d_name[1] ||
699 ((dirent->d_name[1] == '.') && !dirent->d_name[2]))) continue;
700 lstrcpyn32A( p, dirent->d_name, sizeof(buffer) - (int)(p - buffer) );
702 if (!FILE_Stat( buffer, &entry->attr, &entry->size,
703 &entry->date, &entry->time ))
705 fprintf( stderr, "DOSFS_FindNext: can't stat %s\n", buffer );
708 if (entry->attr & ~attr) continue;
709 strcpy( entry->name, hash_name );
710 lstrcpyn32A( entry->unixname, dirent->d_name, sizeof(entry->unixname));
711 dprintf_dosfs( stddeb, "DOSFS_FindNext: returning %s %02x %ld\n",
712 entry->name, entry->attr, entry->size );
714 p[-1] = '\0'; /* Remove trailing slash in buffer */
719 return 0; /* End of directory */
723 /***********************************************************************
724 * GetShortPathNameA (KERNEL32.271)
726 DWORD GetShortPathName32A( LPCSTR longpath, LPSTR shortpath, DWORD shortlen )
730 dprintf_dosfs( stddeb, "GetShortPathName32A(%s,%p,%ld)\n",
731 longpath, shortpath, shortlen );
733 dostruename = DOSFS_GetDosTrueName( longpath, 0 );
734 lstrcpyn32A( shortpath, dostruename, shortlen );
735 return strlen(dostruename);
739 /***********************************************************************
740 * GetShortPathNameW (KERNEL32.272)
742 DWORD GetShortPathName32W( LPCWSTR longpath, LPWSTR shortpath, DWORD shortlen )
744 LPSTR longpatha = STRING32_DupUniToAnsi( longpath );
745 LPCSTR dostruename = DOSFS_GetDosTrueName( longpatha, 0 );
747 lstrcpynAtoW( shortpath, dostruename, shortlen );
748 return strlen(dostruename);