Correct error message in case DOSFS_OpenDevice() fails on a COMx
[wine.git] / files / dos_fs.c
blob671afea7b400c175cdcfc3c2bc7421fce9edef54
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 for (p = buffer + 8; (p > buffer) && (p[-1] == ' '); p--);
265 *p++ = '.';
266 memcpy( p, name + 8, 3 );
267 for (p += 3; p[-1] == ' '; p--);
268 if (p[-1] == '.') p--;
269 *p = '\0';
273 /***********************************************************************
274 * DOSFS_MatchShort
276 * Check a DOS file name against a mask (both in FCB format).
278 static int DOSFS_MatchShort( const char *mask, const char *name )
280 int i;
281 for (i = 11; i > 0; i--, mask++, name++)
282 if ((*mask != '?') && (*mask != *name)) return 0;
283 return 1;
287 /***********************************************************************
288 * DOSFS_MatchLong
290 * Check a long file name against a mask.
292 * Tests (done in W95 DOS shell - case insensitive):
293 * *.txt test1.test.txt *
294 * *st1* test1.txt *
295 * *.t??????.t* test1.ta.tornado.txt *
296 * *tornado* test1.ta.tornado.txt *
297 * t*t test1.ta.tornado.txt *
298 * ?est* test1.txt *
299 * ?est??? test1.txt -
300 * *test1.txt* test1.txt *
301 * h?l?o*t.dat hellothisisatest.dat *
303 static int DOSFS_MatchLong( const char *mask, const char *name,
304 int case_sensitive )
306 const char *lastjoker = NULL;
307 const char *next_to_retry = NULL;
309 if (!strcmp( mask, "*.*" )) return 1;
310 while (*name && *mask)
312 if (*mask == '*')
314 mask++;
315 while (*mask == '*') mask++; /* Skip consecutive '*' */
316 lastjoker = mask;
317 if (!*mask) return 1; /* end of mask is all '*', so match */
319 /* skip to the next match after the joker(s) */
320 if (case_sensitive) while (*name && (*name != *mask)) name++;
321 else while (*name && (FILE_toupper(*name) != FILE_toupper(*mask))) name++;
323 if (!*name) break;
324 next_to_retry = name;
326 else if (*mask != '?')
328 int mismatch = 0;
329 if (case_sensitive)
331 if (*mask != *name) mismatch = 1;
333 else
335 if (FILE_toupper(*mask) != FILE_toupper(*name)) mismatch = 1;
337 if (!mismatch)
339 mask++;
340 name++;
341 if (*mask == '\0')
343 if (*name == '\0')
344 return 1;
345 if (lastjoker)
346 mask = lastjoker;
349 else /* mismatch ! */
351 if (lastjoker) /* we had an '*', so we can try unlimitedly */
353 mask = lastjoker;
355 /* this scan sequence was a mismatch, so restart
356 * 1 char after the first char we checked last time */
357 next_to_retry++;
358 name = next_to_retry;
360 else
361 return 0; /* bad luck */
364 else /* '?' */
366 mask++;
367 name++;
370 while ((*mask == '.') || (*mask == '*'))
371 mask++; /* Ignore trailing '.' or '*' in mask */
372 return (!*name && !*mask);
376 /***********************************************************************
377 * DOSFS_OpenDir
379 static DOS_DIR *DOSFS_OpenDir( LPCSTR path )
381 DOS_DIR *dir = HeapAlloc( GetProcessHeap(), 0, sizeof(*dir) );
382 if (!dir)
384 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
385 return NULL;
388 /* Treat empty path as root directory. This simplifies path split into
389 directory and mask in several other places */
390 if (!*path) path = "/";
392 #ifdef VFAT_IOCTL_READDIR_BOTH
394 /* Check if the VFAT ioctl is supported on this directory */
396 if ((dir->fd = open( path, O_RDONLY )) != -1)
398 if (ioctl( dir->fd, VFAT_IOCTL_READDIR_BOTH, (long)dir->dirent ) == -1)
400 close( dir->fd );
401 dir->fd = -1;
403 else
405 /* Set the file pointer back at the start of the directory */
406 lseek( dir->fd, 0, SEEK_SET );
407 dir->dir = NULL;
408 return dir;
411 #endif /* VFAT_IOCTL_READDIR_BOTH */
413 /* Now use the standard opendir/readdir interface */
415 if (!(dir->dir = opendir( path )))
417 HeapFree( GetProcessHeap(), 0, dir );
418 return NULL;
420 return dir;
424 /***********************************************************************
425 * DOSFS_CloseDir
427 static void DOSFS_CloseDir( DOS_DIR *dir )
429 #ifdef VFAT_IOCTL_READDIR_BOTH
430 if (dir->fd != -1) close( dir->fd );
431 #endif /* VFAT_IOCTL_READDIR_BOTH */
432 if (dir->dir) closedir( dir->dir );
433 HeapFree( GetProcessHeap(), 0, dir );
437 /***********************************************************************
438 * DOSFS_ReadDir
440 static BOOL DOSFS_ReadDir( DOS_DIR *dir, LPCSTR *long_name,
441 LPCSTR *short_name )
443 struct dirent *dirent;
445 #ifdef VFAT_IOCTL_READDIR_BOTH
446 if (dir->fd != -1)
448 if (ioctl( dir->fd, VFAT_IOCTL_READDIR_BOTH, (long)dir->dirent ) != -1) {
449 if (!dir->dirent[0].d_reclen) return FALSE;
450 if (!DOSFS_ToDosFCBFormat( dir->dirent[0].d_name, dir->short_name ))
451 dir->short_name[0] = '\0';
452 *short_name = dir->short_name;
453 if (dir->dirent[1].d_name[0]) *long_name = dir->dirent[1].d_name;
454 else *long_name = dir->dirent[0].d_name;
455 return TRUE;
458 #endif /* VFAT_IOCTL_READDIR_BOTH */
460 if (!(dirent = readdir( dir->dir ))) return FALSE;
461 *long_name = dirent->d_name;
462 *short_name = NULL;
463 return TRUE;
467 /***********************************************************************
468 * DOSFS_Hash
470 * Transform a Unix file name into a hashed DOS name. If the name is a valid
471 * DOS name, it is converted to upper-case; otherwise it is replaced by a
472 * hashed version that fits in 8.3 format.
473 * File name can be terminated by '\0', '\\' or '/'.
474 * 'buffer' must be at least 13 characters long.
476 static void DOSFS_Hash( LPCSTR name, LPSTR buffer, BOOL dir_format,
477 BOOL ignore_case )
479 static const char invalid_chars[] = INVALID_DOS_CHARS "~.";
480 static const char hash_chars[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
482 const char *p, *ext;
483 char *dst;
484 unsigned short hash;
485 int i;
487 if (dir_format) strcpy( buffer, " " );
489 if (DOSFS_ValidDOSName( name, ignore_case ))
491 /* Check for '.' and '..' */
492 if (*name == '.')
494 buffer[0] = '.';
495 if (!dir_format) buffer[1] = buffer[2] = '\0';
496 if (name[1] == '.') buffer[1] = '.';
497 return;
500 /* Simply copy the name, converting to uppercase */
502 for (dst = buffer; !IS_END_OF_NAME(*name) && (*name != '.'); name++)
503 *dst++ = FILE_toupper(*name);
504 if (*name == '.')
506 if (dir_format) dst = buffer + 8;
507 else *dst++ = '.';
508 for (name++; !IS_END_OF_NAME(*name); name++)
509 *dst++ = FILE_toupper(*name);
511 if (!dir_format) *dst = '\0';
512 return;
515 /* Compute the hash code of the file name */
516 /* If you know something about hash functions, feel free to */
517 /* insert a better algorithm here... */
518 if (ignore_case)
520 for (p = name, hash = 0xbeef; !IS_END_OF_NAME(p[1]); p++)
521 hash = (hash<<3) ^ (hash>>5) ^ FILE_tolower(*p) ^ (FILE_tolower(p[1]) << 8);
522 hash = (hash<<3) ^ (hash>>5) ^ FILE_tolower(*p); /* Last character*/
524 else
526 for (p = name, hash = 0xbeef; !IS_END_OF_NAME(p[1]); p++)
527 hash = (hash << 3) ^ (hash >> 5) ^ *p ^ (p[1] << 8);
528 hash = (hash << 3) ^ (hash >> 5) ^ *p; /* Last character */
531 /* Find last dot for start of the extension */
532 for (p = name+1, ext = NULL; !IS_END_OF_NAME(*p); p++)
533 if (*p == '.') ext = p;
534 if (ext && IS_END_OF_NAME(ext[1]))
535 ext = NULL; /* Empty extension ignored */
537 /* Copy first 4 chars, replacing invalid chars with '_' */
538 for (i = 4, p = name, dst = buffer; i > 0; i--, p++)
540 if (IS_END_OF_NAME(*p) || (p == ext)) break;
541 *dst++ = strchr( invalid_chars, *p ) ? '_' : FILE_toupper(*p);
543 /* Pad to 5 chars with '~' */
544 while (i-- >= 0) *dst++ = '~';
546 /* Insert hash code converted to 3 ASCII chars */
547 *dst++ = hash_chars[(hash >> 10) & 0x1f];
548 *dst++ = hash_chars[(hash >> 5) & 0x1f];
549 *dst++ = hash_chars[hash & 0x1f];
551 /* Copy the first 3 chars of the extension (if any) */
552 if (ext)
554 if (!dir_format) *dst++ = '.';
555 for (i = 3, ext++; (i > 0) && !IS_END_OF_NAME(*ext); i--, ext++)
556 *dst++ = strchr( invalid_chars, *ext ) ? '_' : FILE_toupper(*ext);
558 if (!dir_format) *dst = '\0';
562 /***********************************************************************
563 * DOSFS_FindUnixName
565 * Find the Unix file name in a given directory that corresponds to
566 * a file name (either in Unix or DOS format).
567 * File name can be terminated by '\0', '\\' or '/'.
568 * Return TRUE if OK, FALSE if no file name matches.
570 * 'long_buf' must be at least 'long_len' characters long. If the long name
571 * turns out to be larger than that, the function returns FALSE.
572 * 'short_buf' must be at least 13 characters long.
574 BOOL DOSFS_FindUnixName( LPCSTR path, LPCSTR name, LPSTR long_buf,
575 INT long_len, LPSTR short_buf, BOOL ignore_case)
577 DOS_DIR *dir;
578 LPCSTR long_name, short_name;
579 char dos_name[12], tmp_buf[13];
580 BOOL ret;
582 const char *p = strchr( name, '/' );
583 int len = p ? (int)(p - name) : strlen(name);
584 if ((p = strchr( name, '\\' ))) len = min( (int)(p - name), len );
585 /* Ignore trailing dots and spaces */
586 while (len > 1 && (name[len-1] == '.' || name[len-1] == ' ')) len--;
587 if (long_len < len + 1) return FALSE;
589 TRACE("%s,%s\n", path, name );
591 if (!DOSFS_ToDosFCBFormat( name, dos_name )) dos_name[0] = '\0';
593 if (!(dir = DOSFS_OpenDir( path )))
595 WARN("(%s,%s): can't open dir: %s\n",
596 path, name, strerror(errno) );
597 return FALSE;
600 while ((ret = DOSFS_ReadDir( dir, &long_name, &short_name )))
602 /* Check against Unix name */
603 if (len == strlen(long_name))
605 if (!ignore_case)
607 if (!strncmp( long_name, name, len )) break;
609 else
611 if (!FILE_strncasecmp( long_name, name, len )) break;
614 if (dos_name[0])
616 /* Check against hashed DOS name */
617 if (!short_name)
619 DOSFS_Hash( long_name, tmp_buf, TRUE, ignore_case );
620 short_name = tmp_buf;
622 if (!strcmp( dos_name, short_name )) break;
625 if (ret)
627 if (long_buf) strcpy( long_buf, long_name );
628 if (short_buf)
630 if (short_name)
631 DOSFS_ToDosDTAFormat( short_name, short_buf );
632 else
633 DOSFS_Hash( long_name, short_buf, FALSE, ignore_case );
635 TRACE("(%s,%s) -> %s (%s)\n",
636 path, name, long_name, short_buf ? short_buf : "***");
638 else
639 WARN("'%s' not found in '%s'\n", name, path);
640 DOSFS_CloseDir( dir );
641 return ret;
645 /***********************************************************************
646 * DOSFS_GetDevice
648 * Check if a DOS file name represents a DOS device and return the device.
650 const DOS_DEVICE *DOSFS_GetDevice( const char *name )
652 int i;
653 const char *p;
655 if (!name) return NULL; /* if FILE_DupUnixHandle was used */
656 if (name[0] && (name[1] == ':')) name += 2;
657 if ((p = strrchr( name, '/' ))) name = p + 1;
658 if ((p = strrchr( name, '\\' ))) name = p + 1;
659 for (i = 0; i < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0]); i++)
661 const char *dev = DOSFS_Devices[i].name;
662 if (!FILE_strncasecmp( dev, name, strlen(dev) ))
664 p = name + strlen( dev );
665 if (!*p || (*p == '.') || (*p == ':')) return &DOSFS_Devices[i];
668 return NULL;
672 /***********************************************************************
673 * DOSFS_GetDeviceByHandle
675 const DOS_DEVICE *DOSFS_GetDeviceByHandle( HFILE hFile )
677 const DOS_DEVICE *ret = NULL;
678 SERVER_START_REQ
680 struct get_file_info_request *req = server_alloc_req( sizeof(*req), 0 );
682 req->handle = hFile;
683 if (!server_call( REQ_GET_FILE_INFO ) && (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];
703 TRACE("%s %lx\n", name, access);
705 PROFILE_GetWineIniString("serialports",name,"",devname,sizeof devname);
706 if(!devname[0])
707 return 0;
709 TRACE("opening %s as %s\n", devname, name);
711 SERVER_START_REQ
713 size_t len = strlen(devname);
714 struct create_serial_request *req = server_alloc_req( sizeof(*req), len );
716 req->access = access;
717 req->inherit = 0; /*FIXME*/
718 req->sharing = FILE_SHARE_READ|FILE_SHARE_WRITE;
719 memcpy( server_data_ptr(req), devname, len );
720 SetLastError(0);
721 server_call( REQ_CREATE_SERIAL );
722 ret = req->handle;
724 SERVER_END_REQ;
726 if(!ret)
727 ERR("Couldn't open %s ! (check permissions)\n",devname);
728 else
729 TRACE("return %08X\n", ret );
730 return ret;
733 /***********************************************************************
734 * DOSFS_OpenDevice
736 * Open a DOS device. This might not map 1:1 into the UNIX device concept.
737 * Returns 0 on failure.
739 HANDLE DOSFS_OpenDevice( const char *name, DWORD access )
741 int i;
742 const char *p;
743 HANDLE handle;
745 if (name[0] && (name[1] == ':')) name += 2;
746 if ((p = strrchr( name, '/' ))) name = p + 1;
747 if ((p = strrchr( name, '\\' ))) name = p + 1;
748 for (i = 0; i < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0]); i++)
750 const char *dev = DOSFS_Devices[i].name;
751 if (!FILE_strncasecmp( dev, name, strlen(dev) ))
753 p = name + strlen( dev );
754 if (!*p || (*p == '.') || (*p == ':')) {
755 /* got it */
756 if (!strcmp(DOSFS_Devices[i].name,"NUL"))
757 return FILE_CreateFile( "/dev/null", access,
758 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
759 OPEN_EXISTING, 0, 0, TRUE );
760 if (!strcmp(DOSFS_Devices[i].name,"CON")) {
761 HANDLE to_dup;
762 switch (access & (GENERIC_READ|GENERIC_WRITE)) {
763 case GENERIC_READ:
764 to_dup = GetStdHandle( STD_INPUT_HANDLE );
765 break;
766 case GENERIC_WRITE:
767 to_dup = GetStdHandle( STD_OUTPUT_HANDLE );
768 break;
769 default:
770 FIXME("can't open CON read/write\n");
771 return 0;
773 if (!DuplicateHandle( GetCurrentProcess(), to_dup, GetCurrentProcess(),
774 &handle, 0, FALSE, DUPLICATE_SAME_ACCESS ))
775 handle = 0;
776 return handle;
778 if (!strcmp(DOSFS_Devices[i].name,"SCSIMGR$") ||
779 !strcmp(DOSFS_Devices[i].name,"HPSCAN"))
781 return FILE_CreateDevice( i, access, NULL );
784 if( (handle=DOSFS_CreateCommPort(DOSFS_Devices[i].name,access)) )
785 return handle;
786 FIXME("device open %s not supported (yet)\n",DOSFS_Devices[i].name);
787 return 0;
791 return 0;
795 /***********************************************************************
796 * DOSFS_GetPathDrive
798 * Get the drive specified by a given path name (DOS or Unix format).
800 static int DOSFS_GetPathDrive( const char **name )
802 int drive;
803 const char *p = *name;
805 if (*p && (p[1] == ':'))
807 drive = FILE_toupper(*p) - 'A';
808 *name += 2;
810 else if (*p == '/') /* Absolute Unix path? */
812 if ((drive = DRIVE_FindDriveRoot( name )) == -1)
814 MESSAGE("Warning: %s not accessible from a DOS drive\n", *name );
815 /* Assume it really was a DOS name */
816 drive = DRIVE_GetCurrentDrive();
819 else drive = DRIVE_GetCurrentDrive();
821 if (!DRIVE_IsValid(drive))
823 SetLastError( ERROR_INVALID_DRIVE );
824 return -1;
826 return drive;
830 /***********************************************************************
831 * DOSFS_GetFullName
833 * Convert a file name (DOS or mixed DOS/Unix format) to a valid
834 * Unix name / short DOS name pair.
835 * Return FALSE if one of the path components does not exist. The last path
836 * component is only checked if 'check_last' is non-zero.
837 * The buffers pointed to by 'long_buf' and 'short_buf' must be
838 * at least MAX_PATHNAME_LEN long.
840 BOOL DOSFS_GetFullName( LPCSTR name, BOOL check_last, DOS_FULL_NAME *full )
842 BOOL found;
843 UINT flags;
844 char *p_l, *p_s, *root;
846 TRACE("%s (last=%d)\n", name, check_last );
848 if ((!*name) || (*name=='\n'))
849 { /* error code for Win98 */
850 SetLastError(ERROR_BAD_PATHNAME);
851 return FALSE;
854 if ((full->drive = DOSFS_GetPathDrive( &name )) == -1) return FALSE;
855 flags = DRIVE_GetFlags( full->drive );
857 lstrcpynA( full->long_name, DRIVE_GetRoot( full->drive ),
858 sizeof(full->long_name) );
859 if (full->long_name[1]) root = full->long_name + strlen(full->long_name);
860 else root = full->long_name; /* root directory */
862 strcpy( full->short_name, "A:\\" );
863 full->short_name[0] += full->drive;
865 if ((*name == '\\') || (*name == '/')) /* Absolute path */
867 while ((*name == '\\') || (*name == '/')) name++;
869 else /* Relative path */
871 lstrcpynA( root + 1, DRIVE_GetUnixCwd( full->drive ),
872 sizeof(full->long_name) - (root - full->long_name) - 1 );
873 if (root[1]) *root = '/';
874 lstrcpynA( full->short_name + 3, DRIVE_GetDosCwd( full->drive ),
875 sizeof(full->short_name) - 3 );
878 p_l = full->long_name[1] ? full->long_name + strlen(full->long_name)
879 : full->long_name;
880 p_s = full->short_name[3] ? full->short_name + strlen(full->short_name)
881 : full->short_name + 2;
882 found = TRUE;
884 while (*name && found)
886 /* Check for '.' and '..' */
888 if (*name == '.')
890 if (IS_END_OF_NAME(name[1]))
892 name++;
893 while ((*name == '\\') || (*name == '/')) name++;
894 continue;
896 else if ((name[1] == '.') && IS_END_OF_NAME(name[2]))
898 name += 2;
899 while ((*name == '\\') || (*name == '/')) name++;
900 while ((p_l > root) && (*p_l != '/')) p_l--;
901 while ((p_s > full->short_name + 2) && (*p_s != '\\')) p_s--;
902 *p_l = *p_s = '\0'; /* Remove trailing separator */
903 continue;
907 /* Make sure buffers are large enough */
909 if ((p_s >= full->short_name + sizeof(full->short_name) - 14) ||
910 (p_l >= full->long_name + sizeof(full->long_name) - 1))
912 SetLastError( ERROR_PATH_NOT_FOUND );
913 return FALSE;
916 /* Get the long and short name matching the file name */
918 if ((found = DOSFS_FindUnixName( full->long_name, name, p_l + 1,
919 sizeof(full->long_name) - (p_l - full->long_name) - 1,
920 p_s + 1, !(flags & DRIVE_CASE_SENSITIVE) )))
922 *p_l++ = '/';
923 p_l += strlen(p_l);
924 *p_s++ = '\\';
925 p_s += strlen(p_s);
926 while (!IS_END_OF_NAME(*name)) name++;
928 else if (!check_last)
930 *p_l++ = '/';
931 *p_s++ = '\\';
932 while (!IS_END_OF_NAME(*name) &&
933 (p_s < full->short_name + sizeof(full->short_name) - 1) &&
934 (p_l < full->long_name + sizeof(full->long_name) - 1))
936 *p_s++ = FILE_tolower(*name);
937 /* If the drive is case-sensitive we want to create new */
938 /* files in lower-case otherwise we can't reopen them */
939 /* under the same short name. */
940 if (flags & DRIVE_CASE_SENSITIVE) *p_l++ = FILE_tolower(*name);
941 else *p_l++ = *name;
942 name++;
944 /* Ignore trailing dots and spaces */
945 while(p_l[-1] == '.' || p_l[-1] == ' ') {
946 --p_l;
947 --p_s;
949 *p_l = *p_s = '\0';
951 while ((*name == '\\') || (*name == '/')) name++;
954 if (!found)
956 if (check_last)
958 SetLastError( ERROR_FILE_NOT_FOUND );
959 return FALSE;
961 if (*name) /* Not last */
963 SetLastError( ERROR_PATH_NOT_FOUND );
964 return FALSE;
967 if (!full->long_name[0]) strcpy( full->long_name, "/" );
968 if (!full->short_name[2]) strcpy( full->short_name + 2, "\\" );
969 TRACE("returning %s = %s\n", full->long_name, full->short_name );
970 return TRUE;
974 /***********************************************************************
975 * GetShortPathNameA (KERNEL32.271)
977 * NOTES
978 * observed:
979 * longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
980 * *longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
982 * more observations ( with NT 3.51 (WinDD) ):
983 * longpath <= 8.3 -> just copy longpath to shortpath
984 * longpath > 8.3 ->
985 * a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
986 * b) file does exist -> set the short filename.
987 * - trailing slashes are reproduced in the short name, even if the
988 * file is not a directory
989 * - the absolute/relative path of the short name is reproduced like found
990 * in the long name
991 * - longpath and shortpath may have the same adress
992 * Peter Ganten, 1999
994 DWORD WINAPI GetShortPathNameA( LPCSTR longpath, LPSTR shortpath,
995 DWORD shortlen )
997 DOS_FULL_NAME full_name;
998 LPSTR tmpshortpath;
999 DWORD sp = 0, lp = 0;
1000 int tmplen, drive;
1001 UINT flags;
1003 TRACE("%s\n", debugstr_a(longpath));
1005 if (!longpath) {
1006 SetLastError(ERROR_INVALID_PARAMETER);
1007 return 0;
1009 if (!longpath[0]) {
1010 SetLastError(ERROR_BAD_PATHNAME);
1011 return 0;
1014 if ( ( tmpshortpath = HeapAlloc ( GetProcessHeap(), 0, MAX_PATHNAME_LEN ) ) == NULL ) {
1015 SetLastError ( ERROR_NOT_ENOUGH_MEMORY );
1016 return 0;
1019 /* check for drive letter */
1020 if ( longpath[1] == ':' ) {
1021 tmpshortpath[0] = longpath[0];
1022 tmpshortpath[1] = ':';
1023 sp = 2;
1026 if ( ( drive = DOSFS_GetPathDrive ( &longpath )) == -1 ) return 0;
1027 flags = DRIVE_GetFlags ( drive );
1029 while ( longpath[lp] ) {
1031 /* check for path delimiters and reproduce them */
1032 if ( longpath[lp] == '\\' || longpath[lp] == '/' ) {
1033 if (!sp || tmpshortpath[sp-1]!= '\\')
1035 /* strip double "\\" */
1036 tmpshortpath[sp] = '\\';
1037 sp++;
1039 tmpshortpath[sp]=0;/*terminate string*/
1040 lp++;
1041 continue;
1044 tmplen = strcspn ( longpath + lp, "\\/" );
1045 lstrcpynA ( tmpshortpath+sp, longpath + lp, tmplen+1 );
1047 /* Check, if the current element is a valid dos name */
1048 if ( DOSFS_ValidDOSName ( longpath + lp, !(flags & DRIVE_CASE_SENSITIVE) ) ) {
1049 sp += tmplen;
1050 lp += tmplen;
1051 continue;
1054 /* Check if the file exists and use the existing file name */
1055 if ( DOSFS_GetFullName ( tmpshortpath, TRUE, &full_name ) ) {
1056 strcpy( tmpshortpath+sp, strrchr ( full_name.short_name, '\\' ) + 1 );
1057 sp += strlen ( tmpshortpath+sp );
1058 lp += tmplen;
1059 continue;
1062 TRACE("not found!\n" );
1063 SetLastError ( ERROR_FILE_NOT_FOUND );
1064 return 0;
1066 tmpshortpath[sp] = 0;
1068 lstrcpynA ( shortpath, tmpshortpath, shortlen );
1069 TRACE("returning %s\n", debugstr_a(shortpath) );
1070 tmplen = strlen ( tmpshortpath );
1071 HeapFree ( GetProcessHeap(), 0, tmpshortpath );
1073 return tmplen;
1077 /***********************************************************************
1078 * GetShortPathNameW (KERNEL32.272)
1080 DWORD WINAPI GetShortPathNameW( LPCWSTR longpath, LPWSTR shortpath,
1081 DWORD shortlen )
1083 LPSTR longpathA, shortpathA;
1084 DWORD ret = 0;
1086 longpathA = HEAP_strdupWtoA( GetProcessHeap(), 0, longpath );
1087 shortpathA = HeapAlloc ( GetProcessHeap(), 0, shortlen );
1089 ret = GetShortPathNameA ( longpathA, shortpathA, shortlen );
1090 if (shortlen > 0 && !MultiByteToWideChar( CP_ACP, 0, shortpathA, -1, shortpath, shortlen ))
1091 shortpath[shortlen-1] = 0;
1092 HeapFree( GetProcessHeap(), 0, longpathA );
1093 HeapFree( GetProcessHeap(), 0, shortpathA );
1095 return ret;
1099 /***********************************************************************
1100 * GetLongPathNameA (KERNEL32.xxx)
1102 DWORD WINAPI GetLongPathNameA( LPCSTR shortpath, LPSTR longpath,
1103 DWORD longlen )
1105 DOS_FULL_NAME full_name;
1106 char *p, *r, *ll, *ss;
1108 if (!DOSFS_GetFullName( shortpath, TRUE, &full_name )) return 0;
1109 lstrcpynA( longpath, full_name.short_name, longlen );
1111 /* Do some hackery to get the long filename. */
1113 if (longpath) {
1114 ss=longpath+strlen(longpath);
1115 ll=full_name.long_name+strlen(full_name.long_name);
1116 p=NULL;
1117 while (ss>=longpath)
1119 /* FIXME: aren't we more paranoid, than needed? */
1120 while ((ss[0]=='\\') && (ss>=longpath)) ss--;
1121 p=ss;
1122 while ((ss[0]!='\\') && (ss>=longpath)) ss--;
1123 if (ss>=longpath)
1125 /* FIXME: aren't we more paranoid, than needed? */
1126 while ((ll[0]=='/') && (ll>=full_name.long_name)) ll--;
1127 while ((ll[0]!='/') && (ll>=full_name.long_name)) ll--;
1128 if (ll<full_name.long_name)
1130 ERR("Bad longname! (ss=%s ll=%s)\n This should never happen !\n"
1131 ,ss ,ll );
1132 return 0;
1137 /* FIXME: fix for names like "C:\\" (ie. with more '\'s) */
1138 if (p && p[2])
1140 p+=1;
1141 if ((p-longpath)>0) longlen -= (p-longpath);
1142 lstrcpynA( p, ll , longlen);
1144 /* Now, change all '/' to '\' */
1145 for (r=p; r<(p+longlen); r++ )
1146 if (r[0]=='/') r[0]='\\';
1147 return strlen(longpath) - strlen(p) + longlen;
1151 return strlen(longpath);
1155 /***********************************************************************
1156 * GetLongPathNameW (KERNEL32.269)
1158 DWORD WINAPI GetLongPathNameW( LPCWSTR shortpath, LPWSTR longpath,
1159 DWORD longlen )
1161 DOS_FULL_NAME full_name;
1162 DWORD ret = 0;
1163 LPSTR shortpathA = HEAP_strdupWtoA( GetProcessHeap(), 0, shortpath );
1165 /* FIXME: is it correct to always return a fully qualified short path? */
1166 if (DOSFS_GetFullName( shortpathA, TRUE, &full_name ))
1168 ret = strlen( full_name.short_name );
1169 if (longlen > 0 && !MultiByteToWideChar( CP_ACP, 0, full_name.long_name, -1,
1170 longpath, longlen ))
1171 longpath[longlen-1] = 0;
1173 HeapFree( GetProcessHeap(), 0, shortpathA );
1174 return ret;
1178 /***********************************************************************
1179 * DOSFS_DoGetFullPathName
1181 * Implementation of GetFullPathNameA/W.
1183 * bon@elektron 000331:
1184 * A test for GetFullPathName with many pathological cases
1185 * now gives identical output for Wine and OSR2
1187 static DWORD DOSFS_DoGetFullPathName( LPCSTR name, DWORD len, LPSTR result,
1188 BOOL unicode )
1190 DWORD ret;
1191 DOS_FULL_NAME full_name;
1192 char *p,*q;
1193 const char * root;
1194 char drivecur[]="c:.";
1195 char driveletter=0;
1196 int namelen,drive=0;
1198 if ((strlen(name) >1)&& (name[1]==':'))
1199 /*drive letter given */
1201 driveletter = name[0];
1203 if ((strlen(name) >2)&& (name[1]==':') &&
1204 ((name[2]=='\\') || (name[2]=='/')))
1205 /*absolute path given */
1207 lstrcpynA(full_name.short_name,name,MAX_PATHNAME_LEN);
1208 drive = (int)FILE_toupper(name[0]) - 'A';
1210 else
1212 if (driveletter)
1213 drivecur[0]=driveletter;
1214 else
1215 strcpy(drivecur,".");
1216 if (!DOSFS_GetFullName( drivecur, FALSE, &full_name ))
1218 FIXME("internal: error getting drive/path\n");
1219 return 0;
1221 /* find path that drive letter substitutes*/
1222 drive = (int)FILE_toupper(full_name.short_name[0]) -0x41;
1223 root= DRIVE_GetRoot(drive);
1224 if (!root)
1226 FIXME("internal: error getting DOS Drive Root\n");
1227 return 0;
1229 if (!strcmp(root,"/"))
1231 /* we have just the last / and we need it. */
1232 p= full_name.long_name;
1234 else
1236 p= full_name.long_name +strlen(root);
1238 /* append long name (= unix name) to drive */
1239 lstrcpynA(full_name.short_name+2,p,MAX_PATHNAME_LEN-3);
1240 /* append name to treat */
1241 namelen= strlen(full_name.short_name);
1242 p = (char*)name;
1243 if (driveletter)
1244 p += +2; /* skip drive name when appending */
1245 if (namelen +2 + strlen(p) > MAX_PATHNAME_LEN)
1247 FIXME("internal error: buffer too small\n");
1248 return 0;
1250 full_name.short_name[namelen++] ='\\';
1251 full_name.short_name[namelen] = 0;
1252 lstrcpynA(full_name.short_name +namelen,p,MAX_PATHNAME_LEN-namelen);
1254 /* reverse all slashes */
1255 for (p=full_name.short_name;
1256 p < full_name.short_name+strlen(full_name.short_name);
1257 p++)
1259 if ( *p == '/' )
1260 *p = '\\';
1262 /* Use memmove, as areas overlap*/
1263 /* Delete .. */
1264 while ((p = strstr(full_name.short_name,"\\..\\")))
1266 if (p > full_name.short_name+2)
1268 *p = 0;
1269 q = strrchr(full_name.short_name,'\\');
1270 memmove(q+1,p+4,strlen(p+4)+1);
1272 else
1274 memmove(full_name.short_name+3,p+4,strlen(p+4)+1);
1277 if ((full_name.short_name[2]=='.')&&(full_name.short_name[3]=='.'))
1279 /* This case istn't treated yet : c:..\test */
1280 memmove(full_name.short_name+2,full_name.short_name+4,
1281 strlen(full_name.short_name+4)+1);
1283 /* Delete . */
1284 while ((p = strstr(full_name.short_name,"\\.\\")))
1286 *(p+1) = 0;
1287 memmove(p+1,p+3,strlen(p+3)+1);
1289 if (!(DRIVE_GetFlags(drive) & DRIVE_CASE_PRESERVING))
1290 for (p = full_name.short_name; *p; p++) *p = FILE_toupper(*p);
1291 namelen=strlen(full_name.short_name);
1292 if (!strcmp(full_name.short_name+namelen-3,"\\.."))
1294 /* one more starnge case: "c:\test\test1\.."
1295 return "c:\test"*/
1296 *(full_name.short_name+namelen-3)=0;
1297 q = strrchr(full_name.short_name,'\\');
1298 *q =0;
1300 if (full_name.short_name[namelen-1]=='.')
1301 full_name.short_name[(namelen--)-1] =0;
1302 if (!driveletter)
1303 if (full_name.short_name[namelen-1]=='\\')
1304 full_name.short_name[(namelen--)-1] =0;
1305 TRACE("got %s\n",full_name.short_name);
1307 /* If the lpBuffer buffer is too small, the return value is the
1308 size of the buffer, in characters, required to hold the path
1309 plus the terminating \0 (tested against win95osr, bon 001118)
1310 . */
1311 ret = strlen(full_name.short_name);
1312 if (ret >= len )
1314 /* don't touch anything when the buffer is not large enough */
1315 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1316 return ret+1;
1318 if (result)
1320 if (unicode)
1321 MultiByteToWideChar( CP_ACP, 0, full_name.short_name, -1, (LPWSTR)result, len );
1322 else
1323 lstrcpynA( result, full_name.short_name, len );
1326 TRACE("returning '%s'\n", full_name.short_name );
1327 return ret;
1331 /***********************************************************************
1332 * GetFullPathNameA (KERNEL32.272)
1333 * NOTES
1334 * if the path closed with '\', *lastpart is 0
1336 DWORD WINAPI GetFullPathNameA( LPCSTR name, DWORD len, LPSTR buffer,
1337 LPSTR *lastpart )
1339 DWORD ret = DOSFS_DoGetFullPathName( name, len, buffer, FALSE );
1340 if (ret && (ret<=len) && buffer && lastpart)
1342 LPSTR p = buffer + strlen(buffer);
1344 if (*p != '\\')
1346 while ((p > buffer + 2) && (*p != '\\')) p--;
1347 *lastpart = p + 1;
1349 else *lastpart = NULL;
1351 return ret;
1355 /***********************************************************************
1356 * GetFullPathNameW (KERNEL32.273)
1358 DWORD WINAPI GetFullPathNameW( LPCWSTR name, DWORD len, LPWSTR buffer,
1359 LPWSTR *lastpart )
1361 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
1362 DWORD ret = DOSFS_DoGetFullPathName( nameA, len, (LPSTR)buffer, TRUE );
1363 HeapFree( GetProcessHeap(), 0, nameA );
1364 if (ret && (ret<=len) && buffer && lastpart)
1366 LPWSTR p = buffer + strlenW(buffer);
1367 if (*p != (WCHAR)'\\')
1369 while ((p > buffer + 2) && (*p != (WCHAR)'\\')) p--;
1370 *lastpart = p + 1;
1372 else *lastpart = NULL;
1374 return ret;
1378 /***********************************************************************
1379 * wine_get_unix_file_name (Not a Windows API, but exported from KERNEL32)
1381 * Return the full Unix file name for a given path.
1383 BOOL WINAPI wine_get_unix_file_name( LPCSTR dos, LPSTR buffer, DWORD len )
1385 BOOL ret;
1386 DOS_FULL_NAME path;
1387 if ((ret = DOSFS_GetFullName( dos, FALSE, &path ))) lstrcpynA( buffer, path.long_name, len );
1388 return ret;
1392 /***********************************************************************
1393 * DOSFS_FindNextEx
1395 static int DOSFS_FindNextEx( FIND_FIRST_INFO *info, WIN32_FIND_DATAA *entry )
1397 DWORD attr = info->attr | FA_UNUSED | FA_ARCHIVE | FA_RDONLY | FILE_ATTRIBUTE_SYMLINK;
1398 UINT flags = DRIVE_GetFlags( info->drive );
1399 char *p, buffer[MAX_PATHNAME_LEN];
1400 const char *drive_path;
1401 int drive_root;
1402 LPCSTR long_name, short_name;
1403 BY_HANDLE_FILE_INFORMATION fileinfo;
1404 char dos_name[13];
1406 if ((info->attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
1408 if (info->cur_pos) return 0;
1409 entry->dwFileAttributes = FILE_ATTRIBUTE_LABEL;
1410 RtlSecondsSince1970ToTime( (time_t)0, &entry->ftCreationTime );
1411 RtlSecondsSince1970ToTime( (time_t)0, &entry->ftLastAccessTime );
1412 RtlSecondsSince1970ToTime( (time_t)0, &entry->ftLastWriteTime );
1413 entry->nFileSizeHigh = 0;
1414 entry->nFileSizeLow = 0;
1415 entry->dwReserved0 = 0;
1416 entry->dwReserved1 = 0;
1417 DOSFS_ToDosDTAFormat( DRIVE_GetLabel( info->drive ), entry->cFileName );
1418 strcpy( entry->cAlternateFileName, entry->cFileName );
1419 info->cur_pos++;
1420 TRACE("returning %s (%s) as label\n",
1421 entry->cFileName, entry->cAlternateFileName);
1422 return 1;
1425 drive_path = info->path + strlen(DRIVE_GetRoot( info->drive ));
1426 while ((*drive_path == '/') || (*drive_path == '\\')) drive_path++;
1427 drive_root = !*drive_path;
1429 lstrcpynA( buffer, info->path, sizeof(buffer) - 1 );
1430 strcat( buffer, "/" );
1431 p = buffer + strlen(buffer);
1433 while (DOSFS_ReadDir( info->dir, &long_name, &short_name ))
1435 info->cur_pos++;
1437 /* Don't return '.' and '..' in the root of the drive */
1438 if (drive_root && (long_name[0] == '.') &&
1439 (!long_name[1] || ((long_name[1] == '.') && !long_name[2])))
1440 continue;
1442 /* Check the long mask */
1444 if (info->long_mask)
1446 if (!DOSFS_MatchLong( info->long_mask, long_name,
1447 flags & DRIVE_CASE_SENSITIVE )) continue;
1450 /* Check the short mask */
1452 if (info->short_mask)
1454 if (!short_name)
1456 DOSFS_Hash( long_name, dos_name, TRUE,
1457 !(flags & DRIVE_CASE_SENSITIVE) );
1458 short_name = dos_name;
1460 if (!DOSFS_MatchShort( info->short_mask, short_name )) continue;
1463 /* Check the file attributes */
1465 lstrcpynA( p, long_name, sizeof(buffer) - (int)(p - buffer) );
1466 if (!FILE_Stat( buffer, &fileinfo ))
1468 WARN("can't stat %s\n", buffer);
1469 continue;
1471 if ((fileinfo.dwFileAttributes & FILE_ATTRIBUTE_SYMLINK) &&
1472 (fileinfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
1474 static int show_dir_symlinks = -1;
1475 if (show_dir_symlinks == -1)
1476 show_dir_symlinks = PROFILE_GetWineIniBool("wine", "ShowDirSymlinks", 0);
1477 if (!show_dir_symlinks) continue;
1480 if (fileinfo.dwFileAttributes & ~attr) continue;
1482 /* We now have a matching entry; fill the result and return */
1484 entry->dwFileAttributes = fileinfo.dwFileAttributes;
1485 entry->ftCreationTime = fileinfo.ftCreationTime;
1486 entry->ftLastAccessTime = fileinfo.ftLastAccessTime;
1487 entry->ftLastWriteTime = fileinfo.ftLastWriteTime;
1488 entry->nFileSizeHigh = fileinfo.nFileSizeHigh;
1489 entry->nFileSizeLow = fileinfo.nFileSizeLow;
1491 if (short_name)
1492 DOSFS_ToDosDTAFormat( short_name, entry->cAlternateFileName );
1493 else
1494 DOSFS_Hash( long_name, entry->cAlternateFileName, FALSE,
1495 !(flags & DRIVE_CASE_SENSITIVE) );
1497 lstrcpynA( entry->cFileName, long_name, sizeof(entry->cFileName) );
1498 if (!(flags & DRIVE_CASE_PRESERVING)) _strlwr( entry->cFileName );
1499 TRACE("returning %s (%s) %02lx %ld\n",
1500 entry->cFileName, entry->cAlternateFileName,
1501 entry->dwFileAttributes, entry->nFileSizeLow );
1502 return 1;
1504 return 0; /* End of directory */
1507 /***********************************************************************
1508 * DOSFS_FindNext
1510 * Find the next matching file. Return the number of entries read to find
1511 * the matching one, or 0 if no more entries.
1512 * 'short_mask' is the 8.3 mask (in FCB format), 'long_mask' is the long
1513 * file name mask. Either or both can be NULL.
1515 * NOTE: This is supposed to be only called by the int21 emulation
1516 * routines. Thus, we should own the Win16Mutex anyway.
1517 * Nevertheless, we explicitly enter it to ensure the static
1518 * directory cache is protected.
1520 int DOSFS_FindNext( const char *path, const char *short_mask,
1521 const char *long_mask, int drive, BYTE attr,
1522 int skip, WIN32_FIND_DATAA *entry )
1524 static FIND_FIRST_INFO info;
1525 LPCSTR short_name, long_name;
1526 int count;
1528 _EnterWin16Lock();
1530 /* Check the cached directory */
1531 if (!(info.dir && info.path == path && info.short_mask == short_mask
1532 && info.long_mask == long_mask && info.drive == drive
1533 && info.attr == attr && info.cur_pos <= skip))
1535 /* Not in the cache, open it anew */
1536 if (info.dir) DOSFS_CloseDir( info.dir );
1538 info.path = (LPSTR)path;
1539 info.long_mask = (LPSTR)long_mask;
1540 info.short_mask = (LPSTR)short_mask;
1541 info.attr = attr;
1542 info.drive = drive;
1543 info.cur_pos = 0;
1544 info.dir = DOSFS_OpenDir( info.path );
1547 /* Skip to desired position */
1548 while (info.cur_pos < skip)
1549 if (info.dir && DOSFS_ReadDir( info.dir, &long_name, &short_name ))
1550 info.cur_pos++;
1551 else
1552 break;
1554 if (info.dir && info.cur_pos == skip && DOSFS_FindNextEx( &info, entry ))
1555 count = info.cur_pos - skip;
1556 else
1557 count = 0;
1559 if (!count)
1561 if (info.dir) DOSFS_CloseDir( info.dir );
1562 memset( &info, '\0', sizeof(info) );
1565 _LeaveWin16Lock();
1567 return count;
1570 /*************************************************************************
1571 * FindFirstFileExA (KERNEL32)
1573 HANDLE WINAPI FindFirstFileExA(
1574 LPCSTR lpFileName,
1575 FINDEX_INFO_LEVELS fInfoLevelId,
1576 LPVOID lpFindFileData,
1577 FINDEX_SEARCH_OPS fSearchOp,
1578 LPVOID lpSearchFilter,
1579 DWORD dwAdditionalFlags)
1581 DOS_FULL_NAME full_name;
1582 HGLOBAL handle;
1583 FIND_FIRST_INFO *info;
1585 if ((fSearchOp != FindExSearchNameMatch) || (dwAdditionalFlags != 0))
1587 FIXME("options not implemented 0x%08x 0x%08lx\n", fSearchOp, dwAdditionalFlags );
1588 return INVALID_HANDLE_VALUE;
1591 switch(fInfoLevelId)
1593 case FindExInfoStandard:
1595 WIN32_FIND_DATAA * data = (WIN32_FIND_DATAA *) lpFindFileData;
1596 data->dwReserved0 = data->dwReserved1 = 0x0;
1597 if (!lpFileName) return 0;
1598 if (!DOSFS_GetFullName( lpFileName, FALSE, &full_name )) break;
1599 if (!(handle = GlobalAlloc(GMEM_MOVEABLE, sizeof(FIND_FIRST_INFO)))) break;
1600 info = (FIND_FIRST_INFO *)GlobalLock( handle );
1601 info->path = HEAP_strdupA( GetProcessHeap(), 0, full_name.long_name );
1602 info->long_mask = strrchr( info->path, '/' );
1603 *(info->long_mask++) = '\0';
1604 info->short_mask = NULL;
1605 info->attr = 0xff;
1606 if (lpFileName[0] && (lpFileName[1] == ':'))
1607 info->drive = FILE_toupper(*lpFileName) - 'A';
1608 else info->drive = DRIVE_GetCurrentDrive();
1609 info->cur_pos = 0;
1611 info->dir = DOSFS_OpenDir( info->path );
1613 GlobalUnlock( handle );
1614 if (!FindNextFileA( handle, data ))
1616 FindClose( handle );
1617 SetLastError( ERROR_NO_MORE_FILES );
1618 break;
1620 return handle;
1622 break;
1623 default:
1624 FIXME("fInfoLevelId 0x%08x not implemented\n", fInfoLevelId );
1626 return INVALID_HANDLE_VALUE;
1629 /*************************************************************************
1630 * FindFirstFileA (KERNEL32.123)
1632 HANDLE WINAPI FindFirstFileA(
1633 LPCSTR lpFileName,
1634 WIN32_FIND_DATAA *lpFindData )
1636 return FindFirstFileExA(lpFileName, FindExInfoStandard, lpFindData,
1637 FindExSearchNameMatch, NULL, 0);
1640 /*************************************************************************
1641 * FindFirstFileExW (KERNEL32)
1643 HANDLE WINAPI FindFirstFileExW(
1644 LPCWSTR lpFileName,
1645 FINDEX_INFO_LEVELS fInfoLevelId,
1646 LPVOID lpFindFileData,
1647 FINDEX_SEARCH_OPS fSearchOp,
1648 LPVOID lpSearchFilter,
1649 DWORD dwAdditionalFlags)
1651 HANDLE handle;
1652 WIN32_FIND_DATAA dataA;
1653 LPVOID _lpFindFileData;
1654 LPSTR pathA;
1656 switch(fInfoLevelId)
1658 case FindExInfoStandard:
1660 _lpFindFileData = &dataA;
1662 break;
1663 default:
1664 FIXME("fInfoLevelId 0x%08x not implemented\n", fInfoLevelId );
1665 return INVALID_HANDLE_VALUE;
1668 pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
1669 handle = FindFirstFileExA(pathA, fInfoLevelId, _lpFindFileData, fSearchOp, lpSearchFilter, dwAdditionalFlags);
1670 HeapFree( GetProcessHeap(), 0, pathA );
1671 if (handle == INVALID_HANDLE_VALUE) return handle;
1673 switch(fInfoLevelId)
1675 case FindExInfoStandard:
1677 WIN32_FIND_DATAW *dataW = (WIN32_FIND_DATAW*) lpFindFileData;
1678 dataW->dwFileAttributes = dataA.dwFileAttributes;
1679 dataW->ftCreationTime = dataA.ftCreationTime;
1680 dataW->ftLastAccessTime = dataA.ftLastAccessTime;
1681 dataW->ftLastWriteTime = dataA.ftLastWriteTime;
1682 dataW->nFileSizeHigh = dataA.nFileSizeHigh;
1683 dataW->nFileSizeLow = dataA.nFileSizeLow;
1684 MultiByteToWideChar( CP_ACP, 0, dataA.cFileName, -1,
1685 dataW->cFileName, sizeof(dataW->cFileName)/sizeof(WCHAR) );
1686 MultiByteToWideChar( CP_ACP, 0, dataA.cAlternateFileName, -1,
1687 dataW->cAlternateFileName,
1688 sizeof(dataW->cAlternateFileName)/sizeof(WCHAR) );
1690 break;
1691 default:
1692 FIXME("fInfoLevelId 0x%08x not implemented\n", fInfoLevelId );
1693 return INVALID_HANDLE_VALUE;
1695 return handle;
1698 /*************************************************************************
1699 * FindFirstFileW (KERNEL32.124)
1701 HANDLE WINAPI FindFirstFileW( LPCWSTR lpFileName, WIN32_FIND_DATAW *lpFindData )
1703 return FindFirstFileExW(lpFileName, FindExInfoStandard, lpFindData,
1704 FindExSearchNameMatch, NULL, 0);
1707 /*************************************************************************
1708 * FindNextFileA (KERNEL32.126)
1710 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
1712 FIND_FIRST_INFO *info;
1714 if ((handle == INVALID_HANDLE_VALUE) ||
1715 !(info = (FIND_FIRST_INFO *)GlobalLock( handle )))
1717 SetLastError( ERROR_INVALID_HANDLE );
1718 return FALSE;
1720 GlobalUnlock( handle );
1721 if (!info->path || !info->dir)
1723 SetLastError( ERROR_NO_MORE_FILES );
1724 return FALSE;
1726 if (!DOSFS_FindNextEx( info, data ))
1728 DOSFS_CloseDir( info->dir ); info->dir = NULL;
1729 HeapFree( GetProcessHeap(), 0, info->path );
1730 info->path = info->long_mask = NULL;
1731 SetLastError( ERROR_NO_MORE_FILES );
1732 return FALSE;
1734 return TRUE;
1738 /*************************************************************************
1739 * FindNextFileW (KERNEL32.127)
1741 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
1743 WIN32_FIND_DATAA dataA;
1744 if (!FindNextFileA( handle, &dataA )) return FALSE;
1745 data->dwFileAttributes = dataA.dwFileAttributes;
1746 data->ftCreationTime = dataA.ftCreationTime;
1747 data->ftLastAccessTime = dataA.ftLastAccessTime;
1748 data->ftLastWriteTime = dataA.ftLastWriteTime;
1749 data->nFileSizeHigh = dataA.nFileSizeHigh;
1750 data->nFileSizeLow = dataA.nFileSizeLow;
1751 MultiByteToWideChar( CP_ACP, 0, dataA.cFileName, -1,
1752 data->cFileName, sizeof(data->cFileName)/sizeof(WCHAR) );
1753 MultiByteToWideChar( CP_ACP, 0, dataA.cAlternateFileName, -1,
1754 data->cAlternateFileName,
1755 sizeof(data->cAlternateFileName)/sizeof(WCHAR) );
1756 return TRUE;
1759 /*************************************************************************
1760 * FindClose (KERNEL32.119)
1762 BOOL WINAPI FindClose( HANDLE handle )
1764 FIND_FIRST_INFO *info;
1766 if ((handle == INVALID_HANDLE_VALUE) ||
1767 !(info = (FIND_FIRST_INFO *)GlobalLock( handle )))
1769 SetLastError( ERROR_INVALID_HANDLE );
1770 return FALSE;
1772 if (info->dir) DOSFS_CloseDir( info->dir );
1773 if (info->path) HeapFree( GetProcessHeap(), 0, info->path );
1774 GlobalUnlock( handle );
1775 GlobalFree( handle );
1776 return TRUE;
1779 /***********************************************************************
1780 * DOSFS_UnixTimeToFileTime
1782 * Convert a Unix time to FILETIME format.
1783 * The FILETIME structure is a 64-bit value representing the number of
1784 * 100-nanosecond intervals since January 1, 1601, 0:00.
1785 * 'remainder' is the nonnegative number of 100-ns intervals
1786 * corresponding to the time fraction smaller than 1 second that
1787 * couldn't be stored in the time_t value.
1789 void DOSFS_UnixTimeToFileTime( time_t unix_time, FILETIME *filetime,
1790 DWORD remainder )
1792 /* NOTES:
1794 CONSTANTS:
1795 The time difference between 1 January 1601, 00:00:00 and
1796 1 January 1970, 00:00:00 is 369 years, plus the leap years
1797 from 1604 to 1968, excluding 1700, 1800, 1900.
1798 This makes (1968 - 1600) / 4 - 3 = 89 leap days, and a total
1799 of 134774 days.
1801 Any day in that period had 24 * 60 * 60 = 86400 seconds.
1803 The time difference is 134774 * 86400 * 10000000, which can be written
1804 116444736000000000
1805 27111902 * 2^32 + 3577643008
1806 413 * 2^48 + 45534 * 2^32 + 54590 * 2^16 + 32768
1808 If you find that these constants are buggy, please change them in all
1809 instances in both conversion functions.
1811 VERSIONS:
1812 There are two versions, one of them uses long long variables and
1813 is presumably faster but not ISO C. The other one uses standard C
1814 data types and operations but relies on the assumption that negative
1815 numbers are stored as 2's complement (-1 is 0xffff....). If this
1816 assumption is violated, dates before 1970 will not convert correctly.
1817 This should however work on any reasonable architecture where WINE
1818 will run.
1820 DETAILS:
1822 Take care not to remove the casts. I have tested these functions
1823 (in both versions) for a lot of numbers. I would be interested in
1824 results on other compilers than GCC.
1826 The operations have been designed to account for the possibility
1827 of 64-bit time_t in future UNICES. Even the versions without
1828 internal long long numbers will work if time_t only is 64 bit.
1829 A 32-bit shift, which was necessary for that operation, turned out
1830 not to work correctly in GCC, besides giving the warning. So I
1831 used a double 16-bit shift instead. Numbers are in the ISO version
1832 represented by three limbs, the most significant with 32 bit, the
1833 other two with 16 bit each.
1835 As the modulo-operator % is not well-defined for negative numbers,
1836 negative divisors have been avoided in DOSFS_FileTimeToUnixTime.
1838 There might be quicker ways to do this in C. Certainly so in
1839 assembler.
1841 Claus Fischer, fischer@iue.tuwien.ac.at
1844 #if SIZEOF_LONG_LONG >= 8
1845 # define USE_LONG_LONG 1
1846 #else
1847 # define USE_LONG_LONG 0
1848 #endif
1850 #if USE_LONG_LONG /* gcc supports long long type */
1852 long long int t = unix_time;
1853 t *= 10000000;
1854 t += 116444736000000000LL;
1855 t += remainder;
1856 filetime->dwLowDateTime = (UINT)t;
1857 filetime->dwHighDateTime = (UINT)(t >> 32);
1859 #else /* ISO version */
1861 UINT a0; /* 16 bit, low bits */
1862 UINT a1; /* 16 bit, medium bits */
1863 UINT a2; /* 32 bit, high bits */
1865 /* Copy the unix time to a2/a1/a0 */
1866 a0 = unix_time & 0xffff;
1867 a1 = (unix_time >> 16) & 0xffff;
1868 /* This is obsolete if unix_time is only 32 bits, but it does not hurt.
1869 Do not replace this by >> 32, it gives a compiler warning and it does
1870 not work. */
1871 a2 = (unix_time >= 0 ? (unix_time >> 16) >> 16 :
1872 ~((~unix_time >> 16) >> 16));
1874 /* Multiply a by 10000000 (a = a2/a1/a0)
1875 Split the factor into 10000 * 1000 which are both less than 0xffff. */
1876 a0 *= 10000;
1877 a1 = a1 * 10000 + (a0 >> 16);
1878 a2 = a2 * 10000 + (a1 >> 16);
1879 a0 &= 0xffff;
1880 a1 &= 0xffff;
1882 a0 *= 1000;
1883 a1 = a1 * 1000 + (a0 >> 16);
1884 a2 = a2 * 1000 + (a1 >> 16);
1885 a0 &= 0xffff;
1886 a1 &= 0xffff;
1888 /* Add the time difference and the remainder */
1889 a0 += 32768 + (remainder & 0xffff);
1890 a1 += 54590 + (remainder >> 16 ) + (a0 >> 16);
1891 a2 += 27111902 + (a1 >> 16);
1892 a0 &= 0xffff;
1893 a1 &= 0xffff;
1895 /* Set filetime */
1896 filetime->dwLowDateTime = (a1 << 16) + a0;
1897 filetime->dwHighDateTime = a2;
1898 #endif
1902 /***********************************************************************
1903 * DOSFS_FileTimeToUnixTime
1905 * Convert a FILETIME format to Unix time.
1906 * If not NULL, 'remainder' contains the fractional part of the filetime,
1907 * in the range of [0..9999999] (even if time_t is negative).
1909 time_t DOSFS_FileTimeToUnixTime( const FILETIME *filetime, DWORD *remainder )
1911 /* Read the comment in the function DOSFS_UnixTimeToFileTime. */
1912 #if USE_LONG_LONG
1914 long long int t = filetime->dwHighDateTime;
1915 t <<= 32;
1916 t += (UINT)filetime->dwLowDateTime;
1917 t -= 116444736000000000LL;
1918 if (t < 0)
1920 if (remainder) *remainder = 9999999 - (-t - 1) % 10000000;
1921 return -1 - ((-t - 1) / 10000000);
1923 else
1925 if (remainder) *remainder = t % 10000000;
1926 return t / 10000000;
1929 #else /* ISO version */
1931 UINT a0; /* 16 bit, low bits */
1932 UINT a1; /* 16 bit, medium bits */
1933 UINT a2; /* 32 bit, high bits */
1934 UINT r; /* remainder of division */
1935 unsigned int carry; /* carry bit for subtraction */
1936 int negative; /* whether a represents a negative value */
1938 /* Copy the time values to a2/a1/a0 */
1939 a2 = (UINT)filetime->dwHighDateTime;
1940 a1 = ((UINT)filetime->dwLowDateTime ) >> 16;
1941 a0 = ((UINT)filetime->dwLowDateTime ) & 0xffff;
1943 /* Subtract the time difference */
1944 if (a0 >= 32768 ) a0 -= 32768 , carry = 0;
1945 else a0 += (1 << 16) - 32768 , carry = 1;
1947 if (a1 >= 54590 + carry) a1 -= 54590 + carry, carry = 0;
1948 else a1 += (1 << 16) - 54590 - carry, carry = 1;
1950 a2 -= 27111902 + carry;
1952 /* If a is negative, replace a by (-1-a) */
1953 negative = (a2 >= ((UINT)1) << 31);
1954 if (negative)
1956 /* Set a to -a - 1 (a is a2/a1/a0) */
1957 a0 = 0xffff - a0;
1958 a1 = 0xffff - a1;
1959 a2 = ~a2;
1962 /* Divide a by 10000000 (a = a2/a1/a0), put the rest into r.
1963 Split the divisor into 10000 * 1000 which are both less than 0xffff. */
1964 a1 += (a2 % 10000) << 16;
1965 a2 /= 10000;
1966 a0 += (a1 % 10000) << 16;
1967 a1 /= 10000;
1968 r = a0 % 10000;
1969 a0 /= 10000;
1971 a1 += (a2 % 1000) << 16;
1972 a2 /= 1000;
1973 a0 += (a1 % 1000) << 16;
1974 a1 /= 1000;
1975 r += (a0 % 1000) * 10000;
1976 a0 /= 1000;
1978 /* If a was negative, replace a by (-1-a) and r by (9999999 - r) */
1979 if (negative)
1981 /* Set a to -a - 1 (a is a2/a1/a0) */
1982 a0 = 0xffff - a0;
1983 a1 = 0xffff - a1;
1984 a2 = ~a2;
1986 r = 9999999 - r;
1989 if (remainder) *remainder = r;
1991 /* Do not replace this by << 32, it gives a compiler warning and it does
1992 not work. */
1993 return ((((time_t)a2) << 16) << 16) + (a1 << 16) + a0;
1994 #endif
1998 /***********************************************************************
1999 * MulDiv (KERNEL32.391)
2000 * RETURNS
2001 * Result of multiplication and division
2002 * -1: Overflow occurred or Divisor was 0
2004 INT WINAPI MulDiv(
2005 INT nMultiplicand,
2006 INT nMultiplier,
2007 INT nDivisor)
2009 #if SIZEOF_LONG_LONG >= 8
2010 long long ret;
2012 if (!nDivisor) return -1;
2014 /* We want to deal with a positive divisor to simplify the logic. */
2015 if (nDivisor < 0)
2017 nMultiplicand = - nMultiplicand;
2018 nDivisor = -nDivisor;
2021 /* If the result is positive, we "add" to round. else, we subtract to round. */
2022 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
2023 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
2024 ret = (((long long)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
2025 else
2026 ret = (((long long)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
2028 if ((ret > 2147483647) || (ret < -2147483647)) return -1;
2029 return ret;
2030 #else
2031 if (!nDivisor) return -1;
2033 /* We want to deal with a positive divisor to simplify the logic. */
2034 if (nDivisor < 0)
2036 nMultiplicand = - nMultiplicand;
2037 nDivisor = -nDivisor;
2040 /* If the result is positive, we "add" to round. else, we subtract to round. */
2041 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
2042 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
2043 return ((nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
2045 return ((nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
2047 #endif
2051 /***********************************************************************
2052 * DosDateTimeToFileTime (KERNEL32.76)
2054 BOOL WINAPI DosDateTimeToFileTime( WORD fatdate, WORD fattime, LPFILETIME ft)
2056 struct tm newtm;
2058 newtm.tm_sec = (fattime & 0x1f) * 2;
2059 newtm.tm_min = (fattime >> 5) & 0x3f;
2060 newtm.tm_hour = (fattime >> 11);
2061 newtm.tm_mday = (fatdate & 0x1f);
2062 newtm.tm_mon = ((fatdate >> 5) & 0x0f) - 1;
2063 newtm.tm_year = (fatdate >> 9) + 80;
2064 RtlSecondsSince1970ToTime( mktime( &newtm ), ft );
2065 return TRUE;
2069 /***********************************************************************
2070 * FileTimeToDosDateTime (KERNEL32.111)
2072 BOOL WINAPI FileTimeToDosDateTime( const FILETIME *ft, LPWORD fatdate,
2073 LPWORD fattime )
2075 time_t unixtime = DOSFS_FileTimeToUnixTime( ft, NULL );
2076 struct tm *tm = localtime( &unixtime );
2077 if (fattime)
2078 *fattime = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
2079 if (fatdate)
2080 *fatdate = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5)
2081 + tm->tm_mday;
2082 return TRUE;
2086 /***********************************************************************
2087 * LocalFileTimeToFileTime (KERNEL32.373)
2089 BOOL WINAPI LocalFileTimeToFileTime( const FILETIME *localft,
2090 LPFILETIME utcft )
2092 struct tm *xtm;
2093 DWORD remainder;
2095 /* convert from local to UTC. Perhaps not correct. FIXME */
2096 time_t unixtime = DOSFS_FileTimeToUnixTime( localft, &remainder );
2097 xtm = gmtime( &unixtime );
2098 DOSFS_UnixTimeToFileTime( mktime(xtm), utcft, remainder );
2099 return TRUE;
2103 /***********************************************************************
2104 * FileTimeToLocalFileTime (KERNEL32.112)
2106 BOOL WINAPI FileTimeToLocalFileTime( const FILETIME *utcft,
2107 LPFILETIME localft )
2109 DWORD remainder;
2110 /* convert from UTC to local. Perhaps not correct. FIXME */
2111 time_t unixtime = DOSFS_FileTimeToUnixTime( utcft, &remainder );
2112 #ifdef HAVE_TIMEGM
2113 struct tm *xtm = localtime( &unixtime );
2114 time_t localtime;
2116 localtime = timegm(xtm);
2117 DOSFS_UnixTimeToFileTime( localtime, localft, remainder );
2119 #else
2120 struct tm *xtm,*gtm;
2121 time_t time1,time2;
2123 xtm = localtime( &unixtime );
2124 gtm = gmtime( &unixtime );
2125 time1 = mktime(xtm);
2126 time2 = mktime(gtm);
2127 DOSFS_UnixTimeToFileTime( 2*time1-time2, localft, remainder );
2128 #endif
2129 return TRUE;
2133 /***********************************************************************
2134 * FileTimeToSystemTime (KERNEL32.113)
2136 BOOL WINAPI FileTimeToSystemTime( const FILETIME *ft, LPSYSTEMTIME syst )
2138 struct tm *xtm;
2139 DWORD remainder;
2140 time_t xtime = DOSFS_FileTimeToUnixTime( ft, &remainder );
2141 xtm = gmtime(&xtime);
2142 syst->wYear = xtm->tm_year+1900;
2143 syst->wMonth = xtm->tm_mon + 1;
2144 syst->wDayOfWeek = xtm->tm_wday;
2145 syst->wDay = xtm->tm_mday;
2146 syst->wHour = xtm->tm_hour;
2147 syst->wMinute = xtm->tm_min;
2148 syst->wSecond = xtm->tm_sec;
2149 syst->wMilliseconds = remainder / 10000;
2150 return TRUE;
2153 /***********************************************************************
2154 * QueryDosDeviceA (KERNEL32.413)
2156 * returns array of strings terminated by \0, terminated by \0
2158 DWORD WINAPI QueryDosDeviceA(LPCSTR devname,LPSTR target,DWORD bufsize)
2160 LPSTR s;
2161 char buffer[200];
2163 TRACE("(%s,...)\n", devname ? devname : "<null>");
2164 if (!devname) {
2165 /* return known MSDOS devices */
2166 static const char devices[24] = "CON\0COM1\0COM2\0LPT1\0NUL\0\0";
2167 memcpy( target, devices, min(bufsize,sizeof(devices)) );
2168 return min(bufsize,sizeof(devices));
2170 strcpy(buffer,"\\DEV\\");
2171 strcat(buffer,devname);
2172 if ((s=strchr(buffer,':'))) *s='\0';
2173 lstrcpynA(target,buffer,bufsize);
2174 return strlen(buffer)+1;
2178 /***********************************************************************
2179 * QueryDosDeviceW (KERNEL32.414)
2181 * returns array of strings terminated by \0, terminated by \0
2183 DWORD WINAPI QueryDosDeviceW(LPCWSTR devname,LPWSTR target,DWORD bufsize)
2185 LPSTR devnameA = devname?HEAP_strdupWtoA(GetProcessHeap(),0,devname):NULL;
2186 LPSTR targetA = (LPSTR)HeapAlloc(GetProcessHeap(),0,bufsize);
2187 DWORD ret = QueryDosDeviceA(devnameA,targetA,bufsize);
2189 ret = MultiByteToWideChar( CP_ACP, 0, targetA, ret, target, bufsize );
2190 if (devnameA) HeapFree(GetProcessHeap(),0,devnameA);
2191 if (targetA) HeapFree(GetProcessHeap(),0,targetA);
2192 return ret;
2196 /***********************************************************************
2197 * SystemTimeToFileTime (KERNEL32.526)
2199 BOOL WINAPI SystemTimeToFileTime( const SYSTEMTIME *syst, LPFILETIME ft )
2201 #ifdef HAVE_TIMEGM
2202 struct tm xtm;
2203 time_t utctime;
2204 #else
2205 struct tm xtm,*local_tm,*utc_tm;
2206 time_t localtim,utctime;
2207 #endif
2209 xtm.tm_year = syst->wYear-1900;
2210 xtm.tm_mon = syst->wMonth - 1;
2211 xtm.tm_wday = syst->wDayOfWeek;
2212 xtm.tm_mday = syst->wDay;
2213 xtm.tm_hour = syst->wHour;
2214 xtm.tm_min = syst->wMinute;
2215 xtm.tm_sec = syst->wSecond; /* this is UTC */
2216 xtm.tm_isdst = -1;
2217 #ifdef HAVE_TIMEGM
2218 utctime = timegm(&xtm);
2219 DOSFS_UnixTimeToFileTime( utctime, ft,
2220 syst->wMilliseconds * 10000 );
2221 #else
2222 localtim = mktime(&xtm); /* now we've got local time */
2223 local_tm = localtime(&localtim);
2224 utc_tm = gmtime(&localtim);
2225 utctime = mktime(utc_tm);
2226 DOSFS_UnixTimeToFileTime( 2*localtim -utctime, ft,
2227 syst->wMilliseconds * 10000 );
2228 #endif
2229 return TRUE;
2232 /***********************************************************************
2233 * DefineDosDeviceA (KERNEL32.182)
2235 BOOL WINAPI DefineDosDeviceA(DWORD flags,LPCSTR devname,LPCSTR targetpath) {
2236 FIXME("(0x%08lx,%s,%s),stub!\n",flags,devname,targetpath);
2237 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2238 return FALSE;
2242 --- 16 bit functions ---
2245 /*************************************************************************
2246 * FindFirstFile16 (KERNEL.413)
2248 HANDLE16 WINAPI FindFirstFile16( LPCSTR path, WIN32_FIND_DATAA *data )
2250 DOS_FULL_NAME full_name;
2251 HGLOBAL16 handle;
2252 FIND_FIRST_INFO *info;
2254 data->dwReserved0 = data->dwReserved1 = 0x0;
2255 if (!path) return 0;
2256 if (!DOSFS_GetFullName( path, FALSE, &full_name ))
2257 return INVALID_HANDLE_VALUE16;
2258 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE, sizeof(FIND_FIRST_INFO) )))
2259 return INVALID_HANDLE_VALUE16;
2260 info = (FIND_FIRST_INFO *)GlobalLock16( handle );
2261 info->path = HEAP_strdupA( SystemHeap, 0, full_name.long_name );
2262 info->long_mask = strrchr( info->path, '/' );
2263 if (info->long_mask )
2264 *(info->long_mask++) = '\0';
2265 info->short_mask = NULL;
2266 info->attr = 0xff;
2267 if (path[0] && (path[1] == ':')) info->drive = FILE_toupper(*path) - 'A';
2268 else info->drive = DRIVE_GetCurrentDrive();
2269 info->cur_pos = 0;
2271 info->dir = DOSFS_OpenDir( info->path );
2273 GlobalUnlock16( handle );
2274 if (!FindNextFile16( handle, data ))
2276 FindClose16( handle );
2277 SetLastError( ERROR_NO_MORE_FILES );
2278 return INVALID_HANDLE_VALUE16;
2280 return handle;
2283 /*************************************************************************
2284 * FindNextFile16 (KERNEL.414)
2286 BOOL16 WINAPI FindNextFile16( HANDLE16 handle, WIN32_FIND_DATAA *data )
2288 FIND_FIRST_INFO *info;
2290 if ((handle == INVALID_HANDLE_VALUE16) ||
2291 !(info = (FIND_FIRST_INFO *)GlobalLock16( handle )))
2293 SetLastError( ERROR_INVALID_HANDLE );
2294 return FALSE;
2296 GlobalUnlock16( handle );
2297 if (!info->path || !info->dir)
2299 SetLastError( ERROR_NO_MORE_FILES );
2300 return FALSE;
2302 if (!DOSFS_FindNextEx( info, data ))
2304 DOSFS_CloseDir( info->dir ); info->dir = NULL;
2305 HeapFree( SystemHeap, 0, info->path );
2306 info->path = info->long_mask = NULL;
2307 SetLastError( ERROR_NO_MORE_FILES );
2308 return FALSE;
2310 return TRUE;
2313 /*************************************************************************
2314 * FindClose16 (KERNEL.415)
2316 BOOL16 WINAPI FindClose16( HANDLE16 handle )
2318 FIND_FIRST_INFO *info;
2320 if ((handle == INVALID_HANDLE_VALUE16) ||
2321 !(info = (FIND_FIRST_INFO *)GlobalLock16( handle )))
2323 SetLastError( ERROR_INVALID_HANDLE );
2324 return FALSE;
2326 if (info->dir) DOSFS_CloseDir( info->dir );
2327 if (info->path) HeapFree( SystemHeap, 0, info->path );
2328 GlobalUnlock16( handle );
2329 GlobalFree16( handle );
2330 return TRUE;