Copy the first 128 colors from the default colormap to Wine's private
[wine.git] / files / dos_fs.c
blob400223e78a429b860bdb980fe3e9ca585b01cc91
1 /*
2 * DOS file system functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996 Alexandre Julliard
6 */
8 #include "config.h"
9 #include <sys/types.h>
10 #include <ctype.h>
11 #include <dirent.h>
12 #include <errno.h>
13 #ifdef HAVE_SYS_ERRNO_H
14 #include <sys/errno.h>
15 #endif
16 #include <fcntl.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <sys/stat.h>
20 #include <sys/ioctl.h>
21 #include <time.h>
22 #include <unistd.h>
24 #include "windef.h"
25 #include "ntddk.h"
26 #include "winnls.h"
27 #include "wine/winbase16.h"
28 #include "wine/unicode.h"
29 #include "winerror.h"
30 #include "drive.h"
31 #include "file.h"
32 #include "heap.h"
33 #include "msdos.h"
34 #include "server.h"
35 #include "options.h"
36 #include "debugtools.h"
38 DEFAULT_DEBUG_CHANNEL(dosfs);
39 DECLARE_DEBUG_CHANNEL(file);
41 /* Define the VFAT ioctl to get both short and long file names */
42 /* FIXME: is it possible to get this to work on other systems? */
43 #ifdef linux
44 /* We want the real kernel dirent structure, not the libc one */
45 typedef struct
47 long d_ino;
48 long d_off;
49 unsigned short d_reclen;
50 char d_name[256];
51 } KERNEL_DIRENT;
53 #define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, KERNEL_DIRENT [2] )
55 #else /* linux */
56 #undef VFAT_IOCTL_READDIR_BOTH /* just in case... */
57 #endif /* linux */
59 /* Chars we don't want to see in DOS file names */
60 #define INVALID_DOS_CHARS "*?<>|\"+=,;[] \345"
62 static const DOS_DEVICE DOSFS_Devices[] =
63 /* name, device flags (see Int 21/AX=0x4400) */
65 { "CON", 0xc0d3 },
66 { "PRN", 0xa0c0 },
67 { "NUL", 0x80c4 },
68 { "AUX", 0x80c0 },
69 { "LPT1", 0xa0c0 },
70 { "LPT2", 0xa0c0 },
71 { "LPT3", 0xa0c0 },
72 { "LPT4", 0xc0d3 },
73 { "COM1", 0x80c0 },
74 { "COM2", 0x80c0 },
75 { "COM3", 0x80c0 },
76 { "COM4", 0x80c0 },
77 { "SCSIMGR$", 0xc0c0 },
78 { "HPSCAN", 0xc0c0 }
81 #define GET_DRIVE(path) \
82 (((path)[1] == ':') ? FILE_toupper((path)[0]) - 'A' : DOSFS_CurDrive)
84 /* Directory info for DOSFS_ReadDir */
85 typedef struct
87 DIR *dir;
88 #ifdef VFAT_IOCTL_READDIR_BOTH
89 int fd;
90 char short_name[12];
91 KERNEL_DIRENT dirent[2];
92 #endif
93 } DOS_DIR;
95 /* Info structure for FindFirstFile handle */
96 typedef struct
98 LPSTR path;
99 LPSTR long_mask;
100 LPSTR short_mask;
101 BYTE attr;
102 int drive;
103 int cur_pos;
104 DOS_DIR *dir;
105 } FIND_FIRST_INFO;
109 /***********************************************************************
110 * DOSFS_ValidDOSName
112 * Return 1 if Unix file 'name' is also a valid MS-DOS name
113 * (i.e. contains only valid DOS chars, lower-case only, fits in 8.3 format).
114 * File name can be terminated by '\0', '\\' or '/'.
116 static int DOSFS_ValidDOSName( const char *name, int ignore_case )
118 static const char invalid_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" INVALID_DOS_CHARS;
119 const char *p = name;
120 const char *invalid = ignore_case ? (invalid_chars + 26) : invalid_chars;
121 int len = 0;
123 if (*p == '.')
125 /* Check for "." and ".." */
126 p++;
127 if (*p == '.') p++;
128 /* All other names beginning with '.' are invalid */
129 return (IS_END_OF_NAME(*p));
131 while (!IS_END_OF_NAME(*p))
133 if (strchr( invalid, *p )) return 0; /* Invalid char */
134 if (*p == '.') break; /* Start of the extension */
135 if (++len > 8) return 0; /* Name too long */
136 p++;
138 if (*p != '.') return 1; /* End of name */
139 p++;
140 if (IS_END_OF_NAME(*p)) return 0; /* Empty extension not allowed */
141 len = 0;
142 while (!IS_END_OF_NAME(*p))
144 if (strchr( invalid, *p )) return 0; /* Invalid char */
145 if (*p == '.') return 0; /* Second extension not allowed */
146 if (++len > 3) return 0; /* Extension too long */
147 p++;
149 return 1;
153 /***********************************************************************
154 * DOSFS_ToDosFCBFormat
156 * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
157 * expanding wild cards and converting to upper-case in the process.
158 * File name can be terminated by '\0', '\\' or '/'.
159 * Return FALSE if the name is not a valid DOS name.
160 * 'buffer' must be at least 12 characters long.
162 BOOL DOSFS_ToDosFCBFormat( LPCSTR name, LPSTR buffer )
164 static const char invalid_chars[] = INVALID_DOS_CHARS;
165 const char *p = name;
166 int i;
168 /* Check for "." and ".." */
169 if (*p == '.')
171 p++;
172 strcpy( buffer, ". " );
173 if (*p == '.')
175 buffer[1] = '.';
176 p++;
178 return (!*p || (*p == '/') || (*p == '\\'));
181 for (i = 0; i < 8; i++)
183 switch(*p)
185 case '\0':
186 case '\\':
187 case '/':
188 case '.':
189 buffer[i] = ' ';
190 break;
191 case '?':
192 p++;
193 /* fall through */
194 case '*':
195 buffer[i] = '?';
196 break;
197 default:
198 if (strchr( invalid_chars, *p )) return FALSE;
199 buffer[i] = FILE_toupper(*p);
200 p++;
201 break;
205 if (*p == '*')
207 /* Skip all chars after wildcard up to first dot */
208 while (*p && (*p != '/') && (*p != '\\') && (*p != '.')) p++;
210 else
212 /* Check if name too long */
213 if (*p && (*p != '/') && (*p != '\\') && (*p != '.')) return FALSE;
215 if (*p == '.') p++; /* Skip dot */
217 for (i = 8; i < 11; i++)
219 switch(*p)
221 case '\0':
222 case '\\':
223 case '/':
224 buffer[i] = ' ';
225 break;
226 case '.':
227 return FALSE; /* Second extension not allowed */
228 case '?':
229 p++;
230 /* fall through */
231 case '*':
232 buffer[i] = '?';
233 break;
234 default:
235 if (strchr( invalid_chars, *p )) return FALSE;
236 buffer[i] = FILE_toupper(*p);
237 p++;
238 break;
241 buffer[11] = '\0';
243 /* at most 3 character of the extension are processed
244 * is something behind this ?
246 while (*p == '*' || *p == ' ') p++; /* skip wildcards and spaces */
247 return IS_END_OF_NAME(*p);
251 /***********************************************************************
252 * DOSFS_ToDosDTAFormat
254 * Convert a file name from FCB to DTA format (name.ext, null-terminated)
255 * converting to upper-case in the process.
256 * File name can be terminated by '\0', '\\' or '/'.
257 * 'buffer' must be at least 13 characters long.
259 static void DOSFS_ToDosDTAFormat( LPCSTR name, LPSTR buffer )
261 char *p;
263 memcpy( buffer, name, 8 );
264 p = buffer + 8;
265 while ((p > buffer) && (p[-1] == ' ')) p--;
266 *p++ = '.';
267 memcpy( p, name + 8, 3 );
268 p += 3;
269 while (p[-1] == ' ') p--;
270 if (p[-1] == '.') p--;
271 *p = '\0';
275 /***********************************************************************
276 * DOSFS_MatchShort
278 * Check a DOS file name against a mask (both in FCB format).
280 static int DOSFS_MatchShort( const char *mask, const char *name )
282 int i;
283 for (i = 11; i > 0; i--, mask++, name++)
284 if ((*mask != '?') && (*mask != *name)) return 0;
285 return 1;
289 /***********************************************************************
290 * DOSFS_MatchLong
292 * Check a long file name against a mask.
294 * Tests (done in W95 DOS shell - case insensitive):
295 * *.txt test1.test.txt *
296 * *st1* test1.txt *
297 * *.t??????.t* test1.ta.tornado.txt *
298 * *tornado* test1.ta.tornado.txt *
299 * t*t test1.ta.tornado.txt *
300 * ?est* test1.txt *
301 * ?est??? test1.txt -
302 * *test1.txt* test1.txt *
303 * h?l?o*t.dat hellothisisatest.dat *
305 static int DOSFS_MatchLong( const char *mask, const char *name,
306 int case_sensitive )
308 const char *lastjoker = NULL;
309 const char *next_to_retry = NULL;
311 if (!strcmp( mask, "*.*" )) return 1;
312 while (*name && *mask)
314 if (*mask == '*')
316 mask++;
317 while (*mask == '*') mask++; /* Skip consecutive '*' */
318 lastjoker = mask;
319 if (!*mask) return 1; /* end of mask is all '*', so match */
321 /* skip to the next match after the joker(s) */
322 if (case_sensitive) while (*name && (*name != *mask)) name++;
323 else while (*name && (FILE_toupper(*name) != FILE_toupper(*mask))) name++;
325 if (!*name) break;
326 next_to_retry = name;
328 else if (*mask != '?')
330 int mismatch = 0;
331 if (case_sensitive)
333 if (*mask != *name) mismatch = 1;
335 else
337 if (FILE_toupper(*mask) != FILE_toupper(*name)) mismatch = 1;
339 if (!mismatch)
341 mask++;
342 name++;
343 if (*mask == '\0')
345 if (*name == '\0')
346 return 1;
347 if (lastjoker)
348 mask = lastjoker;
351 else /* mismatch ! */
353 if (lastjoker) /* we had an '*', so we can try unlimitedly */
355 mask = lastjoker;
357 /* this scan sequence was a mismatch, so restart
358 * 1 char after the first char we checked last time */
359 next_to_retry++;
360 name = next_to_retry;
362 else
363 return 0; /* bad luck */
366 else /* '?' */
368 mask++;
369 name++;
372 while ((*mask == '.') || (*mask == '*'))
373 mask++; /* Ignore trailing '.' or '*' in mask */
374 return (!*name && !*mask);
378 /***********************************************************************
379 * DOSFS_OpenDir
381 static DOS_DIR *DOSFS_OpenDir( LPCSTR path )
383 DOS_DIR *dir = HeapAlloc( GetProcessHeap(), 0, sizeof(*dir) );
384 if (!dir)
386 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
387 return NULL;
390 /* Treat empty path as root directory. This simplifies path split into
391 directory and mask in several other places */
392 if (!*path) path = "/";
394 #ifdef VFAT_IOCTL_READDIR_BOTH
396 /* Check if the VFAT ioctl is supported on this directory */
398 if ((dir->fd = open( path, O_RDONLY )) != -1)
400 if (ioctl( dir->fd, VFAT_IOCTL_READDIR_BOTH, (long)dir->dirent ) == -1)
402 close( dir->fd );
403 dir->fd = -1;
405 else
407 /* Set the file pointer back at the start of the directory */
408 lseek( dir->fd, 0, SEEK_SET );
409 dir->dir = NULL;
410 return dir;
413 #endif /* VFAT_IOCTL_READDIR_BOTH */
415 /* Now use the standard opendir/readdir interface */
417 if (!(dir->dir = opendir( path )))
419 HeapFree( GetProcessHeap(), 0, dir );
420 return NULL;
422 return dir;
426 /***********************************************************************
427 * DOSFS_CloseDir
429 static void DOSFS_CloseDir( DOS_DIR *dir )
431 #ifdef VFAT_IOCTL_READDIR_BOTH
432 if (dir->fd != -1) close( dir->fd );
433 #endif /* VFAT_IOCTL_READDIR_BOTH */
434 if (dir->dir) closedir( dir->dir );
435 HeapFree( GetProcessHeap(), 0, dir );
439 /***********************************************************************
440 * DOSFS_ReadDir
442 static BOOL DOSFS_ReadDir( DOS_DIR *dir, LPCSTR *long_name,
443 LPCSTR *short_name )
445 struct dirent *dirent;
447 #ifdef VFAT_IOCTL_READDIR_BOTH
448 if (dir->fd != -1)
450 if (ioctl( dir->fd, VFAT_IOCTL_READDIR_BOTH, (long)dir->dirent ) != -1) {
451 if (!dir->dirent[0].d_reclen) return FALSE;
452 if (!DOSFS_ToDosFCBFormat( dir->dirent[0].d_name, dir->short_name ))
453 dir->short_name[0] = '\0';
454 *short_name = dir->short_name;
455 if (dir->dirent[1].d_name[0]) *long_name = dir->dirent[1].d_name;
456 else *long_name = dir->dirent[0].d_name;
457 return TRUE;
460 #endif /* VFAT_IOCTL_READDIR_BOTH */
462 if (!(dirent = readdir( dir->dir ))) return FALSE;
463 *long_name = dirent->d_name;
464 *short_name = NULL;
465 return TRUE;
469 /***********************************************************************
470 * DOSFS_Hash
472 * Transform a Unix file name into a hashed DOS name. If the name is a valid
473 * DOS name, it is converted to upper-case; otherwise it is replaced by a
474 * hashed version that fits in 8.3 format.
475 * File name can be terminated by '\0', '\\' or '/'.
476 * 'buffer' must be at least 13 characters long.
478 static void DOSFS_Hash( LPCSTR name, LPSTR buffer, BOOL dir_format,
479 BOOL ignore_case )
481 static const char invalid_chars[] = INVALID_DOS_CHARS "~.";
482 static const char hash_chars[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
484 const char *p, *ext;
485 char *dst;
486 unsigned short hash;
487 int i;
489 if (dir_format) strcpy( buffer, " " );
491 if (DOSFS_ValidDOSName( name, ignore_case ))
493 /* Check for '.' and '..' */
494 if (*name == '.')
496 buffer[0] = '.';
497 if (!dir_format) buffer[1] = buffer[2] = '\0';
498 if (name[1] == '.') buffer[1] = '.';
499 return;
502 /* Simply copy the name, converting to uppercase */
504 for (dst = buffer; !IS_END_OF_NAME(*name) && (*name != '.'); name++)
505 *dst++ = FILE_toupper(*name);
506 if (*name == '.')
508 if (dir_format) dst = buffer + 8;
509 else *dst++ = '.';
510 for (name++; !IS_END_OF_NAME(*name); name++)
511 *dst++ = FILE_toupper(*name);
513 if (!dir_format) *dst = '\0';
514 return;
517 /* Compute the hash code of the file name */
518 /* If you know something about hash functions, feel free to */
519 /* insert a better algorithm here... */
520 if (ignore_case)
522 for (p = name, hash = 0xbeef; !IS_END_OF_NAME(p[1]); p++)
523 hash = (hash<<3) ^ (hash>>5) ^ FILE_tolower(*p) ^ (FILE_tolower(p[1]) << 8);
524 hash = (hash<<3) ^ (hash>>5) ^ FILE_tolower(*p); /* Last character*/
526 else
528 for (p = name, hash = 0xbeef; !IS_END_OF_NAME(p[1]); p++)
529 hash = (hash << 3) ^ (hash >> 5) ^ *p ^ (p[1] << 8);
530 hash = (hash << 3) ^ (hash >> 5) ^ *p; /* Last character */
533 /* Find last dot for start of the extension */
534 for (p = name+1, ext = NULL; !IS_END_OF_NAME(*p); p++)
535 if (*p == '.') ext = p;
536 if (ext && IS_END_OF_NAME(ext[1]))
537 ext = NULL; /* Empty extension ignored */
539 /* Copy first 4 chars, replacing invalid chars with '_' */
540 for (i = 4, p = name, dst = buffer; i > 0; i--, p++)
542 if (IS_END_OF_NAME(*p) || (p == ext)) break;
543 *dst++ = strchr( invalid_chars, *p ) ? '_' : FILE_toupper(*p);
545 /* Pad to 5 chars with '~' */
546 while (i-- >= 0) *dst++ = '~';
548 /* Insert hash code converted to 3 ASCII chars */
549 *dst++ = hash_chars[(hash >> 10) & 0x1f];
550 *dst++ = hash_chars[(hash >> 5) & 0x1f];
551 *dst++ = hash_chars[hash & 0x1f];
553 /* Copy the first 3 chars of the extension (if any) */
554 if (ext)
556 if (!dir_format) *dst++ = '.';
557 for (i = 3, ext++; (i > 0) && !IS_END_OF_NAME(*ext); i--, ext++)
558 *dst++ = strchr( invalid_chars, *ext ) ? '_' : FILE_toupper(*ext);
560 if (!dir_format) *dst = '\0';
564 /***********************************************************************
565 * DOSFS_FindUnixName
567 * Find the Unix file name in a given directory that corresponds to
568 * a file name (either in Unix or DOS format).
569 * File name can be terminated by '\0', '\\' or '/'.
570 * Return TRUE if OK, FALSE if no file name matches.
572 * 'long_buf' must be at least 'long_len' characters long. If the long name
573 * turns out to be larger than that, the function returns FALSE.
574 * 'short_buf' must be at least 13 characters long.
576 BOOL DOSFS_FindUnixName( LPCSTR path, LPCSTR name, LPSTR long_buf,
577 INT long_len, LPSTR short_buf, BOOL ignore_case)
579 DOS_DIR *dir;
580 LPCSTR long_name, short_name;
581 char dos_name[12], tmp_buf[13];
582 BOOL ret;
584 const char *p = strchr( name, '/' );
585 int len = p ? (int)(p - name) : strlen(name);
586 if ((p = strchr( name, '\\' ))) len = min( (int)(p - name), len );
587 /* Ignore trailing dots and spaces */
588 while (len > 1 && (name[len-1] == '.' || name[len-1] == ' ')) len--;
589 if (long_len < len + 1) return FALSE;
591 TRACE("%s,%s\n", path, name );
593 if (!DOSFS_ToDosFCBFormat( name, dos_name )) dos_name[0] = '\0';
595 if (!(dir = DOSFS_OpenDir( path )))
597 WARN("(%s,%s): can't open dir: %s\n",
598 path, name, strerror(errno) );
599 return FALSE;
602 while ((ret = DOSFS_ReadDir( dir, &long_name, &short_name )))
604 /* Check against Unix name */
605 if (len == strlen(long_name))
607 if (!ignore_case)
609 if (!strncmp( long_name, name, len )) break;
611 else
613 if (!FILE_strncasecmp( long_name, name, len )) break;
616 if (dos_name[0])
618 /* Check against hashed DOS name */
619 if (!short_name)
621 DOSFS_Hash( long_name, tmp_buf, TRUE, ignore_case );
622 short_name = tmp_buf;
624 if (!strcmp( dos_name, short_name )) break;
627 if (ret)
629 if (long_buf) strcpy( long_buf, long_name );
630 if (short_buf)
632 if (short_name)
633 DOSFS_ToDosDTAFormat( short_name, short_buf );
634 else
635 DOSFS_Hash( long_name, short_buf, FALSE, ignore_case );
637 TRACE("(%s,%s) -> %s (%s)\n",
638 path, name, long_name, short_buf ? short_buf : "***");
640 else
641 WARN("'%s' not found in '%s'\n", name, path);
642 DOSFS_CloseDir( dir );
643 return ret;
647 /***********************************************************************
648 * DOSFS_GetDevice
650 * Check if a DOS file name represents a DOS device and return the device.
652 const DOS_DEVICE *DOSFS_GetDevice( const char *name )
654 int i;
655 const char *p;
657 if (!name) return NULL; /* if FILE_DupUnixHandle was used */
658 if (name[0] && (name[1] == ':')) name += 2;
659 if ((p = strrchr( name, '/' ))) name = p + 1;
660 if ((p = strrchr( name, '\\' ))) name = p + 1;
661 for (i = 0; i < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0]); i++)
663 const char *dev = DOSFS_Devices[i].name;
664 if (!FILE_strncasecmp( dev, name, strlen(dev) ))
666 p = name + strlen( dev );
667 if (!*p || (*p == '.') || (*p == ':')) return &DOSFS_Devices[i];
670 return NULL;
674 /***********************************************************************
675 * DOSFS_GetDeviceByHandle
677 const DOS_DEVICE *DOSFS_GetDeviceByHandle( HFILE hFile )
679 const DOS_DEVICE *ret = NULL;
680 SERVER_START_REQ( get_file_info )
682 req->handle = hFile;
683 if (!SERVER_CALL() && (req->type == FILE_TYPE_UNKNOWN))
685 if ((req->attr >= 0) &&
686 (req->attr < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0])))
687 ret = &DOSFS_Devices[req->attr];
690 SERVER_END_REQ;
691 return ret;
695 /**************************************************************************
696 * DOSFS_CreateCommPort
698 static HANDLE DOSFS_CreateCommPort(LPCSTR name, DWORD access)
700 HANDLE ret;
701 char devname[40];
702 size_t len;
704 TRACE("%s %lx\n", name, access);
706 PROFILE_GetWineIniString("serialports",name,"",devname,sizeof devname);
707 if(!devname[0])
708 return 0;
710 TRACE("opening %s as %s\n", devname, name);
712 len = strlen(devname);
713 SERVER_START_VAR_REQ( create_serial, len )
715 req->access = access;
716 req->inherit = 0; /*FIXME*/
717 req->sharing = FILE_SHARE_READ|FILE_SHARE_WRITE;
718 memcpy( server_data_ptr(req), devname, len );
719 SetLastError(0);
720 SERVER_CALL_ERR();
721 ret = req->handle;
723 SERVER_END_VAR_REQ;
725 if(!ret)
726 ERR("Couldn't open %s ! (check permissions)\n",devname);
727 else
728 TRACE("return %08X\n", ret );
729 return ret;
732 /***********************************************************************
733 * DOSFS_OpenDevice
735 * Open a DOS device. This might not map 1:1 into the UNIX device concept.
736 * Returns 0 on failure.
738 HANDLE DOSFS_OpenDevice( const char *name, DWORD access )
740 int i;
741 const char *p;
742 HANDLE handle;
744 if (name[0] && (name[1] == ':')) name += 2;
745 if ((p = strrchr( name, '/' ))) name = p + 1;
746 if ((p = strrchr( name, '\\' ))) name = p + 1;
747 for (i = 0; i < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0]); i++)
749 const char *dev = DOSFS_Devices[i].name;
750 if (!FILE_strncasecmp( dev, name, strlen(dev) ))
752 p = name + strlen( dev );
753 if (!*p || (*p == '.') || (*p == ':')) {
754 /* got it */
755 if (!strcmp(DOSFS_Devices[i].name,"NUL"))
756 return FILE_CreateFile( "/dev/null", access,
757 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
758 OPEN_EXISTING, 0, 0, TRUE );
759 if (!strcmp(DOSFS_Devices[i].name,"CON")) {
760 HANDLE to_dup;
761 switch (access & (GENERIC_READ|GENERIC_WRITE)) {
762 case GENERIC_READ:
763 to_dup = GetStdHandle( STD_INPUT_HANDLE );
764 break;
765 case GENERIC_WRITE:
766 to_dup = GetStdHandle( STD_OUTPUT_HANDLE );
767 break;
768 default:
769 FIXME("can't open CON read/write\n");
770 return 0;
772 if (!DuplicateHandle( GetCurrentProcess(), to_dup, GetCurrentProcess(),
773 &handle, 0, FALSE, DUPLICATE_SAME_ACCESS ))
774 handle = 0;
775 return handle;
777 if (!strcmp(DOSFS_Devices[i].name,"SCSIMGR$") ||
778 !strcmp(DOSFS_Devices[i].name,"HPSCAN"))
780 return FILE_CreateDevice( i, access, NULL );
783 if( (handle=DOSFS_CreateCommPort(DOSFS_Devices[i].name,access)) )
784 return handle;
785 FIXME("device open %s not supported (yet)\n",DOSFS_Devices[i].name);
786 return 0;
790 return 0;
794 /***********************************************************************
795 * DOSFS_GetPathDrive
797 * Get the drive specified by a given path name (DOS or Unix format).
799 static int DOSFS_GetPathDrive( const char **name )
801 int drive;
802 const char *p = *name;
804 if (*p && (p[1] == ':'))
806 drive = FILE_toupper(*p) - 'A';
807 *name += 2;
809 else if (*p == '/') /* Absolute Unix path? */
811 if ((drive = DRIVE_FindDriveRoot( name )) == -1)
813 MESSAGE("Warning: %s not accessible from a DOS drive\n", *name );
814 /* Assume it really was a DOS name */
815 drive = DRIVE_GetCurrentDrive();
818 else drive = DRIVE_GetCurrentDrive();
820 if (!DRIVE_IsValid(drive))
822 SetLastError( ERROR_INVALID_DRIVE );
823 return -1;
825 return drive;
829 /***********************************************************************
830 * DOSFS_GetFullName
832 * Convert a file name (DOS or mixed DOS/Unix format) to a valid
833 * Unix name / short DOS name pair.
834 * Return FALSE if one of the path components does not exist. The last path
835 * component is only checked if 'check_last' is non-zero.
836 * The buffers pointed to by 'long_buf' and 'short_buf' must be
837 * at least MAX_PATHNAME_LEN long.
839 BOOL DOSFS_GetFullName( LPCSTR name, BOOL check_last, DOS_FULL_NAME *full )
841 BOOL found;
842 UINT flags;
843 char *p_l, *p_s, *root;
845 TRACE("%s (last=%d)\n", name, check_last );
847 if ((!*name) || (*name=='\n'))
848 { /* error code for Win98 */
849 SetLastError(ERROR_BAD_PATHNAME);
850 return FALSE;
853 if ((full->drive = DOSFS_GetPathDrive( &name )) == -1) return FALSE;
854 flags = DRIVE_GetFlags( full->drive );
856 lstrcpynA( full->long_name, DRIVE_GetRoot( full->drive ),
857 sizeof(full->long_name) );
858 if (full->long_name[1]) root = full->long_name + strlen(full->long_name);
859 else root = full->long_name; /* root directory */
861 strcpy( full->short_name, "A:\\" );
862 full->short_name[0] += full->drive;
864 if ((*name == '\\') || (*name == '/')) /* Absolute path */
866 while ((*name == '\\') || (*name == '/')) name++;
868 else /* Relative path */
870 lstrcpynA( root + 1, DRIVE_GetUnixCwd( full->drive ),
871 sizeof(full->long_name) - (root - full->long_name) - 1 );
872 if (root[1]) *root = '/';
873 lstrcpynA( full->short_name + 3, DRIVE_GetDosCwd( full->drive ),
874 sizeof(full->short_name) - 3 );
877 p_l = full->long_name[1] ? full->long_name + strlen(full->long_name)
878 : full->long_name;
879 p_s = full->short_name[3] ? full->short_name + strlen(full->short_name)
880 : full->short_name + 2;
881 found = TRUE;
883 while (*name && found)
885 /* Check for '.' and '..' */
887 if (*name == '.')
889 if (IS_END_OF_NAME(name[1]))
891 name++;
892 while ((*name == '\\') || (*name == '/')) name++;
893 continue;
895 else if ((name[1] == '.') && IS_END_OF_NAME(name[2]))
897 name += 2;
898 while ((*name == '\\') || (*name == '/')) name++;
899 while ((p_l > root) && (*p_l != '/')) p_l--;
900 while ((p_s > full->short_name + 2) && (*p_s != '\\')) p_s--;
901 *p_l = *p_s = '\0'; /* Remove trailing separator */
902 continue;
906 /* Make sure buffers are large enough */
908 if ((p_s >= full->short_name + sizeof(full->short_name) - 14) ||
909 (p_l >= full->long_name + sizeof(full->long_name) - 1))
911 SetLastError( ERROR_PATH_NOT_FOUND );
912 return FALSE;
915 /* Get the long and short name matching the file name */
917 if ((found = DOSFS_FindUnixName( full->long_name, name, p_l + 1,
918 sizeof(full->long_name) - (p_l - full->long_name) - 1,
919 p_s + 1, !(flags & DRIVE_CASE_SENSITIVE) )))
921 *p_l++ = '/';
922 p_l += strlen(p_l);
923 *p_s++ = '\\';
924 p_s += strlen(p_s);
925 while (!IS_END_OF_NAME(*name)) name++;
927 else if (!check_last)
929 *p_l++ = '/';
930 *p_s++ = '\\';
931 while (!IS_END_OF_NAME(*name) &&
932 (p_s < full->short_name + sizeof(full->short_name) - 1) &&
933 (p_l < full->long_name + sizeof(full->long_name) - 1))
935 *p_s++ = FILE_tolower(*name);
936 /* If the drive is case-sensitive we want to create new */
937 /* files in lower-case otherwise we can't reopen them */
938 /* under the same short name. */
939 if (flags & DRIVE_CASE_SENSITIVE) *p_l++ = FILE_tolower(*name);
940 else *p_l++ = *name;
941 name++;
943 /* Ignore trailing dots and spaces */
944 while(p_l[-1] == '.' || p_l[-1] == ' ') {
945 --p_l;
946 --p_s;
948 *p_l = *p_s = '\0';
950 while ((*name == '\\') || (*name == '/')) name++;
953 if (!found)
955 if (check_last)
957 SetLastError( ERROR_FILE_NOT_FOUND );
958 return FALSE;
960 if (*name) /* Not last */
962 SetLastError( ERROR_PATH_NOT_FOUND );
963 return FALSE;
966 if (!full->long_name[0]) strcpy( full->long_name, "/" );
967 if (!full->short_name[2]) strcpy( full->short_name + 2, "\\" );
968 TRACE("returning %s = %s\n", full->long_name, full->short_name );
969 return TRUE;
973 /***********************************************************************
974 * GetShortPathNameA (KERNEL32.271)
976 * NOTES
977 * observed:
978 * longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
979 * *longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
981 * more observations ( with NT 3.51 (WinDD) ):
982 * longpath <= 8.3 -> just copy longpath to shortpath
983 * longpath > 8.3 ->
984 * a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
985 * b) file does exist -> set the short filename.
986 * - trailing slashes are reproduced in the short name, even if the
987 * file is not a directory
988 * - the absolute/relative path of the short name is reproduced like found
989 * in the long name
990 * - longpath and shortpath may have the same adress
991 * Peter Ganten, 1999
993 DWORD WINAPI GetShortPathNameA( LPCSTR longpath, LPSTR shortpath,
994 DWORD shortlen )
996 DOS_FULL_NAME full_name;
997 LPSTR tmpshortpath;
998 DWORD sp = 0, lp = 0;
999 int tmplen, drive;
1000 UINT flags;
1002 TRACE("%s\n", debugstr_a(longpath));
1004 if (!longpath) {
1005 SetLastError(ERROR_INVALID_PARAMETER);
1006 return 0;
1008 if (!longpath[0]) {
1009 SetLastError(ERROR_BAD_PATHNAME);
1010 return 0;
1013 if ( ( tmpshortpath = HeapAlloc ( GetProcessHeap(), 0, MAX_PATHNAME_LEN ) ) == NULL ) {
1014 SetLastError ( ERROR_NOT_ENOUGH_MEMORY );
1015 return 0;
1018 /* check for drive letter */
1019 if ( longpath[1] == ':' ) {
1020 tmpshortpath[0] = longpath[0];
1021 tmpshortpath[1] = ':';
1022 sp = 2;
1025 if ( ( drive = DOSFS_GetPathDrive ( &longpath )) == -1 ) return 0;
1026 flags = DRIVE_GetFlags ( drive );
1028 while ( longpath[lp] ) {
1030 /* check for path delimiters and reproduce them */
1031 if ( longpath[lp] == '\\' || longpath[lp] == '/' ) {
1032 if (!sp || tmpshortpath[sp-1]!= '\\')
1034 /* strip double "\\" */
1035 tmpshortpath[sp] = '\\';
1036 sp++;
1038 tmpshortpath[sp]=0;/*terminate string*/
1039 lp++;
1040 continue;
1043 tmplen = strcspn ( longpath + lp, "\\/" );
1044 lstrcpynA ( tmpshortpath+sp, longpath + lp, tmplen+1 );
1046 /* Check, if the current element is a valid dos name */
1047 if ( DOSFS_ValidDOSName ( longpath + lp, !(flags & DRIVE_CASE_SENSITIVE) ) ) {
1048 sp += tmplen;
1049 lp += tmplen;
1050 continue;
1053 /* Check if the file exists and use the existing file name */
1054 if ( DOSFS_GetFullName ( tmpshortpath, TRUE, &full_name ) ) {
1055 strcpy( tmpshortpath+sp, strrchr ( full_name.short_name, '\\' ) + 1 );
1056 sp += strlen ( tmpshortpath+sp );
1057 lp += tmplen;
1058 continue;
1061 TRACE("not found!\n" );
1062 SetLastError ( ERROR_FILE_NOT_FOUND );
1063 return 0;
1065 tmpshortpath[sp] = 0;
1067 lstrcpynA ( shortpath, tmpshortpath, shortlen );
1068 TRACE("returning %s\n", debugstr_a(shortpath) );
1069 tmplen = strlen ( tmpshortpath );
1070 HeapFree ( GetProcessHeap(), 0, tmpshortpath );
1072 return tmplen;
1076 /***********************************************************************
1077 * GetShortPathNameW (KERNEL32.272)
1079 DWORD WINAPI GetShortPathNameW( LPCWSTR longpath, LPWSTR shortpath,
1080 DWORD shortlen )
1082 LPSTR longpathA, shortpathA;
1083 DWORD ret = 0;
1085 longpathA = HEAP_strdupWtoA( GetProcessHeap(), 0, longpath );
1086 shortpathA = HeapAlloc ( GetProcessHeap(), 0, shortlen );
1088 ret = GetShortPathNameA ( longpathA, shortpathA, shortlen );
1089 if (shortlen > 0 && !MultiByteToWideChar( CP_ACP, 0, shortpathA, -1, shortpath, shortlen ))
1090 shortpath[shortlen-1] = 0;
1091 HeapFree( GetProcessHeap(), 0, longpathA );
1092 HeapFree( GetProcessHeap(), 0, shortpathA );
1094 return ret;
1098 /***********************************************************************
1099 * GetLongPathNameA (KERNEL32.xxx)
1101 DWORD WINAPI GetLongPathNameA( LPCSTR shortpath, LPSTR longpath,
1102 DWORD longlen )
1104 DOS_FULL_NAME full_name;
1105 char *p, *r, *ll, *ss;
1107 if (!DOSFS_GetFullName( shortpath, TRUE, &full_name )) return 0;
1108 lstrcpynA( longpath, full_name.short_name, longlen );
1110 /* Do some hackery to get the long filename. */
1112 if (longpath) {
1113 ss=longpath+strlen(longpath);
1114 ll=full_name.long_name+strlen(full_name.long_name);
1115 p=NULL;
1116 while (ss>=longpath)
1118 /* FIXME: aren't we more paranoid, than needed? */
1119 while ((ss[0]=='\\') && (ss>=longpath)) ss--;
1120 p=ss;
1121 while ((ss[0]!='\\') && (ss>=longpath)) ss--;
1122 if (ss>=longpath)
1124 /* FIXME: aren't we more paranoid, than needed? */
1125 while ((ll[0]=='/') && (ll>=full_name.long_name)) ll--;
1126 while ((ll[0]!='/') && (ll>=full_name.long_name)) ll--;
1127 if (ll<full_name.long_name)
1129 ERR("Bad longname! (ss=%s ll=%s)\n This should never happen !\n"
1130 ,ss ,ll );
1131 return 0;
1136 /* FIXME: fix for names like "C:\\" (ie. with more '\'s) */
1137 if (p && p[2])
1139 p+=1;
1140 if ((p-longpath)>0) longlen -= (p-longpath);
1141 lstrcpynA( p, ll , longlen);
1143 /* Now, change all '/' to '\' */
1144 for (r=p; r<(p+longlen); r++ )
1145 if (r[0]=='/') r[0]='\\';
1146 return strlen(longpath) - strlen(p) + longlen;
1150 return strlen(longpath);
1154 /***********************************************************************
1155 * GetLongPathNameW (KERNEL32.269)
1157 DWORD WINAPI GetLongPathNameW( LPCWSTR shortpath, LPWSTR longpath,
1158 DWORD longlen )
1160 DOS_FULL_NAME full_name;
1161 DWORD ret = 0;
1162 LPSTR shortpathA = HEAP_strdupWtoA( GetProcessHeap(), 0, shortpath );
1164 /* FIXME: is it correct to always return a fully qualified short path? */
1165 if (DOSFS_GetFullName( shortpathA, TRUE, &full_name ))
1167 ret = strlen( full_name.short_name );
1168 if (longlen > 0 && !MultiByteToWideChar( CP_ACP, 0, full_name.long_name, -1,
1169 longpath, longlen ))
1170 longpath[longlen-1] = 0;
1172 HeapFree( GetProcessHeap(), 0, shortpathA );
1173 return ret;
1177 /***********************************************************************
1178 * DOSFS_DoGetFullPathName
1180 * Implementation of GetFullPathNameA/W.
1182 * bon@elektron 000331:
1183 * A test for GetFullPathName with many pathological cases
1184 * now gives identical output for Wine and OSR2
1186 static DWORD DOSFS_DoGetFullPathName( LPCSTR name, DWORD len, LPSTR result,
1187 BOOL unicode )
1189 DWORD ret;
1190 DOS_FULL_NAME full_name;
1191 char *p,*q;
1192 const char * root;
1193 char drivecur[]="c:.";
1194 char driveletter=0;
1195 int namelen,drive=0;
1197 if ((strlen(name) >1)&& (name[1]==':'))
1198 /*drive letter given */
1200 driveletter = name[0];
1202 if ((strlen(name) >2)&& (name[1]==':') &&
1203 ((name[2]=='\\') || (name[2]=='/')))
1204 /*absolute path given */
1206 lstrcpynA(full_name.short_name,name,MAX_PATHNAME_LEN);
1207 drive = (int)FILE_toupper(name[0]) - 'A';
1209 else
1211 if (driveletter)
1212 drivecur[0]=driveletter;
1213 else
1214 strcpy(drivecur,".");
1215 if (!DOSFS_GetFullName( drivecur, FALSE, &full_name ))
1217 FIXME("internal: error getting drive/path\n");
1218 return 0;
1220 /* find path that drive letter substitutes*/
1221 drive = (int)FILE_toupper(full_name.short_name[0]) -0x41;
1222 root= DRIVE_GetRoot(drive);
1223 if (!root)
1225 FIXME("internal: error getting DOS Drive Root\n");
1226 return 0;
1228 if (!strcmp(root,"/"))
1230 /* we have just the last / and we need it. */
1231 p= full_name.long_name;
1233 else
1235 p= full_name.long_name +strlen(root);
1237 /* append long name (= unix name) to drive */
1238 lstrcpynA(full_name.short_name+2,p,MAX_PATHNAME_LEN-3);
1239 /* append name to treat */
1240 namelen= strlen(full_name.short_name);
1241 p = (char*)name;
1242 if (driveletter)
1243 p += +2; /* skip drive name when appending */
1244 if (namelen +2 + strlen(p) > MAX_PATHNAME_LEN)
1246 FIXME("internal error: buffer too small\n");
1247 return 0;
1249 full_name.short_name[namelen++] ='\\';
1250 full_name.short_name[namelen] = 0;
1251 lstrcpynA(full_name.short_name +namelen,p,MAX_PATHNAME_LEN-namelen);
1253 /* reverse all slashes */
1254 for (p=full_name.short_name;
1255 p < full_name.short_name+strlen(full_name.short_name);
1256 p++)
1258 if ( *p == '/' )
1259 *p = '\\';
1261 /* Use memmove, as areas overlap*/
1262 /* Delete .. */
1263 while ((p = strstr(full_name.short_name,"\\..\\")))
1265 if (p > full_name.short_name+2)
1267 *p = 0;
1268 q = strrchr(full_name.short_name,'\\');
1269 memmove(q+1,p+4,strlen(p+4)+1);
1271 else
1273 memmove(full_name.short_name+3,p+4,strlen(p+4)+1);
1276 if ((full_name.short_name[2]=='.')&&(full_name.short_name[3]=='.'))
1278 /* This case istn't treated yet : c:..\test */
1279 memmove(full_name.short_name+2,full_name.short_name+4,
1280 strlen(full_name.short_name+4)+1);
1282 /* Delete . */
1283 while ((p = strstr(full_name.short_name,"\\.\\")))
1285 *(p+1) = 0;
1286 memmove(p+1,p+3,strlen(p+3)+1);
1288 if (!(DRIVE_GetFlags(drive) & DRIVE_CASE_PRESERVING))
1289 for (p = full_name.short_name; *p; p++) *p = FILE_toupper(*p);
1290 namelen=strlen(full_name.short_name);
1291 if (!strcmp(full_name.short_name+namelen-3,"\\.."))
1293 /* one more starnge case: "c:\test\test1\.."
1294 return "c:\test"*/
1295 *(full_name.short_name+namelen-3)=0;
1296 q = strrchr(full_name.short_name,'\\');
1297 *q =0;
1299 if (full_name.short_name[namelen-1]=='.')
1300 full_name.short_name[(namelen--)-1] =0;
1301 if (!driveletter)
1302 if (full_name.short_name[namelen-1]=='\\')
1303 full_name.short_name[(namelen--)-1] =0;
1304 TRACE("got %s\n",full_name.short_name);
1306 /* If the lpBuffer buffer is too small, the return value is the
1307 size of the buffer, in characters, required to hold the path
1308 plus the terminating \0 (tested against win95osr, bon 001118)
1309 . */
1310 ret = strlen(full_name.short_name);
1311 if (ret >= len )
1313 /* don't touch anything when the buffer is not large enough */
1314 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1315 return ret+1;
1317 if (result)
1319 if (unicode)
1320 MultiByteToWideChar( CP_ACP, 0, full_name.short_name, -1, (LPWSTR)result, len );
1321 else
1322 lstrcpynA( result, full_name.short_name, len );
1325 TRACE("returning '%s'\n", full_name.short_name );
1326 return ret;
1330 /***********************************************************************
1331 * GetFullPathNameA (KERNEL32.272)
1332 * NOTES
1333 * if the path closed with '\', *lastpart is 0
1335 DWORD WINAPI GetFullPathNameA( LPCSTR name, DWORD len, LPSTR buffer,
1336 LPSTR *lastpart )
1338 DWORD ret = DOSFS_DoGetFullPathName( name, len, buffer, FALSE );
1339 if (ret && (ret<=len) && buffer && lastpart)
1341 LPSTR p = buffer + strlen(buffer);
1343 if (*p != '\\')
1345 while ((p > buffer + 2) && (*p != '\\')) p--;
1346 *lastpart = p + 1;
1348 else *lastpart = NULL;
1350 return ret;
1354 /***********************************************************************
1355 * GetFullPathNameW (KERNEL32.273)
1357 DWORD WINAPI GetFullPathNameW( LPCWSTR name, DWORD len, LPWSTR buffer,
1358 LPWSTR *lastpart )
1360 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
1361 DWORD ret = DOSFS_DoGetFullPathName( nameA, len, (LPSTR)buffer, TRUE );
1362 HeapFree( GetProcessHeap(), 0, nameA );
1363 if (ret && (ret<=len) && buffer && lastpart)
1365 LPWSTR p = buffer + strlenW(buffer);
1366 if (*p != (WCHAR)'\\')
1368 while ((p > buffer + 2) && (*p != (WCHAR)'\\')) p--;
1369 *lastpart = p + 1;
1371 else *lastpart = NULL;
1373 return ret;
1377 /***********************************************************************
1378 * wine_get_unix_file_name (Not a Windows API, but exported from KERNEL32)
1380 * Return the full Unix file name for a given path.
1382 BOOL WINAPI wine_get_unix_file_name( LPCSTR dos, LPSTR buffer, DWORD len )
1384 BOOL ret;
1385 DOS_FULL_NAME path;
1386 if ((ret = DOSFS_GetFullName( dos, FALSE, &path ))) lstrcpynA( buffer, path.long_name, len );
1387 return ret;
1391 /***********************************************************************
1392 * DOSFS_FindNextEx
1394 static int DOSFS_FindNextEx( FIND_FIRST_INFO *info, WIN32_FIND_DATAA *entry )
1396 DWORD attr = info->attr | FA_UNUSED | FA_ARCHIVE | FA_RDONLY | FILE_ATTRIBUTE_SYMLINK;
1397 UINT flags = DRIVE_GetFlags( info->drive );
1398 char *p, buffer[MAX_PATHNAME_LEN];
1399 const char *drive_path;
1400 int drive_root;
1401 LPCSTR long_name, short_name;
1402 BY_HANDLE_FILE_INFORMATION fileinfo;
1403 char dos_name[13];
1405 if ((info->attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
1407 if (info->cur_pos) return 0;
1408 entry->dwFileAttributes = FILE_ATTRIBUTE_LABEL;
1409 RtlSecondsSince1970ToTime( (time_t)0, &entry->ftCreationTime );
1410 RtlSecondsSince1970ToTime( (time_t)0, &entry->ftLastAccessTime );
1411 RtlSecondsSince1970ToTime( (time_t)0, &entry->ftLastWriteTime );
1412 entry->nFileSizeHigh = 0;
1413 entry->nFileSizeLow = 0;
1414 entry->dwReserved0 = 0;
1415 entry->dwReserved1 = 0;
1416 DOSFS_ToDosDTAFormat( DRIVE_GetLabel( info->drive ), entry->cFileName );
1417 strcpy( entry->cAlternateFileName, entry->cFileName );
1418 info->cur_pos++;
1419 TRACE("returning %s (%s) as label\n",
1420 entry->cFileName, entry->cAlternateFileName);
1421 return 1;
1424 drive_path = info->path + strlen(DRIVE_GetRoot( info->drive ));
1425 while ((*drive_path == '/') || (*drive_path == '\\')) drive_path++;
1426 drive_root = !*drive_path;
1428 lstrcpynA( buffer, info->path, sizeof(buffer) - 1 );
1429 strcat( buffer, "/" );
1430 p = buffer + strlen(buffer);
1432 while (DOSFS_ReadDir( info->dir, &long_name, &short_name ))
1434 info->cur_pos++;
1436 /* Don't return '.' and '..' in the root of the drive */
1437 if (drive_root && (long_name[0] == '.') &&
1438 (!long_name[1] || ((long_name[1] == '.') && !long_name[2])))
1439 continue;
1441 /* Check the long mask */
1443 if (info->long_mask)
1445 if (!DOSFS_MatchLong( info->long_mask, long_name,
1446 flags & DRIVE_CASE_SENSITIVE )) continue;
1449 /* Check the short mask */
1451 if (info->short_mask)
1453 if (!short_name)
1455 DOSFS_Hash( long_name, dos_name, TRUE,
1456 !(flags & DRIVE_CASE_SENSITIVE) );
1457 short_name = dos_name;
1459 if (!DOSFS_MatchShort( info->short_mask, short_name )) continue;
1462 /* Check the file attributes */
1464 lstrcpynA( p, long_name, sizeof(buffer) - (int)(p - buffer) );
1465 if (!FILE_Stat( buffer, &fileinfo ))
1467 WARN("can't stat %s\n", buffer);
1468 continue;
1470 if ((fileinfo.dwFileAttributes & FILE_ATTRIBUTE_SYMLINK) &&
1471 (fileinfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
1473 static int show_dir_symlinks = -1;
1474 if (show_dir_symlinks == -1)
1475 show_dir_symlinks = PROFILE_GetWineIniBool("wine", "ShowDirSymlinks", 0);
1476 if (!show_dir_symlinks) continue;
1479 if (fileinfo.dwFileAttributes & ~attr) continue;
1481 /* We now have a matching entry; fill the result and return */
1483 entry->dwFileAttributes = fileinfo.dwFileAttributes;
1484 entry->ftCreationTime = fileinfo.ftCreationTime;
1485 entry->ftLastAccessTime = fileinfo.ftLastAccessTime;
1486 entry->ftLastWriteTime = fileinfo.ftLastWriteTime;
1487 entry->nFileSizeHigh = fileinfo.nFileSizeHigh;
1488 entry->nFileSizeLow = fileinfo.nFileSizeLow;
1490 if (short_name)
1491 DOSFS_ToDosDTAFormat( short_name, entry->cAlternateFileName );
1492 else
1493 DOSFS_Hash( long_name, entry->cAlternateFileName, FALSE,
1494 !(flags & DRIVE_CASE_SENSITIVE) );
1496 lstrcpynA( entry->cFileName, long_name, sizeof(entry->cFileName) );
1497 if (!(flags & DRIVE_CASE_PRESERVING)) _strlwr( entry->cFileName );
1498 TRACE("returning %s (%s) %02lx %ld\n",
1499 entry->cFileName, entry->cAlternateFileName,
1500 entry->dwFileAttributes, entry->nFileSizeLow );
1501 return 1;
1503 return 0; /* End of directory */
1506 /***********************************************************************
1507 * DOSFS_FindNext
1509 * Find the next matching file. Return the number of entries read to find
1510 * the matching one, or 0 if no more entries.
1511 * 'short_mask' is the 8.3 mask (in FCB format), 'long_mask' is the long
1512 * file name mask. Either or both can be NULL.
1514 * NOTE: This is supposed to be only called by the int21 emulation
1515 * routines. Thus, we should own the Win16Mutex anyway.
1516 * Nevertheless, we explicitly enter it to ensure the static
1517 * directory cache is protected.
1519 int DOSFS_FindNext( const char *path, const char *short_mask,
1520 const char *long_mask, int drive, BYTE attr,
1521 int skip, WIN32_FIND_DATAA *entry )
1523 static FIND_FIRST_INFO info;
1524 LPCSTR short_name, long_name;
1525 int count;
1527 _EnterWin16Lock();
1529 /* Check the cached directory */
1530 if (!(info.dir && info.path == path && info.short_mask == short_mask
1531 && info.long_mask == long_mask && info.drive == drive
1532 && info.attr == attr && info.cur_pos <= skip))
1534 /* Not in the cache, open it anew */
1535 if (info.dir) DOSFS_CloseDir( info.dir );
1537 info.path = (LPSTR)path;
1538 info.long_mask = (LPSTR)long_mask;
1539 info.short_mask = (LPSTR)short_mask;
1540 info.attr = attr;
1541 info.drive = drive;
1542 info.cur_pos = 0;
1543 info.dir = DOSFS_OpenDir( info.path );
1546 /* Skip to desired position */
1547 while (info.cur_pos < skip)
1548 if (info.dir && DOSFS_ReadDir( info.dir, &long_name, &short_name ))
1549 info.cur_pos++;
1550 else
1551 break;
1553 if (info.dir && info.cur_pos == skip && DOSFS_FindNextEx( &info, entry ))
1554 count = info.cur_pos - skip;
1555 else
1556 count = 0;
1558 if (!count)
1560 if (info.dir) DOSFS_CloseDir( info.dir );
1561 memset( &info, '\0', sizeof(info) );
1564 _LeaveWin16Lock();
1566 return count;
1569 /*************************************************************************
1570 * FindFirstFileExA (KERNEL32)
1572 HANDLE WINAPI FindFirstFileExA(
1573 LPCSTR lpFileName,
1574 FINDEX_INFO_LEVELS fInfoLevelId,
1575 LPVOID lpFindFileData,
1576 FINDEX_SEARCH_OPS fSearchOp,
1577 LPVOID lpSearchFilter,
1578 DWORD dwAdditionalFlags)
1580 DOS_FULL_NAME full_name;
1581 HGLOBAL handle;
1582 FIND_FIRST_INFO *info;
1584 if ((fSearchOp != FindExSearchNameMatch) || (dwAdditionalFlags != 0))
1586 FIXME("options not implemented 0x%08x 0x%08lx\n", fSearchOp, dwAdditionalFlags );
1587 return INVALID_HANDLE_VALUE;
1590 switch(fInfoLevelId)
1592 case FindExInfoStandard:
1594 WIN32_FIND_DATAA * data = (WIN32_FIND_DATAA *) lpFindFileData;
1595 data->dwReserved0 = data->dwReserved1 = 0x0;
1596 if (!lpFileName) return 0;
1597 if (!DOSFS_GetFullName( lpFileName, FALSE, &full_name )) break;
1598 if (!(handle = GlobalAlloc(GMEM_MOVEABLE, sizeof(FIND_FIRST_INFO)))) break;
1599 info = (FIND_FIRST_INFO *)GlobalLock( handle );
1600 info->path = HEAP_strdupA( GetProcessHeap(), 0, full_name.long_name );
1601 info->long_mask = strrchr( info->path, '/' );
1602 *(info->long_mask++) = '\0';
1603 info->short_mask = NULL;
1604 info->attr = 0xff;
1605 if (lpFileName[0] && (lpFileName[1] == ':'))
1606 info->drive = FILE_toupper(*lpFileName) - 'A';
1607 else info->drive = DRIVE_GetCurrentDrive();
1608 info->cur_pos = 0;
1610 info->dir = DOSFS_OpenDir( info->path );
1612 GlobalUnlock( handle );
1613 if (!FindNextFileA( handle, data ))
1615 FindClose( handle );
1616 SetLastError( ERROR_NO_MORE_FILES );
1617 break;
1619 return handle;
1621 break;
1622 default:
1623 FIXME("fInfoLevelId 0x%08x not implemented\n", fInfoLevelId );
1625 return INVALID_HANDLE_VALUE;
1628 /*************************************************************************
1629 * FindFirstFileA (KERNEL32.123)
1631 HANDLE WINAPI FindFirstFileA(
1632 LPCSTR lpFileName,
1633 WIN32_FIND_DATAA *lpFindData )
1635 return FindFirstFileExA(lpFileName, FindExInfoStandard, lpFindData,
1636 FindExSearchNameMatch, NULL, 0);
1639 /*************************************************************************
1640 * FindFirstFileExW (KERNEL32)
1642 HANDLE WINAPI FindFirstFileExW(
1643 LPCWSTR lpFileName,
1644 FINDEX_INFO_LEVELS fInfoLevelId,
1645 LPVOID lpFindFileData,
1646 FINDEX_SEARCH_OPS fSearchOp,
1647 LPVOID lpSearchFilter,
1648 DWORD dwAdditionalFlags)
1650 HANDLE handle;
1651 WIN32_FIND_DATAA dataA;
1652 LPVOID _lpFindFileData;
1653 LPSTR pathA;
1655 switch(fInfoLevelId)
1657 case FindExInfoStandard:
1659 _lpFindFileData = &dataA;
1661 break;
1662 default:
1663 FIXME("fInfoLevelId 0x%08x not implemented\n", fInfoLevelId );
1664 return INVALID_HANDLE_VALUE;
1667 pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
1668 handle = FindFirstFileExA(pathA, fInfoLevelId, _lpFindFileData, fSearchOp, lpSearchFilter, dwAdditionalFlags);
1669 HeapFree( GetProcessHeap(), 0, pathA );
1670 if (handle == INVALID_HANDLE_VALUE) return handle;
1672 switch(fInfoLevelId)
1674 case FindExInfoStandard:
1676 WIN32_FIND_DATAW *dataW = (WIN32_FIND_DATAW*) lpFindFileData;
1677 dataW->dwFileAttributes = dataA.dwFileAttributes;
1678 dataW->ftCreationTime = dataA.ftCreationTime;
1679 dataW->ftLastAccessTime = dataA.ftLastAccessTime;
1680 dataW->ftLastWriteTime = dataA.ftLastWriteTime;
1681 dataW->nFileSizeHigh = dataA.nFileSizeHigh;
1682 dataW->nFileSizeLow = dataA.nFileSizeLow;
1683 MultiByteToWideChar( CP_ACP, 0, dataA.cFileName, -1,
1684 dataW->cFileName, sizeof(dataW->cFileName)/sizeof(WCHAR) );
1685 MultiByteToWideChar( CP_ACP, 0, dataA.cAlternateFileName, -1,
1686 dataW->cAlternateFileName,
1687 sizeof(dataW->cAlternateFileName)/sizeof(WCHAR) );
1689 break;
1690 default:
1691 FIXME("fInfoLevelId 0x%08x not implemented\n", fInfoLevelId );
1692 return INVALID_HANDLE_VALUE;
1694 return handle;
1697 /*************************************************************************
1698 * FindFirstFileW (KERNEL32.124)
1700 HANDLE WINAPI FindFirstFileW( LPCWSTR lpFileName, WIN32_FIND_DATAW *lpFindData )
1702 return FindFirstFileExW(lpFileName, FindExInfoStandard, lpFindData,
1703 FindExSearchNameMatch, NULL, 0);
1706 /*************************************************************************
1707 * FindNextFileA (KERNEL32.126)
1709 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
1711 FIND_FIRST_INFO *info;
1713 if ((handle == INVALID_HANDLE_VALUE) ||
1714 !(info = (FIND_FIRST_INFO *)GlobalLock( handle )))
1716 SetLastError( ERROR_INVALID_HANDLE );
1717 return FALSE;
1719 GlobalUnlock( handle );
1720 if (!info->path || !info->dir)
1722 SetLastError( ERROR_NO_MORE_FILES );
1723 return FALSE;
1725 if (!DOSFS_FindNextEx( info, data ))
1727 DOSFS_CloseDir( info->dir ); info->dir = NULL;
1728 HeapFree( GetProcessHeap(), 0, info->path );
1729 info->path = info->long_mask = NULL;
1730 SetLastError( ERROR_NO_MORE_FILES );
1731 return FALSE;
1733 return TRUE;
1737 /*************************************************************************
1738 * FindNextFileW (KERNEL32.127)
1740 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
1742 WIN32_FIND_DATAA dataA;
1743 if (!FindNextFileA( handle, &dataA )) return FALSE;
1744 data->dwFileAttributes = dataA.dwFileAttributes;
1745 data->ftCreationTime = dataA.ftCreationTime;
1746 data->ftLastAccessTime = dataA.ftLastAccessTime;
1747 data->ftLastWriteTime = dataA.ftLastWriteTime;
1748 data->nFileSizeHigh = dataA.nFileSizeHigh;
1749 data->nFileSizeLow = dataA.nFileSizeLow;
1750 MultiByteToWideChar( CP_ACP, 0, dataA.cFileName, -1,
1751 data->cFileName, sizeof(data->cFileName)/sizeof(WCHAR) );
1752 MultiByteToWideChar( CP_ACP, 0, dataA.cAlternateFileName, -1,
1753 data->cAlternateFileName,
1754 sizeof(data->cAlternateFileName)/sizeof(WCHAR) );
1755 return TRUE;
1758 /*************************************************************************
1759 * FindClose (KERNEL32.119)
1761 BOOL WINAPI FindClose( HANDLE handle )
1763 FIND_FIRST_INFO *info;
1765 if ((handle == INVALID_HANDLE_VALUE) ||
1766 !(info = (FIND_FIRST_INFO *)GlobalLock( handle )))
1768 SetLastError( ERROR_INVALID_HANDLE );
1769 return FALSE;
1771 if (info->dir) DOSFS_CloseDir( info->dir );
1772 if (info->path) HeapFree( GetProcessHeap(), 0, info->path );
1773 GlobalUnlock( handle );
1774 GlobalFree( handle );
1775 return TRUE;
1778 /***********************************************************************
1779 * DOSFS_UnixTimeToFileTime
1781 * Convert a Unix time to FILETIME format.
1782 * The FILETIME structure is a 64-bit value representing the number of
1783 * 100-nanosecond intervals since January 1, 1601, 0:00.
1784 * 'remainder' is the nonnegative number of 100-ns intervals
1785 * corresponding to the time fraction smaller than 1 second that
1786 * couldn't be stored in the time_t value.
1788 void DOSFS_UnixTimeToFileTime( time_t unix_time, FILETIME *filetime,
1789 DWORD remainder )
1791 /* NOTES:
1793 CONSTANTS:
1794 The time difference between 1 January 1601, 00:00:00 and
1795 1 January 1970, 00:00:00 is 369 years, plus the leap years
1796 from 1604 to 1968, excluding 1700, 1800, 1900.
1797 This makes (1968 - 1600) / 4 - 3 = 89 leap days, and a total
1798 of 134774 days.
1800 Any day in that period had 24 * 60 * 60 = 86400 seconds.
1802 The time difference is 134774 * 86400 * 10000000, which can be written
1803 116444736000000000
1804 27111902 * 2^32 + 3577643008
1805 413 * 2^48 + 45534 * 2^32 + 54590 * 2^16 + 32768
1807 If you find that these constants are buggy, please change them in all
1808 instances in both conversion functions.
1810 VERSIONS:
1811 There are two versions, one of them uses long long variables and
1812 is presumably faster but not ISO C. The other one uses standard C
1813 data types and operations but relies on the assumption that negative
1814 numbers are stored as 2's complement (-1 is 0xffff....). If this
1815 assumption is violated, dates before 1970 will not convert correctly.
1816 This should however work on any reasonable architecture where WINE
1817 will run.
1819 DETAILS:
1821 Take care not to remove the casts. I have tested these functions
1822 (in both versions) for a lot of numbers. I would be interested in
1823 results on other compilers than GCC.
1825 The operations have been designed to account for the possibility
1826 of 64-bit time_t in future UNICES. Even the versions without
1827 internal long long numbers will work if time_t only is 64 bit.
1828 A 32-bit shift, which was necessary for that operation, turned out
1829 not to work correctly in GCC, besides giving the warning. So I
1830 used a double 16-bit shift instead. Numbers are in the ISO version
1831 represented by three limbs, the most significant with 32 bit, the
1832 other two with 16 bit each.
1834 As the modulo-operator % is not well-defined for negative numbers,
1835 negative divisors have been avoided in DOSFS_FileTimeToUnixTime.
1837 There might be quicker ways to do this in C. Certainly so in
1838 assembler.
1840 Claus Fischer, fischer@iue.tuwien.ac.at
1843 #if SIZEOF_LONG_LONG >= 8
1844 # define USE_LONG_LONG 1
1845 #else
1846 # define USE_LONG_LONG 0
1847 #endif
1849 #if USE_LONG_LONG /* gcc supports long long type */
1851 long long int t = unix_time;
1852 t *= 10000000;
1853 t += 116444736000000000LL;
1854 t += remainder;
1855 filetime->dwLowDateTime = (UINT)t;
1856 filetime->dwHighDateTime = (UINT)(t >> 32);
1858 #else /* ISO version */
1860 UINT a0; /* 16 bit, low bits */
1861 UINT a1; /* 16 bit, medium bits */
1862 UINT a2; /* 32 bit, high bits */
1864 /* Copy the unix time to a2/a1/a0 */
1865 a0 = unix_time & 0xffff;
1866 a1 = (unix_time >> 16) & 0xffff;
1867 /* This is obsolete if unix_time is only 32 bits, but it does not hurt.
1868 Do not replace this by >> 32, it gives a compiler warning and it does
1869 not work. */
1870 a2 = (unix_time >= 0 ? (unix_time >> 16) >> 16 :
1871 ~((~unix_time >> 16) >> 16));
1873 /* Multiply a by 10000000 (a = a2/a1/a0)
1874 Split the factor into 10000 * 1000 which are both less than 0xffff. */
1875 a0 *= 10000;
1876 a1 = a1 * 10000 + (a0 >> 16);
1877 a2 = a2 * 10000 + (a1 >> 16);
1878 a0 &= 0xffff;
1879 a1 &= 0xffff;
1881 a0 *= 1000;
1882 a1 = a1 * 1000 + (a0 >> 16);
1883 a2 = a2 * 1000 + (a1 >> 16);
1884 a0 &= 0xffff;
1885 a1 &= 0xffff;
1887 /* Add the time difference and the remainder */
1888 a0 += 32768 + (remainder & 0xffff);
1889 a1 += 54590 + (remainder >> 16 ) + (a0 >> 16);
1890 a2 += 27111902 + (a1 >> 16);
1891 a0 &= 0xffff;
1892 a1 &= 0xffff;
1894 /* Set filetime */
1895 filetime->dwLowDateTime = (a1 << 16) + a0;
1896 filetime->dwHighDateTime = a2;
1897 #endif
1901 /***********************************************************************
1902 * DOSFS_FileTimeToUnixTime
1904 * Convert a FILETIME format to Unix time.
1905 * If not NULL, 'remainder' contains the fractional part of the filetime,
1906 * in the range of [0..9999999] (even if time_t is negative).
1908 time_t DOSFS_FileTimeToUnixTime( const FILETIME *filetime, DWORD *remainder )
1910 /* Read the comment in the function DOSFS_UnixTimeToFileTime. */
1911 #if USE_LONG_LONG
1913 long long int t = filetime->dwHighDateTime;
1914 t <<= 32;
1915 t += (UINT)filetime->dwLowDateTime;
1916 t -= 116444736000000000LL;
1917 if (t < 0)
1919 if (remainder) *remainder = 9999999 - (-t - 1) % 10000000;
1920 return -1 - ((-t - 1) / 10000000);
1922 else
1924 if (remainder) *remainder = t % 10000000;
1925 return t / 10000000;
1928 #else /* ISO version */
1930 UINT a0; /* 16 bit, low bits */
1931 UINT a1; /* 16 bit, medium bits */
1932 UINT a2; /* 32 bit, high bits */
1933 UINT r; /* remainder of division */
1934 unsigned int carry; /* carry bit for subtraction */
1935 int negative; /* whether a represents a negative value */
1937 /* Copy the time values to a2/a1/a0 */
1938 a2 = (UINT)filetime->dwHighDateTime;
1939 a1 = ((UINT)filetime->dwLowDateTime ) >> 16;
1940 a0 = ((UINT)filetime->dwLowDateTime ) & 0xffff;
1942 /* Subtract the time difference */
1943 if (a0 >= 32768 ) a0 -= 32768 , carry = 0;
1944 else a0 += (1 << 16) - 32768 , carry = 1;
1946 if (a1 >= 54590 + carry) a1 -= 54590 + carry, carry = 0;
1947 else a1 += (1 << 16) - 54590 - carry, carry = 1;
1949 a2 -= 27111902 + carry;
1951 /* If a is negative, replace a by (-1-a) */
1952 negative = (a2 >= ((UINT)1) << 31);
1953 if (negative)
1955 /* Set a to -a - 1 (a is a2/a1/a0) */
1956 a0 = 0xffff - a0;
1957 a1 = 0xffff - a1;
1958 a2 = ~a2;
1961 /* Divide a by 10000000 (a = a2/a1/a0), put the rest into r.
1962 Split the divisor into 10000 * 1000 which are both less than 0xffff. */
1963 a1 += (a2 % 10000) << 16;
1964 a2 /= 10000;
1965 a0 += (a1 % 10000) << 16;
1966 a1 /= 10000;
1967 r = a0 % 10000;
1968 a0 /= 10000;
1970 a1 += (a2 % 1000) << 16;
1971 a2 /= 1000;
1972 a0 += (a1 % 1000) << 16;
1973 a1 /= 1000;
1974 r += (a0 % 1000) * 10000;
1975 a0 /= 1000;
1977 /* If a was negative, replace a by (-1-a) and r by (9999999 - r) */
1978 if (negative)
1980 /* Set a to -a - 1 (a is a2/a1/a0) */
1981 a0 = 0xffff - a0;
1982 a1 = 0xffff - a1;
1983 a2 = ~a2;
1985 r = 9999999 - r;
1988 if (remainder) *remainder = r;
1990 /* Do not replace this by << 32, it gives a compiler warning and it does
1991 not work. */
1992 return ((((time_t)a2) << 16) << 16) + (a1 << 16) + a0;
1993 #endif
1997 /***********************************************************************
1998 * MulDiv (KERNEL32.391)
1999 * RETURNS
2000 * Result of multiplication and division
2001 * -1: Overflow occurred or Divisor was 0
2003 INT WINAPI MulDiv(
2004 INT nMultiplicand,
2005 INT nMultiplier,
2006 INT nDivisor)
2008 #if SIZEOF_LONG_LONG >= 8
2009 long long ret;
2011 if (!nDivisor) return -1;
2013 /* We want to deal with a positive divisor to simplify the logic. */
2014 if (nDivisor < 0)
2016 nMultiplicand = - nMultiplicand;
2017 nDivisor = -nDivisor;
2020 /* If the result is positive, we "add" to round. else, we subtract to round. */
2021 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
2022 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
2023 ret = (((long long)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
2024 else
2025 ret = (((long long)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
2027 if ((ret > 2147483647) || (ret < -2147483647)) return -1;
2028 return ret;
2029 #else
2030 if (!nDivisor) return -1;
2032 /* We want to deal with a positive divisor to simplify the logic. */
2033 if (nDivisor < 0)
2035 nMultiplicand = - nMultiplicand;
2036 nDivisor = -nDivisor;
2039 /* If the result is positive, we "add" to round. else, we subtract to round. */
2040 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
2041 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
2042 return ((nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
2044 return ((nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
2046 #endif
2050 /***********************************************************************
2051 * DosDateTimeToFileTime (KERNEL32.76)
2053 BOOL WINAPI DosDateTimeToFileTime( WORD fatdate, WORD fattime, LPFILETIME ft)
2055 struct tm newtm;
2057 newtm.tm_sec = (fattime & 0x1f) * 2;
2058 newtm.tm_min = (fattime >> 5) & 0x3f;
2059 newtm.tm_hour = (fattime >> 11);
2060 newtm.tm_mday = (fatdate & 0x1f);
2061 newtm.tm_mon = ((fatdate >> 5) & 0x0f) - 1;
2062 newtm.tm_year = (fatdate >> 9) + 80;
2063 RtlSecondsSince1970ToTime( mktime( &newtm ), ft );
2064 return TRUE;
2068 /***********************************************************************
2069 * FileTimeToDosDateTime (KERNEL32.111)
2071 BOOL WINAPI FileTimeToDosDateTime( const FILETIME *ft, LPWORD fatdate,
2072 LPWORD fattime )
2074 time_t unixtime = DOSFS_FileTimeToUnixTime( ft, NULL );
2075 struct tm *tm = localtime( &unixtime );
2076 if (fattime)
2077 *fattime = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
2078 if (fatdate)
2079 *fatdate = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5)
2080 + tm->tm_mday;
2081 return TRUE;
2085 /***********************************************************************
2086 * LocalFileTimeToFileTime (KERNEL32.373)
2088 BOOL WINAPI LocalFileTimeToFileTime( const FILETIME *localft,
2089 LPFILETIME utcft )
2091 struct tm *xtm;
2092 DWORD remainder;
2094 /* convert from local to UTC. Perhaps not correct. FIXME */
2095 time_t unixtime = DOSFS_FileTimeToUnixTime( localft, &remainder );
2096 xtm = gmtime( &unixtime );
2097 DOSFS_UnixTimeToFileTime( mktime(xtm), utcft, remainder );
2098 return TRUE;
2102 /***********************************************************************
2103 * FileTimeToLocalFileTime (KERNEL32.112)
2105 BOOL WINAPI FileTimeToLocalFileTime( const FILETIME *utcft,
2106 LPFILETIME localft )
2108 DWORD remainder;
2109 /* convert from UTC to local. Perhaps not correct. FIXME */
2110 time_t unixtime = DOSFS_FileTimeToUnixTime( utcft, &remainder );
2111 #ifdef HAVE_TIMEGM
2112 struct tm *xtm = localtime( &unixtime );
2113 time_t localtime;
2115 localtime = timegm(xtm);
2116 DOSFS_UnixTimeToFileTime( localtime, localft, remainder );
2118 #else
2119 struct tm *xtm,*gtm;
2120 time_t time1,time2;
2122 xtm = localtime( &unixtime );
2123 gtm = gmtime( &unixtime );
2124 time1 = mktime(xtm);
2125 time2 = mktime(gtm);
2126 DOSFS_UnixTimeToFileTime( 2*time1-time2, localft, remainder );
2127 #endif
2128 return TRUE;
2132 /***********************************************************************
2133 * FileTimeToSystemTime (KERNEL32.113)
2135 BOOL WINAPI FileTimeToSystemTime( const FILETIME *ft, LPSYSTEMTIME syst )
2137 struct tm *xtm;
2138 DWORD remainder;
2139 time_t xtime = DOSFS_FileTimeToUnixTime( ft, &remainder );
2140 xtm = gmtime(&xtime);
2141 syst->wYear = xtm->tm_year+1900;
2142 syst->wMonth = xtm->tm_mon + 1;
2143 syst->wDayOfWeek = xtm->tm_wday;
2144 syst->wDay = xtm->tm_mday;
2145 syst->wHour = xtm->tm_hour;
2146 syst->wMinute = xtm->tm_min;
2147 syst->wSecond = xtm->tm_sec;
2148 syst->wMilliseconds = remainder / 10000;
2149 return TRUE;
2152 /***********************************************************************
2153 * QueryDosDeviceA (KERNEL32.413)
2155 * returns array of strings terminated by \0, terminated by \0
2157 DWORD WINAPI QueryDosDeviceA(LPCSTR devname,LPSTR target,DWORD bufsize)
2159 LPSTR s;
2160 char buffer[200];
2162 TRACE("(%s,...)\n", devname ? devname : "<null>");
2163 if (!devname) {
2164 /* return known MSDOS devices */
2165 static const char devices[24] = "CON\0COM1\0COM2\0LPT1\0NUL\0\0";
2166 memcpy( target, devices, min(bufsize,sizeof(devices)) );
2167 return min(bufsize,sizeof(devices));
2169 strcpy(buffer,"\\DEV\\");
2170 strcat(buffer,devname);
2171 if ((s=strchr(buffer,':'))) *s='\0';
2172 lstrcpynA(target,buffer,bufsize);
2173 return strlen(buffer)+1;
2177 /***********************************************************************
2178 * QueryDosDeviceW (KERNEL32.414)
2180 * returns array of strings terminated by \0, terminated by \0
2182 DWORD WINAPI QueryDosDeviceW(LPCWSTR devname,LPWSTR target,DWORD bufsize)
2184 LPSTR devnameA = devname?HEAP_strdupWtoA(GetProcessHeap(),0,devname):NULL;
2185 LPSTR targetA = (LPSTR)HeapAlloc(GetProcessHeap(),0,bufsize);
2186 DWORD ret = QueryDosDeviceA(devnameA,targetA,bufsize);
2188 ret = MultiByteToWideChar( CP_ACP, 0, targetA, ret, target, bufsize );
2189 if (devnameA) HeapFree(GetProcessHeap(),0,devnameA);
2190 if (targetA) HeapFree(GetProcessHeap(),0,targetA);
2191 return ret;
2195 /***********************************************************************
2196 * SystemTimeToFileTime (KERNEL32.526)
2198 BOOL WINAPI SystemTimeToFileTime( const SYSTEMTIME *syst, LPFILETIME ft )
2200 #ifdef HAVE_TIMEGM
2201 struct tm xtm;
2202 time_t utctime;
2203 #else
2204 struct tm xtm,*local_tm,*utc_tm;
2205 time_t localtim,utctime;
2206 #endif
2208 xtm.tm_year = syst->wYear-1900;
2209 xtm.tm_mon = syst->wMonth - 1;
2210 xtm.tm_wday = syst->wDayOfWeek;
2211 xtm.tm_mday = syst->wDay;
2212 xtm.tm_hour = syst->wHour;
2213 xtm.tm_min = syst->wMinute;
2214 xtm.tm_sec = syst->wSecond; /* this is UTC */
2215 xtm.tm_isdst = -1;
2216 #ifdef HAVE_TIMEGM
2217 utctime = timegm(&xtm);
2218 DOSFS_UnixTimeToFileTime( utctime, ft,
2219 syst->wMilliseconds * 10000 );
2220 #else
2221 localtim = mktime(&xtm); /* now we've got local time */
2222 local_tm = localtime(&localtim);
2223 utc_tm = gmtime(&localtim);
2224 utctime = mktime(utc_tm);
2225 DOSFS_UnixTimeToFileTime( 2*localtim -utctime, ft,
2226 syst->wMilliseconds * 10000 );
2227 #endif
2228 return TRUE;
2231 /***********************************************************************
2232 * DefineDosDeviceA (KERNEL32.182)
2234 BOOL WINAPI DefineDosDeviceA(DWORD flags,LPCSTR devname,LPCSTR targetpath) {
2235 FIXME("(0x%08lx,%s,%s),stub!\n",flags,devname,targetpath);
2236 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2237 return FALSE;
2241 --- 16 bit functions ---
2244 /*************************************************************************
2245 * FindFirstFile16 (KERNEL.413)
2247 HANDLE16 WINAPI FindFirstFile16( LPCSTR path, WIN32_FIND_DATAA *data )
2249 DOS_FULL_NAME full_name;
2250 HGLOBAL16 handle;
2251 FIND_FIRST_INFO *info;
2253 data->dwReserved0 = data->dwReserved1 = 0x0;
2254 if (!path) return 0;
2255 if (!DOSFS_GetFullName( path, FALSE, &full_name ))
2256 return INVALID_HANDLE_VALUE16;
2257 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE, sizeof(FIND_FIRST_INFO) )))
2258 return INVALID_HANDLE_VALUE16;
2259 info = (FIND_FIRST_INFO *)GlobalLock16( handle );
2260 info->path = HEAP_strdupA( GetProcessHeap(), 0, full_name.long_name );
2261 info->long_mask = strrchr( info->path, '/' );
2262 if (info->long_mask )
2263 *(info->long_mask++) = '\0';
2264 info->short_mask = NULL;
2265 info->attr = 0xff;
2266 if (path[0] && (path[1] == ':')) info->drive = FILE_toupper(*path) - 'A';
2267 else info->drive = DRIVE_GetCurrentDrive();
2268 info->cur_pos = 0;
2270 info->dir = DOSFS_OpenDir( info->path );
2272 GlobalUnlock16( handle );
2273 if (!FindNextFile16( handle, data ))
2275 FindClose16( handle );
2276 SetLastError( ERROR_NO_MORE_FILES );
2277 return INVALID_HANDLE_VALUE16;
2279 return handle;
2282 /*************************************************************************
2283 * FindNextFile16 (KERNEL.414)
2285 BOOL16 WINAPI FindNextFile16( HANDLE16 handle, WIN32_FIND_DATAA *data )
2287 FIND_FIRST_INFO *info;
2289 if ((handle == INVALID_HANDLE_VALUE16) ||
2290 !(info = (FIND_FIRST_INFO *)GlobalLock16( handle )))
2292 SetLastError( ERROR_INVALID_HANDLE );
2293 return FALSE;
2295 GlobalUnlock16( handle );
2296 if (!info->path || !info->dir)
2298 SetLastError( ERROR_NO_MORE_FILES );
2299 return FALSE;
2301 if (!DOSFS_FindNextEx( info, data ))
2303 DOSFS_CloseDir( info->dir ); info->dir = NULL;
2304 HeapFree( GetProcessHeap(), 0, info->path );
2305 info->path = info->long_mask = NULL;
2306 SetLastError( ERROR_NO_MORE_FILES );
2307 return FALSE;
2309 return TRUE;
2312 /*************************************************************************
2313 * FindClose16 (KERNEL.415)
2315 BOOL16 WINAPI FindClose16( HANDLE16 handle )
2317 FIND_FIRST_INFO *info;
2319 if ((handle == INVALID_HANDLE_VALUE16) ||
2320 !(info = (FIND_FIRST_INFO *)GlobalLock16( handle )))
2322 SetLastError( ERROR_INVALID_HANDLE );
2323 return FALSE;
2325 if (info->dir) DOSFS_CloseDir( info->dir );
2326 if (info->path) HeapFree( GetProcessHeap(), 0, info->path );
2327 GlobalUnlock16( handle );
2328 GlobalFree16( handle );
2329 return TRUE;