2 * DOS file system functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996 Alexandre Julliard
13 #ifdef HAVE_SYS_ERRNO_H
14 #include <sys/errno.h>
20 #include <sys/ioctl.h>
27 #include "wine/winbase16.h"
37 #include "debugtools.h"
39 DEFAULT_DEBUG_CHANNEL(dosfs
)
40 DECLARE_DEBUG_CHANNEL(file
)
42 /* Define the VFAT ioctl to get both short and long file names */
43 /* FIXME: is it possible to get this to work on other systems? */
45 /* We want the real kernel dirent structure, not the libc one */
50 unsigned short d_reclen
;
54 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, KERNEL_DIRENT [2] )
57 #undef VFAT_IOCTL_READDIR_BOTH /* just in case... */
60 /* Chars we don't want to see in DOS file names */
61 #define INVALID_DOS_CHARS "*?<>|\"+=,;[] \345"
63 static const DOS_DEVICE DOSFS_Devices
[] =
64 /* name, device flags (see Int 21/AX=0x4400) */
78 { "SCSIMGR$", 0xc0c0 },
82 #define GET_DRIVE(path) \
83 (((path)[1] == ':') ? toupper((path)[0]) - 'A' : DOSFS_CurDrive)
85 /* Directory info for DOSFS_ReadDir */
89 #ifdef VFAT_IOCTL_READDIR_BOTH
92 KERNEL_DIRENT dirent
[2];
96 /* Info structure for FindFirstFile handle */
110 /***********************************************************************
113 * Return 1 if Unix file 'name' is also a valid MS-DOS name
114 * (i.e. contains only valid DOS chars, lower-case only, fits in 8.3 format).
115 * File name can be terminated by '\0', '\\' or '/'.
117 static int DOSFS_ValidDOSName( const char *name
, int ignore_case
)
119 static const char invalid_chars
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" INVALID_DOS_CHARS
;
120 const char *p
= name
;
121 const char *invalid
= ignore_case
? (invalid_chars
+ 26) : invalid_chars
;
126 /* Check for "." and ".." */
129 /* All other names beginning with '.' are invalid */
130 return (IS_END_OF_NAME(*p
));
132 while (!IS_END_OF_NAME(*p
))
134 if (strchr( invalid
, *p
)) return 0; /* Invalid char */
135 if (*p
== '.') break; /* Start of the extension */
136 if (++len
> 8) return 0; /* Name too long */
139 if (*p
!= '.') return 1; /* End of name */
141 if (IS_END_OF_NAME(*p
)) return 0; /* Empty extension not allowed */
143 while (!IS_END_OF_NAME(*p
))
145 if (strchr( invalid
, *p
)) return 0; /* Invalid char */
146 if (*p
== '.') return 0; /* Second extension not allowed */
147 if (++len
> 3) return 0; /* Extension too long */
154 /***********************************************************************
155 * DOSFS_ToDosFCBFormat
157 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
158 * expanding wild cards and converting to upper-case in the process.
159 * File name can be terminated by '\0', '\\' or '/'.
160 * Return FALSE if the name is not a valid DOS name.
161 * 'buffer' must be at least 12 characters long.
163 BOOL
DOSFS_ToDosFCBFormat( LPCSTR name
, LPSTR buffer
)
165 static const char invalid_chars
[] = INVALID_DOS_CHARS
;
166 const char *p
= name
;
169 /* Check for "." and ".." */
173 strcpy( buffer
, ". " );
179 return (!*p
|| (*p
== '/') || (*p
== '\\'));
182 for (i
= 0; i
< 8; i
++)
199 if (strchr( invalid_chars
, *p
)) return FALSE
;
200 buffer
[i
] = toupper(*p
);
208 /* Skip all chars after wildcard up to first dot */
209 while (*p
&& (*p
!= '/') && (*p
!= '\\') && (*p
!= '.')) p
++;
213 /* Check if name too long */
214 if (*p
&& (*p
!= '/') && (*p
!= '\\') && (*p
!= '.')) return FALSE
;
216 if (*p
== '.') p
++; /* Skip dot */
218 for (i
= 8; i
< 11; i
++)
228 return FALSE
; /* Second extension not allowed */
236 if (strchr( invalid_chars
, *p
)) return FALSE
;
237 buffer
[i
] = toupper(*p
);
244 /* at most 3 character of the extension are processed
245 * is something behind this ?
247 while (*p
== '*' || *p
== ' ') p
++; /* skip wildcards and spaces */
248 return IS_END_OF_NAME(*p
);
252 /***********************************************************************
253 * DOSFS_ToDosDTAFormat
255 * Convert a file name from FCB to DTA format (name.ext, null-terminated)
256 * converting to upper-case in the process.
257 * File name can be terminated by '\0', '\\' or '/'.
258 * 'buffer' must be at least 13 characters long.
260 static void DOSFS_ToDosDTAFormat( LPCSTR name
, LPSTR buffer
)
264 memcpy( buffer
, name
, 8 );
265 for (p
= buffer
+ 8; (p
> buffer
) && (p
[-1] == ' '); p
--);
267 memcpy( p
, name
+ 8, 3 );
268 for (p
+= 3; p
[-1] == ' '; p
--);
269 if (p
[-1] == '.') p
--;
274 /***********************************************************************
277 * Check a DOS file name against a mask (both in FCB format).
279 static int DOSFS_MatchShort( const char *mask
, const char *name
)
282 for (i
= 11; i
> 0; i
--, mask
++, name
++)
283 if ((*mask
!= '?') && (*mask
!= *name
)) return 0;
288 /***********************************************************************
291 * Check a long file name against a mask.
293 static int DOSFS_MatchLong( const char *mask
, const char *name
,
296 if (!strcmp( mask
, "*.*" )) return 1;
297 while (*name
&& *mask
)
302 while (*mask
== '*') mask
++; /* Skip consecutive '*' */
303 if (!*mask
) return 1;
304 if (case_sensitive
) while (*name
&& (*name
!= *mask
)) name
++;
305 else while (*name
&& (toupper(*name
) != toupper(*mask
))) name
++;
308 else if (*mask
!= '?')
312 if (*mask
!= *name
) return 0;
314 else if (toupper(*mask
) != toupper(*name
)) return 0;
319 if (*mask
== '.') mask
++; /* Ignore trailing '.' in mask */
320 return (!*name
&& !*mask
);
324 /***********************************************************************
327 static DOS_DIR
*DOSFS_OpenDir( LPCSTR path
)
329 DOS_DIR
*dir
= HeapAlloc( GetProcessHeap(), 0, sizeof(*dir
) );
332 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
336 /* Treat empty path as root directory. This simplifies path split into
337 directory and mask in several other places */
338 if (!*path
) path
= "/";
340 #ifdef VFAT_IOCTL_READDIR_BOTH
342 /* Check if the VFAT ioctl is supported on this directory */
344 if ((dir
->fd
= open( path
, O_RDONLY
)) != -1)
346 if (ioctl( dir
->fd
, VFAT_IOCTL_READDIR_BOTH
, (long)dir
->dirent
) == -1)
353 /* Set the file pointer back at the start of the directory */
354 lseek( dir
->fd
, 0, SEEK_SET
);
359 #endif /* VFAT_IOCTL_READDIR_BOTH */
361 /* Now use the standard opendir/readdir interface */
363 if (!(dir
->dir
= opendir( path
)))
365 HeapFree( GetProcessHeap(), 0, dir
);
372 /***********************************************************************
375 static void DOSFS_CloseDir( DOS_DIR
*dir
)
377 #ifdef VFAT_IOCTL_READDIR_BOTH
378 if (dir
->fd
!= -1) close( dir
->fd
);
379 #endif /* VFAT_IOCTL_READDIR_BOTH */
380 if (dir
->dir
) closedir( dir
->dir
);
381 HeapFree( GetProcessHeap(), 0, dir
);
385 /***********************************************************************
388 static BOOL
DOSFS_ReadDir( DOS_DIR
*dir
, LPCSTR
*long_name
,
391 struct dirent
*dirent
;
393 #ifdef VFAT_IOCTL_READDIR_BOTH
396 if (ioctl( dir
->fd
, VFAT_IOCTL_READDIR_BOTH
, (long)dir
->dirent
) != -1) {
397 if (!dir
->dirent
[0].d_reclen
) return FALSE
;
398 if (!DOSFS_ToDosFCBFormat( dir
->dirent
[0].d_name
, dir
->short_name
))
399 dir
->short_name
[0] = '\0';
400 *short_name
= dir
->short_name
;
401 if (dir
->dirent
[1].d_name
[0]) *long_name
= dir
->dirent
[1].d_name
;
402 else *long_name
= dir
->dirent
[0].d_name
;
406 #endif /* VFAT_IOCTL_READDIR_BOTH */
408 if (!(dirent
= readdir( dir
->dir
))) return FALSE
;
409 *long_name
= dirent
->d_name
;
415 /***********************************************************************
418 * Transform a Unix file name into a hashed DOS name. If the name is a valid
419 * DOS name, it is converted to upper-case; otherwise it is replaced by a
420 * hashed version that fits in 8.3 format.
421 * File name can be terminated by '\0', '\\' or '/'.
422 * 'buffer' must be at least 13 characters long.
424 static void DOSFS_Hash( LPCSTR name
, LPSTR buffer
, BOOL dir_format
,
427 static const char invalid_chars
[] = INVALID_DOS_CHARS
"~.";
428 static const char hash_chars
[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
435 if (dir_format
) strcpy( buffer
, " " );
437 if (DOSFS_ValidDOSName( name
, ignore_case
))
439 /* Check for '.' and '..' */
443 if (!dir_format
) buffer
[1] = buffer
[2] = '\0';
444 if (name
[1] == '.') buffer
[1] = '.';
448 /* Simply copy the name, converting to uppercase */
450 for (dst
= buffer
; !IS_END_OF_NAME(*name
) && (*name
!= '.'); name
++)
451 *dst
++ = toupper(*name
);
454 if (dir_format
) dst
= buffer
+ 8;
456 for (name
++; !IS_END_OF_NAME(*name
); name
++)
457 *dst
++ = toupper(*name
);
459 if (!dir_format
) *dst
= '\0';
463 /* Compute the hash code of the file name */
464 /* If you know something about hash functions, feel free to */
465 /* insert a better algorithm here... */
468 for (p
= name
, hash
= 0xbeef; !IS_END_OF_NAME(p
[1]); p
++)
469 hash
= (hash
<<3) ^ (hash
>>5) ^ tolower(*p
) ^ (tolower(p
[1]) << 8);
470 hash
= (hash
<<3) ^ (hash
>>5) ^ tolower(*p
); /* Last character*/
474 for (p
= name
, hash
= 0xbeef; !IS_END_OF_NAME(p
[1]); p
++)
475 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
^ (p
[1] << 8);
476 hash
= (hash
<< 3) ^ (hash
>> 5) ^ *p
; /* Last character */
479 /* Find last dot for start of the extension */
480 for (p
= name
+1, ext
= NULL
; !IS_END_OF_NAME(*p
); p
++)
481 if (*p
== '.') ext
= p
;
482 if (ext
&& IS_END_OF_NAME(ext
[1]))
483 ext
= NULL
; /* Empty extension ignored */
485 /* Copy first 4 chars, replacing invalid chars with '_' */
486 for (i
= 4, p
= name
, dst
= buffer
; i
> 0; i
--, p
++)
488 if (IS_END_OF_NAME(*p
) || (p
== ext
)) break;
489 *dst
++ = strchr( invalid_chars
, *p
) ? '_' : toupper(*p
);
491 /* Pad to 5 chars with '~' */
492 while (i
-- >= 0) *dst
++ = '~';
494 /* Insert hash code converted to 3 ASCII chars */
495 *dst
++ = hash_chars
[(hash
>> 10) & 0x1f];
496 *dst
++ = hash_chars
[(hash
>> 5) & 0x1f];
497 *dst
++ = hash_chars
[hash
& 0x1f];
499 /* Copy the first 3 chars of the extension (if any) */
502 if (!dir_format
) *dst
++ = '.';
503 for (i
= 3, ext
++; (i
> 0) && !IS_END_OF_NAME(*ext
); i
--, ext
++)
504 *dst
++ = strchr( invalid_chars
, *ext
) ? '_' : toupper(*ext
);
506 if (!dir_format
) *dst
= '\0';
510 /***********************************************************************
513 * Find the Unix file name in a given directory that corresponds to
514 * a file name (either in Unix or DOS format).
515 * File name can be terminated by '\0', '\\' or '/'.
516 * Return TRUE if OK, FALSE if no file name matches.
518 * 'long_buf' must be at least 'long_len' characters long. If the long name
519 * turns out to be larger than that, the function returns FALSE.
520 * 'short_buf' must be at least 13 characters long.
522 BOOL
DOSFS_FindUnixName( LPCSTR path
, LPCSTR name
, LPSTR long_buf
,
523 INT long_len
, LPSTR short_buf
, BOOL ignore_case
)
526 LPCSTR long_name
, short_name
;
527 char dos_name
[12], tmp_buf
[13];
530 const char *p
= strchr( name
, '/' );
531 int len
= p
? (int)(p
- name
) : strlen(name
);
532 if ((p
= strchr( name
, '\\' ))) len
= min( (int)(p
- name
), len
);
533 /* Ignore trailing dots */
534 while (len
> 1 && name
[len
-1] == '.') len
--;
535 if (long_len
< len
+ 1) return FALSE
;
537 TRACE("%s,%s\n", path
, name
);
539 if (!DOSFS_ToDosFCBFormat( name
, dos_name
)) dos_name
[0] = '\0';
541 if (!(dir
= DOSFS_OpenDir( path
)))
543 WARN("(%s,%s): can't open dir: %s\n",
544 path
, name
, strerror(errno
) );
548 while ((ret
= DOSFS_ReadDir( dir
, &long_name
, &short_name
)))
550 /* Check against Unix name */
551 if (len
== strlen(long_name
))
555 if (!strncmp( long_name
, name
, len
)) break;
559 if (!lstrncmpiA( long_name
, name
, len
)) break;
564 /* Check against hashed DOS name */
567 DOSFS_Hash( long_name
, tmp_buf
, TRUE
, ignore_case
);
568 short_name
= tmp_buf
;
570 if (!strcmp( dos_name
, short_name
)) break;
575 if (long_buf
) strcpy( long_buf
, long_name
);
579 DOSFS_ToDosDTAFormat( short_name
, short_buf
);
581 DOSFS_Hash( long_name
, short_buf
, FALSE
, ignore_case
);
583 TRACE("(%s,%s) -> %s (%s)\n",
584 path
, name
, long_name
, short_buf
? short_buf
: "***");
587 WARN("'%s' not found in '%s'\n", name
, path
);
588 DOSFS_CloseDir( dir
);
593 /***********************************************************************
596 * Check if a DOS file name represents a DOS device and return the device.
598 const DOS_DEVICE
*DOSFS_GetDevice( const char *name
)
603 if (!name
) return NULL
; /* if FILE_DupUnixHandle was used */
604 if (name
[0] && (name
[1] == ':')) name
+= 2;
605 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
606 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
607 for (i
= 0; i
< sizeof(DOSFS_Devices
)/sizeof(DOSFS_Devices
[0]); i
++)
609 const char *dev
= DOSFS_Devices
[i
].name
;
610 if (!lstrncmpiA( dev
, name
, strlen(dev
) ))
612 p
= name
+ strlen( dev
);
613 if (!*p
|| (*p
== '.')) return &DOSFS_Devices
[i
];
620 /***********************************************************************
621 * DOSFS_GetDeviceByHandle
623 const DOS_DEVICE
*DOSFS_GetDeviceByHandle( HFILE hFile
)
625 struct get_file_info_request
*req
= get_req_buffer();
628 if (!server_call( REQ_GET_FILE_INFO
) && (req
->type
== FILE_TYPE_UNKNOWN
))
630 if ((req
->attr
>= 0) &&
631 (req
->attr
< sizeof(DOSFS_Devices
)/sizeof(DOSFS_Devices
[0])))
632 return &DOSFS_Devices
[req
->attr
];
638 /***********************************************************************
641 * Open a DOS device. This might not map 1:1 into the UNIX device concept.
643 HFILE
DOSFS_OpenDevice( const char *name
, DWORD access
)
648 if (!name
) return (HFILE
)NULL
; /* if FILE_DupUnixHandle was used */
649 if (name
[0] && (name
[1] == ':')) name
+= 2;
650 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
651 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
652 for (i
= 0; i
< sizeof(DOSFS_Devices
)/sizeof(DOSFS_Devices
[0]); i
++)
654 const char *dev
= DOSFS_Devices
[i
].name
;
655 if (!lstrncmpiA( dev
, name
, strlen(dev
) ))
657 p
= name
+ strlen( dev
);
658 if (!*p
|| (*p
== '.')) {
660 if (!strcmp(DOSFS_Devices
[i
].name
,"NUL"))
661 return FILE_CreateFile( "/dev/null", access
,
662 FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
,
663 OPEN_EXISTING
, 0, -1, TRUE
);
664 if (!strcmp(DOSFS_Devices
[i
].name
,"CON")) {
667 switch (access
& (GENERIC_READ
|GENERIC_WRITE
)) {
669 to_dup
= GetStdHandle( STD_INPUT_HANDLE
);
672 to_dup
= GetStdHandle( STD_OUTPUT_HANDLE
);
675 FIXME("can't open CON read/write\n");
679 if (!DuplicateHandle( GetCurrentProcess(), to_dup
, GetCurrentProcess(),
680 &handle
, 0, FALSE
, DUPLICATE_SAME_ACCESS
))
681 handle
= HFILE_ERROR
;
684 if (!strcmp(DOSFS_Devices
[i
].name
,"SCSIMGR$") ||
685 !strcmp(DOSFS_Devices
[i
].name
,"HPSCAN"))
687 return FILE_CreateDevice( i
, access
, NULL
);
692 PROFILE_GetWineIniString("serialports",name
,"",devname
,sizeof devname
);
696 TRACE_(file
)("DOSFS_OpenDevice %s is %s\n",
697 DOSFS_Devices
[i
].name
,devname
);
698 r
= FILE_CreateFile( devname
, access
,
699 FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
,
700 OPEN_EXISTING
, 0, -1, TRUE
);
701 TRACE_(file
)("Create_File return %08X\n",r
);
706 FIXME("device open %s not supported (yet)\n",DOSFS_Devices
[i
].name
);
715 /***********************************************************************
718 * Get the drive specified by a given path name (DOS or Unix format).
720 static int DOSFS_GetPathDrive( const char **name
)
723 const char *p
= *name
;
725 if (*p
&& (p
[1] == ':'))
727 drive
= toupper(*p
) - 'A';
730 else if (*p
== '/') /* Absolute Unix path? */
732 if ((drive
= DRIVE_FindDriveRoot( name
)) == -1)
734 MESSAGE("Warning: %s not accessible from a DOS drive\n", *name
);
735 /* Assume it really was a DOS name */
736 drive
= DRIVE_GetCurrentDrive();
739 else drive
= DRIVE_GetCurrentDrive();
741 if (!DRIVE_IsValid(drive
))
743 SetLastError( ERROR_INVALID_DRIVE
);
750 /***********************************************************************
753 * Convert a file name (DOS or mixed DOS/Unix format) to a valid
754 * Unix name / short DOS name pair.
755 * Return FALSE if one of the path components does not exist. The last path
756 * component is only checked if 'check_last' is non-zero.
757 * The buffers pointed to by 'long_buf' and 'short_buf' must be
758 * at least MAX_PATHNAME_LEN long.
760 BOOL
DOSFS_GetFullName( LPCSTR name
, BOOL check_last
, DOS_FULL_NAME
*full
)
764 char *p_l
, *p_s
, *root
;
766 TRACE("%s (last=%d)\n", name
, check_last
);
768 if ((full
->drive
= DOSFS_GetPathDrive( &name
)) == -1) return FALSE
;
769 flags
= DRIVE_GetFlags( full
->drive
);
771 lstrcpynA( full
->long_name
, DRIVE_GetRoot( full
->drive
),
772 sizeof(full
->long_name
) );
773 if (full
->long_name
[1]) root
= full
->long_name
+ strlen(full
->long_name
);
774 else root
= full
->long_name
; /* root directory */
776 strcpy( full
->short_name
, "A:\\" );
777 full
->short_name
[0] += full
->drive
;
779 if ((*name
== '\\') || (*name
== '/')) /* Absolute path */
781 while ((*name
== '\\') || (*name
== '/')) name
++;
783 else /* Relative path */
785 lstrcpynA( root
+ 1, DRIVE_GetUnixCwd( full
->drive
),
786 sizeof(full
->long_name
) - (root
- full
->long_name
) - 1 );
787 if (root
[1]) *root
= '/';
788 lstrcpynA( full
->short_name
+ 3, DRIVE_GetDosCwd( full
->drive
),
789 sizeof(full
->short_name
) - 3 );
792 p_l
= full
->long_name
[1] ? full
->long_name
+ strlen(full
->long_name
)
794 p_s
= full
->short_name
[3] ? full
->short_name
+ strlen(full
->short_name
)
795 : full
->short_name
+ 2;
798 while (*name
&& found
)
800 /* Check for '.' and '..' */
804 if (IS_END_OF_NAME(name
[1]))
807 while ((*name
== '\\') || (*name
== '/')) name
++;
810 else if ((name
[1] == '.') && IS_END_OF_NAME(name
[2]))
813 while ((*name
== '\\') || (*name
== '/')) name
++;
814 while ((p_l
> root
) && (*p_l
!= '/')) p_l
--;
815 while ((p_s
> full
->short_name
+ 2) && (*p_s
!= '\\')) p_s
--;
816 *p_l
= *p_s
= '\0'; /* Remove trailing separator */
821 /* Make sure buffers are large enough */
823 if ((p_s
>= full
->short_name
+ sizeof(full
->short_name
) - 14) ||
824 (p_l
>= full
->long_name
+ sizeof(full
->long_name
) - 1))
826 SetLastError( ERROR_PATH_NOT_FOUND
);
830 /* Get the long and short name matching the file name */
832 if ((found
= DOSFS_FindUnixName( full
->long_name
, name
, p_l
+ 1,
833 sizeof(full
->long_name
) - (p_l
- full
->long_name
) - 1,
834 p_s
+ 1, !(flags
& DRIVE_CASE_SENSITIVE
) )))
840 while (!IS_END_OF_NAME(*name
)) name
++;
842 else if (!check_last
)
846 while (!IS_END_OF_NAME(*name
) &&
847 (p_s
< full
->short_name
+ sizeof(full
->short_name
) - 1) &&
848 (p_l
< full
->long_name
+ sizeof(full
->long_name
) - 1))
850 *p_s
++ = tolower(*name
);
851 /* If the drive is case-sensitive we want to create new */
852 /* files in lower-case otherwise we can't reopen them */
853 /* under the same short name. */
854 if (flags
& DRIVE_CASE_SENSITIVE
) *p_l
++ = tolower(*name
);
860 while ((*name
== '\\') || (*name
== '/')) name
++;
867 SetLastError( ERROR_FILE_NOT_FOUND
);
870 if (*name
) /* Not last */
872 SetLastError( ERROR_PATH_NOT_FOUND
);
876 if (!full
->long_name
[0]) strcpy( full
->long_name
, "/" );
877 if (!full
->short_name
[2]) strcpy( full
->short_name
+ 2, "\\" );
878 TRACE("returning %s = %s\n", full
->long_name
, full
->short_name
);
883 /***********************************************************************
884 * GetShortPathNameA (KERNEL32.271)
888 * longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
889 * *longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
891 * more observations ( with NT 3.51 (WinDD) ):
892 * longpath <= 8.3 -> just copy longpath to shortpath
894 * a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
895 * b) file does exist -> set the short filename.
896 * - trailing slashes are reproduced in the short name, even if the
897 * file is not a directory
898 * - the absolute/relative path of the short name is reproduced like found
900 * - longpath and shortpath may have the same adress
903 DWORD WINAPI
GetShortPathNameA( LPCSTR longpath
, LPSTR shortpath
,
906 DOS_FULL_NAME full_name
;
908 DWORD sp
= 0, lp
= 0;
912 TRACE("%s\n", debugstr_a(longpath
));
915 SetLastError(ERROR_INVALID_PARAMETER
);
919 SetLastError(ERROR_BAD_PATHNAME
);
923 if ( ( tmpshortpath
= HeapAlloc ( GetProcessHeap(), 0, MAX_PATHNAME_LEN
) ) == NULL
) {
924 SetLastError ( ERROR_NOT_ENOUGH_MEMORY
);
928 /* check for drive letter */
929 if ( longpath
[1] == ':' ) {
930 tmpshortpath
[0] = longpath
[0];
931 tmpshortpath
[1] = ':';
935 if ( ( drive
= DOSFS_GetPathDrive ( &longpath
)) == -1 ) return 0;
936 flags
= DRIVE_GetFlags ( drive
);
938 while ( longpath
[lp
] ) {
940 /* check for path delimiters and reproduce them */
941 if ( longpath
[lp
] == '\\' || longpath
[lp
] == '/' ) {
942 if (!sp
|| tmpshortpath
[sp
-1]!= '\\')
944 /* strip double "\\" */
945 tmpshortpath
[sp
] = '\\';
948 tmpshortpath
[sp
]=0;/*terminate string*/
953 tmplen
= strcspn ( longpath
+ lp
, "\\/" );
954 lstrcpynA ( tmpshortpath
+sp
, longpath
+ lp
, tmplen
+1 );
956 /* Check, if the current element is a valid dos name */
957 if ( DOSFS_ValidDOSName ( longpath
+ lp
, !(flags
& DRIVE_CASE_SENSITIVE
) ) ) {
963 /* Check if the file exists and use the existing file name */
964 if ( DOSFS_GetFullName ( tmpshortpath
, TRUE
, &full_name
) ) {
965 lstrcpyA ( tmpshortpath
+sp
, strrchr ( full_name
.short_name
, '\\' ) + 1 );
966 sp
+= lstrlenA ( tmpshortpath
+sp
);
971 TRACE("not found!\n" );
972 SetLastError ( ERROR_FILE_NOT_FOUND
);
975 tmpshortpath
[sp
] = 0;
977 lstrcpynA ( shortpath
, tmpshortpath
, shortlen
);
978 TRACE("returning %s\n", debugstr_a(shortpath
) );
979 tmplen
= lstrlenA ( tmpshortpath
);
980 HeapFree ( GetProcessHeap(), 0, tmpshortpath
);
986 /***********************************************************************
987 * GetShortPathNameW (KERNEL32.272)
989 DWORD WINAPI
GetShortPathNameW( LPCWSTR longpath
, LPWSTR shortpath
,
992 LPSTR longpathA
, shortpathA
;
995 longpathA
= HEAP_strdupWtoA( GetProcessHeap(), 0, longpath
);
996 shortpathA
= HeapAlloc ( GetProcessHeap(), 0, shortlen
);
998 ret
= GetShortPathNameA ( longpathA
, shortpathA
, shortlen
);
999 lstrcpynAtoW ( shortpath
, shortpathA
, shortlen
);
1001 HeapFree( GetProcessHeap(), 0, longpathA
);
1002 HeapFree( GetProcessHeap(), 0, shortpathA
);
1008 /***********************************************************************
1009 * GetLongPathNameA (KERNEL32.xxx)
1011 DWORD WINAPI
GetLongPathNameA( LPCSTR shortpath
, LPSTR longpath
,
1014 DOS_FULL_NAME full_name
;
1015 char *p
, *r
, *ll
, *ss
;
1017 if (!DOSFS_GetFullName( shortpath
, TRUE
, &full_name
)) return 0;
1018 lstrcpynA( longpath
, full_name
.short_name
, longlen
);
1020 /* Do some hackery to get the long filename. */
1023 ss
=longpath
+strlen(longpath
);
1024 ll
=full_name
.long_name
+strlen(full_name
.long_name
);
1026 while (ss
>=longpath
)
1028 /* FIXME: aren't we more paranoid, than needed? */
1029 while ((ss
[0]=='\\') && (ss
>=longpath
)) ss
--;
1031 while ((ss
[0]!='\\') && (ss
>=longpath
)) ss
--;
1034 /* FIXME: aren't we more paranoid, than needed? */
1035 while ((ll
[0]=='/') && (ll
>=full_name
.long_name
)) ll
--;
1036 while ((ll
[0]!='/') && (ll
>=full_name
.long_name
)) ll
--;
1037 if (ll
<full_name
.long_name
)
1039 ERR("Bad longname! (ss=%s ll=%s)\n This should never happen !\n"
1046 /* FIXME: fix for names like "C:\\" (ie. with more '\'s) */
1050 if ((p
-longpath
)>0) longlen
-= (p
-longpath
);
1051 lstrcpynA( p
, ll
, longlen
);
1053 /* Now, change all '/' to '\' */
1054 for (r
=p
; r
<(p
+longlen
); r
++ )
1055 if (r
[0]=='/') r
[0]='\\';
1056 return strlen(longpath
) - strlen(p
) + longlen
;
1060 return strlen(longpath
);
1064 /***********************************************************************
1065 * GetLongPathNameW (KERNEL32.269)
1067 DWORD WINAPI
GetLongPathNameW( LPCWSTR shortpath
, LPWSTR longpath
,
1070 DOS_FULL_NAME full_name
;
1072 LPSTR shortpathA
= HEAP_strdupWtoA( GetProcessHeap(), 0, shortpath
);
1074 /* FIXME: is it correct to always return a fully qualified short path? */
1075 if (DOSFS_GetFullName( shortpathA
, TRUE
, &full_name
))
1077 ret
= strlen( full_name
.short_name
);
1078 lstrcpynAtoW( longpath
, full_name
.long_name
, longlen
);
1080 HeapFree( GetProcessHeap(), 0, shortpathA
);
1085 /***********************************************************************
1086 * DOSFS_DoGetFullPathName
1088 * Implementation of GetFullPathNameA/W.
1090 * bon@elektron 000331:
1091 * A test for GetFullPathName with many patholotical case
1092 * gives now identical output for Wine and OSR2
1094 static DWORD
DOSFS_DoGetFullPathName( LPCSTR name
, DWORD len
, LPSTR result
,
1098 DOS_FULL_NAME full_name
;
1101 char drivecur
[]="c:.";
1103 int namelen
,drive
=0;
1105 if ((strlen(name
) >1)&& (name
[1]==':'))
1106 /*drive letter given */
1108 driveletter
= name
[0];
1110 if ((strlen(name
) >2)&& (name
[1]==':') &&
1111 ((name
[2]=='\\') || (name
[2]=='/')))
1112 /*absolute path given */
1114 lstrcpynA(full_name
.short_name
,name
,MAX_PATHNAME_LEN
);
1115 drive
= (int)toupper(name
[0]) - 'A';
1120 drivecur
[0]=driveletter
;
1122 strcpy(drivecur
,".");
1123 if (!DOSFS_GetFullName( drivecur
, FALSE
, &full_name
))
1125 FIXME("internal: error getting drive/path\n");
1128 /* find path that drive letter substitutes*/
1129 drive
= (int)toupper(full_name
.short_name
[0]) -0x41;
1130 root
= DRIVE_GetRoot(drive
);
1133 FIXME("internal: error getting DOS Drive Root\n");
1136 p
= full_name
.long_name
+strlen(root
);
1137 /* append long name (= unix name) to drive */
1138 lstrcpynA(full_name
.short_name
+2,p
,MAX_PATHNAME_LEN
-3);
1139 /* append name to treat */
1140 namelen
= strlen(full_name
.short_name
);
1143 p
+= +2; /* skip drive name when appending */
1144 if (namelen
+2 + strlen(p
) > MAX_PATHNAME_LEN
)
1146 FIXME("internal error: buffer too small\n");
1149 full_name
.short_name
[namelen
++] ='\\';
1150 full_name
.short_name
[namelen
] = 0;
1151 lstrcpynA(full_name
.short_name
+namelen
,p
,MAX_PATHNAME_LEN
-namelen
);
1153 /* reverse all slashes */
1154 for (p
=full_name
.short_name
;
1155 p
< full_name
.short_name
+strlen(full_name
.short_name
);
1161 /* Use memmove, as areas overlap*/
1163 while ((p
= strstr(full_name
.short_name
,"\\..\\")))
1165 if (p
> full_name
.short_name
+2)
1168 q
= strrchr(full_name
.short_name
,'\\');
1169 memmove(q
+1,p
+4,strlen(p
+4)+1);
1173 memmove(full_name
.short_name
+3,p
+4,strlen(p
+4)+1);
1176 if ((full_name
.short_name
[2]=='.')&&(full_name
.short_name
[3]=='.'))
1178 /* This case istn't treated yet : c:..\test */
1179 memmove(full_name
.short_name
+2,full_name
.short_name
+4,
1180 strlen(full_name
.short_name
+4)+1);
1183 while ((p
= strstr(full_name
.short_name
,"\\.\\")))
1186 memmove(p
+1,p
+3,strlen(p
+3)+1);
1188 if (!(DRIVE_GetFlags(drive
) & DRIVE_CASE_PRESERVING
))
1189 CharUpperA( full_name
.short_name
);
1190 namelen
=strlen(full_name
.short_name
);
1191 if (!strcmp(full_name
.short_name
+namelen
-3,"\\.."))
1193 /* one more starnge case: "c:\test\test1\.."
1195 *(full_name
.short_name
+namelen
-3)=0;
1196 q
= strrchr(full_name
.short_name
,'\\');
1199 if (full_name
.short_name
[namelen
-1]=='.')
1200 full_name
.short_name
[(namelen
--)-1] =0;
1202 if (full_name
.short_name
[namelen
-1]=='\\')
1203 full_name
.short_name
[(namelen
--)-1] =0;
1204 TRACE("got %s\n",full_name
.short_name
);
1206 /* If the lpBuffer buffer is too small, the return value is the
1207 size of the buffer, in characters, required to hold the path
1208 plus the terminating \0 (tested against win95osr, bon 001118)
1210 ret
= strlen(full_name
.short_name
);
1213 /* don't touch anything when the buffer is not large enough */
1214 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1220 lstrcpynAtoW( (LPWSTR
)result
, full_name
.short_name
, len
);
1222 lstrcpynA( result
, full_name
.short_name
, len
);
1225 TRACE("returning '%s'\n", full_name
.short_name
);
1230 /***********************************************************************
1231 * GetFullPathNameA (KERNEL32.272)
1233 * if the path closed with '\', *lastpart is 0
1235 DWORD WINAPI
GetFullPathNameA( LPCSTR name
, DWORD len
, LPSTR buffer
,
1238 DWORD ret
= DOSFS_DoGetFullPathName( name
, len
, buffer
, FALSE
);
1239 if (ret
&& (ret
<=len
) && buffer
&& lastpart
)
1241 LPSTR p
= buffer
+ strlen(buffer
);
1245 while ((p
> buffer
+ 2) && (*p
!= '\\')) p
--;
1248 else *lastpart
= NULL
;
1254 /***********************************************************************
1255 * GetFullPathNameW (KERNEL32.273)
1257 DWORD WINAPI
GetFullPathNameW( LPCWSTR name
, DWORD len
, LPWSTR buffer
,
1260 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
1261 DWORD ret
= DOSFS_DoGetFullPathName( nameA
, len
, (LPSTR
)buffer
, TRUE
);
1262 HeapFree( GetProcessHeap(), 0, nameA
);
1263 if (ret
&& (ret
<=len
) && buffer
&& lastpart
)
1265 LPWSTR p
= buffer
+ lstrlenW(buffer
);
1266 if (*p
!= (WCHAR
)'\\')
1268 while ((p
> buffer
+ 2) && (*p
!= (WCHAR
)'\\')) p
--;
1271 else *lastpart
= NULL
;
1276 /***********************************************************************
1279 static int DOSFS_FindNextEx( FIND_FIRST_INFO
*info
, WIN32_FIND_DATAA
*entry
)
1281 BYTE attr
= info
->attr
| FA_UNUSED
| FA_ARCHIVE
| FA_RDONLY
;
1282 UINT flags
= DRIVE_GetFlags( info
->drive
);
1283 char *p
, buffer
[MAX_PATHNAME_LEN
];
1284 const char *drive_path
;
1286 LPCSTR long_name
, short_name
;
1287 BY_HANDLE_FILE_INFORMATION fileinfo
;
1290 if ((info
->attr
& ~(FA_UNUSED
| FA_ARCHIVE
| FA_RDONLY
)) == FA_LABEL
)
1292 if (info
->cur_pos
) return 0;
1293 entry
->dwFileAttributes
= FILE_ATTRIBUTE_LABEL
;
1294 DOSFS_UnixTimeToFileTime( (time_t)0, &entry
->ftCreationTime
, 0 );
1295 DOSFS_UnixTimeToFileTime( (time_t)0, &entry
->ftLastAccessTime
, 0 );
1296 DOSFS_UnixTimeToFileTime( (time_t)0, &entry
->ftLastWriteTime
, 0 );
1297 entry
->nFileSizeHigh
= 0;
1298 entry
->nFileSizeLow
= 0;
1299 entry
->dwReserved0
= 0;
1300 entry
->dwReserved1
= 0;
1301 DOSFS_ToDosDTAFormat( DRIVE_GetLabel( info
->drive
), entry
->cFileName
);
1302 strcpy( entry
->cAlternateFileName
, entry
->cFileName
);
1307 drive_path
= info
->path
+ strlen(DRIVE_GetRoot( info
->drive
));
1308 while ((*drive_path
== '/') || (*drive_path
== '\\')) drive_path
++;
1309 drive_root
= !*drive_path
;
1311 lstrcpynA( buffer
, info
->path
, sizeof(buffer
) - 1 );
1312 strcat( buffer
, "/" );
1313 p
= buffer
+ strlen(buffer
);
1315 while (DOSFS_ReadDir( info
->dir
, &long_name
, &short_name
))
1319 /* Don't return '.' and '..' in the root of the drive */
1320 if (drive_root
&& (long_name
[0] == '.') &&
1321 (!long_name
[1] || ((long_name
[1] == '.') && !long_name
[2])))
1324 /* Check the long mask */
1326 if (info
->long_mask
)
1328 if (!DOSFS_MatchLong( info
->long_mask
, long_name
,
1329 flags
& DRIVE_CASE_SENSITIVE
)) continue;
1332 /* Check the short mask */
1334 if (info
->short_mask
)
1338 DOSFS_Hash( long_name
, dos_name
, TRUE
,
1339 !(flags
& DRIVE_CASE_SENSITIVE
) );
1340 short_name
= dos_name
;
1342 if (!DOSFS_MatchShort( info
->short_mask
, short_name
)) continue;
1345 /* Check the file attributes */
1347 lstrcpynA( p
, long_name
, sizeof(buffer
) - (int)(p
- buffer
) );
1348 if (!FILE_Stat( buffer
, &fileinfo
))
1350 WARN("can't stat %s\n", buffer
);
1353 if (fileinfo
.dwFileAttributes
& ~attr
) continue;
1355 /* We now have a matching entry; fill the result and return */
1357 entry
->dwFileAttributes
= fileinfo
.dwFileAttributes
;
1358 entry
->ftCreationTime
= fileinfo
.ftCreationTime
;
1359 entry
->ftLastAccessTime
= fileinfo
.ftLastAccessTime
;
1360 entry
->ftLastWriteTime
= fileinfo
.ftLastWriteTime
;
1361 entry
->nFileSizeHigh
= fileinfo
.nFileSizeHigh
;
1362 entry
->nFileSizeLow
= fileinfo
.nFileSizeLow
;
1365 DOSFS_ToDosDTAFormat( short_name
, entry
->cAlternateFileName
);
1367 DOSFS_Hash( long_name
, entry
->cAlternateFileName
, FALSE
,
1368 !(flags
& DRIVE_CASE_SENSITIVE
) );
1370 lstrcpynA( entry
->cFileName
, long_name
, sizeof(entry
->cFileName
) );
1371 if (!(flags
& DRIVE_CASE_PRESERVING
)) CharLowerA( entry
->cFileName
);
1372 TRACE("returning %s (%s) %02lx %ld\n",
1373 entry
->cFileName
, entry
->cAlternateFileName
,
1374 entry
->dwFileAttributes
, entry
->nFileSizeLow
);
1377 return 0; /* End of directory */
1380 /***********************************************************************
1383 * Find the next matching file. Return the number of entries read to find
1384 * the matching one, or 0 if no more entries.
1385 * 'short_mask' is the 8.3 mask (in FCB format), 'long_mask' is the long
1386 * file name mask. Either or both can be NULL.
1388 * NOTE: This is supposed to be only called by the int21 emulation
1389 * routines. Thus, we should own the Win16Mutex anyway.
1390 * Nevertheless, we explicitly enter it to ensure the static
1391 * directory cache is protected.
1393 int DOSFS_FindNext( const char *path
, const char *short_mask
,
1394 const char *long_mask
, int drive
, BYTE attr
,
1395 int skip
, WIN32_FIND_DATAA
*entry
)
1397 static FIND_FIRST_INFO info
= { NULL
};
1398 LPCSTR short_name
, long_name
;
1401 SYSLEVEL_EnterWin16Lock();
1403 /* Check the cached directory */
1404 if (!(info
.dir
&& info
.path
== path
&& info
.short_mask
== short_mask
1405 && info
.long_mask
== long_mask
&& info
.drive
== drive
1406 && info
.attr
== attr
&& info
.cur_pos
<= skip
))
1408 /* Not in the cache, open it anew */
1409 if (info
.dir
) DOSFS_CloseDir( info
.dir
);
1411 info
.path
= (LPSTR
)path
;
1412 info
.long_mask
= (LPSTR
)long_mask
;
1413 info
.short_mask
= (LPSTR
)short_mask
;
1417 info
.dir
= DOSFS_OpenDir( info
.path
);
1420 /* Skip to desired position */
1421 while (info
.cur_pos
< skip
)
1422 if (info
.dir
&& DOSFS_ReadDir( info
.dir
, &long_name
, &short_name
))
1427 if (info
.dir
&& info
.cur_pos
== skip
&& DOSFS_FindNextEx( &info
, entry
))
1428 count
= info
.cur_pos
- skip
;
1434 if (info
.dir
) DOSFS_CloseDir( info
.dir
);
1435 memset( &info
, '\0', sizeof(info
) );
1438 SYSLEVEL_LeaveWin16Lock();
1443 /*************************************************************************
1444 * FindFirstFileExA (KERNEL32)
1446 HANDLE WINAPI
FindFirstFileExA(
1448 FINDEX_INFO_LEVELS fInfoLevelId
,
1449 LPVOID lpFindFileData
,
1450 FINDEX_SEARCH_OPS fSearchOp
,
1451 LPVOID lpSearchFilter
,
1452 DWORD dwAdditionalFlags
)
1454 DOS_FULL_NAME full_name
;
1456 FIND_FIRST_INFO
*info
;
1458 if ((fSearchOp
!= FindExSearchNameMatch
) || (dwAdditionalFlags
!= 0))
1460 FIXME("options not implemented 0x%08x 0x%08lx\n", fSearchOp
, dwAdditionalFlags
);
1461 return INVALID_HANDLE_VALUE
;
1464 switch(fInfoLevelId
)
1466 case FindExInfoStandard
:
1468 WIN32_FIND_DATAA
* data
= (WIN32_FIND_DATAA
*) lpFindFileData
;
1469 data
->dwReserved0
= data
->dwReserved1
= 0x0;
1470 if (!lpFileName
) return 0;
1471 if (!DOSFS_GetFullName( lpFileName
, FALSE
, &full_name
)) break;
1472 if (!(handle
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(FIND_FIRST_INFO
)))) break;
1473 info
= (FIND_FIRST_INFO
*)GlobalLock( handle
);
1474 info
->path
= HEAP_strdupA( GetProcessHeap(), 0, full_name
.long_name
);
1475 info
->long_mask
= strrchr( info
->path
, '/' );
1476 *(info
->long_mask
++) = '\0';
1477 info
->short_mask
= NULL
;
1479 if (lpFileName
[0] && (lpFileName
[1] == ':'))
1480 info
->drive
= toupper(*lpFileName
) - 'A';
1481 else info
->drive
= DRIVE_GetCurrentDrive();
1484 info
->dir
= DOSFS_OpenDir( info
->path
);
1486 GlobalUnlock( handle
);
1487 if (!FindNextFileA( handle
, data
))
1489 FindClose( handle
);
1490 SetLastError( ERROR_NO_MORE_FILES
);
1497 FIXME("fInfoLevelId 0x%08x not implemented\n", fInfoLevelId
);
1499 return INVALID_HANDLE_VALUE
;
1502 /*************************************************************************
1503 * FindFirstFileA (KERNEL32.123)
1505 HANDLE WINAPI
FindFirstFileA(
1507 WIN32_FIND_DATAA
*lpFindData
)
1509 return FindFirstFileExA(lpFileName
, FindExInfoStandard
, lpFindData
,
1510 FindExSearchNameMatch
, NULL
, 0);
1513 /*************************************************************************
1514 * FindFirstFileExW (KERNEL32)
1516 HANDLE WINAPI
FindFirstFileExW(
1518 FINDEX_INFO_LEVELS fInfoLevelId
,
1519 LPVOID lpFindFileData
,
1520 FINDEX_SEARCH_OPS fSearchOp
,
1521 LPVOID lpSearchFilter
,
1522 DWORD dwAdditionalFlags
)
1525 WIN32_FIND_DATAA dataA
;
1526 LPVOID _lpFindFileData
;
1529 switch(fInfoLevelId
)
1531 case FindExInfoStandard
:
1533 _lpFindFileData
= &dataA
;
1537 FIXME("fInfoLevelId 0x%08x not implemented\n", fInfoLevelId
);
1538 return INVALID_HANDLE_VALUE
;
1541 pathA
= HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName
);
1542 handle
= FindFirstFileExA(pathA
, fInfoLevelId
, _lpFindFileData
, fSearchOp
, lpSearchFilter
, dwAdditionalFlags
);
1543 HeapFree( GetProcessHeap(), 0, pathA
);
1544 if (handle
== INVALID_HANDLE_VALUE
) return handle
;
1546 switch(fInfoLevelId
)
1548 case FindExInfoStandard
:
1550 WIN32_FIND_DATAW
*dataW
= (WIN32_FIND_DATAW
*) lpFindFileData
;
1551 dataW
->dwFileAttributes
= dataA
.dwFileAttributes
;
1552 dataW
->ftCreationTime
= dataA
.ftCreationTime
;
1553 dataW
->ftLastAccessTime
= dataA
.ftLastAccessTime
;
1554 dataW
->ftLastWriteTime
= dataA
.ftLastWriteTime
;
1555 dataW
->nFileSizeHigh
= dataA
.nFileSizeHigh
;
1556 dataW
->nFileSizeLow
= dataA
.nFileSizeLow
;
1557 lstrcpyAtoW( dataW
->cFileName
, dataA
.cFileName
);
1558 lstrcpyAtoW( dataW
->cAlternateFileName
, dataA
.cAlternateFileName
);
1562 FIXME("fInfoLevelId 0x%08x not implemented\n", fInfoLevelId
);
1563 return INVALID_HANDLE_VALUE
;
1568 /*************************************************************************
1569 * FindFirstFileW (KERNEL32.124)
1571 HANDLE WINAPI
FindFirstFileW( LPCWSTR lpFileName
, WIN32_FIND_DATAW
*lpFindData
)
1573 return FindFirstFileExW(lpFileName
, FindExInfoStandard
, lpFindData
,
1574 FindExSearchNameMatch
, NULL
, 0);
1577 /*************************************************************************
1578 * FindNextFileA (KERNEL32.126)
1580 BOOL WINAPI
FindNextFileA( HANDLE handle
, WIN32_FIND_DATAA
*data
)
1582 FIND_FIRST_INFO
*info
;
1584 if ((handle
== INVALID_HANDLE_VALUE
) ||
1585 !(info
= (FIND_FIRST_INFO
*)GlobalLock( handle
)))
1587 SetLastError( ERROR_INVALID_HANDLE
);
1590 GlobalUnlock( handle
);
1591 if (!info
->path
|| !info
->dir
)
1593 SetLastError( ERROR_NO_MORE_FILES
);
1596 if (!DOSFS_FindNextEx( info
, data
))
1598 DOSFS_CloseDir( info
->dir
); info
->dir
= NULL
;
1599 HeapFree( GetProcessHeap(), 0, info
->path
);
1600 info
->path
= info
->long_mask
= NULL
;
1601 SetLastError( ERROR_NO_MORE_FILES
);
1608 /*************************************************************************
1609 * FindNextFileW (KERNEL32.127)
1611 BOOL WINAPI
FindNextFileW( HANDLE handle
, WIN32_FIND_DATAW
*data
)
1613 WIN32_FIND_DATAA dataA
;
1614 if (!FindNextFileA( handle
, &dataA
)) return FALSE
;
1615 data
->dwFileAttributes
= dataA
.dwFileAttributes
;
1616 data
->ftCreationTime
= dataA
.ftCreationTime
;
1617 data
->ftLastAccessTime
= dataA
.ftLastAccessTime
;
1618 data
->ftLastWriteTime
= dataA
.ftLastWriteTime
;
1619 data
->nFileSizeHigh
= dataA
.nFileSizeHigh
;
1620 data
->nFileSizeLow
= dataA
.nFileSizeLow
;
1621 lstrcpyAtoW( data
->cFileName
, dataA
.cFileName
);
1622 lstrcpyAtoW( data
->cAlternateFileName
, dataA
.cAlternateFileName
);
1626 /*************************************************************************
1627 * FindClose (KERNEL32.119)
1629 BOOL WINAPI
FindClose( HANDLE handle
)
1631 FIND_FIRST_INFO
*info
;
1633 if ((handle
== INVALID_HANDLE_VALUE
) ||
1634 !(info
= (FIND_FIRST_INFO
*)GlobalLock( handle
)))
1636 SetLastError( ERROR_INVALID_HANDLE
);
1639 if (info
->dir
) DOSFS_CloseDir( info
->dir
);
1640 if (info
->path
) HeapFree( GetProcessHeap(), 0, info
->path
);
1641 GlobalUnlock( handle
);
1642 GlobalFree( handle
);
1646 /***********************************************************************
1647 * DOSFS_UnixTimeToFileTime
1649 * Convert a Unix time to FILETIME format.
1650 * The FILETIME structure is a 64-bit value representing the number of
1651 * 100-nanosecond intervals since January 1, 1601, 0:00.
1652 * 'remainder' is the nonnegative number of 100-ns intervals
1653 * corresponding to the time fraction smaller than 1 second that
1654 * couldn't be stored in the time_t value.
1656 void DOSFS_UnixTimeToFileTime( time_t unix_time
, FILETIME
*filetime
,
1662 The time difference between 1 January 1601, 00:00:00 and
1663 1 January 1970, 00:00:00 is 369 years, plus the leap years
1664 from 1604 to 1968, excluding 1700, 1800, 1900.
1665 This makes (1968 - 1600) / 4 - 3 = 89 leap days, and a total
1668 Any day in that period had 24 * 60 * 60 = 86400 seconds.
1670 The time difference is 134774 * 86400 * 10000000, which can be written
1672 27111902 * 2^32 + 3577643008
1673 413 * 2^48 + 45534 * 2^32 + 54590 * 2^16 + 32768
1675 If you find that these constants are buggy, please change them in all
1676 instances in both conversion functions.
1679 There are two versions, one of them uses long long variables and
1680 is presumably faster but not ISO C. The other one uses standard C
1681 data types and operations but relies on the assumption that negative
1682 numbers are stored as 2's complement (-1 is 0xffff....). If this
1683 assumption is violated, dates before 1970 will not convert correctly.
1684 This should however work on any reasonable architecture where WINE
1689 Take care not to remove the casts. I have tested these functions
1690 (in both versions) for a lot of numbers. I would be interested in
1691 results on other compilers than GCC.
1693 The operations have been designed to account for the possibility
1694 of 64-bit time_t in future UNICES. Even the versions without
1695 internal long long numbers will work if time_t only is 64 bit.
1696 A 32-bit shift, which was necessary for that operation, turned out
1697 not to work correctly in GCC, besides giving the warning. So I
1698 used a double 16-bit shift instead. Numbers are in the ISO version
1699 represented by three limbs, the most significant with 32 bit, the
1700 other two with 16 bit each.
1702 As the modulo-operator % is not well-defined for negative numbers,
1703 negative divisors have been avoided in DOSFS_FileTimeToUnixTime.
1705 There might be quicker ways to do this in C. Certainly so in
1708 Claus Fischer, fischer@iue.tuwien.ac.at
1711 #if SIZEOF_LONG_LONG >= 8
1712 # define USE_LONG_LONG 1
1714 # define USE_LONG_LONG 0
1717 #if USE_LONG_LONG /* gcc supports long long type */
1719 long long int t
= unix_time
;
1721 t
+= 116444736000000000LL;
1723 filetime
->dwLowDateTime
= (UINT
)t
;
1724 filetime
->dwHighDateTime
= (UINT
)(t
>> 32);
1726 #else /* ISO version */
1728 UINT a0
; /* 16 bit, low bits */
1729 UINT a1
; /* 16 bit, medium bits */
1730 UINT a2
; /* 32 bit, high bits */
1732 /* Copy the unix time to a2/a1/a0 */
1733 a0
= unix_time
& 0xffff;
1734 a1
= (unix_time
>> 16) & 0xffff;
1735 /* This is obsolete if unix_time is only 32 bits, but it does not hurt.
1736 Do not replace this by >> 32, it gives a compiler warning and it does
1738 a2
= (unix_time
>= 0 ? (unix_time
>> 16) >> 16 :
1739 ~((~unix_time
>> 16) >> 16));
1741 /* Multiply a by 10000000 (a = a2/a1/a0)
1742 Split the factor into 10000 * 1000 which are both less than 0xffff. */
1744 a1
= a1
* 10000 + (a0
>> 16);
1745 a2
= a2
* 10000 + (a1
>> 16);
1750 a1
= a1
* 1000 + (a0
>> 16);
1751 a2
= a2
* 1000 + (a1
>> 16);
1755 /* Add the time difference and the remainder */
1756 a0
+= 32768 + (remainder
& 0xffff);
1757 a1
+= 54590 + (remainder
>> 16 ) + (a0
>> 16);
1758 a2
+= 27111902 + (a1
>> 16);
1763 filetime
->dwLowDateTime
= (a1
<< 16) + a0
;
1764 filetime
->dwHighDateTime
= a2
;
1769 /***********************************************************************
1770 * DOSFS_FileTimeToUnixTime
1772 * Convert a FILETIME format to Unix time.
1773 * If not NULL, 'remainder' contains the fractional part of the filetime,
1774 * in the range of [0..9999999] (even if time_t is negative).
1776 time_t DOSFS_FileTimeToUnixTime( const FILETIME
*filetime
, DWORD
*remainder
)
1778 /* Read the comment in the function DOSFS_UnixTimeToFileTime. */
1781 long long int t
= filetime
->dwHighDateTime
;
1783 t
+= (UINT
)filetime
->dwLowDateTime
;
1784 t
-= 116444736000000000LL;
1787 if (remainder
) *remainder
= 9999999 - (-t
- 1) % 10000000;
1788 return -1 - ((-t
- 1) / 10000000);
1792 if (remainder
) *remainder
= t
% 10000000;
1793 return t
/ 10000000;
1796 #else /* ISO version */
1798 UINT a0
; /* 16 bit, low bits */
1799 UINT a1
; /* 16 bit, medium bits */
1800 UINT a2
; /* 32 bit, high bits */
1801 UINT r
; /* remainder of division */
1802 unsigned int carry
; /* carry bit for subtraction */
1803 int negative
; /* whether a represents a negative value */
1805 /* Copy the time values to a2/a1/a0 */
1806 a2
= (UINT
)filetime
->dwHighDateTime
;
1807 a1
= ((UINT
)filetime
->dwLowDateTime
) >> 16;
1808 a0
= ((UINT
)filetime
->dwLowDateTime
) & 0xffff;
1810 /* Subtract the time difference */
1811 if (a0
>= 32768 ) a0
-= 32768 , carry
= 0;
1812 else a0
+= (1 << 16) - 32768 , carry
= 1;
1814 if (a1
>= 54590 + carry
) a1
-= 54590 + carry
, carry
= 0;
1815 else a1
+= (1 << 16) - 54590 - carry
, carry
= 1;
1817 a2
-= 27111902 + carry
;
1819 /* If a is negative, replace a by (-1-a) */
1820 negative
= (a2
>= ((UINT
)1) << 31);
1823 /* Set a to -a - 1 (a is a2/a1/a0) */
1829 /* Divide a by 10000000 (a = a2/a1/a0), put the rest into r.
1830 Split the divisor into 10000 * 1000 which are both less than 0xffff. */
1831 a1
+= (a2
% 10000) << 16;
1833 a0
+= (a1
% 10000) << 16;
1838 a1
+= (a2
% 1000) << 16;
1840 a0
+= (a1
% 1000) << 16;
1842 r
+= (a0
% 1000) * 10000;
1845 /* If a was negative, replace a by (-1-a) and r by (9999999 - r) */
1848 /* Set a to -a - 1 (a is a2/a1/a0) */
1856 if (remainder
) *remainder
= r
;
1858 /* Do not replace this by << 32, it gives a compiler warning and it does
1860 return ((((time_t)a2
) << 16) << 16) + (a1
<< 16) + a0
;
1865 /***********************************************************************
1866 * DosDateTimeToFileTime (KERNEL32.76)
1868 BOOL WINAPI
DosDateTimeToFileTime( WORD fatdate
, WORD fattime
, LPFILETIME ft
)
1872 newtm
.tm_sec
= (fattime
& 0x1f) * 2;
1873 newtm
.tm_min
= (fattime
>> 5) & 0x3f;
1874 newtm
.tm_hour
= (fattime
>> 11);
1875 newtm
.tm_mday
= (fatdate
& 0x1f);
1876 newtm
.tm_mon
= ((fatdate
>> 5) & 0x0f) - 1;
1877 newtm
.tm_year
= (fatdate
>> 9) + 80;
1878 DOSFS_UnixTimeToFileTime( mktime( &newtm
), ft
, 0 );
1883 /***********************************************************************
1884 * FileTimeToDosDateTime (KERNEL32.111)
1886 BOOL WINAPI
FileTimeToDosDateTime( const FILETIME
*ft
, LPWORD fatdate
,
1889 time_t unixtime
= DOSFS_FileTimeToUnixTime( ft
, NULL
);
1890 struct tm
*tm
= localtime( &unixtime
);
1892 *fattime
= (tm
->tm_hour
<< 11) + (tm
->tm_min
<< 5) + (tm
->tm_sec
/ 2);
1894 *fatdate
= ((tm
->tm_year
- 80) << 9) + ((tm
->tm_mon
+ 1) << 5)
1900 /***********************************************************************
1901 * LocalFileTimeToFileTime (KERNEL32.373)
1903 BOOL WINAPI
LocalFileTimeToFileTime( const FILETIME
*localft
,
1909 /* convert from local to UTC. Perhaps not correct. FIXME */
1910 time_t unixtime
= DOSFS_FileTimeToUnixTime( localft
, &remainder
);
1911 xtm
= gmtime( &unixtime
);
1912 DOSFS_UnixTimeToFileTime( mktime(xtm
), utcft
, remainder
);
1917 /***********************************************************************
1918 * FileTimeToLocalFileTime (KERNEL32.112)
1920 BOOL WINAPI
FileTimeToLocalFileTime( const FILETIME
*utcft
,
1921 LPFILETIME localft
)
1924 /* convert from UTC to local. Perhaps not correct. FIXME */
1925 time_t unixtime
= DOSFS_FileTimeToUnixTime( utcft
, &remainder
);
1927 struct tm
*xtm
= localtime( &unixtime
);
1930 localtime
= timegm(xtm
);
1931 DOSFS_UnixTimeToFileTime( localtime
, localft
, remainder
);
1934 struct tm
*xtm
,*gtm
;
1937 xtm
= localtime( &unixtime
);
1938 gtm
= gmtime( &unixtime
);
1939 time1
= mktime(xtm
);
1940 time2
= mktime(gtm
);
1941 DOSFS_UnixTimeToFileTime( 2*time1
-time2
, localft
, remainder
);
1947 /***********************************************************************
1948 * FileTimeToSystemTime (KERNEL32.113)
1950 BOOL WINAPI
FileTimeToSystemTime( const FILETIME
*ft
, LPSYSTEMTIME syst
)
1954 time_t xtime
= DOSFS_FileTimeToUnixTime( ft
, &remainder
);
1955 xtm
= gmtime(&xtime
);
1956 syst
->wYear
= xtm
->tm_year
+1900;
1957 syst
->wMonth
= xtm
->tm_mon
+ 1;
1958 syst
->wDayOfWeek
= xtm
->tm_wday
;
1959 syst
->wDay
= xtm
->tm_mday
;
1960 syst
->wHour
= xtm
->tm_hour
;
1961 syst
->wMinute
= xtm
->tm_min
;
1962 syst
->wSecond
= xtm
->tm_sec
;
1963 syst
->wMilliseconds
= remainder
/ 10000;
1967 /***********************************************************************
1968 * QueryDosDeviceA (KERNEL32.413)
1970 * returns array of strings terminated by \0, terminated by \0
1972 DWORD WINAPI
QueryDosDeviceA(LPCSTR devname
,LPSTR target
,DWORD bufsize
)
1977 TRACE("(%s,...)\n", devname
? devname
: "<null>");
1979 /* return known MSDOS devices */
1980 strcpy(buffer
,"CON COM1 COM2 LPT1 NUL ");
1981 while ((s
=strchr(buffer
,' ')))
1984 lstrcpynA(target
,buffer
,bufsize
);
1985 return strlen(buffer
);
1987 strcpy(buffer
,"\\DEV\\");
1988 strcat(buffer
,devname
);
1989 if ((s
=strchr(buffer
,':'))) *s
='\0';
1990 lstrcpynA(target
,buffer
,bufsize
);
1991 return strlen(buffer
);
1995 /***********************************************************************
1996 * QueryDosDeviceW (KERNEL32.414)
1998 * returns array of strings terminated by \0, terminated by \0
2000 DWORD WINAPI
QueryDosDeviceW(LPCWSTR devname
,LPWSTR target
,DWORD bufsize
)
2002 LPSTR devnameA
= devname
?HEAP_strdupWtoA(GetProcessHeap(),0,devname
):NULL
;
2003 LPSTR targetA
= (LPSTR
)HeapAlloc(GetProcessHeap(),0,bufsize
);
2004 DWORD ret
= QueryDosDeviceA(devnameA
,targetA
,bufsize
);
2006 lstrcpynAtoW(target
,targetA
,bufsize
);
2007 if (devnameA
) HeapFree(GetProcessHeap(),0,devnameA
);
2008 if (targetA
) HeapFree(GetProcessHeap(),0,targetA
);
2013 /***********************************************************************
2014 * SystemTimeToFileTime (KERNEL32.526)
2016 BOOL WINAPI
SystemTimeToFileTime( const SYSTEMTIME
*syst
, LPFILETIME ft
)
2022 struct tm xtm
,*local_tm
,*utc_tm
;
2023 time_t localtim
,utctime
;
2026 xtm
.tm_year
= syst
->wYear
-1900;
2027 xtm
.tm_mon
= syst
->wMonth
- 1;
2028 xtm
.tm_wday
= syst
->wDayOfWeek
;
2029 xtm
.tm_mday
= syst
->wDay
;
2030 xtm
.tm_hour
= syst
->wHour
;
2031 xtm
.tm_min
= syst
->wMinute
;
2032 xtm
.tm_sec
= syst
->wSecond
; /* this is UTC */
2035 utctime
= timegm(&xtm
);
2036 DOSFS_UnixTimeToFileTime( utctime
, ft
,
2037 syst
->wMilliseconds
* 10000 );
2039 localtim
= mktime(&xtm
); /* now we've got local time */
2040 local_tm
= localtime(&localtim
);
2041 utc_tm
= gmtime(&localtim
);
2042 utctime
= mktime(utc_tm
);
2043 DOSFS_UnixTimeToFileTime( 2*localtim
-utctime
, ft
,
2044 syst
->wMilliseconds
* 10000 );
2049 /***********************************************************************
2050 * DefineDosDeviceA (KERNEL32.182)
2052 BOOL WINAPI
DefineDosDeviceA(DWORD flags
,LPCSTR devname
,LPCSTR targetpath
) {
2053 FIXME("(0x%08lx,%s,%s),stub!\n",flags
,devname
,targetpath
);
2054 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
2059 --- 16 bit functions ---
2062 /*************************************************************************
2063 * FindFirstFile16 (KERNEL.413)
2065 HANDLE16 WINAPI
FindFirstFile16( LPCSTR path
, WIN32_FIND_DATAA
*data
)
2067 DOS_FULL_NAME full_name
;
2069 FIND_FIRST_INFO
*info
;
2071 data
->dwReserved0
= data
->dwReserved1
= 0x0;
2072 if (!path
) return 0;
2073 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
))
2074 return INVALID_HANDLE_VALUE16
;
2075 if (!(handle
= GlobalAlloc16( GMEM_MOVEABLE
, sizeof(FIND_FIRST_INFO
) )))
2076 return INVALID_HANDLE_VALUE16
;
2077 info
= (FIND_FIRST_INFO
*)GlobalLock16( handle
);
2078 info
->path
= HEAP_strdupA( SystemHeap
, 0, full_name
.long_name
);
2079 info
->long_mask
= strrchr( info
->path
, '/' );
2080 if (info
->long_mask
)
2081 *(info
->long_mask
++) = '\0';
2082 info
->short_mask
= NULL
;
2084 if (path
[0] && (path
[1] == ':')) info
->drive
= toupper(*path
) - 'A';
2085 else info
->drive
= DRIVE_GetCurrentDrive();
2088 info
->dir
= DOSFS_OpenDir( info
->path
);
2090 GlobalUnlock16( handle
);
2091 if (!FindNextFile16( handle
, data
))
2093 FindClose16( handle
);
2094 SetLastError( ERROR_NO_MORE_FILES
);
2095 return INVALID_HANDLE_VALUE16
;
2100 /*************************************************************************
2101 * FindNextFile16 (KERNEL.414)
2103 BOOL16 WINAPI
FindNextFile16( HANDLE16 handle
, WIN32_FIND_DATAA
*data
)
2105 FIND_FIRST_INFO
*info
;
2107 if ((handle
== INVALID_HANDLE_VALUE16
) ||
2108 !(info
= (FIND_FIRST_INFO
*)GlobalLock16( handle
)))
2110 SetLastError( ERROR_INVALID_HANDLE
);
2113 GlobalUnlock16( handle
);
2114 if (!info
->path
|| !info
->dir
)
2116 SetLastError( ERROR_NO_MORE_FILES
);
2119 if (!DOSFS_FindNextEx( info
, data
))
2121 DOSFS_CloseDir( info
->dir
); info
->dir
= NULL
;
2122 HeapFree( SystemHeap
, 0, info
->path
);
2123 info
->path
= info
->long_mask
= NULL
;
2124 SetLastError( ERROR_NO_MORE_FILES
);
2130 /*************************************************************************
2131 * FindClose16 (KERNEL.415)
2133 BOOL16 WINAPI
FindClose16( HANDLE16 handle
)
2135 FIND_FIRST_INFO
*info
;
2137 if ((handle
== INVALID_HANDLE_VALUE16
) ||
2138 !(info
= (FIND_FIRST_INFO
*)GlobalLock16( handle
)))
2140 SetLastError( ERROR_INVALID_HANDLE
);
2143 if (info
->dir
) DOSFS_CloseDir( info
->dir
);
2144 if (info
->path
) HeapFree( SystemHeap
, 0, info
->path
);
2145 GlobalUnlock16( handle
);
2146 GlobalFree16( handle
);