Added MMDEVLDR DeviceIoctl(5) stub (msacm32 is a bit happier now).
[wine/hacks.git] / files / dos_fs.c
blob3e5a2814fc114e99a9fbf8276ce7ba07ab267f80
1 /*
2 * DOS file system functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996 Alexandre Julliard
6 */
8 #include "config.h"
9 #include <sys/types.h>
10 #include <ctype.h>
11 #include <dirent.h>
12 #include <errno.h>
13 #include <sys/errno.h>
14 #include <fcntl.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <sys/stat.h>
18 #include <sys/ioctl.h>
19 #include <time.h>
20 #include <unistd.h>
22 #include "windef.h"
23 #include "winuser.h"
24 #include "wine/winbase16.h"
25 #include "winerror.h"
26 #include "drive.h"
27 #include "file.h"
28 #include "heap.h"
29 #include "msdos.h"
30 #include "syslevel.h"
31 #include "server.h"
32 #include "process.h"
33 #include "options.h"
34 #include "debug.h"
36 DECLARE_DEBUG_CHANNEL(dosfs)
37 DECLARE_DEBUG_CHANNEL(file)
39 /* Define the VFAT ioctl to get both short and long file names */
40 /* FIXME: is it possible to get this to work on other systems? */
41 #ifdef linux
42 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, long)
43 /* We want the real kernel dirent structure, not the libc one */
44 typedef struct
46 long d_ino;
47 long d_off;
48 unsigned short d_reclen;
49 char d_name[256];
50 } KERNEL_DIRENT;
52 #else /* linux */
53 #undef VFAT_IOCTL_READDIR_BOTH /* just in case... */
54 #endif /* linux */
56 /* Chars we don't want to see in DOS file names */
57 #define INVALID_DOS_CHARS "*?<>|\"+=,;[] \345"
59 static const DOS_DEVICE DOSFS_Devices[] =
60 /* name, device flags (see Int 21/AX=0x4400) */
62 { "CON", 0xc0d3 },
63 { "PRN", 0xa0c0 },
64 { "NUL", 0x80c4 },
65 { "AUX", 0x80c0 },
66 { "LPT1", 0xa0c0 },
67 { "LPT2", 0xa0c0 },
68 { "LPT3", 0xa0c0 },
69 { "LPT4", 0xc0d3 },
70 { "COM1", 0x80c0 },
71 { "COM2", 0x80c0 },
72 { "COM3", 0x80c0 },
73 { "COM4", 0x80c0 },
74 { "SCSIMGR$", 0xc0c0 },
75 { "HPSCAN", 0xc0c0 }
78 #define GET_DRIVE(path) \
79 (((path)[1] == ':') ? toupper((path)[0]) - 'A' : DOSFS_CurDrive)
81 /* Directory info for DOSFS_ReadDir */
82 typedef struct
84 DIR *dir;
85 #ifdef VFAT_IOCTL_READDIR_BOTH
86 int fd;
87 char short_name[12];
88 KERNEL_DIRENT dirent[2];
89 #endif
90 } DOS_DIR;
92 /* Info structure for FindFirstFile handle */
93 typedef struct
95 LPSTR path;
96 LPSTR long_mask;
97 LPSTR short_mask;
98 BYTE attr;
99 int drive;
100 int cur_pos;
101 DOS_DIR *dir;
102 } FIND_FIRST_INFO;
106 /***********************************************************************
107 * DOSFS_ValidDOSName
109 * Return 1 if Unix file 'name' is also a valid MS-DOS name
110 * (i.e. contains only valid DOS chars, lower-case only, fits in 8.3 format).
111 * File name can be terminated by '\0', '\\' or '/'.
113 static int DOSFS_ValidDOSName( const char *name, int ignore_case )
115 static const char invalid_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" INVALID_DOS_CHARS;
116 const char *p = name;
117 const char *invalid = ignore_case ? (invalid_chars + 26) : invalid_chars;
118 int len = 0;
120 if (*p == '.')
122 /* Check for "." and ".." */
123 p++;
124 if (*p == '.') p++;
125 /* All other names beginning with '.' are invalid */
126 return (IS_END_OF_NAME(*p));
128 while (!IS_END_OF_NAME(*p))
130 if (strchr( invalid, *p )) return 0; /* Invalid char */
131 if (*p == '.') break; /* Start of the extension */
132 if (++len > 8) return 0; /* Name too long */
133 p++;
135 if (*p != '.') return 1; /* End of name */
136 p++;
137 if (IS_END_OF_NAME(*p)) return 0; /* Empty extension not allowed */
138 len = 0;
139 while (!IS_END_OF_NAME(*p))
141 if (strchr( invalid, *p )) return 0; /* Invalid char */
142 if (*p == '.') return 0; /* Second extension not allowed */
143 if (++len > 3) return 0; /* Extension too long */
144 p++;
146 return 1;
150 /***********************************************************************
151 * DOSFS_ToDosFCBFormat
153 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
154 * expanding wild cards and converting to upper-case in the process.
155 * File name can be terminated by '\0', '\\' or '/'.
156 * Return FALSE if the name is not a valid DOS name.
157 * 'buffer' must be at least 12 characters long.
159 BOOL DOSFS_ToDosFCBFormat( LPCSTR name, LPSTR buffer )
161 static const char invalid_chars[] = INVALID_DOS_CHARS;
162 const char *p = name;
163 int i;
165 /* Check for "." and ".." */
166 if (*p == '.')
168 p++;
169 strcpy( buffer, ". " );
170 if (*p == '.')
172 buffer[1] = '.';
173 p++;
175 return (!*p || (*p == '/') || (*p == '\\'));
178 for (i = 0; i < 8; i++)
180 switch(*p)
182 case '\0':
183 case '\\':
184 case '/':
185 case '.':
186 buffer[i] = ' ';
187 break;
188 case '?':
189 p++;
190 /* fall through */
191 case '*':
192 buffer[i] = '?';
193 break;
194 default:
195 if (strchr( invalid_chars, *p )) return FALSE;
196 buffer[i] = toupper(*p);
197 p++;
198 break;
202 if (*p == '*')
204 /* Skip all chars after wildcard up to first dot */
205 while (*p && (*p != '/') && (*p != '\\') && (*p != '.')) p++;
207 else
209 /* Check if name too long */
210 if (*p && (*p != '/') && (*p != '\\') && (*p != '.')) return FALSE;
212 if (*p == '.') p++; /* Skip dot */
214 for (i = 8; i < 11; i++)
216 switch(*p)
218 case '\0':
219 case '\\':
220 case '/':
221 buffer[i] = ' ';
222 break;
223 case '.':
224 return FALSE; /* Second extension not allowed */
225 case '?':
226 p++;
227 /* fall through */
228 case '*':
229 buffer[i] = '?';
230 break;
231 default:
232 if (strchr( invalid_chars, *p )) return FALSE;
233 buffer[i] = toupper(*p);
234 p++;
235 break;
238 buffer[11] = '\0';
239 return TRUE;
243 /***********************************************************************
244 * DOSFS_ToDosDTAFormat
246 * Convert a file name from FCB to DTA format (name.ext, null-terminated)
247 * converting to upper-case in the process.
248 * File name can be terminated by '\0', '\\' or '/'.
249 * 'buffer' must be at least 13 characters long.
251 static void DOSFS_ToDosDTAFormat( LPCSTR name, LPSTR buffer )
253 char *p;
255 memcpy( buffer, name, 8 );
256 for (p = buffer + 8; (p > buffer) && (p[-1] == ' '); p--);
257 *p++ = '.';
258 memcpy( p, name + 8, 3 );
259 for (p += 3; p[-1] == ' '; p--);
260 if (p[-1] == '.') p--;
261 *p = '\0';
265 /***********************************************************************
266 * DOSFS_MatchShort
268 * Check a DOS file name against a mask (both in FCB format).
270 static int DOSFS_MatchShort( const char *mask, const char *name )
272 int i;
273 for (i = 11; i > 0; i--, mask++, name++)
274 if ((*mask != '?') && (*mask != *name)) return 0;
275 return 1;
279 /***********************************************************************
280 * DOSFS_MatchLong
282 * Check a long file name against a mask.
284 static int DOSFS_MatchLong( const char *mask, const char *name,
285 int case_sensitive )
287 if (!strcmp( mask, "*.*" )) return 1;
288 while (*name && *mask)
290 if (*mask == '*')
292 mask++;
293 while (*mask == '*') mask++; /* Skip consecutive '*' */
294 if (!*mask) return 1;
295 if (case_sensitive) while (*name && (*name != *mask)) name++;
296 else while (*name && (toupper(*name) != toupper(*mask))) name++;
297 if (!*name) break;
299 else if (*mask != '?')
301 if (case_sensitive)
303 if (*mask != *name) return 0;
305 else if (toupper(*mask) != toupper(*name)) return 0;
307 mask++;
308 name++;
310 if (*mask == '.') mask++; /* Ignore trailing '.' in mask */
311 return (!*name && !*mask);
315 /***********************************************************************
316 * DOSFS_OpenDir
318 static DOS_DIR *DOSFS_OpenDir( LPCSTR path )
320 DOS_DIR *dir = HeapAlloc( SystemHeap, 0, sizeof(*dir) );
321 if (!dir)
323 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
324 return NULL;
327 /* Treat empty path as root directory. This simplifies path split into
328 directory and mask in several other places */
329 if (!*path) path = "/";
331 #ifdef VFAT_IOCTL_READDIR_BOTH
333 /* Check if the VFAT ioctl is supported on this directory */
335 if ((dir->fd = open( path, O_RDONLY )) != -1)
337 if (ioctl( dir->fd, VFAT_IOCTL_READDIR_BOTH, (long)dir->dirent ) == -1)
339 close( dir->fd );
340 dir->fd = -1;
342 else
344 /* Set the file pointer back at the start of the directory */
345 lseek( dir->fd, 0, SEEK_SET );
346 dir->dir = NULL;
347 return dir;
350 #endif /* VFAT_IOCTL_READDIR_BOTH */
352 /* Now use the standard opendir/readdir interface */
354 if (!(dir->dir = opendir( path )))
356 HeapFree( SystemHeap, 0, dir );
357 return NULL;
359 return dir;
363 /***********************************************************************
364 * DOSFS_CloseDir
366 static void DOSFS_CloseDir( DOS_DIR *dir )
368 #ifdef VFAT_IOCTL_READDIR_BOTH
369 if (dir->fd != -1) close( dir->fd );
370 #endif /* VFAT_IOCTL_READDIR_BOTH */
371 if (dir->dir) closedir( dir->dir );
372 HeapFree( SystemHeap, 0, dir );
376 /***********************************************************************
377 * DOSFS_ReadDir
379 static BOOL DOSFS_ReadDir( DOS_DIR *dir, LPCSTR *long_name,
380 LPCSTR *short_name )
382 struct dirent *dirent;
384 #ifdef VFAT_IOCTL_READDIR_BOTH
385 if (dir->fd != -1)
387 if (ioctl( dir->fd, VFAT_IOCTL_READDIR_BOTH, (long)dir->dirent ) != -1) {
388 if (!dir->dirent[0].d_reclen) return FALSE;
389 if (!DOSFS_ToDosFCBFormat( dir->dirent[0].d_name, dir->short_name ))
390 dir->short_name[0] = '\0';
391 *short_name = dir->short_name;
392 if (dir->dirent[1].d_name[0]) *long_name = dir->dirent[1].d_name;
393 else *long_name = dir->dirent[0].d_name;
394 return TRUE;
397 #endif /* VFAT_IOCTL_READDIR_BOTH */
399 if (!(dirent = readdir( dir->dir ))) return FALSE;
400 *long_name = dirent->d_name;
401 *short_name = NULL;
402 return TRUE;
406 /***********************************************************************
407 * DOSFS_Hash
409 * Transform a Unix file name into a hashed DOS name. If the name is a valid
410 * DOS name, it is converted to upper-case; otherwise it is replaced by a
411 * hashed version that fits in 8.3 format.
412 * File name can be terminated by '\0', '\\' or '/'.
413 * 'buffer' must be at least 13 characters long.
415 static void DOSFS_Hash( LPCSTR name, LPSTR buffer, BOOL dir_format,
416 BOOL ignore_case )
418 static const char invalid_chars[] = INVALID_DOS_CHARS "~.";
419 static const char hash_chars[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
421 const char *p, *ext;
422 char *dst;
423 unsigned short hash;
424 int i;
426 if (dir_format) strcpy( buffer, " " );
428 if (DOSFS_ValidDOSName( name, ignore_case ))
430 /* Check for '.' and '..' */
431 if (*name == '.')
433 buffer[0] = '.';
434 if (!dir_format) buffer[1] = buffer[2] = '\0';
435 if (name[1] == '.') buffer[1] = '.';
436 return;
439 /* Simply copy the name, converting to uppercase */
441 for (dst = buffer; !IS_END_OF_NAME(*name) && (*name != '.'); name++)
442 *dst++ = toupper(*name);
443 if (*name == '.')
445 if (dir_format) dst = buffer + 8;
446 else *dst++ = '.';
447 for (name++; !IS_END_OF_NAME(*name); name++)
448 *dst++ = toupper(*name);
450 if (!dir_format) *dst = '\0';
451 return;
454 /* Compute the hash code of the file name */
455 /* If you know something about hash functions, feel free to */
456 /* insert a better algorithm here... */
457 if (ignore_case)
459 for (p = name, hash = 0xbeef; !IS_END_OF_NAME(p[1]); p++)
460 hash = (hash<<3) ^ (hash>>5) ^ tolower(*p) ^ (tolower(p[1]) << 8);
461 hash = (hash<<3) ^ (hash>>5) ^ tolower(*p); /* Last character*/
463 else
465 for (p = name, hash = 0xbeef; !IS_END_OF_NAME(p[1]); p++)
466 hash = (hash << 3) ^ (hash >> 5) ^ *p ^ (p[1] << 8);
467 hash = (hash << 3) ^ (hash >> 5) ^ *p; /* Last character */
470 /* Find last dot for start of the extension */
471 for (p = name+1, ext = NULL; !IS_END_OF_NAME(*p); p++)
472 if (*p == '.') ext = p;
473 if (ext && IS_END_OF_NAME(ext[1]))
474 ext = NULL; /* Empty extension ignored */
476 /* Copy first 4 chars, replacing invalid chars with '_' */
477 for (i = 4, p = name, dst = buffer; i > 0; i--, p++)
479 if (IS_END_OF_NAME(*p) || (p == ext)) break;
480 *dst++ = strchr( invalid_chars, *p ) ? '_' : toupper(*p);
482 /* Pad to 5 chars with '~' */
483 while (i-- >= 0) *dst++ = '~';
485 /* Insert hash code converted to 3 ASCII chars */
486 *dst++ = hash_chars[(hash >> 10) & 0x1f];
487 *dst++ = hash_chars[(hash >> 5) & 0x1f];
488 *dst++ = hash_chars[hash & 0x1f];
490 /* Copy the first 3 chars of the extension (if any) */
491 if (ext)
493 if (!dir_format) *dst++ = '.';
494 for (i = 3, ext++; (i > 0) && !IS_END_OF_NAME(*ext); i--, ext++)
495 *dst++ = strchr( invalid_chars, *ext ) ? '_' : toupper(*ext);
497 if (!dir_format) *dst = '\0';
501 /***********************************************************************
502 * DOSFS_FindUnixName
504 * Find the Unix file name in a given directory that corresponds to
505 * a file name (either in Unix or DOS format).
506 * File name can be terminated by '\0', '\\' or '/'.
507 * Return TRUE if OK, FALSE if no file name matches.
509 * 'long_buf' must be at least 'long_len' characters long. If the long name
510 * turns out to be larger than that, the function returns FALSE.
511 * 'short_buf' must be at least 13 characters long.
513 BOOL DOSFS_FindUnixName( LPCSTR path, LPCSTR name, LPSTR long_buf,
514 INT long_len, LPSTR short_buf, BOOL ignore_case)
516 DOS_DIR *dir;
517 LPCSTR long_name, short_name;
518 char dos_name[12], tmp_buf[13];
519 BOOL ret;
521 const char *p = strchr( name, '/' );
522 int len = p ? (int)(p - name) : strlen(name);
523 if ((p = strchr( name, '\\' ))) len = MIN( (int)(p - name), len );
524 if (long_len < len + 1) return FALSE;
526 TRACE(dosfs, "%s,%s\n", path, name );
528 if (!DOSFS_ToDosFCBFormat( name, dos_name )) dos_name[0] = '\0';
530 if (!(dir = DOSFS_OpenDir( path )))
532 WARN(dosfs, "(%s,%s): can't open dir: %s\n",
533 path, name, strerror(errno) );
534 return FALSE;
537 while ((ret = DOSFS_ReadDir( dir, &long_name, &short_name )))
539 /* Check against Unix name */
540 if (len == strlen(long_name))
542 if (!ignore_case)
544 if (!lstrncmpA( long_name, name, len )) break;
546 else
548 if (!lstrncmpiA( long_name, name, len )) break;
551 if (dos_name[0])
553 /* Check against hashed DOS name */
554 if (!short_name)
556 DOSFS_Hash( long_name, tmp_buf, TRUE, ignore_case );
557 short_name = tmp_buf;
559 if (!strcmp( dos_name, short_name )) break;
562 if (ret)
564 if (long_buf) strcpy( long_buf, long_name );
565 if (short_buf)
567 if (short_name)
568 DOSFS_ToDosDTAFormat( short_name, short_buf );
569 else
570 DOSFS_Hash( long_name, short_buf, FALSE, ignore_case );
572 TRACE(dosfs, "(%s,%s) -> %s (%s)\n",
573 path, name, long_name, short_buf ? short_buf : "***");
575 else
576 WARN(dosfs, "'%s' not found in '%s'\n", name, path);
577 DOSFS_CloseDir( dir );
578 return ret;
582 /***********************************************************************
583 * DOSFS_GetDevice
585 * Check if a DOS file name represents a DOS device and return the device.
587 const DOS_DEVICE *DOSFS_GetDevice( const char *name )
589 int i;
590 const char *p;
592 if (!name) return NULL; /* if FILE_DupUnixHandle was used */
593 if (name[0] && (name[1] == ':')) name += 2;
594 if ((p = strrchr( name, '/' ))) name = p + 1;
595 if ((p = strrchr( name, '\\' ))) name = p + 1;
596 for (i = 0; i < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0]); i++)
598 const char *dev = DOSFS_Devices[i].name;
599 if (!lstrncmpiA( dev, name, strlen(dev) ))
601 p = name + strlen( dev );
602 if (!*p || (*p == '.')) return &DOSFS_Devices[i];
605 return NULL;
609 /***********************************************************************
610 * DOSFS_GetDeviceByHandle
612 const DOS_DEVICE *DOSFS_GetDeviceByHandle( HFILE hFile )
614 struct get_file_info_request req;
615 struct get_file_info_reply reply;
617 req.handle = hFile;
618 CLIENT_SendRequest( REQ_GET_FILE_INFO, -1, 1, &req, sizeof(req) );
619 if (!CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ) &&
620 (reply.type == FILE_TYPE_UNKNOWN))
622 if ((reply.attr >= 0) &&
623 (reply.attr < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0])))
624 return &DOSFS_Devices[reply.attr];
626 return NULL;
630 /***********************************************************************
631 * DOSFS_OpenDevice
633 * Open a DOS device. This might not map 1:1 into the UNIX device concept.
635 HFILE DOSFS_OpenDevice( const char *name, DWORD access )
637 int i;
638 const char *p;
640 if (!name) return (HFILE)NULL; /* if FILE_DupUnixHandle was used */
641 if (name[0] && (name[1] == ':')) name += 2;
642 if ((p = strrchr( name, '/' ))) name = p + 1;
643 if ((p = strrchr( name, '\\' ))) name = p + 1;
644 for (i = 0; i < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0]); i++)
646 const char *dev = DOSFS_Devices[i].name;
647 if (!lstrncmpiA( dev, name, strlen(dev) ))
649 p = name + strlen( dev );
650 if (!*p || (*p == '.')) {
651 /* got it */
652 if (!strcmp(DOSFS_Devices[i].name,"NUL"))
653 return FILE_CreateFile( "/dev/null", access,
654 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
655 OPEN_EXISTING, 0, -1 );
656 if (!strcmp(DOSFS_Devices[i].name,"CON")) {
657 HFILE to_dup;
658 HFILE handle;
659 switch (access & (GENERIC_READ|GENERIC_WRITE)) {
660 case GENERIC_READ:
661 to_dup = GetStdHandle( STD_INPUT_HANDLE );
662 break;
663 case GENERIC_WRITE:
664 to_dup = GetStdHandle( STD_OUTPUT_HANDLE );
665 break;
666 default:
667 FIXME(dosfs,"can't open CON read/write\n");
668 return HFILE_ERROR;
669 break;
671 if (!DuplicateHandle( GetCurrentProcess(), to_dup, GetCurrentProcess(),
672 &handle, 0, FALSE, DUPLICATE_SAME_ACCESS ))
673 handle = HFILE_ERROR;
674 return handle;
676 if (!strcmp(DOSFS_Devices[i].name,"SCSIMGR$") ||
677 !strcmp(DOSFS_Devices[i].name,"HPSCAN"))
679 return FILE_CreateDevice( i, access, NULL );
682 HFILE r;
683 char devname[40];
684 PROFILE_GetWineIniString("serialports",name,"",devname,sizeof devname);
686 if(devname[0])
688 TRACE(file,"DOSFS_OpenDevice %s is %s\n",
689 DOSFS_Devices[i].name,devname);
690 r = FILE_CreateFile( devname, access,
691 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
692 OPEN_EXISTING, 0, -1 );
693 TRACE(file,"Create_File return %08X\n",r);
694 return r;
698 FIXME(dosfs,"device open %s not supported (yet)\n",DOSFS_Devices[i].name);
699 return HFILE_ERROR;
703 return HFILE_ERROR;
707 /***********************************************************************
708 * DOSFS_GetPathDrive
710 * Get the drive specified by a given path name (DOS or Unix format).
712 static int DOSFS_GetPathDrive( const char **name )
714 int drive;
715 const char *p = *name;
717 if (*p && (p[1] == ':'))
719 drive = toupper(*p) - 'A';
720 *name += 2;
722 else if (*p == '/') /* Absolute Unix path? */
724 if ((drive = DRIVE_FindDriveRoot( name )) == -1)
726 MSG("Warning: %s not accessible from a DOS drive\n", *name );
727 /* Assume it really was a DOS name */
728 drive = DRIVE_GetCurrentDrive();
731 else drive = DRIVE_GetCurrentDrive();
733 if (!DRIVE_IsValid(drive))
735 SetLastError( ERROR_INVALID_DRIVE );
736 return -1;
738 return drive;
742 /***********************************************************************
743 * DOSFS_GetFullName
745 * Convert a file name (DOS or mixed DOS/Unix format) to a valid
746 * Unix name / short DOS name pair.
747 * Return FALSE if one of the path components does not exist. The last path
748 * component is only checked if 'check_last' is non-zero.
749 * The buffers pointed to by 'long_buf' and 'short_buf' must be
750 * at least MAX_PATHNAME_LEN long.
752 BOOL DOSFS_GetFullName( LPCSTR name, BOOL check_last, DOS_FULL_NAME *full )
754 BOOL found;
755 UINT flags;
756 char *p_l, *p_s, *root;
758 TRACE(dosfs, "%s (last=%d)\n",
759 name, check_last );
761 if ((full->drive = DOSFS_GetPathDrive( &name )) == -1) return FALSE;
762 flags = DRIVE_GetFlags( full->drive );
764 lstrcpynA( full->long_name, DRIVE_GetRoot( full->drive ),
765 sizeof(full->long_name) );
766 if (full->long_name[1]) root = full->long_name + strlen(full->long_name);
767 else root = full->long_name; /* root directory */
769 strcpy( full->short_name, "A:\\" );
770 full->short_name[0] += full->drive;
772 if ((*name == '\\') || (*name == '/')) /* Absolute path */
774 while ((*name == '\\') || (*name == '/')) name++;
776 else /* Relative path */
778 lstrcpynA( root + 1, DRIVE_GetUnixCwd( full->drive ),
779 sizeof(full->long_name) - (root - full->long_name) - 1 );
780 if (root[1]) *root = '/';
781 lstrcpynA( full->short_name + 3, DRIVE_GetDosCwd( full->drive ),
782 sizeof(full->short_name) - 3 );
785 p_l = full->long_name[1] ? full->long_name + strlen(full->long_name)
786 : full->long_name;
787 p_s = full->short_name[3] ? full->short_name + strlen(full->short_name)
788 : full->short_name + 2;
789 found = TRUE;
791 while (*name && found)
793 /* Check for '.' and '..' */
795 if (*name == '.')
797 if (IS_END_OF_NAME(name[1]))
799 name++;
800 while ((*name == '\\') || (*name == '/')) name++;
801 continue;
803 else if ((name[1] == '.') && IS_END_OF_NAME(name[2]))
805 name += 2;
806 while ((*name == '\\') || (*name == '/')) name++;
807 while ((p_l > root) && (*p_l != '/')) p_l--;
808 while ((p_s > full->short_name + 2) && (*p_s != '\\')) p_s--;
809 *p_l = *p_s = '\0'; /* Remove trailing separator */
810 continue;
814 /* Make sure buffers are large enough */
816 if ((p_s >= full->short_name + sizeof(full->short_name) - 14) ||
817 (p_l >= full->long_name + sizeof(full->long_name) - 1))
819 SetLastError( ERROR_PATH_NOT_FOUND );
820 return FALSE;
823 /* Get the long and short name matching the file name */
825 if ((found = DOSFS_FindUnixName( full->long_name, name, p_l + 1,
826 sizeof(full->long_name) - (p_l - full->long_name) - 1,
827 p_s + 1, !(flags & DRIVE_CASE_SENSITIVE) )))
829 *p_l++ = '/';
830 p_l += strlen(p_l);
831 *p_s++ = '\\';
832 p_s += strlen(p_s);
833 while (!IS_END_OF_NAME(*name)) name++;
835 else if (!check_last)
837 *p_l++ = '/';
838 *p_s++ = '\\';
839 while (!IS_END_OF_NAME(*name) &&
840 (p_s < full->short_name + sizeof(full->short_name) - 1) &&
841 (p_l < full->long_name + sizeof(full->long_name) - 1))
843 *p_s++ = tolower(*name);
844 /* If the drive is case-sensitive we want to create new */
845 /* files in lower-case otherwise we can't reopen them */
846 /* under the same short name. */
847 if (flags & DRIVE_CASE_SENSITIVE) *p_l++ = tolower(*name);
848 else *p_l++ = *name;
849 name++;
851 *p_l = *p_s = '\0';
853 while ((*name == '\\') || (*name == '/')) name++;
856 if (!found)
858 if (check_last)
860 SetLastError( ERROR_FILE_NOT_FOUND );
861 return FALSE;
863 if (*name) /* Not last */
865 SetLastError( ERROR_PATH_NOT_FOUND );
866 return FALSE;
869 if (!full->long_name[0]) strcpy( full->long_name, "/" );
870 if (!full->short_name[2]) strcpy( full->short_name + 2, "\\" );
871 TRACE(dosfs, "returning %s = %s\n",
872 full->long_name, full->short_name );
873 return TRUE;
877 /***********************************************************************
878 * GetShortPathNameA (KERNEL32.271)
880 * NOTES
881 * observed:
882 * longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
883 * *longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
885 * more observations ( with NT 3.51 (WinDD) ):
886 * longpath <= 8.3 -> just copy longpath to shortpath
887 * longpath > 8.3 ->
888 * a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
889 * b) file does exist -> set the short filename.
890 * - trailing slashes are reproduced in the short name, even if the
891 * file is not a directory
892 * - the absolute/relative path of the short name is reproduced in the
893 * same way, like the long name
894 * - longpath and shortpath may have the same adress
895 * Peter Ganten, 1999
897 DWORD WINAPI GetShortPathNameA( LPCSTR longpath, LPSTR shortpath,
898 DWORD shortlen )
900 DOS_FULL_NAME full_name;
901 LPSTR tmpshortpath;
902 DWORD length = 0, pos = 0;
903 INT start=-1, end=-1, tmplen;
905 if (!longpath) {
906 SetLastError(ERROR_INVALID_PARAMETER);
907 return 0;
909 if (!longpath[0]) {
910 SetLastError(ERROR_BAD_PATHNAME);
911 return 0;
914 tmpshortpath = HeapAlloc( GetProcessHeap(), 0, MAX_PATHNAME_LEN );
915 if ( !tmpshortpath ) {
916 SetLastError ( ERROR_NOT_ENOUGH_MEMORY );
917 return 0;
920 /* Check for Drive-Letter */
921 if ( longpath[1] == ':' ) {
922 lstrcpynA ( tmpshortpath, longpath, 3 );
923 length = 2;
924 pos = 2;
927 /* loop over each part of the name */
928 while ( longpath[pos] ) {
930 if (( longpath[pos] == '\\' ) ||
931 ( longpath[pos+1] == '\0' ) ||
932 ( longpath[pos] == '/')) {
934 if ( start != -1 ) {
935 if ( DOSFS_ValidDOSName ( longpath + start, TRUE )) {
936 tmplen = end - start + ( (( longpath[pos] == '\\' ) || ( longpath[pos] == '/' )) ? 1 : 2 );
937 lstrcpynA ( tmpshortpath+length, longpath+start, tmplen );
938 length += tmplen - 1;
940 else {
941 DOSFS_Hash ( longpath + start, tmpshortpath+length, FALSE, FALSE );
942 length = lstrlenA ( tmpshortpath );
944 /* Check if the path, up to this point exists */
945 if ( !DOSFS_GetFullName ( tmpshortpath, TRUE, &full_name ) ) {
946 SetLastError ( ERROR_FILE_NOT_FOUND );
947 return 0;
953 if (( longpath[pos] == '\\' ) || ( longpath[pos] == '/' )) {
954 tmpshortpath[length] = '\\';
955 tmpshortpath[length+1]='\0';
956 length++;
958 pos++;
960 start = -1;
961 end = -1;
962 continue;
965 if ( start == -1 ) {
966 start = pos;
968 pos++;
969 end = pos;
972 lstrcpynA ( shortpath, tmpshortpath, shortlen );
973 length = lstrlenA ( tmpshortpath );
974 HeapFree ( GetProcessHeap(), 0, tmpshortpath );
976 return length;
980 /***********************************************************************
981 * GetShortPathName32W (KERNEL32.272)
983 DWORD WINAPI GetShortPathNameW( LPCWSTR longpath, LPWSTR shortpath,
984 DWORD shortlen )
986 LPSTR longpathA, shortpathA;
987 DWORD ret = 0;
989 longpathA = HEAP_strdupWtoA( GetProcessHeap(), 0, longpath );
990 shortpathA = HEAP_xalloc ( GetProcessHeap(), 0, shortlen );
992 ret = GetShortPathNameA ( longpathA, shortpathA, shortlen );
993 lstrcpynAtoW ( shortpath, shortpathA, shortlen );
995 HeapFree( GetProcessHeap(), 0, longpathA );
996 HeapFree( GetProcessHeap(), 0, shortpathA );
998 return ret;
1002 /***********************************************************************
1003 * GetLongPathName32A (KERNEL32.xxx)
1005 DWORD WINAPI GetLongPathNameA( LPCSTR shortpath, LPSTR longpath,
1006 DWORD longlen )
1008 DOS_FULL_NAME full_name;
1009 char *p;
1010 char *longfilename;
1011 DWORD shortpathlen;
1013 if (!DOSFS_GetFullName( shortpath, TRUE, &full_name )) return 0;
1014 lstrcpynA( longpath, full_name.short_name, longlen );
1015 /* Do some hackery to get the long filename.
1016 * FIXME: Would be better if it returned the
1017 * long version of the directories too
1019 longfilename = strrchr(full_name.long_name, '/')+1;
1020 if (longpath != NULL) {
1021 if ((p = strrchr( longpath, '\\' )) != NULL) {
1022 p++;
1023 longlen -= (p-longpath);
1024 lstrcpynA( p, longfilename , longlen);
1027 shortpathlen =
1028 ((strrchr( full_name.short_name, '\\' ) - full_name.short_name) + 1);
1029 return shortpathlen + strlen( longfilename );
1033 /***********************************************************************
1034 * GetLongPathName32W (KERNEL32.269)
1036 DWORD WINAPI GetLongPathNameW( LPCWSTR shortpath, LPWSTR longpath,
1037 DWORD longlen )
1039 DOS_FULL_NAME full_name;
1040 DWORD ret = 0;
1041 LPSTR shortpathA = HEAP_strdupWtoA( GetProcessHeap(), 0, shortpath );
1043 /* FIXME: is it correct to always return a fully qualified short path? */
1044 if (DOSFS_GetFullName( shortpathA, TRUE, &full_name ))
1046 ret = strlen( full_name.short_name );
1047 lstrcpynAtoW( longpath, full_name.long_name, longlen );
1049 HeapFree( GetProcessHeap(), 0, shortpathA );
1050 return ret;
1054 /***********************************************************************
1055 * DOSFS_DoGetFullPathName
1057 * Implementation of GetFullPathName32A/W.
1059 static DWORD DOSFS_DoGetFullPathName( LPCSTR name, DWORD len, LPSTR result,
1060 BOOL unicode )
1062 char buffer[MAX_PATHNAME_LEN];
1063 int drive;
1064 char *p;
1065 DWORD ret;
1067 /* last possible position for a char != 0 */
1068 char *endchar = buffer + sizeof(buffer) - 2;
1069 *endchar = '\0';
1071 TRACE(dosfs, "converting '%s'\n", name );
1073 if (!name || !result || ((drive = DOSFS_GetPathDrive( &name )) == -1) )
1074 { SetLastError( ERROR_INVALID_PARAMETER );
1075 return 0;
1078 p = buffer;
1079 *p++ = 'A' + drive;
1080 *p++ = ':';
1081 if (IS_END_OF_NAME(*name) && (*name)) /* Absolute path */
1083 while (((*name == '\\') || (*name == '/')) && (!*endchar) )
1084 *p++ = *name++;
1086 else /* Relative path or empty path */
1088 *p++ = '\\';
1089 lstrcpynA( p, DRIVE_GetDosCwd(drive), sizeof(buffer) - 4 );
1090 if ( *p )
1092 p += strlen(p);
1093 *p++ = '\\';
1096 *p = '\0';
1098 while (*name)
1100 if (*name == '.')
1102 if (IS_END_OF_NAME(name[1]))
1104 name++;
1105 while ((*name == '\\') || (*name == '/')) name++;
1106 continue;
1108 else if ((name[1] == '.') && IS_END_OF_NAME(name[2]))
1110 name += 2;
1111 while ((*name == '\\') || (*name == '/')) name++;
1113 if (p < buffer + 3) /* no previous dir component */
1114 continue;
1115 p--; /* skip previously added '\\' */
1116 while ((*p == '\\') || (*p == '/')) p--;
1117 /* skip previous dir component */
1118 while ((*p != '\\') && (*p != '/')) p--;
1119 p++;
1120 continue;
1123 if ( *endchar )
1124 { SetLastError( ERROR_PATH_NOT_FOUND );
1125 return 0;
1127 while (!IS_END_OF_NAME(*name) && (!*endchar) )
1128 *p++ = *name++;
1129 while (((*name == '\\') || (*name == '/')) && (!*endchar) )
1130 *p++ = *name++;
1132 *p = '\0';
1134 if (!(DRIVE_GetFlags(drive) & DRIVE_CASE_PRESERVING))
1135 CharUpperA( buffer );
1137 if (unicode)
1138 lstrcpynAtoW( (LPWSTR)result, buffer, len );
1139 else
1140 lstrcpynA( result, buffer, len );
1142 TRACE(dosfs, "returning '%s'\n", buffer );
1144 /* If the lpBuffer buffer is too small, the return value is the
1145 size of the buffer, in characters, required to hold the path. */
1147 ret = strlen(buffer);
1149 if (ret >= len )
1150 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1152 return ret;
1156 /***********************************************************************
1157 * GetFullPathName32A (KERNEL32.272)
1158 * NOTES
1159 * if the path closed with '\', *lastpart is 0
1161 DWORD WINAPI GetFullPathNameA( LPCSTR name, DWORD len, LPSTR buffer,
1162 LPSTR *lastpart )
1164 DWORD ret = DOSFS_DoGetFullPathName( name, len, buffer, FALSE );
1165 if (ret && lastpart)
1167 LPSTR p = buffer + strlen(buffer);
1169 if (*p != '\\')
1171 while ((p > buffer + 2) && (*p != '\\')) p--;
1172 *lastpart = p + 1;
1174 else *lastpart = NULL;
1176 return ret;
1180 /***********************************************************************
1181 * GetFullPathName32W (KERNEL32.273)
1183 DWORD WINAPI GetFullPathNameW( LPCWSTR name, DWORD len, LPWSTR buffer,
1184 LPWSTR *lastpart )
1186 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
1187 DWORD ret = DOSFS_DoGetFullPathName( nameA, len, (LPSTR)buffer, TRUE );
1188 HeapFree( GetProcessHeap(), 0, nameA );
1189 if (ret && lastpart)
1191 LPWSTR p = buffer + lstrlenW(buffer);
1192 if (*p != (WCHAR)'\\')
1194 while ((p > buffer + 2) && (*p != (WCHAR)'\\')) p--;
1195 *lastpart = p + 1;
1197 else *lastpart = NULL;
1199 return ret;
1202 /***********************************************************************
1203 * DOSFS_FindNextEx
1205 static int DOSFS_FindNextEx( FIND_FIRST_INFO *info, WIN32_FIND_DATAA *entry )
1207 BYTE attr = info->attr | FA_UNUSED | FA_ARCHIVE | FA_RDONLY;
1208 UINT flags = DRIVE_GetFlags( info->drive );
1209 char *p, buffer[MAX_PATHNAME_LEN];
1210 const char *drive_path;
1211 int drive_root;
1212 LPCSTR long_name, short_name;
1213 BY_HANDLE_FILE_INFORMATION fileinfo;
1214 char dos_name[13];
1216 if ((info->attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
1218 if (info->cur_pos) return 0;
1219 entry->dwFileAttributes = FILE_ATTRIBUTE_LABEL;
1220 DOSFS_UnixTimeToFileTime( (time_t)0, &entry->ftCreationTime, 0 );
1221 DOSFS_UnixTimeToFileTime( (time_t)0, &entry->ftLastAccessTime, 0 );
1222 DOSFS_UnixTimeToFileTime( (time_t)0, &entry->ftLastWriteTime, 0 );
1223 entry->nFileSizeHigh = 0;
1224 entry->nFileSizeLow = 0;
1225 entry->dwReserved0 = 0;
1226 entry->dwReserved1 = 0;
1227 DOSFS_ToDosDTAFormat( DRIVE_GetLabel( info->drive ), entry->cFileName );
1228 strcpy( entry->cAlternateFileName, entry->cFileName );
1229 info->cur_pos++;
1230 return 1;
1233 drive_path = info->path + strlen(DRIVE_GetRoot( info->drive ));
1234 while ((*drive_path == '/') || (*drive_path == '\\')) drive_path++;
1235 drive_root = !*drive_path;
1237 lstrcpynA( buffer, info->path, sizeof(buffer) - 1 );
1238 strcat( buffer, "/" );
1239 p = buffer + strlen(buffer);
1241 while (DOSFS_ReadDir( info->dir, &long_name, &short_name ))
1243 info->cur_pos++;
1245 /* Don't return '.' and '..' in the root of the drive */
1246 if (drive_root && (long_name[0] == '.') &&
1247 (!long_name[1] || ((long_name[1] == '.') && !long_name[2])))
1248 continue;
1250 /* Check the long mask */
1252 if (info->long_mask)
1254 if (!DOSFS_MatchLong( info->long_mask, long_name,
1255 flags & DRIVE_CASE_SENSITIVE )) continue;
1258 /* Check the short mask */
1260 if (info->short_mask)
1262 if (!short_name)
1264 DOSFS_Hash( long_name, dos_name, TRUE,
1265 !(flags & DRIVE_CASE_SENSITIVE) );
1266 short_name = dos_name;
1268 if (!DOSFS_MatchShort( info->short_mask, short_name )) continue;
1271 /* Check the file attributes */
1273 lstrcpynA( p, long_name, sizeof(buffer) - (int)(p - buffer) );
1274 if (!FILE_Stat( buffer, &fileinfo ))
1276 WARN(dosfs, "can't stat %s\n", buffer);
1277 continue;
1279 if (fileinfo.dwFileAttributes & ~attr) continue;
1281 /* We now have a matching entry; fill the result and return */
1283 entry->dwFileAttributes = fileinfo.dwFileAttributes;
1284 entry->ftCreationTime = fileinfo.ftCreationTime;
1285 entry->ftLastAccessTime = fileinfo.ftLastAccessTime;
1286 entry->ftLastWriteTime = fileinfo.ftLastWriteTime;
1287 entry->nFileSizeHigh = fileinfo.nFileSizeHigh;
1288 entry->nFileSizeLow = fileinfo.nFileSizeLow;
1290 if (short_name)
1291 DOSFS_ToDosDTAFormat( short_name, entry->cAlternateFileName );
1292 else
1293 DOSFS_Hash( long_name, entry->cAlternateFileName, FALSE,
1294 !(flags & DRIVE_CASE_SENSITIVE) );
1296 lstrcpynA( entry->cFileName, long_name, sizeof(entry->cFileName) );
1297 if (!(flags & DRIVE_CASE_PRESERVING)) CharLowerA( entry->cFileName );
1298 TRACE(dosfs, "returning %s (%s) %02lx %ld\n",
1299 entry->cFileName, entry->cAlternateFileName,
1300 entry->dwFileAttributes, entry->nFileSizeLow );
1301 return 1;
1303 return 0; /* End of directory */
1306 /***********************************************************************
1307 * DOSFS_FindNext
1309 * Find the next matching file. Return the number of entries read to find
1310 * the matching one, or 0 if no more entries.
1311 * 'short_mask' is the 8.3 mask (in FCB format), 'long_mask' is the long
1312 * file name mask. Either or both can be NULL.
1314 * NOTE: This is supposed to be only called by the int21 emulation
1315 * routines. Thus, we should own the Win16Mutex anyway.
1316 * Nevertheless, we explicitly enter it to ensure the static
1317 * directory cache is protected.
1319 int DOSFS_FindNext( const char *path, const char *short_mask,
1320 const char *long_mask, int drive, BYTE attr,
1321 int skip, WIN32_FIND_DATAA *entry )
1323 static FIND_FIRST_INFO info = { NULL };
1324 LPCSTR short_name, long_name;
1325 int count;
1327 SYSLEVEL_EnterWin16Lock();
1329 /* Check the cached directory */
1330 if (!(info.dir && info.path == path && info.short_mask == short_mask
1331 && info.long_mask == long_mask && info.drive == drive
1332 && info.attr == attr && info.cur_pos <= skip))
1334 /* Not in the cache, open it anew */
1335 if (info.dir) DOSFS_CloseDir( info.dir );
1337 info.path = (LPSTR)path;
1338 info.long_mask = (LPSTR)long_mask;
1339 info.short_mask = (LPSTR)short_mask;
1340 info.attr = attr;
1341 info.drive = drive;
1342 info.cur_pos = 0;
1343 info.dir = DOSFS_OpenDir( info.path );
1346 /* Skip to desired position */
1347 while (info.cur_pos < skip)
1348 if (info.dir && DOSFS_ReadDir( info.dir, &long_name, &short_name ))
1349 info.cur_pos++;
1350 else
1351 break;
1353 if (info.dir && info.cur_pos == skip && DOSFS_FindNextEx( &info, entry ))
1354 count = info.cur_pos - skip;
1355 else
1356 count = 0;
1358 if (!count)
1360 if (info.dir) DOSFS_CloseDir( info.dir );
1361 memset( &info, '\0', sizeof(info) );
1364 SYSLEVEL_LeaveWin16Lock();
1366 return count;
1371 /*************************************************************************
1372 * FindFirstFile16 (KERNEL.413)
1374 HANDLE16 WINAPI FindFirstFile16( LPCSTR path, WIN32_FIND_DATAA *data )
1376 DOS_FULL_NAME full_name;
1377 HGLOBAL16 handle;
1378 FIND_FIRST_INFO *info;
1380 data->dwReserved0 = data->dwReserved1 = 0x0;
1381 if (!path) return 0;
1382 if (!DOSFS_GetFullName( path, FALSE, &full_name ))
1383 return INVALID_HANDLE_VALUE16;
1384 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE, sizeof(FIND_FIRST_INFO) )))
1385 return INVALID_HANDLE_VALUE16;
1386 info = (FIND_FIRST_INFO *)GlobalLock16( handle );
1387 info->path = HEAP_strdupA( SystemHeap, 0, full_name.long_name );
1388 info->long_mask = strrchr( info->path, '/' );
1389 *(info->long_mask++) = '\0';
1390 info->short_mask = NULL;
1391 info->attr = 0xff;
1392 if (path[0] && (path[1] == ':')) info->drive = toupper(*path) - 'A';
1393 else info->drive = DRIVE_GetCurrentDrive();
1394 info->cur_pos = 0;
1396 info->dir = DOSFS_OpenDir( info->path );
1398 GlobalUnlock16( handle );
1399 if (!FindNextFile16( handle, data ))
1401 FindClose16( handle );
1402 SetLastError( ERROR_NO_MORE_FILES );
1403 return INVALID_HANDLE_VALUE16;
1405 return handle;
1409 /*************************************************************************
1410 * FindFirstFile32A (KERNEL32.123)
1412 HANDLE WINAPI FindFirstFileA( LPCSTR path, WIN32_FIND_DATAA *data )
1414 HANDLE handle = FindFirstFile16( path, data );
1415 if (handle == INVALID_HANDLE_VALUE16) return INVALID_HANDLE_VALUE;
1416 return handle;
1420 /*************************************************************************
1421 * FindFirstFile32W (KERNEL32.124)
1423 HANDLE WINAPI FindFirstFileW( LPCWSTR path, WIN32_FIND_DATAW *data )
1425 WIN32_FIND_DATAA dataA;
1426 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1427 HANDLE handle = FindFirstFileA( pathA, &dataA );
1428 HeapFree( GetProcessHeap(), 0, pathA );
1429 if (handle != INVALID_HANDLE_VALUE)
1431 data->dwFileAttributes = dataA.dwFileAttributes;
1432 data->ftCreationTime = dataA.ftCreationTime;
1433 data->ftLastAccessTime = dataA.ftLastAccessTime;
1434 data->ftLastWriteTime = dataA.ftLastWriteTime;
1435 data->nFileSizeHigh = dataA.nFileSizeHigh;
1436 data->nFileSizeLow = dataA.nFileSizeLow;
1437 lstrcpyAtoW( data->cFileName, dataA.cFileName );
1438 lstrcpyAtoW( data->cAlternateFileName, dataA.cAlternateFileName );
1440 return handle;
1444 /*************************************************************************
1445 * FindNextFile16 (KERNEL.414)
1447 BOOL16 WINAPI FindNextFile16( HANDLE16 handle, WIN32_FIND_DATAA *data )
1449 FIND_FIRST_INFO *info;
1451 if (!(info = (FIND_FIRST_INFO *)GlobalLock16( handle )))
1453 SetLastError( ERROR_INVALID_HANDLE );
1454 return FALSE;
1456 GlobalUnlock16( handle );
1457 if (!info->path || !info->dir)
1459 SetLastError( ERROR_NO_MORE_FILES );
1460 return FALSE;
1462 if (!DOSFS_FindNextEx( info, data ))
1464 DOSFS_CloseDir( info->dir ); info->dir = NULL;
1465 HeapFree( SystemHeap, 0, info->path );
1466 info->path = info->long_mask = NULL;
1467 SetLastError( ERROR_NO_MORE_FILES );
1468 return FALSE;
1470 return TRUE;
1474 /*************************************************************************
1475 * FindNextFile32A (KERNEL32.126)
1477 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
1479 return FindNextFile16( handle, data );
1483 /*************************************************************************
1484 * FindNextFile32W (KERNEL32.127)
1486 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
1488 WIN32_FIND_DATAA dataA;
1489 if (!FindNextFileA( handle, &dataA )) return FALSE;
1490 data->dwFileAttributes = dataA.dwFileAttributes;
1491 data->ftCreationTime = dataA.ftCreationTime;
1492 data->ftLastAccessTime = dataA.ftLastAccessTime;
1493 data->ftLastWriteTime = dataA.ftLastWriteTime;
1494 data->nFileSizeHigh = dataA.nFileSizeHigh;
1495 data->nFileSizeLow = dataA.nFileSizeLow;
1496 lstrcpyAtoW( data->cFileName, dataA.cFileName );
1497 lstrcpyAtoW( data->cAlternateFileName, dataA.cAlternateFileName );
1498 return TRUE;
1502 /*************************************************************************
1503 * FindClose16 (KERNEL.415)
1505 BOOL16 WINAPI FindClose16( HANDLE16 handle )
1507 FIND_FIRST_INFO *info;
1509 if ((handle == INVALID_HANDLE_VALUE16) ||
1510 !(info = (FIND_FIRST_INFO *)GlobalLock16( handle )))
1512 SetLastError( ERROR_INVALID_HANDLE );
1513 return FALSE;
1515 if (info->dir) DOSFS_CloseDir( info->dir );
1516 if (info->path) HeapFree( SystemHeap, 0, info->path );
1517 GlobalUnlock16( handle );
1518 GlobalFree16( handle );
1519 return TRUE;
1523 /*************************************************************************
1524 * FindClose32 (KERNEL32.119)
1526 BOOL WINAPI FindClose( HANDLE handle )
1528 return FindClose16( (HANDLE16)handle );
1532 /***********************************************************************
1533 * DOSFS_UnixTimeToFileTime
1535 * Convert a Unix time to FILETIME format.
1536 * The FILETIME structure is a 64-bit value representing the number of
1537 * 100-nanosecond intervals since January 1, 1601, 0:00.
1538 * 'remainder' is the nonnegative number of 100-ns intervals
1539 * corresponding to the time fraction smaller than 1 second that
1540 * couldn't be stored in the time_t value.
1542 void DOSFS_UnixTimeToFileTime( time_t unix_time, FILETIME *filetime,
1543 DWORD remainder )
1545 /* NOTES:
1547 CONSTANTS:
1548 The time difference between 1 January 1601, 00:00:00 and
1549 1 January 1970, 00:00:00 is 369 years, plus the leap years
1550 from 1604 to 1968, excluding 1700, 1800, 1900.
1551 This makes (1968 - 1600) / 4 - 3 = 89 leap days, and a total
1552 of 134774 days.
1554 Any day in that period had 24 * 60 * 60 = 86400 seconds.
1556 The time difference is 134774 * 86400 * 10000000, which can be written
1557 116444736000000000
1558 27111902 * 2^32 + 3577643008
1559 413 * 2^48 + 45534 * 2^32 + 54590 * 2^16 + 32768
1561 If you find that these constants are buggy, please change them in all
1562 instances in both conversion functions.
1564 VERSIONS:
1565 There are two versions, one of them uses long long variables and
1566 is presumably faster but not ISO C. The other one uses standard C
1567 data types and operations but relies on the assumption that negative
1568 numbers are stored as 2's complement (-1 is 0xffff....). If this
1569 assumption is violated, dates before 1970 will not convert correctly.
1570 This should however work on any reasonable architecture where WINE
1571 will run.
1573 DETAILS:
1575 Take care not to remove the casts. I have tested these functions
1576 (in both versions) for a lot of numbers. I would be interested in
1577 results on other compilers than GCC.
1579 The operations have been designed to account for the possibility
1580 of 64-bit time_t in future UNICES. Even the versions without
1581 internal long long numbers will work if time_t only is 64 bit.
1582 A 32-bit shift, which was necessary for that operation, turned out
1583 not to work correctly in GCC, besides giving the warning. So I
1584 used a double 16-bit shift instead. Numbers are in the ISO version
1585 represented by three limbs, the most significant with 32 bit, the
1586 other two with 16 bit each.
1588 As the modulo-operator % is not well-defined for negative numbers,
1589 negative divisors have been avoided in DOSFS_FileTimeToUnixTime.
1591 There might be quicker ways to do this in C. Certainly so in
1592 assembler.
1594 Claus Fischer, fischer@iue.tuwien.ac.at
1597 #if (SIZEOF_LONG_LONG >= 8)
1598 # define USE_LONG_LONG 1
1599 #else
1600 # define USE_LONG_LONG 0
1601 #endif
1603 #if USE_LONG_LONG /* gcc supports long long type */
1605 long long int t = unix_time;
1606 t *= 10000000;
1607 t += 116444736000000000LL;
1608 t += remainder;
1609 filetime->dwLowDateTime = (UINT)t;
1610 filetime->dwHighDateTime = (UINT)(t >> 32);
1612 #else /* ISO version */
1614 UINT a0; /* 16 bit, low bits */
1615 UINT a1; /* 16 bit, medium bits */
1616 UINT a2; /* 32 bit, high bits */
1618 /* Copy the unix time to a2/a1/a0 */
1619 a0 = unix_time & 0xffff;
1620 a1 = (unix_time >> 16) & 0xffff;
1621 /* This is obsolete if unix_time is only 32 bits, but it does not hurt.
1622 Do not replace this by >> 32, it gives a compiler warning and it does
1623 not work. */
1624 a2 = (unix_time >= 0 ? (unix_time >> 16) >> 16 :
1625 ~((~unix_time >> 16) >> 16));
1627 /* Multiply a by 10000000 (a = a2/a1/a0)
1628 Split the factor into 10000 * 1000 which are both less than 0xffff. */
1629 a0 *= 10000;
1630 a1 = a1 * 10000 + (a0 >> 16);
1631 a2 = a2 * 10000 + (a1 >> 16);
1632 a0 &= 0xffff;
1633 a1 &= 0xffff;
1635 a0 *= 1000;
1636 a1 = a1 * 1000 + (a0 >> 16);
1637 a2 = a2 * 1000 + (a1 >> 16);
1638 a0 &= 0xffff;
1639 a1 &= 0xffff;
1641 /* Add the time difference and the remainder */
1642 a0 += 32768 + (remainder & 0xffff);
1643 a1 += 54590 + (remainder >> 16 ) + (a0 >> 16);
1644 a2 += 27111902 + (a1 >> 16);
1645 a0 &= 0xffff;
1646 a1 &= 0xffff;
1648 /* Set filetime */
1649 filetime->dwLowDateTime = (a1 << 16) + a0;
1650 filetime->dwHighDateTime = a2;
1651 #endif
1655 /***********************************************************************
1656 * DOSFS_FileTimeToUnixTime
1658 * Convert a FILETIME format to Unix time.
1659 * If not NULL, 'remainder' contains the fractional part of the filetime,
1660 * in the range of [0..9999999] (even if time_t is negative).
1662 time_t DOSFS_FileTimeToUnixTime( const FILETIME *filetime, DWORD *remainder )
1664 /* Read the comment in the function DOSFS_UnixTimeToFileTime. */
1665 #if USE_LONG_LONG
1667 long long int t = filetime->dwHighDateTime;
1668 t <<= 32;
1669 t += (UINT)filetime->dwLowDateTime;
1670 t -= 116444736000000000LL;
1671 if (t < 0)
1673 if (remainder) *remainder = 9999999 - (-t - 1) % 10000000;
1674 return -1 - ((-t - 1) / 10000000);
1676 else
1678 if (remainder) *remainder = t % 10000000;
1679 return t / 10000000;
1682 #else /* ISO version */
1684 UINT a0; /* 16 bit, low bits */
1685 UINT a1; /* 16 bit, medium bits */
1686 UINT a2; /* 32 bit, high bits */
1687 UINT r; /* remainder of division */
1688 unsigned int carry; /* carry bit for subtraction */
1689 int negative; /* whether a represents a negative value */
1691 /* Copy the time values to a2/a1/a0 */
1692 a2 = (UINT)filetime->dwHighDateTime;
1693 a1 = ((UINT)filetime->dwLowDateTime ) >> 16;
1694 a0 = ((UINT)filetime->dwLowDateTime ) & 0xffff;
1696 /* Subtract the time difference */
1697 if (a0 >= 32768 ) a0 -= 32768 , carry = 0;
1698 else a0 += (1 << 16) - 32768 , carry = 1;
1700 if (a1 >= 54590 + carry) a1 -= 54590 + carry, carry = 0;
1701 else a1 += (1 << 16) - 54590 - carry, carry = 1;
1703 a2 -= 27111902 + carry;
1705 /* If a is negative, replace a by (-1-a) */
1706 negative = (a2 >= ((UINT)1) << 31);
1707 if (negative)
1709 /* Set a to -a - 1 (a is a2/a1/a0) */
1710 a0 = 0xffff - a0;
1711 a1 = 0xffff - a1;
1712 a2 = ~a2;
1715 /* Divide a by 10000000 (a = a2/a1/a0), put the rest into r.
1716 Split the divisor into 10000 * 1000 which are both less than 0xffff. */
1717 a1 += (a2 % 10000) << 16;
1718 a2 /= 10000;
1719 a0 += (a1 % 10000) << 16;
1720 a1 /= 10000;
1721 r = a0 % 10000;
1722 a0 /= 10000;
1724 a1 += (a2 % 1000) << 16;
1725 a2 /= 1000;
1726 a0 += (a1 % 1000) << 16;
1727 a1 /= 1000;
1728 r += (a0 % 1000) * 10000;
1729 a0 /= 1000;
1731 /* If a was negative, replace a by (-1-a) and r by (9999999 - r) */
1732 if (negative)
1734 /* Set a to -a - 1 (a is a2/a1/a0) */
1735 a0 = 0xffff - a0;
1736 a1 = 0xffff - a1;
1737 a2 = ~a2;
1739 r = 9999999 - r;
1742 if (remainder) *remainder = r;
1744 /* Do not replace this by << 32, it gives a compiler warning and it does
1745 not work. */
1746 return ((((time_t)a2) << 16) << 16) + (a1 << 16) + a0;
1747 #endif
1751 /***********************************************************************
1752 * DosDateTimeToFileTime (KERNEL32.76)
1754 BOOL WINAPI DosDateTimeToFileTime( WORD fatdate, WORD fattime, LPFILETIME ft)
1756 struct tm newtm;
1758 newtm.tm_sec = (fattime & 0x1f) * 2;
1759 newtm.tm_min = (fattime >> 5) & 0x3f;
1760 newtm.tm_hour = (fattime >> 11);
1761 newtm.tm_mday = (fatdate & 0x1f);
1762 newtm.tm_mon = ((fatdate >> 5) & 0x0f) - 1;
1763 newtm.tm_year = (fatdate >> 9) + 80;
1764 DOSFS_UnixTimeToFileTime( mktime( &newtm ), ft, 0 );
1765 return TRUE;
1769 /***********************************************************************
1770 * FileTimeToDosDateTime (KERNEL32.111)
1772 BOOL WINAPI FileTimeToDosDateTime( const FILETIME *ft, LPWORD fatdate,
1773 LPWORD fattime )
1775 time_t unixtime = DOSFS_FileTimeToUnixTime( ft, NULL );
1776 struct tm *tm = localtime( &unixtime );
1777 if (fattime)
1778 *fattime = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
1779 if (fatdate)
1780 *fatdate = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5)
1781 + tm->tm_mday;
1782 return TRUE;
1786 /***********************************************************************
1787 * LocalFileTimeToFileTime (KERNEL32.373)
1789 BOOL WINAPI LocalFileTimeToFileTime( const FILETIME *localft,
1790 LPFILETIME utcft )
1792 struct tm *xtm;
1793 DWORD remainder;
1795 /* convert from local to UTC. Perhaps not correct. FIXME */
1796 time_t unixtime = DOSFS_FileTimeToUnixTime( localft, &remainder );
1797 xtm = gmtime( &unixtime );
1798 DOSFS_UnixTimeToFileTime( mktime(xtm), utcft, remainder );
1799 return TRUE;
1803 /***********************************************************************
1804 * FileTimeToLocalFileTime (KERNEL32.112)
1806 BOOL WINAPI FileTimeToLocalFileTime( const FILETIME *utcft,
1807 LPFILETIME localft )
1809 DWORD remainder;
1810 /* convert from UTC to local. Perhaps not correct. FIXME */
1811 time_t unixtime = DOSFS_FileTimeToUnixTime( utcft, &remainder );
1812 #ifdef HAVE_TIMEGM
1813 struct tm *xtm = localtime( &unixtime );
1814 time_t localtime;
1816 localtime = timegm(xtm);
1817 DOSFS_UnixTimeToFileTime( localtime, localft, remainder );
1819 #else
1820 struct tm *xtm,*gtm;
1821 time_t time1,time2;
1823 xtm = localtime( &unixtime );
1824 gtm = gmtime( &unixtime );
1825 time1 = mktime(xtm);
1826 time2 = mktime(gtm);
1827 DOSFS_UnixTimeToFileTime( 2*time1-time2, localft, remainder );
1828 #endif
1829 return TRUE;
1833 /***********************************************************************
1834 * FileTimeToSystemTime (KERNEL32.113)
1836 BOOL WINAPI FileTimeToSystemTime( const FILETIME *ft, LPSYSTEMTIME syst )
1838 struct tm *xtm;
1839 DWORD remainder;
1840 time_t xtime = DOSFS_FileTimeToUnixTime( ft, &remainder );
1841 xtm = gmtime(&xtime);
1842 syst->wYear = xtm->tm_year+1900;
1843 syst->wMonth = xtm->tm_mon + 1;
1844 syst->wDayOfWeek = xtm->tm_wday;
1845 syst->wDay = xtm->tm_mday;
1846 syst->wHour = xtm->tm_hour;
1847 syst->wMinute = xtm->tm_min;
1848 syst->wSecond = xtm->tm_sec;
1849 syst->wMilliseconds = remainder / 10000;
1850 return TRUE;
1853 /***********************************************************************
1854 * QueryDosDeviceA (KERNEL32.413)
1856 * returns array of strings terminated by \0, terminated by \0
1858 DWORD WINAPI QueryDosDeviceA(LPCSTR devname,LPSTR target,DWORD bufsize)
1860 LPSTR s;
1861 char buffer[200];
1863 TRACE(dosfs,"(%s,...)\n",devname?devname:"<null>");
1864 if (!devname) {
1865 /* return known MSDOS devices */
1866 lstrcpyA(buffer,"CON COM1 COM2 LPT1 NUL ");
1867 while ((s=strchr(buffer,' ')))
1868 *s='\0';
1870 lstrcpynA(target,buffer,bufsize);
1871 return strlen(buffer);
1873 lstrcpyA(buffer,"\\DEV\\");
1874 lstrcatA(buffer,devname);
1875 if ((s=strchr(buffer,':'))) *s='\0';
1876 lstrcpynA(target,buffer,bufsize);
1877 return strlen(buffer);
1881 /***********************************************************************
1882 * QueryDosDeviceW (KERNEL32.414)
1884 * returns array of strings terminated by \0, terminated by \0
1886 DWORD WINAPI QueryDosDeviceW(LPCWSTR devname,LPWSTR target,DWORD bufsize)
1888 LPSTR devnameA = devname?HEAP_strdupWtoA(GetProcessHeap(),0,devname):NULL;
1889 LPSTR targetA = (LPSTR)HEAP_xalloc(GetProcessHeap(),0,bufsize);
1890 DWORD ret = QueryDosDeviceA(devnameA,targetA,bufsize);
1892 lstrcpynAtoW(target,targetA,bufsize);
1893 if (devnameA) HeapFree(GetProcessHeap(),0,devnameA);
1894 if (targetA) HeapFree(GetProcessHeap(),0,targetA);
1895 return ret;
1899 /***********************************************************************
1900 * SystemTimeToFileTime (KERNEL32.526)
1902 BOOL WINAPI SystemTimeToFileTime( const SYSTEMTIME *syst, LPFILETIME ft )
1904 #ifdef HAVE_TIMEGM
1905 struct tm xtm;
1906 time_t utctime;
1907 #else
1908 struct tm xtm,*local_tm,*utc_tm;
1909 time_t localtim,utctime;
1910 #endif
1912 xtm.tm_year = syst->wYear-1900;
1913 xtm.tm_mon = syst->wMonth - 1;
1914 xtm.tm_wday = syst->wDayOfWeek;
1915 xtm.tm_mday = syst->wDay;
1916 xtm.tm_hour = syst->wHour;
1917 xtm.tm_min = syst->wMinute;
1918 xtm.tm_sec = syst->wSecond; /* this is UTC */
1919 xtm.tm_isdst = -1;
1920 #ifdef HAVE_TIMEGM
1921 utctime = timegm(&xtm);
1922 DOSFS_UnixTimeToFileTime( utctime, ft,
1923 syst->wMilliseconds * 10000 );
1924 #else
1925 localtim = mktime(&xtm); /* now we've got local time */
1926 local_tm = localtime(&localtim);
1927 utc_tm = gmtime(&localtim);
1928 utctime = mktime(utc_tm);
1929 DOSFS_UnixTimeToFileTime( 2*localtim -utctime, ft,
1930 syst->wMilliseconds * 10000 );
1931 #endif
1932 return TRUE;
1935 BOOL WINAPI DefineDosDeviceA(DWORD flags,LPCSTR devname,LPCSTR targetpath) {
1936 FIXME(dosfs,"(0x%08lx,%s,%s),stub!\n",flags,devname,targetpath);
1937 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1938 return FALSE;