2 * DOS file system functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996 Alexandre Julliard
13 #include <sys/errno.h>
18 #include <sys/ioctl.h>
24 #include "wine/winbase16.h"
34 #include "debugtools.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? */
42 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, long)
43 /* We want the real kernel dirent structure, not the libc one */
48 unsigned short d_reclen;
53 #undef VFAT_IOCTL_READDIR_BOTH /* just in case... */
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) */
74 { "SCSIMGR$", 0xc0c0 },
78 #define GET_DRIVE(path) \
79 (((path)[1] == ':') ? toupper((path)[0]) - 'A' : DOSFS_CurDrive)
81 /* Directory info for DOSFS_ReadDir */
85 #ifdef VFAT_IOCTL_READDIR_BOTH
88 KERNEL_DIRENT dirent[2];
92 /* Info structure for FindFirstFile handle */
106 /***********************************************************************
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;
122 /* Check for "." and ".." */
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 */
135 if (*p != '.') return 1; /* End of name */
137 if (IS_END_OF_NAME(*p)) return 0; /* Empty extension not allowed */
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 */
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;
165 /* Check for "." and ".." */
169 strcpy( buffer, ". " );
175 return (!*p || (*p == '/') || (*p == '\\'));
178 for (i = 0; i < 8; i++)
195 if (strchr( invalid_chars, *p )) return FALSE;
196 buffer[i] = toupper(*p);
204 /* Skip all chars after wildcard up to first dot */
205 while (*p && (*p != '/') && (*p != '\\') && (*p != '.')) p++;
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++)
224 return FALSE; /* Second extension not allowed */
232 if (strchr( invalid_chars, *p )) return FALSE;
233 buffer[i] = toupper(*p);
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 )
255 memcpy( buffer, name, 8 );
256 for (p = buffer + 8; (p > buffer) && (p[-1] == ' '); p--);
258 memcpy( p, name + 8, 3 );
259 for (p += 3; p[-1] == ' '; p--);
260 if (p[-1] == '.') p--;
265 /***********************************************************************
268 * Check a DOS file name against a mask (both in FCB format).
270 static int DOSFS_MatchShort( const char *mask, const char *name )
273 for (i = 11; i > 0; i--, mask++, name++)
274 if ((*mask != '?') && (*mask != *name)) return 0;
279 /***********************************************************************
282 * Check a long file name against a mask.
284 static int DOSFS_MatchLong( const char *mask, const char *name,
287 if (!strcmp( mask, "*.*" )) return 1;
288 while (*name && *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++;
299 else if (*mask != '?')
303 if (*mask != *name) return 0;
305 else if (toupper(*mask) != toupper(*name)) return 0;
310 if (*mask == '.') mask++; /* Ignore trailing '.' in mask */
311 return (!*name && !*mask);
315 /***********************************************************************
318 static DOS_DIR *DOSFS_OpenDir( LPCSTR path )
320 DOS_DIR *dir = HeapAlloc( SystemHeap, 0, sizeof(*dir) );
323 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
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)
344 /* Set the file pointer back at the start of the directory */
345 lseek( dir->fd, 0, SEEK_SET );
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 );
363 /***********************************************************************
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 /***********************************************************************
379 static BOOL DOSFS_ReadDir( DOS_DIR *dir, LPCSTR *long_name,
382 struct dirent *dirent;
384 #ifdef VFAT_IOCTL_READDIR_BOTH
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;
397 #endif /* VFAT_IOCTL_READDIR_BOTH */
399 if (!(dirent = readdir( dir->dir ))) return FALSE;
400 *long_name = dirent->d_name;
406 /***********************************************************************
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,
418 static const char invalid_chars[] = INVALID_DOS_CHARS "~.";
419 static const char hash_chars[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
426 if (dir_format) strcpy( buffer, " " );
428 if (DOSFS_ValidDOSName( name, ignore_case ))
430 /* Check for '.' and '..' */
434 if (!dir_format) buffer[1] = buffer[2] = '\0';
435 if (name[1] == '.') buffer[1] = '.';
439 /* Simply copy the name, converting to uppercase */
441 for (dst = buffer; !IS_END_OF_NAME(*name) && (*name != '.'); name++)
442 *dst++ = toupper(*name);
445 if (dir_format) dst = buffer + 8;
447 for (name++; !IS_END_OF_NAME(*name); name++)
448 *dst++ = toupper(*name);
450 if (!dir_format) *dst = '\0';
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... */
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*/
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) */
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 /***********************************************************************
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)
517 LPCSTR long_name, short_name;
518 char dos_name[12], tmp_buf[13];
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 /* Ignore trailing dots */
525 while (len > 1 && name[len-1] == '.') len--;
526 if (long_len < len + 1) return FALSE;
528 TRACE_(dosfs)("%s,%s\n", path, name );
530 if (!DOSFS_ToDosFCBFormat( name, dos_name )) dos_name[0] = '\0';
532 if (!(dir = DOSFS_OpenDir( path )))
534 WARN_(dosfs)("(%s,%s): can't open dir: %s\n",
535 path, name, strerror(errno) );
539 while ((ret = DOSFS_ReadDir( dir, &long_name, &short_name )))
541 /* Check against Unix name */
542 if (len == strlen(long_name))
546 if (!lstrncmpA( long_name, name, len )) break;
550 if (!lstrncmpiA( long_name, name, len )) break;
555 /* Check against hashed DOS name */
558 DOSFS_Hash( long_name, tmp_buf, TRUE, ignore_case );
559 short_name = tmp_buf;
561 if (!strcmp( dos_name, short_name )) break;
566 if (long_buf) strcpy( long_buf, long_name );
570 DOSFS_ToDosDTAFormat( short_name, short_buf );
572 DOSFS_Hash( long_name, short_buf, FALSE, ignore_case );
574 TRACE_(dosfs)("(%s,%s) -> %s (%s)\n",
575 path, name, long_name, short_buf ? short_buf : "***");
578 WARN_(dosfs)("'%s' not found in '%s'\n", name, path);
579 DOSFS_CloseDir( dir );
584 /***********************************************************************
587 * Check if a DOS file name represents a DOS device and return the device.
589 const DOS_DEVICE *DOSFS_GetDevice( const char *name )
594 if (!name) return NULL; /* if FILE_DupUnixHandle was used */
595 if (name[0] && (name[1] == ':')) name += 2;
596 if ((p = strrchr( name, '/' ))) name = p + 1;
597 if ((p = strrchr( name, '\\' ))) name = p + 1;
598 for (i = 0; i < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0]); i++)
600 const char *dev = DOSFS_Devices[i].name;
601 if (!lstrncmpiA( dev, name, strlen(dev) ))
603 p = name + strlen( dev );
604 if (!*p || (*p == '.')) return &DOSFS_Devices[i];
611 /***********************************************************************
612 * DOSFS_GetDeviceByHandle
614 const DOS_DEVICE *DOSFS_GetDeviceByHandle( HFILE hFile )
616 struct get_file_info_request *req = get_req_buffer();
619 if (!server_call( REQ_GET_FILE_INFO ) && (req->type == FILE_TYPE_UNKNOWN))
621 if ((req->attr >= 0) &&
622 (req->attr < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0])))
623 return &DOSFS_Devices[req->attr];
629 /***********************************************************************
632 * Open a DOS device. This might not map 1:1 into the UNIX device concept.
634 HFILE DOSFS_OpenDevice( const char *name, DWORD access )
639 if (!name) return (HFILE)NULL; /* if FILE_DupUnixHandle was used */
640 if (name[0] && (name[1] == ':')) name += 2;
641 if ((p = strrchr( name, '/' ))) name = p + 1;
642 if ((p = strrchr( name, '\\' ))) name = p + 1;
643 for (i = 0; i < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0]); i++)
645 const char *dev = DOSFS_Devices[i].name;
646 if (!lstrncmpiA( dev, name, strlen(dev) ))
648 p = name + strlen( dev );
649 if (!*p || (*p == '.')) {
651 if (!strcmp(DOSFS_Devices[i].name,"NUL"))
652 return FILE_CreateFile( "/dev/null", access,
653 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
654 OPEN_EXISTING, 0, -1 );
655 if (!strcmp(DOSFS_Devices[i].name,"CON")) {
658 switch (access & (GENERIC_READ|GENERIC_WRITE)) {
660 to_dup = GetStdHandle( STD_INPUT_HANDLE );
663 to_dup = GetStdHandle( STD_OUTPUT_HANDLE );
666 FIXME_(dosfs)("can't open CON read/write\n");
670 if (!DuplicateHandle( GetCurrentProcess(), to_dup, GetCurrentProcess(),
671 &handle, 0, FALSE, DUPLICATE_SAME_ACCESS ))
672 handle = HFILE_ERROR;
675 if (!strcmp(DOSFS_Devices[i].name,"SCSIMGR$") ||
676 !strcmp(DOSFS_Devices[i].name,"HPSCAN"))
678 return FILE_CreateDevice( i, access, NULL );
683 PROFILE_GetWineIniString("serialports",name,"",devname,sizeof devname);
687 TRACE_(file)("DOSFS_OpenDevice %s is %s\n",
688 DOSFS_Devices[i].name,devname);
689 r = FILE_CreateFile( devname, access,
690 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
691 OPEN_EXISTING, 0, -1 );
692 TRACE_(file)("Create_File return %08X\n",r);
697 FIXME_(dosfs)("device open %s not supported (yet)\n",DOSFS_Devices[i].name);
706 /***********************************************************************
709 * Get the drive specified by a given path name (DOS or Unix format).
711 static int DOSFS_GetPathDrive( const char **name )
714 const char *p = *name;
716 if (*p && (p[1] == ':'))
718 drive = toupper(*p) - 'A';
721 else if (*p == '/') /* Absolute Unix path? */
723 if ((drive = DRIVE_FindDriveRoot( name )) == -1)
725 MESSAGE("Warning: %s not accessible from a DOS drive\n", *name );
726 /* Assume it really was a DOS name */
727 drive = DRIVE_GetCurrentDrive();
730 else drive = DRIVE_GetCurrentDrive();
732 if (!DRIVE_IsValid(drive))
734 SetLastError( ERROR_INVALID_DRIVE );
741 /***********************************************************************
744 * Convert a file name (DOS or mixed DOS/Unix format) to a valid
745 * Unix name / short DOS name pair.
746 * Return FALSE if one of the path components does not exist. The last path
747 * component is only checked if 'check_last' is non-zero.
748 * The buffers pointed to by 'long_buf' and 'short_buf' must be
749 * at least MAX_PATHNAME_LEN long.
751 BOOL DOSFS_GetFullName( LPCSTR name, BOOL check_last, DOS_FULL_NAME *full )
755 char *p_l, *p_s, *root;
757 TRACE_(dosfs)("%s (last=%d)\n",
760 if ((full->drive = DOSFS_GetPathDrive( &name )) == -1) return FALSE;
761 flags = DRIVE_GetFlags( full->drive );
763 lstrcpynA( full->long_name, DRIVE_GetRoot( full->drive ),
764 sizeof(full->long_name) );
765 if (full->long_name[1]) root = full->long_name + strlen(full->long_name);
766 else root = full->long_name; /* root directory */
768 strcpy( full->short_name, "A:\\" );
769 full->short_name[0] += full->drive;
771 if ((*name == '\\') || (*name == '/')) /* Absolute path */
773 while ((*name == '\\') || (*name == '/')) name++;
775 else /* Relative path */
777 lstrcpynA( root + 1, DRIVE_GetUnixCwd( full->drive ),
778 sizeof(full->long_name) - (root - full->long_name) - 1 );
779 if (root[1]) *root = '/';
780 lstrcpynA( full->short_name + 3, DRIVE_GetDosCwd( full->drive ),
781 sizeof(full->short_name) - 3 );
784 p_l = full->long_name[1] ? full->long_name + strlen(full->long_name)
786 p_s = full->short_name[3] ? full->short_name + strlen(full->short_name)
787 : full->short_name + 2;
790 while (*name && found)
792 /* Check for '.' and '..' */
796 if (IS_END_OF_NAME(name[1]))
799 while ((*name == '\\') || (*name == '/')) name++;
802 else if ((name[1] == '.') && IS_END_OF_NAME(name[2]))
805 while ((*name == '\\') || (*name == '/')) name++;
806 while ((p_l > root) && (*p_l != '/')) p_l--;
807 while ((p_s > full->short_name + 2) && (*p_s != '\\')) p_s--;
808 *p_l = *p_s = '\0'; /* Remove trailing separator */
813 /* Make sure buffers are large enough */
815 if ((p_s >= full->short_name + sizeof(full->short_name) - 14) ||
816 (p_l >= full->long_name + sizeof(full->long_name) - 1))
818 SetLastError( ERROR_PATH_NOT_FOUND );
822 /* Get the long and short name matching the file name */
824 if ((found = DOSFS_FindUnixName( full->long_name, name, p_l + 1,
825 sizeof(full->long_name) - (p_l - full->long_name) - 1,
826 p_s + 1, !(flags & DRIVE_CASE_SENSITIVE) )))
832 while (!IS_END_OF_NAME(*name)) name++;
834 else if (!check_last)
838 while (!IS_END_OF_NAME(*name) &&
839 (p_s < full->short_name + sizeof(full->short_name) - 1) &&
840 (p_l < full->long_name + sizeof(full->long_name) - 1))
842 *p_s++ = tolower(*name);
843 /* If the drive is case-sensitive we want to create new */
844 /* files in lower-case otherwise we can't reopen them */
845 /* under the same short name. */
846 if (flags & DRIVE_CASE_SENSITIVE) *p_l++ = tolower(*name);
852 while ((*name == '\\') || (*name == '/')) name++;
859 SetLastError( ERROR_FILE_NOT_FOUND );
862 if (*name) /* Not last */
864 SetLastError( ERROR_PATH_NOT_FOUND );
868 if (!full->long_name[0]) strcpy( full->long_name, "/" );
869 if (!full->short_name[2]) strcpy( full->short_name + 2, "\\" );
870 TRACE_(dosfs)("returning %s = %s\n",
871 full->long_name, full->short_name );
876 /***********************************************************************
877 * GetShortPathNameA (KERNEL32.271)
881 * longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
882 * *longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
884 * more observations ( with NT 3.51 (WinDD) ):
885 * longpath <= 8.3 -> just copy longpath to shortpath
887 * a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
888 * b) file does exist -> set the short filename.
889 * - trailing slashes are reproduced in the short name, even if the
890 * file is not a directory
891 * - the absolute/relative path of the short name is reproduced in the
892 * same way, like the long name
893 * - longpath and shortpath may have the same adress
896 DWORD WINAPI GetShortPathNameA( LPCSTR longpath, LPSTR shortpath,
899 DOS_FULL_NAME full_name;
901 DWORD length = 0, pos = 0;
902 INT start=-1, end=-1, tmplen;
905 SetLastError(ERROR_INVALID_PARAMETER);
909 SetLastError(ERROR_BAD_PATHNAME);
913 tmpshortpath = HeapAlloc( GetProcessHeap(), 0, MAX_PATHNAME_LEN );
914 if ( !tmpshortpath ) {
915 SetLastError ( ERROR_NOT_ENOUGH_MEMORY );
919 /* Check for Drive-Letter */
920 if ( longpath[1] == ':' ) {
921 lstrcpynA ( tmpshortpath, longpath, 3 );
926 /* loop over each part of the name */
927 while ( longpath[pos] ) {
929 if (( longpath[pos] == '\\' ) ||
930 ( longpath[pos+1] == '\0' ) ||
931 ( longpath[pos] == '/')) {
934 if ( DOSFS_ValidDOSName ( longpath + start, TRUE )) {
935 tmplen = end - start + ( (( longpath[pos] == '\\' ) || ( longpath[pos] == '/' )) ? 1 : 2 );
936 lstrcpynA ( tmpshortpath+length, longpath+start, tmplen );
937 length += tmplen - 1;
940 DOSFS_Hash ( longpath + start, tmpshortpath+length, FALSE, FALSE );
941 length = lstrlenA ( tmpshortpath );
943 /* Check if the path, up to this point exists */
944 if ( !DOSFS_GetFullName ( tmpshortpath, TRUE, &full_name ) ) {
945 SetLastError ( ERROR_FILE_NOT_FOUND );
952 if (( longpath[pos] == '\\' ) || ( longpath[pos] == '/' )) {
953 tmpshortpath[length] = '\\';
954 tmpshortpath[length+1]='\0';
971 lstrcpynA ( shortpath, tmpshortpath, shortlen );
972 length = lstrlenA ( tmpshortpath );
973 HeapFree ( GetProcessHeap(), 0, tmpshortpath );
979 /***********************************************************************
980 * GetShortPathName32W (KERNEL32.272)
982 DWORD WINAPI GetShortPathNameW( LPCWSTR longpath, LPWSTR shortpath,
985 LPSTR longpathA, shortpathA;
988 longpathA = HEAP_strdupWtoA( GetProcessHeap(), 0, longpath );
989 shortpathA = HEAP_xalloc ( GetProcessHeap(), 0, shortlen );
991 ret = GetShortPathNameA ( longpathA, shortpathA, shortlen );
992 lstrcpynAtoW ( shortpath, shortpathA, shortlen );
994 HeapFree( GetProcessHeap(), 0, longpathA );
995 HeapFree( GetProcessHeap(), 0, shortpathA );
1001 /***********************************************************************
1002 * GetLongPathName32A (KERNEL32.xxx)
1004 DWORD WINAPI GetLongPathNameA( LPCSTR shortpath, LPSTR longpath,
1007 DOS_FULL_NAME full_name;
1012 if (!DOSFS_GetFullName( shortpath, TRUE, &full_name )) return 0;
1013 lstrcpynA( longpath, full_name.short_name, longlen );
1014 /* Do some hackery to get the long filename.
1015 * FIXME: Would be better if it returned the
1016 * long version of the directories too
1018 longfilename = strrchr(full_name.long_name, '/')+1;
1019 if (longpath != NULL) {
1020 if ((p = strrchr( longpath, '\\' )) != NULL) {
1022 longlen -= (p-longpath);
1023 lstrcpynA( p, longfilename , longlen);
1027 ((strrchr( full_name.short_name, '\\' ) - full_name.short_name) + 1);
1028 return shortpathlen + strlen( longfilename );
1032 /***********************************************************************
1033 * GetLongPathName32W (KERNEL32.269)
1035 DWORD WINAPI GetLongPathNameW( LPCWSTR shortpath, LPWSTR longpath,
1038 DOS_FULL_NAME full_name;
1040 LPSTR shortpathA = HEAP_strdupWtoA( GetProcessHeap(), 0, shortpath );
1042 /* FIXME: is it correct to always return a fully qualified short path? */
1043 if (DOSFS_GetFullName( shortpathA, TRUE, &full_name ))
1045 ret = strlen( full_name.short_name );
1046 lstrcpynAtoW( longpath, full_name.long_name, longlen );
1048 HeapFree( GetProcessHeap(), 0, shortpathA );
1053 /***********************************************************************
1054 * DOSFS_DoGetFullPathName
1056 * Implementation of GetFullPathName32A/W.
1058 static DWORD DOSFS_DoGetFullPathName( LPCSTR name, DWORD len, LPSTR result,
1061 char buffer[MAX_PATHNAME_LEN];
1066 /* last possible position for a char != 0 */
1067 char *endchar = buffer + sizeof(buffer) - 2;
1070 TRACE_(dosfs)("converting '%s'\n", name );
1072 if (!name || ((drive = DOSFS_GetPathDrive( &name )) == -1) )
1073 { SetLastError( ERROR_INVALID_PARAMETER );
1080 if (IS_END_OF_NAME(*name) && (*name)) /* Absolute path */
1082 while (((*name == '\\') || (*name == '/')) && (!*endchar) )
1085 else /* Relative path or empty path */
1088 lstrcpynA( p, DRIVE_GetDosCwd(drive), sizeof(buffer) - 4 );
1101 if (IS_END_OF_NAME(name[1]))
1104 while ((*name == '\\') || (*name == '/')) name++;
1107 else if ((name[1] == '.') && IS_END_OF_NAME(name[2]))
1110 while ((*name == '\\') || (*name == '/')) name++;
1112 if (p < buffer + 3) /* no previous dir component */
1114 p--; /* skip previously added '\\' */
1115 while ((*p == '\\') || (*p == '/')) p--;
1116 /* skip previous dir component */
1117 while ((*p != '\\') && (*p != '/')) p--;
1123 { SetLastError( ERROR_PATH_NOT_FOUND );
1126 while (!IS_END_OF_NAME(*name) && (!*endchar) )
1128 while (((*name == '\\') || (*name == '/')) && (!*endchar) )
1133 if (!(DRIVE_GetFlags(drive) & DRIVE_CASE_PRESERVING))
1134 CharUpperA( buffer );
1139 lstrcpynAtoW( (LPWSTR)result, buffer, len );
1141 lstrcpynA( result, buffer, len );
1144 TRACE_(dosfs)("returning '%s'\n", buffer );
1146 /* If the lpBuffer buffer is too small, the return value is the
1147 size of the buffer, in characters, required to hold the path. */
1149 ret = strlen(buffer);
1152 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1158 /***********************************************************************
1159 * GetFullPathName32A (KERNEL32.272)
1161 * if the path closed with '\', *lastpart is 0
1163 DWORD WINAPI GetFullPathNameA( LPCSTR name, DWORD len, LPSTR buffer,
1166 DWORD ret = DOSFS_DoGetFullPathName( name, len, buffer, FALSE );
1167 if (ret && buffer && lastpart)
1169 LPSTR p = buffer + strlen(buffer);
1173 while ((p > buffer + 2) && (*p != '\\')) p--;
1176 else *lastpart = NULL;
1182 /***********************************************************************
1183 * GetFullPathName32W (KERNEL32.273)
1185 DWORD WINAPI GetFullPathNameW( LPCWSTR name, DWORD len, LPWSTR buffer,
1188 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
1189 DWORD ret = DOSFS_DoGetFullPathName( nameA, len, (LPSTR)buffer, TRUE );
1190 HeapFree( GetProcessHeap(), 0, nameA );
1191 if (ret && buffer && lastpart)
1193 LPWSTR p = buffer + lstrlenW(buffer);
1194 if (*p != (WCHAR)'\\')
1196 while ((p > buffer + 2) && (*p != (WCHAR)'\\')) p--;
1199 else *lastpart = NULL;
1204 /***********************************************************************
1207 static int DOSFS_FindNextEx( FIND_FIRST_INFO *info, WIN32_FIND_DATAA *entry )
1209 BYTE attr = info->attr | FA_UNUSED | FA_ARCHIVE | FA_RDONLY;
1210 UINT flags = DRIVE_GetFlags( info->drive );
1211 char *p, buffer[MAX_PATHNAME_LEN];
1212 const char *drive_path;
1214 LPCSTR long_name, short_name;
1215 BY_HANDLE_FILE_INFORMATION fileinfo;
1218 if ((info->attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
1220 if (info->cur_pos) return 0;
1221 entry->dwFileAttributes = FILE_ATTRIBUTE_LABEL;
1222 DOSFS_UnixTimeToFileTime( (time_t)0, &entry->ftCreationTime, 0 );
1223 DOSFS_UnixTimeToFileTime( (time_t)0, &entry->ftLastAccessTime, 0 );
1224 DOSFS_UnixTimeToFileTime( (time_t)0, &entry->ftLastWriteTime, 0 );
1225 entry->nFileSizeHigh = 0;
1226 entry->nFileSizeLow = 0;
1227 entry->dwReserved0 = 0;
1228 entry->dwReserved1 = 0;
1229 DOSFS_ToDosDTAFormat( DRIVE_GetLabel( info->drive ), entry->cFileName );
1230 strcpy( entry->cAlternateFileName, entry->cFileName );
1235 drive_path = info->path + strlen(DRIVE_GetRoot( info->drive ));
1236 while ((*drive_path == '/') || (*drive_path == '\\')) drive_path++;
1237 drive_root = !*drive_path;
1239 lstrcpynA( buffer, info->path, sizeof(buffer) - 1 );
1240 strcat( buffer, "/" );
1241 p = buffer + strlen(buffer);
1243 while (DOSFS_ReadDir( info->dir, &long_name, &short_name ))
1247 /* Don't return '.' and '..' in the root of the drive */
1248 if (drive_root && (long_name[0] == '.') &&
1249 (!long_name[1] || ((long_name[1] == '.') && !long_name[2])))
1252 /* Check the long mask */
1254 if (info->long_mask)
1256 if (!DOSFS_MatchLong( info->long_mask, long_name,
1257 flags & DRIVE_CASE_SENSITIVE )) continue;
1260 /* Check the short mask */
1262 if (info->short_mask)
1266 DOSFS_Hash( long_name, dos_name, TRUE,
1267 !(flags & DRIVE_CASE_SENSITIVE) );
1268 short_name = dos_name;
1270 if (!DOSFS_MatchShort( info->short_mask, short_name )) continue;
1273 /* Check the file attributes */
1275 lstrcpynA( p, long_name, sizeof(buffer) - (int)(p - buffer) );
1276 if (!FILE_Stat( buffer, &fileinfo ))
1278 WARN_(dosfs)("can't stat %s\n", buffer);
1281 if (fileinfo.dwFileAttributes & ~attr) continue;
1283 /* We now have a matching entry; fill the result and return */
1285 entry->dwFileAttributes = fileinfo.dwFileAttributes;
1286 entry->ftCreationTime = fileinfo.ftCreationTime;
1287 entry->ftLastAccessTime = fileinfo.ftLastAccessTime;
1288 entry->ftLastWriteTime = fileinfo.ftLastWriteTime;
1289 entry->nFileSizeHigh = fileinfo.nFileSizeHigh;
1290 entry->nFileSizeLow = fileinfo.nFileSizeLow;
1293 DOSFS_ToDosDTAFormat( short_name, entry->cAlternateFileName );
1295 DOSFS_Hash( long_name, entry->cAlternateFileName, FALSE,
1296 !(flags & DRIVE_CASE_SENSITIVE) );
1298 lstrcpynA( entry->cFileName, long_name, sizeof(entry->cFileName) );
1299 if (!(flags & DRIVE_CASE_PRESERVING)) CharLowerA( entry->cFileName );
1300 TRACE_(dosfs)("returning %s (%s) %02lx %ld\n",
1301 entry->cFileName, entry->cAlternateFileName,
1302 entry->dwFileAttributes, entry->nFileSizeLow );
1305 return 0; /* End of directory */
1308 /***********************************************************************
1311 * Find the next matching file. Return the number of entries read to find
1312 * the matching one, or 0 if no more entries.
1313 * 'short_mask' is the 8.3 mask (in FCB format), 'long_mask' is the long
1314 * file name mask. Either or both can be NULL.
1316 * NOTE: This is supposed to be only called by the int21 emulation
1317 * routines. Thus, we should own the Win16Mutex anyway.
1318 * Nevertheless, we explicitly enter it to ensure the static
1319 * directory cache is protected.
1321 int DOSFS_FindNext( const char *path, const char *short_mask,
1322 const char *long_mask, int drive, BYTE attr,
1323 int skip, WIN32_FIND_DATAA *entry )
1325 static FIND_FIRST_INFO info = { NULL };
1326 LPCSTR short_name, long_name;
1329 SYSLEVEL_EnterWin16Lock();
1331 /* Check the cached directory */
1332 if (!(info.dir && info.path == path && info.short_mask == short_mask
1333 && info.long_mask == long_mask && info.drive == drive
1334 && info.attr == attr && info.cur_pos <= skip))
1336 /* Not in the cache, open it anew */
1337 if (info.dir) DOSFS_CloseDir( info.dir );
1339 info.path = (LPSTR)path;
1340 info.long_mask = (LPSTR)long_mask;
1341 info.short_mask = (LPSTR)short_mask;
1345 info.dir = DOSFS_OpenDir( info.path );
1348 /* Skip to desired position */
1349 while (info.cur_pos < skip)
1350 if (info.dir && DOSFS_ReadDir( info.dir, &long_name, &short_name ))
1355 if (info.dir && info.cur_pos == skip && DOSFS_FindNextEx( &info, entry ))
1356 count = info.cur_pos - skip;
1362 if (info.dir) DOSFS_CloseDir( info.dir );
1363 memset( &info, '\0', sizeof(info) );
1366 SYSLEVEL_LeaveWin16Lock();
1373 /*************************************************************************
1374 * FindFirstFile16 (KERNEL.413)
1376 HANDLE16 WINAPI FindFirstFile16( LPCSTR path, WIN32_FIND_DATAA *data )
1378 DOS_FULL_NAME full_name;
1380 FIND_FIRST_INFO *info;
1382 data->dwReserved0 = data->dwReserved1 = 0x0;
1383 if (!path) return 0;
1384 if (!DOSFS_GetFullName( path, FALSE, &full_name ))
1385 return INVALID_HANDLE_VALUE16;
1386 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE, sizeof(FIND_FIRST_INFO) )))
1387 return INVALID_HANDLE_VALUE16;
1388 info = (FIND_FIRST_INFO *)GlobalLock16( handle );
1389 info->path = HEAP_strdupA( SystemHeap, 0, full_name.long_name );
1390 info->long_mask = strrchr( info->path, '/' );
1391 *(info->long_mask++) = '\0';
1392 info->short_mask = NULL;
1394 if (path[0] && (path[1] == ':')) info->drive = toupper(*path) - 'A';
1395 else info->drive = DRIVE_GetCurrentDrive();
1398 info->dir = DOSFS_OpenDir( info->path );
1400 GlobalUnlock16( handle );
1401 if (!FindNextFile16( handle, data ))
1403 FindClose16( handle );
1404 SetLastError( ERROR_NO_MORE_FILES );
1405 return INVALID_HANDLE_VALUE16;
1411 /*************************************************************************
1412 * FindFirstFile32A (KERNEL32.123)
1414 HANDLE WINAPI FindFirstFileA( LPCSTR path, WIN32_FIND_DATAA *data )
1416 HANDLE handle = FindFirstFile16( path, data );
1417 if (handle == INVALID_HANDLE_VALUE16) return INVALID_HANDLE_VALUE;
1422 /*************************************************************************
1423 * FindFirstFile32W (KERNEL32.124)
1425 HANDLE WINAPI FindFirstFileW( LPCWSTR path, WIN32_FIND_DATAW *data )
1427 WIN32_FIND_DATAA dataA;
1428 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
1429 HANDLE handle = FindFirstFileA( pathA, &dataA );
1430 HeapFree( GetProcessHeap(), 0, pathA );
1431 if (handle != INVALID_HANDLE_VALUE)
1433 data->dwFileAttributes = dataA.dwFileAttributes;
1434 data->ftCreationTime = dataA.ftCreationTime;
1435 data->ftLastAccessTime = dataA.ftLastAccessTime;
1436 data->ftLastWriteTime = dataA.ftLastWriteTime;
1437 data->nFileSizeHigh = dataA.nFileSizeHigh;
1438 data->nFileSizeLow = dataA.nFileSizeLow;
1439 lstrcpyAtoW( data->cFileName, dataA.cFileName );
1440 lstrcpyAtoW( data->cAlternateFileName, dataA.cAlternateFileName );
1446 /*************************************************************************
1447 * FindNextFile16 (KERNEL.414)
1449 BOOL16 WINAPI FindNextFile16( HANDLE16 handle, WIN32_FIND_DATAA *data )
1451 FIND_FIRST_INFO *info;
1453 if (!(info = (FIND_FIRST_INFO *)GlobalLock16( handle )))
1455 SetLastError( ERROR_INVALID_HANDLE );
1458 GlobalUnlock16( handle );
1459 if (!info->path || !info->dir)
1461 SetLastError( ERROR_NO_MORE_FILES );
1464 if (!DOSFS_FindNextEx( info, data ))
1466 DOSFS_CloseDir( info->dir ); info->dir = NULL;
1467 HeapFree( SystemHeap, 0, info->path );
1468 info->path = info->long_mask = NULL;
1469 SetLastError( ERROR_NO_MORE_FILES );
1476 /*************************************************************************
1477 * FindNextFile32A (KERNEL32.126)
1479 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
1481 return FindNextFile16( handle, data );
1485 /*************************************************************************
1486 * FindNextFile32W (KERNEL32.127)
1488 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
1490 WIN32_FIND_DATAA dataA;
1491 if (!FindNextFileA( handle, &dataA )) return FALSE;
1492 data->dwFileAttributes = dataA.dwFileAttributes;
1493 data->ftCreationTime = dataA.ftCreationTime;
1494 data->ftLastAccessTime = dataA.ftLastAccessTime;
1495 data->ftLastWriteTime = dataA.ftLastWriteTime;
1496 data->nFileSizeHigh = dataA.nFileSizeHigh;
1497 data->nFileSizeLow = dataA.nFileSizeLow;
1498 lstrcpyAtoW( data->cFileName, dataA.cFileName );
1499 lstrcpyAtoW( data->cAlternateFileName, dataA.cAlternateFileName );
1504 /*************************************************************************
1505 * FindClose16 (KERNEL.415)
1507 BOOL16 WINAPI FindClose16( HANDLE16 handle )
1509 FIND_FIRST_INFO *info;
1511 if ((handle == INVALID_HANDLE_VALUE16) ||
1512 !(info = (FIND_FIRST_INFO *)GlobalLock16( handle )))
1514 SetLastError( ERROR_INVALID_HANDLE );
1517 if (info->dir) DOSFS_CloseDir( info->dir );
1518 if (info->path) HeapFree( SystemHeap, 0, info->path );
1519 GlobalUnlock16( handle );
1520 GlobalFree16( handle );
1525 /*************************************************************************
1526 * FindClose32 (KERNEL32.119)
1528 BOOL WINAPI FindClose( HANDLE handle )
1530 return FindClose16( (HANDLE16)handle );
1534 /***********************************************************************
1535 * DOSFS_UnixTimeToFileTime
1537 * Convert a Unix time to FILETIME format.
1538 * The FILETIME structure is a 64-bit value representing the number of
1539 * 100-nanosecond intervals since January 1, 1601, 0:00.
1540 * 'remainder' is the nonnegative number of 100-ns intervals
1541 * corresponding to the time fraction smaller than 1 second that
1542 * couldn't be stored in the time_t value.
1544 void DOSFS_UnixTimeToFileTime( time_t unix_time, FILETIME *filetime,
1550 The time difference between 1 January 1601, 00:00:00 and
1551 1 January 1970, 00:00:00 is 369 years, plus the leap years
1552 from 1604 to 1968, excluding 1700, 1800, 1900.
1553 This makes (1968 - 1600) / 4 - 3 = 89 leap days, and a total
1556 Any day in that period had 24 * 60 * 60 = 86400 seconds.
1558 The time difference is 134774 * 86400 * 10000000, which can be written
1560 27111902 * 2^32 + 3577643008
1561 413 * 2^48 + 45534 * 2^32 + 54590 * 2^16 + 32768
1563 If you find that these constants are buggy, please change them in all
1564 instances in both conversion functions.
1567 There are two versions, one of them uses long long variables and
1568 is presumably faster but not ISO C. The other one uses standard C
1569 data types and operations but relies on the assumption that negative
1570 numbers are stored as 2's complement (-1 is 0xffff....). If this
1571 assumption is violated, dates before 1970 will not convert correctly.
1572 This should however work on any reasonable architecture where WINE
1577 Take care not to remove the casts. I have tested these functions
1578 (in both versions) for a lot of numbers. I would be interested in
1579 results on other compilers than GCC.
1581 The operations have been designed to account for the possibility
1582 of 64-bit time_t in future UNICES. Even the versions without
1583 internal long long numbers will work if time_t only is 64 bit.
1584 A 32-bit shift, which was necessary for that operation, turned out
1585 not to work correctly in GCC, besides giving the warning. So I
1586 used a double 16-bit shift instead. Numbers are in the ISO version
1587 represented by three limbs, the most significant with 32 bit, the
1588 other two with 16 bit each.
1590 As the modulo-operator % is not well-defined for negative numbers,
1591 negative divisors have been avoided in DOSFS_FileTimeToUnixTime.
1593 There might be quicker ways to do this in C. Certainly so in
1596 Claus Fischer, fischer@iue.tuwien.ac.at
1599 #if (SIZEOF_LONG_LONG >= 8)
1600 # define USE_LONG_LONG 1
1602 # define USE_LONG_LONG 0
1605 #if USE_LONG_LONG /* gcc supports long long type */
1607 long long int t = unix_time;
1609 t += 116444736000000000LL;
1611 filetime->dwLowDateTime = (UINT)t;
1612 filetime->dwHighDateTime = (UINT)(t >> 32);
1614 #else /* ISO version */
1616 UINT a0; /* 16 bit, low bits */
1617 UINT a1; /* 16 bit, medium bits */
1618 UINT a2; /* 32 bit, high bits */
1620 /* Copy the unix time to a2/a1/a0 */
1621 a0 = unix_time & 0xffff;
1622 a1 = (unix_time >> 16) & 0xffff;
1623 /* This is obsolete if unix_time is only 32 bits, but it does not hurt.
1624 Do not replace this by >> 32, it gives a compiler warning and it does
1626 a2 = (unix_time >= 0 ? (unix_time >> 16) >> 16 :
1627 ~((~unix_time >> 16) >> 16));
1629 /* Multiply a by 10000000 (a = a2/a1/a0)
1630 Split the factor into 10000 * 1000 which are both less than 0xffff. */
1632 a1 = a1 * 10000 + (a0 >> 16);
1633 a2 = a2 * 10000 + (a1 >> 16);
1638 a1 = a1 * 1000 + (a0 >> 16);
1639 a2 = a2 * 1000 + (a1 >> 16);
1643 /* Add the time difference and the remainder */
1644 a0 += 32768 + (remainder & 0xffff);
1645 a1 += 54590 + (remainder >> 16 ) + (a0 >> 16);
1646 a2 += 27111902 + (a1 >> 16);
1651 filetime->dwLowDateTime = (a1 << 16) + a0;
1652 filetime->dwHighDateTime = a2;
1657 /***********************************************************************
1658 * DOSFS_FileTimeToUnixTime
1660 * Convert a FILETIME format to Unix time.
1661 * If not NULL, 'remainder' contains the fractional part of the filetime,
1662 * in the range of [0..9999999] (even if time_t is negative).
1664 time_t DOSFS_FileTimeToUnixTime( const FILETIME *filetime, DWORD *remainder )
1666 /* Read the comment in the function DOSFS_UnixTimeToFileTime. */
1669 long long int t = filetime->dwHighDateTime;
1671 t += (UINT)filetime->dwLowDateTime;
1672 t -= 116444736000000000LL;
1675 if (remainder) *remainder = 9999999 - (-t - 1) % 10000000;
1676 return -1 - ((-t - 1) / 10000000);
1680 if (remainder) *remainder = t % 10000000;
1681 return t / 10000000;
1684 #else /* ISO version */
1686 UINT a0; /* 16 bit, low bits */
1687 UINT a1; /* 16 bit, medium bits */
1688 UINT a2; /* 32 bit, high bits */
1689 UINT r; /* remainder of division */
1690 unsigned int carry; /* carry bit for subtraction */
1691 int negative; /* whether a represents a negative value */
1693 /* Copy the time values to a2/a1/a0 */
1694 a2 = (UINT)filetime->dwHighDateTime;
1695 a1 = ((UINT)filetime->dwLowDateTime ) >> 16;
1696 a0 = ((UINT)filetime->dwLowDateTime ) & 0xffff;
1698 /* Subtract the time difference */
1699 if (a0 >= 32768 ) a0 -= 32768 , carry = 0;
1700 else a0 += (1 << 16) - 32768 , carry = 1;
1702 if (a1 >= 54590 + carry) a1 -= 54590 + carry, carry = 0;
1703 else a1 += (1 << 16) - 54590 - carry, carry = 1;
1705 a2 -= 27111902 + carry;
1707 /* If a is negative, replace a by (-1-a) */
1708 negative = (a2 >= ((UINT)1) << 31);
1711 /* Set a to -a - 1 (a is a2/a1/a0) */
1717 /* Divide a by 10000000 (a = a2/a1/a0), put the rest into r.
1718 Split the divisor into 10000 * 1000 which are both less than 0xffff. */
1719 a1 += (a2 % 10000) << 16;
1721 a0 += (a1 % 10000) << 16;
1726 a1 += (a2 % 1000) << 16;
1728 a0 += (a1 % 1000) << 16;
1730 r += (a0 % 1000) * 10000;
1733 /* If a was negative, replace a by (-1-a) and r by (9999999 - r) */
1736 /* Set a to -a - 1 (a is a2/a1/a0) */
1744 if (remainder) *remainder = r;
1746 /* Do not replace this by << 32, it gives a compiler warning and it does
1748 return ((((time_t)a2) << 16) << 16) + (a1 << 16) + a0;
1753 /***********************************************************************
1754 * DosDateTimeToFileTime (KERNEL32.76)
1756 BOOL WINAPI DosDateTimeToFileTime( WORD fatdate, WORD fattime, LPFILETIME ft)
1760 newtm.tm_sec = (fattime & 0x1f) * 2;
1761 newtm.tm_min = (fattime >> 5) & 0x3f;
1762 newtm.tm_hour = (fattime >> 11);
1763 newtm.tm_mday = (fatdate & 0x1f);
1764 newtm.tm_mon = ((fatdate >> 5) & 0x0f) - 1;
1765 newtm.tm_year = (fatdate >> 9) + 80;
1766 DOSFS_UnixTimeToFileTime( mktime( &newtm ), ft, 0 );
1771 /***********************************************************************
1772 * FileTimeToDosDateTime (KERNEL32.111)
1774 BOOL WINAPI FileTimeToDosDateTime( const FILETIME *ft, LPWORD fatdate,
1777 time_t unixtime = DOSFS_FileTimeToUnixTime( ft, NULL );
1778 struct tm *tm = localtime( &unixtime );
1780 *fattime = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
1782 *fatdate = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5)
1788 /***********************************************************************
1789 * LocalFileTimeToFileTime (KERNEL32.373)
1791 BOOL WINAPI LocalFileTimeToFileTime( const FILETIME *localft,
1797 /* convert from local to UTC. Perhaps not correct. FIXME */
1798 time_t unixtime = DOSFS_FileTimeToUnixTime( localft, &remainder );
1799 xtm = gmtime( &unixtime );
1800 DOSFS_UnixTimeToFileTime( mktime(xtm), utcft, remainder );
1805 /***********************************************************************
1806 * FileTimeToLocalFileTime (KERNEL32.112)
1808 BOOL WINAPI FileTimeToLocalFileTime( const FILETIME *utcft,
1809 LPFILETIME localft )
1812 /* convert from UTC to local. Perhaps not correct. FIXME */
1813 time_t unixtime = DOSFS_FileTimeToUnixTime( utcft, &remainder );
1815 struct tm *xtm = localtime( &unixtime );
1818 localtime = timegm(xtm);
1819 DOSFS_UnixTimeToFileTime( localtime, localft, remainder );
1822 struct tm *xtm,*gtm;
1825 xtm = localtime( &unixtime );
1826 gtm = gmtime( &unixtime );
1827 time1 = mktime(xtm);
1828 time2 = mktime(gtm);
1829 DOSFS_UnixTimeToFileTime( 2*time1-time2, localft, remainder );
1835 /***********************************************************************
1836 * FileTimeToSystemTime (KERNEL32.113)
1838 BOOL WINAPI FileTimeToSystemTime( const FILETIME *ft, LPSYSTEMTIME syst )
1842 time_t xtime = DOSFS_FileTimeToUnixTime( ft, &remainder );
1843 xtm = gmtime(&xtime);
1844 syst->wYear = xtm->tm_year+1900;
1845 syst->wMonth = xtm->tm_mon + 1;
1846 syst->wDayOfWeek = xtm->tm_wday;
1847 syst->wDay = xtm->tm_mday;
1848 syst->wHour = xtm->tm_hour;
1849 syst->wMinute = xtm->tm_min;
1850 syst->wSecond = xtm->tm_sec;
1851 syst->wMilliseconds = remainder / 10000;
1855 /***********************************************************************
1856 * QueryDosDeviceA (KERNEL32.413)
1858 * returns array of strings terminated by \0, terminated by \0
1860 DWORD WINAPI QueryDosDeviceA(LPCSTR devname,LPSTR target,DWORD bufsize)
1865 TRACE_(dosfs)("(%s,...)\n",devname?devname:"<null>");
1867 /* return known MSDOS devices */
1868 lstrcpyA(buffer,"CON COM1 COM2 LPT1 NUL ");
1869 while ((s=strchr(buffer,' ')))
1872 lstrcpynA(target,buffer,bufsize);
1873 return strlen(buffer);
1875 lstrcpyA(buffer,"\\DEV\\");
1876 lstrcatA(buffer,devname);
1877 if ((s=strchr(buffer,':'))) *s='\0';
1878 lstrcpynA(target,buffer,bufsize);
1879 return strlen(buffer);
1883 /***********************************************************************
1884 * QueryDosDeviceW (KERNEL32.414)
1886 * returns array of strings terminated by \0, terminated by \0
1888 DWORD WINAPI QueryDosDeviceW(LPCWSTR devname,LPWSTR target,DWORD bufsize)
1890 LPSTR devnameA = devname?HEAP_strdupWtoA(GetProcessHeap(),0,devname):NULL;
1891 LPSTR targetA = (LPSTR)HEAP_xalloc(GetProcessHeap(),0,bufsize);
1892 DWORD ret = QueryDosDeviceA(devnameA,targetA,bufsize);
1894 lstrcpynAtoW(target,targetA,bufsize);
1895 if (devnameA) HeapFree(GetProcessHeap(),0,devnameA);
1896 if (targetA) HeapFree(GetProcessHeap(),0,targetA);
1901 /***********************************************************************
1902 * SystemTimeToFileTime (KERNEL32.526)
1904 BOOL WINAPI SystemTimeToFileTime( const SYSTEMTIME *syst, LPFILETIME ft )
1910 struct tm xtm,*local_tm,*utc_tm;
1911 time_t localtim,utctime;
1914 xtm.tm_year = syst->wYear-1900;
1915 xtm.tm_mon = syst->wMonth - 1;
1916 xtm.tm_wday = syst->wDayOfWeek;
1917 xtm.tm_mday = syst->wDay;
1918 xtm.tm_hour = syst->wHour;
1919 xtm.tm_min = syst->wMinute;
1920 xtm.tm_sec = syst->wSecond; /* this is UTC */
1923 utctime = timegm(&xtm);
1924 DOSFS_UnixTimeToFileTime( utctime, ft,
1925 syst->wMilliseconds * 10000 );
1927 localtim = mktime(&xtm); /* now we've got local time */
1928 local_tm = localtime(&localtim);
1929 utc_tm = gmtime(&localtim);
1930 utctime = mktime(utc_tm);
1931 DOSFS_UnixTimeToFileTime( 2*localtim -utctime, ft,
1932 syst->wMilliseconds * 10000 );
1937 BOOL WINAPI DefineDosDeviceA(DWORD flags,LPCSTR devname,LPCSTR targetpath) {
1938 FIXME_(dosfs)("(0x%08lx,%s,%s),stub!\n",flags,devname,targetpath);
1939 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);