- Correct RBN_CHILDSIZE rect value for CCS_VERT rebar.
[wine] / files / dos_fs.c
1 /*
2  * DOS file system functions
3  *
4  * Copyright 1993 Erik Bos
5  * Copyright 1996 Alexandre Julliard
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23
24 #include <sys/types.h>
25 #include <ctype.h>
26 #include <dirent.h>
27 #include <errno.h>
28 #ifdef HAVE_SYS_ERRNO_H
29 #include <sys/errno.h>
30 #endif
31 #include <fcntl.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <sys/stat.h>
35 #include <sys/ioctl.h>
36 #include <time.h>
37 #include <unistd.h>
38
39 #include "windef.h"
40 #include "winerror.h"
41 #include "wingdi.h"
42
43 #include "wine/unicode.h"
44 #include "wine/winbase16.h"
45 #include "drive.h"
46 #include "file.h"
47 #include "heap.h"
48 #include "msdos.h"
49 #include "ntddk.h"
50 #include "options.h"
51 #include "wine/server.h"
52 #include "msvcrt/excpt.h"
53
54 #include "wine/debug.h"
55
56 WINE_DEFAULT_DEBUG_CHANNEL(dosfs);
57 WINE_DECLARE_DEBUG_CHANNEL(file);
58
59 /* Define the VFAT ioctl to get both short and long file names */
60 /* FIXME: is it possible to get this to work on other systems? */
61 #ifdef linux
62 /* We want the real kernel dirent structure, not the libc one */
63 typedef struct
64 {
65     long d_ino;
66     long d_off;
67     unsigned short d_reclen;
68     char d_name[256];
69 } KERNEL_DIRENT;
70
71 #define VFAT_IOCTL_READDIR_BOTH  _IOR('r', 1, KERNEL_DIRENT [2] )
72
73 #else   /* linux */
74 #undef VFAT_IOCTL_READDIR_BOTH  /* just in case... */
75 #endif  /* linux */
76
77 /* Chars we don't want to see in DOS file names */
78 #define INVALID_DOS_CHARS  "*?<>|\"+=,;[] \345"
79
80 static const DOS_DEVICE DOSFS_Devices[] =
81 /* name, device flags (see Int 21/AX=0x4400) */
82 {
83     { "CON",            0xc0d3 },
84     { "PRN",            0xa0c0 },
85     { "NUL",            0x80c4 },
86     { "AUX",            0x80c0 },
87     { "LPT1",           0xa0c0 },
88     { "LPT2",           0xa0c0 },
89     { "LPT3",           0xa0c0 },
90     { "LPT4",           0xc0d3 },
91     { "COM1",           0x80c0 },
92     { "COM2",           0x80c0 },
93     { "COM3",           0x80c0 },
94     { "COM4",           0x80c0 },
95     { "SCSIMGR$",       0xc0c0 },
96     { "HPSCAN",         0xc0c0 },
97     { "EMMXXXX0",       0x0000 }
98 };
99
100 #define GET_DRIVE(path) \
101     (((path)[1] == ':') ? FILE_toupper((path)[0]) - 'A' : DOSFS_CurDrive)
102
103 /* Directory info for DOSFS_ReadDir */
104 typedef struct
105 {
106     DIR           *dir;
107 #ifdef VFAT_IOCTL_READDIR_BOTH
108     int            fd;
109     char           short_name[12];
110     KERNEL_DIRENT  dirent[2];
111 #endif
112 } DOS_DIR;
113
114 /* Info structure for FindFirstFile handle */
115 typedef struct
116 {
117     LPSTR path;
118     LPSTR long_mask;
119     LPSTR short_mask;
120     BYTE  attr;
121     int   drive;
122     int   cur_pos;
123     DOS_DIR *dir;
124 } FIND_FIRST_INFO;
125
126
127 static WINE_EXCEPTION_FILTER(page_fault)
128 {
129     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
130         return EXCEPTION_EXECUTE_HANDLER;
131     return EXCEPTION_CONTINUE_SEARCH;
132 }
133
134
135 /***********************************************************************
136  *           DOSFS_ValidDOSName
137  *
138  * Return 1 if Unix file 'name' is also a valid MS-DOS name
139  * (i.e. contains only valid DOS chars, lower-case only, fits in 8.3 format).
140  * File name can be terminated by '\0', '\\' or '/'.
141  */
142 static int DOSFS_ValidDOSName( const char *name, int ignore_case )
143 {
144     static const char invalid_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" INVALID_DOS_CHARS;
145     const char *p = name;
146     const char *invalid = ignore_case ? (invalid_chars + 26) : invalid_chars;
147     int len = 0;
148
149     if (*p == '.')
150     {
151         /* Check for "." and ".." */
152         p++;
153         if (*p == '.') p++;
154         /* All other names beginning with '.' are invalid */
155         return (IS_END_OF_NAME(*p));
156     }
157     while (!IS_END_OF_NAME(*p))
158     {
159         if (strchr( invalid, *p )) return 0;  /* Invalid char */
160         if (*p == '.') break;  /* Start of the extension */
161         if (++len > 8) return 0;  /* Name too long */
162         p++;
163     }
164     if (*p != '.') return 1;  /* End of name */
165     p++;
166     if (IS_END_OF_NAME(*p)) return 0;  /* Empty extension not allowed */
167     len = 0;
168     while (!IS_END_OF_NAME(*p))
169     {
170         if (strchr( invalid, *p )) return 0;  /* Invalid char */
171         if (*p == '.') return 0;  /* Second extension not allowed */
172         if (++len > 3) return 0;  /* Extension too long */
173         p++;
174     }
175     return 1;
176 }
177
178
179 /***********************************************************************
180  *           DOSFS_ToDosFCBFormat
181  *
182  * Convert a file name to DOS FCB format (8+3 chars, padded with blanks),
183  * expanding wild cards and converting to upper-case in the process.
184  * File name can be terminated by '\0', '\\' or '/'.
185  * Return FALSE if the name is not a valid DOS name.
186  * 'buffer' must be at least 12 characters long.
187  */
188 BOOL DOSFS_ToDosFCBFormat( LPCSTR name, LPSTR buffer )
189 {
190     static const char invalid_chars[] = INVALID_DOS_CHARS;
191     const char *p = name;
192     int i;
193
194     /* Check for "." and ".." */
195     if (*p == '.')
196     {
197         p++;
198         strcpy( buffer, ".          " );
199         if (*p == '.')
200         {
201             buffer[1] = '.';
202             p++;
203         }
204         return (!*p || (*p == '/') || (*p == '\\'));
205     }
206
207     for (i = 0; i < 8; i++)
208     {
209         switch(*p)
210         {
211         case '\0':
212         case '\\':
213         case '/':
214         case '.':
215             buffer[i] = ' ';
216             break;
217         case '?':
218             p++;
219             /* fall through */
220         case '*':
221             buffer[i] = '?';
222             break;
223         default:
224             if (strchr( invalid_chars, *p )) return FALSE;
225             buffer[i] = FILE_toupper(*p);
226             p++;
227             break;
228         }
229     }
230
231     if (*p == '*')
232     {
233         /* Skip all chars after wildcard up to first dot */
234         while (*p && (*p != '/') && (*p != '\\') && (*p != '.')) p++;
235     }
236     else
237     {
238         /* Check if name too long */
239         if (*p && (*p != '/') && (*p != '\\') && (*p != '.')) return FALSE;
240     }
241     if (*p == '.') p++;  /* Skip dot */
242
243     for (i = 8; i < 11; i++)
244     {
245         switch(*p)
246         {
247         case '\0':
248         case '\\':
249         case '/':
250             buffer[i] = ' ';
251             break;
252         case '.':
253             return FALSE;  /* Second extension not allowed */
254         case '?':
255             p++;
256             /* fall through */
257         case '*':
258             buffer[i] = '?';
259             break;
260         default:
261             if (strchr( invalid_chars, *p )) return FALSE;
262             buffer[i] = FILE_toupper(*p);
263             p++;
264             break;
265         }
266     }
267     buffer[11] = '\0';
268
269     /* at most 3 character of the extension are processed
270      * is something behind this ? 
271      */
272     while (*p == '*' || *p == ' ') p++; /* skip wildcards and spaces */
273     return IS_END_OF_NAME(*p);
274 }
275
276
277 /***********************************************************************
278  *           DOSFS_ToDosDTAFormat
279  *
280  * Convert a file name from FCB to DTA format (name.ext, null-terminated)
281  * converting to upper-case in the process.
282  * File name can be terminated by '\0', '\\' or '/'.
283  * 'buffer' must be at least 13 characters long.
284  */
285 static void DOSFS_ToDosDTAFormat( LPCSTR name, LPSTR buffer )
286 {
287     char *p;
288
289     memcpy( buffer, name, 8 );
290     p = buffer + 8;
291     while ((p > buffer) && (p[-1] == ' ')) p--;
292     *p++ = '.';
293     memcpy( p, name + 8, 3 );
294     p += 3;
295     while (p[-1] == ' ') p--;
296     if (p[-1] == '.') p--;
297     *p = '\0';
298 }
299
300
301 /***********************************************************************
302  *           DOSFS_MatchShort
303  *
304  * Check a DOS file name against a mask (both in FCB format).
305  */
306 static int DOSFS_MatchShort( const char *mask, const char *name )
307 {
308     int i;
309     for (i = 11; i > 0; i--, mask++, name++)
310         if ((*mask != '?') && (*mask != *name)) return 0;
311     return 1;
312 }
313
314
315 /***********************************************************************
316  *           DOSFS_MatchLong
317  *
318  * Check a long file name against a mask.
319  *
320  * Tests (done in W95 DOS shell - case insensitive):
321  * *.txt                        test1.test.txt                          *
322  * *st1*                        test1.txt                               *
323  * *.t??????.t*                 test1.ta.tornado.txt                    *
324  * *tornado*                    test1.ta.tornado.txt                    *
325  * t*t                          test1.ta.tornado.txt                    *
326  * ?est*                        test1.txt                               *
327  * ?est???                      test1.txt                               -
328  * *test1.txt*                  test1.txt                               * 
329  * h?l?o*t.dat                  hellothisisatest.dat                    *
330  */
331 static int DOSFS_MatchLong( const char *mask, const char *name,
332                             int case_sensitive )
333 {
334     const char *lastjoker = NULL;
335     const char *next_to_retry = NULL;
336
337     if (!strcmp( mask, "*.*" )) return 1;
338     while (*name && *mask)
339     {
340         if (*mask == '*')
341         {
342             mask++;
343             while (*mask == '*') mask++;  /* Skip consecutive '*' */
344             lastjoker = mask;
345             if (!*mask) return 1; /* end of mask is all '*', so match */
346
347             /* skip to the next match after the joker(s) */
348             if (case_sensitive) while (*name && (*name != *mask)) name++;
349             else while (*name && (FILE_toupper(*name) != FILE_toupper(*mask))) name++;
350
351             if (!*name) break;
352             next_to_retry = name;
353         }
354         else if (*mask != '?')
355         {
356             int mismatch = 0;
357             if (case_sensitive)
358             {
359                 if (*mask != *name) mismatch = 1;
360             }
361             else
362             {
363                 if (FILE_toupper(*mask) != FILE_toupper(*name)) mismatch = 1;
364             }
365             if (!mismatch)
366             {
367                 mask++;
368                 name++;
369                 if (*mask == '\0')
370                 {
371                     if (*name == '\0')
372                         return 1;
373                     if (lastjoker)
374                         mask = lastjoker;
375                 }
376             }
377             else /* mismatch ! */
378             {
379                 if (lastjoker) /* we had an '*', so we can try unlimitedly */
380                 {
381                     mask = lastjoker;
382
383                     /* this scan sequence was a mismatch, so restart
384                      * 1 char after the first char we checked last time */
385                     next_to_retry++;
386                     name = next_to_retry;
387                 }
388                 else
389                     return 0; /* bad luck */
390             }
391         }
392         else /* '?' */
393         {
394             mask++;
395             name++;
396         }
397     }
398     while ((*mask == '.') || (*mask == '*'))
399         mask++;  /* Ignore trailing '.' or '*' in mask */
400     return (!*name && !*mask);
401 }
402
403
404 /***********************************************************************
405  *           DOSFS_OpenDir
406  */
407 static DOS_DIR *DOSFS_OpenDir( LPCSTR path )
408 {
409     DOS_DIR *dir = HeapAlloc( GetProcessHeap(), 0, sizeof(*dir) );
410     if (!dir)
411     {
412         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
413         return NULL;
414     }
415
416     /* Treat empty path as root directory. This simplifies path split into
417        directory and mask in several other places */
418     if (!*path) path = "/";
419
420 #ifdef VFAT_IOCTL_READDIR_BOTH
421
422     /* Check if the VFAT ioctl is supported on this directory */
423
424     if ((dir->fd = open( path, O_RDONLY )) != -1)
425     {
426         if (ioctl( dir->fd, VFAT_IOCTL_READDIR_BOTH, (long)dir->dirent ) == -1)
427         {
428             close( dir->fd );
429             dir->fd = -1;
430         }
431         else
432         {
433             /* Set the file pointer back at the start of the directory */
434             lseek( dir->fd, 0, SEEK_SET );
435             dir->dir = NULL;
436             return dir;
437         }
438     }
439 #endif  /* VFAT_IOCTL_READDIR_BOTH */
440
441     /* Now use the standard opendir/readdir interface */
442
443     if (!(dir->dir = opendir( path )))
444     {
445         HeapFree( GetProcessHeap(), 0, dir );
446         return NULL;
447     }
448     return dir;
449 }
450
451
452 /***********************************************************************
453  *           DOSFS_CloseDir
454  */
455 static void DOSFS_CloseDir( DOS_DIR *dir )
456 {
457 #ifdef VFAT_IOCTL_READDIR_BOTH
458     if (dir->fd != -1) close( dir->fd );
459 #endif  /* VFAT_IOCTL_READDIR_BOTH */
460     if (dir->dir) closedir( dir->dir );
461     HeapFree( GetProcessHeap(), 0, dir );
462 }
463
464
465 /***********************************************************************
466  *           DOSFS_ReadDir
467  */
468 static BOOL DOSFS_ReadDir( DOS_DIR *dir, LPCSTR *long_name,
469                              LPCSTR *short_name )
470 {
471     struct dirent *dirent;
472
473 #ifdef VFAT_IOCTL_READDIR_BOTH
474     if (dir->fd != -1)
475     {
476         if (ioctl( dir->fd, VFAT_IOCTL_READDIR_BOTH, (long)dir->dirent ) != -1) {
477             if (!dir->dirent[0].d_reclen) return FALSE;
478             if (!DOSFS_ToDosFCBFormat( dir->dirent[0].d_name, dir->short_name ))
479                 dir->short_name[0] = '\0';
480             *short_name = dir->short_name;
481             if (dir->dirent[1].d_name[0]) *long_name = dir->dirent[1].d_name;
482             else *long_name = dir->dirent[0].d_name;
483             return TRUE;
484         }
485     }
486 #endif  /* VFAT_IOCTL_READDIR_BOTH */
487
488     if (!(dirent = readdir( dir->dir ))) return FALSE;
489     *long_name  = dirent->d_name;
490     *short_name = NULL;
491     return TRUE;
492 }
493
494
495 /***********************************************************************
496  *           DOSFS_Hash
497  *
498  * Transform a Unix file name into a hashed DOS name. If the name is a valid
499  * DOS name, it is converted to upper-case; otherwise it is replaced by a
500  * hashed version that fits in 8.3 format.
501  * File name can be terminated by '\0', '\\' or '/'.
502  * 'buffer' must be at least 13 characters long.
503  */
504 static void DOSFS_Hash( LPCSTR name, LPSTR buffer, BOOL dir_format,
505                         BOOL ignore_case )
506 {
507     static const char invalid_chars[] = INVALID_DOS_CHARS "~.";
508     static const char hash_chars[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
509
510     const char *p, *ext;
511     char *dst;
512     unsigned short hash;
513     int i;
514
515     if (dir_format) strcpy( buffer, "           " );
516
517     if (DOSFS_ValidDOSName( name, ignore_case ))
518     {
519         /* Check for '.' and '..' */
520         if (*name == '.')
521         {
522             buffer[0] = '.';
523             if (!dir_format) buffer[1] = buffer[2] = '\0';
524             if (name[1] == '.') buffer[1] = '.';
525             return;
526         }
527
528         /* Simply copy the name, converting to uppercase */
529
530         for (dst = buffer; !IS_END_OF_NAME(*name) && (*name != '.'); name++)
531             *dst++ = FILE_toupper(*name);
532         if (*name == '.')
533         {
534             if (dir_format) dst = buffer + 8;
535             else *dst++ = '.';
536             for (name++; !IS_END_OF_NAME(*name); name++)
537                 *dst++ = FILE_toupper(*name);
538         }
539         if (!dir_format) *dst = '\0';
540         return;
541     }
542
543     /* Compute the hash code of the file name */
544     /* If you know something about hash functions, feel free to */
545     /* insert a better algorithm here... */
546     if (ignore_case)
547     {
548         for (p = name, hash = 0xbeef; !IS_END_OF_NAME(p[1]); p++)
549             hash = (hash<<3) ^ (hash>>5) ^ FILE_tolower(*p) ^ (FILE_tolower(p[1]) << 8);
550         hash = (hash<<3) ^ (hash>>5) ^ FILE_tolower(*p); /* Last character*/
551     }
552     else
553     {
554         for (p = name, hash = 0xbeef; !IS_END_OF_NAME(p[1]); p++)
555             hash = (hash << 3) ^ (hash >> 5) ^ *p ^ (p[1] << 8);
556         hash = (hash << 3) ^ (hash >> 5) ^ *p;  /* Last character */
557     }
558
559     /* Find last dot for start of the extension */
560     for (p = name+1, ext = NULL; !IS_END_OF_NAME(*p); p++)
561         if (*p == '.') ext = p;
562     if (ext && IS_END_OF_NAME(ext[1]))
563         ext = NULL;  /* Empty extension ignored */
564
565     /* Copy first 4 chars, replacing invalid chars with '_' */
566     for (i = 4, p = name, dst = buffer; i > 0; i--, p++)
567     {
568         if (IS_END_OF_NAME(*p) || (p == ext)) break;
569         *dst++ = strchr( invalid_chars, *p ) ? '_' : FILE_toupper(*p);
570     }
571     /* Pad to 5 chars with '~' */
572     while (i-- >= 0) *dst++ = '~';
573
574     /* Insert hash code converted to 3 ASCII chars */
575     *dst++ = hash_chars[(hash >> 10) & 0x1f];
576     *dst++ = hash_chars[(hash >> 5) & 0x1f];
577     *dst++ = hash_chars[hash & 0x1f];
578
579     /* Copy the first 3 chars of the extension (if any) */
580     if (ext)
581     {
582         if (!dir_format) *dst++ = '.';
583         for (i = 3, ext++; (i > 0) && !IS_END_OF_NAME(*ext); i--, ext++)
584             *dst++ = strchr( invalid_chars, *ext ) ? '_' : FILE_toupper(*ext);
585     }
586     if (!dir_format) *dst = '\0';
587 }
588
589
590 /***********************************************************************
591  *           DOSFS_FindUnixName
592  *
593  * Find the Unix file name in a given directory that corresponds to
594  * a file name (either in Unix or DOS format).
595  * File name can be terminated by '\0', '\\' or '/'.
596  * Return TRUE if OK, FALSE if no file name matches.
597  *
598  * 'long_buf' must be at least 'long_len' characters long. If the long name
599  * turns out to be larger than that, the function returns FALSE.
600  * 'short_buf' must be at least 13 characters long.
601  */
602 BOOL DOSFS_FindUnixName( LPCSTR path, LPCSTR name, LPSTR long_buf,
603                            INT long_len, LPSTR short_buf, BOOL ignore_case)
604 {
605     DOS_DIR *dir;
606     LPCSTR long_name, short_name;
607     char dos_name[12], tmp_buf[13];
608     BOOL ret;
609
610     const char *p = strchr( name, '/' );
611     int len = p ? (int)(p - name) : strlen(name);
612     if ((p = strchr( name, '\\' ))) len = min( (int)(p - name), len );
613     /* Ignore trailing dots and spaces */
614     while (len > 1 && (name[len-1] == '.' || name[len-1] == ' ')) len--;
615     if (long_len < len + 1) return FALSE;
616
617     TRACE("%s,%s\n", path, name );
618
619     if (!DOSFS_ToDosFCBFormat( name, dos_name )) dos_name[0] = '\0';
620
621     if (!(dir = DOSFS_OpenDir( path )))
622     {
623         WARN("(%s,%s): can't open dir: %s\n",
624                        path, name, strerror(errno) );
625         return FALSE;
626     }
627
628     while ((ret = DOSFS_ReadDir( dir, &long_name, &short_name )))
629     {
630         /* Check against Unix name */
631         if (len == strlen(long_name))
632         {
633             if (!ignore_case)
634             {
635                 if (!strncmp( long_name, name, len )) break;
636             }
637             else
638             {
639                 if (!FILE_strncasecmp( long_name, name, len )) break;
640             }
641         }
642         if (dos_name[0])
643         {
644             /* Check against hashed DOS name */
645             if (!short_name)
646             {
647                 DOSFS_Hash( long_name, tmp_buf, TRUE, ignore_case );
648                 short_name = tmp_buf;
649             }
650             if (!strcmp( dos_name, short_name )) break;
651         }
652     }
653     if (ret)
654     {
655         if (long_buf) strcpy( long_buf, long_name );
656         if (short_buf)
657         {
658             if (short_name)
659                 DOSFS_ToDosDTAFormat( short_name, short_buf );
660             else
661                 DOSFS_Hash( long_name, short_buf, FALSE, ignore_case );
662         }
663         TRACE("(%s,%s) -> %s (%s)\n",
664               path, name, long_name, short_buf ? short_buf : "***");
665     }
666     else
667         WARN("'%s' not found in '%s'\n", name, path);
668     DOSFS_CloseDir( dir );
669     return ret;
670 }
671
672
673 /***********************************************************************
674  *           DOSFS_GetDevice
675  *
676  * Check if a DOS file name represents a DOS device and return the device.
677  */
678 const DOS_DEVICE *DOSFS_GetDevice( const char *name )
679 {
680     int i;
681     const char *p;
682
683     if (!name) return NULL; /* if FILE_DupUnixHandle was used */
684     if (name[0] && (name[1] == ':')) name += 2;
685     if ((p = strrchr( name, '/' ))) name = p + 1;
686     if ((p = strrchr( name, '\\' ))) name = p + 1;
687     for (i = 0; i < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0]); i++)
688     {
689         const char *dev = DOSFS_Devices[i].name;
690         if (!FILE_strncasecmp( dev, name, strlen(dev) ))
691         {
692             p = name + strlen( dev );
693             if (!*p || (*p == '.') || (*p == ':')) return &DOSFS_Devices[i];
694         }
695     }
696     return NULL;
697 }
698
699
700 /***********************************************************************
701  *           DOSFS_GetDeviceByHandle
702  */
703 const DOS_DEVICE *DOSFS_GetDeviceByHandle( HFILE hFile )
704 {
705     const DOS_DEVICE *ret = NULL;
706     SERVER_START_REQ( get_file_info )
707     {
708         req->handle = hFile;
709         if (!wine_server_call( req ) && (reply->type == FILE_TYPE_UNKNOWN))
710         {
711             if ((reply->attr >= 0) &&
712                 (reply->attr < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0])))
713                 ret = &DOSFS_Devices[reply->attr];
714         }
715     }
716     SERVER_END_REQ;
717     return ret;
718 }
719
720
721 /**************************************************************************
722  *         DOSFS_CreateCommPort
723  */
724 static HANDLE DOSFS_CreateCommPort(LPCSTR name, DWORD access, DWORD attributes, LPSECURITY_ATTRIBUTES sa)
725 {
726     HANDLE ret;
727     char devname[40];
728
729     TRACE_(file)("%s %lx %lx\n", name, access, attributes);
730
731     PROFILE_GetWineIniString("serialports",name,"",devname,sizeof devname);
732     if(!devname[0])
733         return 0;
734
735     TRACE("opening %s as %s\n", devname, name);
736
737     SERVER_START_REQ( create_serial )
738     {
739         req->access  = access;
740         req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
741         req->attributes = attributes;
742         req->sharing = FILE_SHARE_READ|FILE_SHARE_WRITE;
743         wine_server_add_data( req, devname, strlen(devname) );
744         SetLastError(0);
745         wine_server_call_err( req );
746         ret = reply->handle;
747     }
748     SERVER_END_REQ;
749
750     if(!ret)
751         ERR("Couldn't open device '%s' ! (check permissions)\n",devname);
752     else
753         TRACE("return %08X\n", ret );
754     return ret;
755 }
756
757 /***********************************************************************
758  *           DOSFS_OpenDevice
759  *
760  * Open a DOS device. This might not map 1:1 into the UNIX device concept.
761  * Returns 0 on failure.
762  */
763 HANDLE DOSFS_OpenDevice( const char *name, DWORD access, DWORD attributes, LPSECURITY_ATTRIBUTES sa )
764 {
765     int i;
766     const char *p;
767     HANDLE handle;
768
769     if (name[0] && (name[1] == ':')) name += 2;
770     if ((p = strrchr( name, '/' ))) name = p + 1;
771     if ((p = strrchr( name, '\\' ))) name = p + 1;
772     for (i = 0; i < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0]); i++)
773     {
774         const char *dev = DOSFS_Devices[i].name;
775         if (!FILE_strncasecmp( dev, name, strlen(dev) ))
776         {
777             p = name + strlen( dev );
778             if (!*p || (*p == '.') || (*p == ':')) {
779                 /* got it */
780                 if (!strcmp(DOSFS_Devices[i].name,"NUL"))
781                     return FILE_CreateFile( "/dev/null", access,
782                                             FILE_SHARE_READ|FILE_SHARE_WRITE, sa,
783                                             OPEN_EXISTING, 0, 0, TRUE, DRIVE_UNKNOWN );
784                 if (!strcmp(DOSFS_Devices[i].name,"CON")) {
785                         HANDLE to_dup;
786                         switch (access & (GENERIC_READ|GENERIC_WRITE)) {
787                         case GENERIC_READ:
788                                 to_dup = GetStdHandle( STD_INPUT_HANDLE );
789                                 break;
790                         case GENERIC_WRITE:
791                                 to_dup = GetStdHandle( STD_OUTPUT_HANDLE );
792                                 break;
793                         default:
794                                 FIXME("can't open CON read/write\n");
795                                 return 0;
796                         }
797                         if (!DuplicateHandle( GetCurrentProcess(), to_dup, GetCurrentProcess(),
798                                               &handle, 0, 
799                                               sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle, 
800                                               DUPLICATE_SAME_ACCESS ))
801                             handle = 0;
802                         return handle;
803                 }
804                 if (!strcmp(DOSFS_Devices[i].name,"SCSIMGR$") ||
805                     !strcmp(DOSFS_Devices[i].name,"HPSCAN") ||
806                     !strcmp(DOSFS_Devices[i].name,"EMMXXXX0"))
807                 {
808                     return FILE_CreateDevice( i, access, sa );
809                 }
810
811                 if( (handle=DOSFS_CreateCommPort(DOSFS_Devices[i].name,access,attributes,sa)) )
812                     return handle;
813                 FIXME("device open %s not supported (yet)\n",DOSFS_Devices[i].name);
814                 return 0;
815             }
816         }
817     }
818     return 0;
819 }
820
821
822 /***********************************************************************
823  *           DOSFS_GetPathDrive
824  *
825  * Get the drive specified by a given path name (DOS or Unix format).
826  */
827 static int DOSFS_GetPathDrive( const char **name )
828 {
829     int drive;
830     const char *p = *name;
831
832     if (*p && (p[1] == ':'))
833     {
834         drive = FILE_toupper(*p) - 'A';
835         *name += 2;
836     }
837     else if (*p == '/') /* Absolute Unix path? */
838     {
839         if ((drive = DRIVE_FindDriveRoot( name )) == -1)
840         {
841             MESSAGE("Warning: %s not accessible from a configured DOS drive\n", *name );
842             /* Assume it really was a DOS name */
843             drive = DRIVE_GetCurrentDrive();            
844         }
845     }
846     else drive = DRIVE_GetCurrentDrive();
847
848     if (!DRIVE_IsValid(drive))
849     {
850         SetLastError( ERROR_INVALID_DRIVE );
851         return -1;
852     }
853     return drive;
854 }
855
856
857 /***********************************************************************
858  *           DOSFS_GetFullName
859  *
860  * Convert a file name (DOS or mixed DOS/Unix format) to a valid
861  * Unix name / short DOS name pair.
862  * Return FALSE if one of the path components does not exist. The last path
863  * component is only checked if 'check_last' is non-zero.
864  * The buffers pointed to by 'long_buf' and 'short_buf' must be
865  * at least MAX_PATHNAME_LEN long.
866  */
867 BOOL DOSFS_GetFullName( LPCSTR name, BOOL check_last, DOS_FULL_NAME *full )
868 {
869     BOOL found;
870     UINT flags;
871     char *p_l, *p_s, *root;
872
873     TRACE("%s (last=%d)\n", name, check_last );
874
875     if ((!*name) || (*name=='\n'))
876     { /* error code for Win98 */
877         SetLastError(ERROR_BAD_PATHNAME);
878         return FALSE;
879     }
880
881     if ((full->drive = DOSFS_GetPathDrive( &name )) == -1) return FALSE;
882     flags = DRIVE_GetFlags( full->drive );
883
884     lstrcpynA( full->long_name, DRIVE_GetRoot( full->drive ),
885                  sizeof(full->long_name) );
886     if (full->long_name[1]) root = full->long_name + strlen(full->long_name);
887     else root = full->long_name;  /* root directory */
888
889     strcpy( full->short_name, "A:\\" );
890     full->short_name[0] += full->drive;
891
892     if ((*name == '\\') || (*name == '/'))  /* Absolute path */
893     {
894         while ((*name == '\\') || (*name == '/')) name++;
895     }
896     else  /* Relative path */
897     {
898         lstrcpynA( root + 1, DRIVE_GetUnixCwd( full->drive ),
899                      sizeof(full->long_name) - (root - full->long_name) - 1 );
900         if (root[1]) *root = '/';
901         lstrcpynA( full->short_name + 3, DRIVE_GetDosCwd( full->drive ),
902                      sizeof(full->short_name) - 3 );
903     }
904
905     p_l = full->long_name[1] ? full->long_name + strlen(full->long_name)
906                              : full->long_name;
907     p_s = full->short_name[3] ? full->short_name + strlen(full->short_name)
908                               : full->short_name + 2;
909     found = TRUE;
910
911     while (*name && found)
912     {
913         /* Check for '.' and '..' */
914
915         if (*name == '.')
916         {
917             if (IS_END_OF_NAME(name[1]))
918             {
919                 name++;
920                 while ((*name == '\\') || (*name == '/')) name++;
921                 continue;
922             }
923             else if ((name[1] == '.') && IS_END_OF_NAME(name[2]))
924             {
925                 name += 2;
926                 while ((*name == '\\') || (*name == '/')) name++;
927                 while ((p_l > root) && (*p_l != '/')) p_l--;
928                 while ((p_s > full->short_name + 2) && (*p_s != '\\')) p_s--;
929                 *p_l = *p_s = '\0';  /* Remove trailing separator */
930                 continue;
931             }
932         }
933
934         /* Make sure buffers are large enough */
935
936         if ((p_s >= full->short_name + sizeof(full->short_name) - 14) ||
937             (p_l >= full->long_name + sizeof(full->long_name) - 1))
938         {
939             SetLastError( ERROR_PATH_NOT_FOUND );
940             return FALSE;
941         }
942
943         /* Get the long and short name matching the file name */
944
945         if ((found = DOSFS_FindUnixName( full->long_name, name, p_l + 1,
946                          sizeof(full->long_name) - (p_l - full->long_name) - 1,
947                          p_s + 1, !(flags & DRIVE_CASE_SENSITIVE) )))
948         {
949             *p_l++ = '/';
950             p_l   += strlen(p_l);
951             *p_s++ = '\\';
952             p_s   += strlen(p_s);
953             while (!IS_END_OF_NAME(*name)) name++;
954         }
955         else if (!check_last)
956         {
957             *p_l++ = '/';
958             *p_s++ = '\\';
959             while (!IS_END_OF_NAME(*name) &&
960                    (p_s < full->short_name + sizeof(full->short_name) - 1) &&
961                    (p_l < full->long_name + sizeof(full->long_name) - 1))
962             {
963                 *p_s++ = FILE_tolower(*name);
964                 /* If the drive is case-sensitive we want to create new */
965                 /* files in lower-case otherwise we can't reopen them   */
966                 /* under the same short name. */
967                 if (flags & DRIVE_CASE_SENSITIVE) *p_l++ = FILE_tolower(*name);
968                 else *p_l++ = *name;
969                 name++;
970             }
971             /* Ignore trailing dots and spaces */
972             while(p_l[-1] == '.' || p_l[-1] == ' ') {
973                 --p_l;
974                 --p_s;
975             }
976             *p_l = *p_s = '\0';
977         }
978         while ((*name == '\\') || (*name == '/')) name++;
979     }
980
981     if (!found)
982     {
983         if (check_last)
984         {
985             SetLastError( ERROR_FILE_NOT_FOUND );
986             return FALSE;
987         }
988         if (*name)  /* Not last */
989         {
990             SetLastError( ERROR_PATH_NOT_FOUND );
991             return FALSE;
992         }
993     }
994     if (!full->long_name[0]) strcpy( full->long_name, "/" );
995     if (!full->short_name[2]) strcpy( full->short_name + 2, "\\" );
996     TRACE("returning %s = %s\n", full->long_name, full->short_name );
997     return TRUE;
998 }
999
1000
1001 /***********************************************************************
1002  *           GetShortPathNameA   (KERNEL32.@)
1003  *
1004  * NOTES
1005  *  observed:
1006  *  longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
1007  *  longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
1008  * 
1009  * more observations ( with NT 3.51 (WinDD) ):
1010  * longpath <= 8.3 -> just copy longpath to shortpath
1011  * longpath > 8.3  -> 
1012  *             a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
1013  *             b) file does exist     -> set the short filename.
1014  * - trailing slashes are reproduced in the short name, even if the
1015  *   file is not a directory
1016  * - the absolute/relative path of the short name is reproduced like found
1017  *   in the long name
1018  * - longpath and shortpath may have the same address
1019  * Peter Ganten, 1999
1020  */
1021 DWORD WINAPI GetShortPathNameA( LPCSTR longpath, LPSTR shortpath,
1022                                   DWORD shortlen )
1023 {
1024     DOS_FULL_NAME full_name;
1025     LPSTR tmpshortpath;
1026     DWORD sp = 0, lp = 0;
1027     int tmplen, drive;
1028     UINT flags;
1029
1030     TRACE("%s\n", debugstr_a(longpath));
1031
1032     if (!longpath) {
1033       SetLastError(ERROR_INVALID_PARAMETER);
1034       return 0;
1035     }
1036     if (!longpath[0]) {
1037       SetLastError(ERROR_BAD_PATHNAME);
1038       return 0;
1039     }
1040
1041     if ( ( tmpshortpath = HeapAlloc ( GetProcessHeap(), 0, MAX_PATHNAME_LEN ) ) == NULL ) {
1042       SetLastError ( ERROR_NOT_ENOUGH_MEMORY );
1043       return 0;
1044     }
1045
1046     /* check for drive letter */
1047     if ( longpath[1] == ':' ) {
1048       tmpshortpath[0] = longpath[0];
1049       tmpshortpath[1] = ':';
1050       sp = 2;
1051     }
1052
1053     if ( ( drive = DOSFS_GetPathDrive ( &longpath )) == -1 ) return 0;
1054     flags = DRIVE_GetFlags ( drive );
1055
1056     while ( longpath[lp] ) {
1057
1058       /* check for path delimiters and reproduce them */
1059       if ( longpath[lp] == '\\' || longpath[lp] == '/' ) {
1060         if (!sp || tmpshortpath[sp-1]!= '\\') 
1061         {
1062             /* strip double "\\" */
1063             tmpshortpath[sp] = '\\';
1064             sp++;
1065         }
1066         tmpshortpath[sp]=0;/*terminate string*/
1067         lp++;
1068         continue;
1069       }
1070
1071       tmplen = strcspn ( longpath + lp, "\\/" ); 
1072       lstrcpynA ( tmpshortpath+sp, longpath + lp, tmplen+1 );
1073       
1074       /* Check, if the current element is a valid dos name */
1075       if ( DOSFS_ValidDOSName ( longpath + lp, !(flags & DRIVE_CASE_SENSITIVE) ) ) {
1076         sp += tmplen;
1077         lp += tmplen;
1078         continue;
1079       }
1080
1081       /* Check if the file exists and use the existing file name */
1082       if ( DOSFS_GetFullName ( tmpshortpath, TRUE, &full_name ) ) {
1083         strcpy( tmpshortpath+sp, strrchr ( full_name.short_name, '\\' ) + 1 );
1084         sp += strlen ( tmpshortpath+sp );
1085         lp += tmplen;
1086         continue;
1087       }
1088
1089       TRACE("not found!\n" );
1090       SetLastError ( ERROR_FILE_NOT_FOUND );
1091       return 0;
1092     }
1093     tmpshortpath[sp] = 0;
1094
1095     lstrcpynA ( shortpath, tmpshortpath, shortlen );
1096     TRACE("returning %s\n", debugstr_a(shortpath) );
1097     tmplen = strlen ( tmpshortpath );
1098     HeapFree ( GetProcessHeap(), 0, tmpshortpath );
1099     
1100     return tmplen;
1101 }
1102
1103
1104 /***********************************************************************
1105  *           GetShortPathNameW   (KERNEL32.@)
1106  */
1107 DWORD WINAPI GetShortPathNameW( LPCWSTR longpath, LPWSTR shortpath,
1108                                   DWORD shortlen )
1109 {
1110     LPSTR longpathA, shortpathA;
1111     DWORD ret = 0;
1112
1113     longpathA = HEAP_strdupWtoA( GetProcessHeap(), 0, longpath );
1114     shortpathA = HeapAlloc ( GetProcessHeap(), 0, shortlen );
1115
1116     ret = GetShortPathNameA ( longpathA, shortpathA, shortlen );
1117     if (shortlen > 0 && !MultiByteToWideChar( CP_ACP, 0, shortpathA, -1, shortpath, shortlen ))
1118         shortpath[shortlen-1] = 0;
1119     HeapFree( GetProcessHeap(), 0, longpathA );
1120     HeapFree( GetProcessHeap(), 0, shortpathA );
1121
1122     return ret;
1123 }
1124
1125
1126 /***********************************************************************
1127  *           GetLongPathNameA   (KERNEL32.@)
1128  *
1129  * NOTES
1130  *  observed (Win2000):
1131  *  shortpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
1132  *  shortpath="":   LastError=ERROR_PATH_NOT_FOUND, ret=0
1133  */
1134 DWORD WINAPI GetLongPathNameA( LPCSTR shortpath, LPSTR longpath,
1135                                   DWORD longlen )
1136 {
1137     DOS_FULL_NAME full_name;
1138     char *p, *r, *ll, *ss;
1139
1140     if (!shortpath) {
1141       SetLastError(ERROR_INVALID_PARAMETER);
1142       return 0;
1143     }
1144     if (!shortpath[0]) {
1145       SetLastError(ERROR_PATH_NOT_FOUND);
1146       return 0;
1147     }
1148
1149     if (!DOSFS_GetFullName( shortpath, TRUE, &full_name )) return 0;
1150     lstrcpynA( longpath, full_name.short_name, longlen );
1151
1152     /* Do some hackery to get the long filename. */
1153
1154     if (longpath) {
1155      ss=longpath+strlen(longpath);
1156      ll=full_name.long_name+strlen(full_name.long_name);
1157      p=NULL;
1158      while (ss>=longpath)
1159      {
1160        /* FIXME: aren't we more paranoid, than needed? */
1161        while ((ss[0]=='\\') && (ss>=longpath)) ss--;
1162        p=ss;
1163        while ((ss[0]!='\\') && (ss>=longpath)) ss--;
1164        if (ss>=longpath) 
1165          {
1166          /* FIXME: aren't we more paranoid, than needed? */
1167          while ((ll[0]=='/') && (ll>=full_name.long_name)) ll--;
1168          while ((ll[0]!='/') && (ll>=full_name.long_name)) ll--;
1169          if (ll<full_name.long_name) 
1170               { 
1171               ERR("Bad longname! (ss=%s ll=%s)\n This should never happen !\n"
1172                   ,ss ,ll ); 
1173               return 0;
1174               }
1175          }
1176      }
1177
1178    /* FIXME: fix for names like "C:\\" (ie. with more '\'s) */
1179       if (p && p[2]) 
1180         {
1181         p+=1;
1182         if ((p-longpath)>0) longlen -= (p-longpath);
1183         lstrcpynA( p, ll , longlen);
1184
1185         /* Now, change all '/' to '\' */
1186         for (r=p; r<(p+longlen); r++ ) 
1187           if (r[0]=='/') r[0]='\\';
1188         return strlen(longpath) - strlen(p) + longlen;
1189         }
1190     }
1191
1192     return strlen(longpath);
1193 }
1194
1195
1196 /***********************************************************************
1197  *           GetLongPathNameW   (KERNEL32.@)
1198  */
1199 DWORD WINAPI GetLongPathNameW( LPCWSTR shortpath, LPWSTR longpath,
1200                                   DWORD longlen )
1201 {
1202     DOS_FULL_NAME full_name;
1203     DWORD ret = 0;
1204     LPSTR shortpathA = HEAP_strdupWtoA( GetProcessHeap(), 0, shortpath );
1205
1206     /* FIXME: is it correct to always return a fully qualified short path? */
1207     if (DOSFS_GetFullName( shortpathA, TRUE, &full_name ))
1208     {
1209         ret = strlen( full_name.short_name );
1210         if (longlen > 0 && !MultiByteToWideChar( CP_ACP, 0, full_name.long_name, -1,
1211                                                  longpath, longlen ))
1212             longpath[longlen-1] = 0;
1213     }
1214     HeapFree( GetProcessHeap(), 0, shortpathA );
1215     return ret;
1216 }
1217
1218
1219 /***********************************************************************
1220  *           DOSFS_DoGetFullPathName
1221  *
1222  * Implementation of GetFullPathNameA/W.
1223  *
1224  * bon@elektron 000331:
1225  * A test for GetFullPathName with many pathological cases 
1226  * now gives identical output for Wine and OSR2
1227  */
1228 static DWORD DOSFS_DoGetFullPathName( LPCSTR name, DWORD len, LPSTR result,
1229                                       BOOL unicode )
1230 {
1231     DWORD ret;
1232     DOS_FULL_NAME full_name;
1233     char *p,*q;
1234     const char * root;
1235     char drivecur[]="c:.";
1236     char driveletter=0;
1237     int namelen,drive=0;
1238
1239     if ((strlen(name) >1)&& (name[1]==':'))
1240       /* drive letter given */
1241       {
1242         driveletter = name[0];
1243       }
1244     if ((strlen(name) >2)&& (name[1]==':') &&
1245              ((name[2]=='\\') || (name[2]=='/')))
1246       /* absolute path given */
1247       {
1248         lstrcpynA(full_name.short_name,name,MAX_PATHNAME_LEN);
1249         drive = (int)FILE_toupper(name[0]) - 'A';
1250       }
1251     else
1252       {
1253         if (driveletter)
1254           drivecur[0]=driveletter;
1255         else
1256           strcpy(drivecur,".");
1257         if (!DOSFS_GetFullName( drivecur, FALSE, &full_name ))
1258           {
1259             FIXME("internal: error getting drive/path\n");
1260             return 0;
1261           }
1262         /* find path that drive letter substitutes*/
1263         drive = (int)FILE_toupper(full_name.short_name[0]) -0x41;
1264         root= DRIVE_GetRoot(drive);
1265         if (!root)
1266           {
1267             FIXME("internal: error getting DOS Drive Root\n");
1268             return 0;
1269           }
1270         if (!strcmp(root,"/"))
1271           {
1272             /* we have just the last / and we need it. */
1273             p= full_name.long_name;
1274           }
1275         else
1276           {
1277             p= full_name.long_name +strlen(root);
1278           }
1279         /* append long name (= unix name) to drive */
1280         lstrcpynA(full_name.short_name+2,p,MAX_PATHNAME_LEN-3);
1281         /* append name to treat */
1282         namelen= strlen(full_name.short_name);
1283         p = (char*)name;
1284         if (driveletter)
1285           p += +2; /* skip drive name when appending */
1286         if (namelen +2  + strlen(p) > MAX_PATHNAME_LEN)
1287           {
1288             FIXME("internal error: buffer too small\n");
1289              return 0;
1290           }
1291         full_name.short_name[namelen++] ='\\';
1292         full_name.short_name[namelen] = 0;
1293         lstrcpynA(full_name.short_name +namelen,p,MAX_PATHNAME_LEN-namelen);
1294       }
1295     /* reverse all slashes */
1296     for (p=full_name.short_name;
1297          p < full_name.short_name+strlen(full_name.short_name);
1298          p++)
1299       {
1300         if ( *p == '/' )
1301           *p = '\\';
1302       }
1303      /* Use memmove, as areas overlap */
1304      /* Delete .. */
1305     while ((p = strstr(full_name.short_name,"\\..\\")))
1306       {
1307         if (p > full_name.short_name+2)
1308           {
1309             *p = 0;
1310             q = strrchr(full_name.short_name,'\\');
1311             memmove(q+1,p+4,strlen(p+4)+1);
1312           }
1313         else
1314           {
1315             memmove(full_name.short_name+3,p+4,strlen(p+4)+1);
1316           }
1317       }
1318     if ((full_name.short_name[2]=='.')&&(full_name.short_name[3]=='.'))
1319         {
1320           /* This case istn't treated yet : c:..\test */
1321           memmove(full_name.short_name+2,full_name.short_name+4,
1322                   strlen(full_name.short_name+4)+1);
1323         }
1324      /* Delete . */
1325     while ((p = strstr(full_name.short_name,"\\.\\")))
1326       {
1327         *(p+1) = 0;
1328         memmove(p+1,p+3,strlen(p+3)+1);
1329       }
1330     if (!(DRIVE_GetFlags(drive) & DRIVE_CASE_PRESERVING))
1331         for (p = full_name.short_name; *p; p++) *p = FILE_toupper(*p);
1332     namelen=strlen(full_name.short_name);
1333     if (!strcmp(full_name.short_name+namelen-3,"\\.."))
1334         {
1335           /* one more strange case: "c:\test\test1\.." 
1336            return "c:\test" */
1337           *(full_name.short_name+namelen-3)=0;
1338           q = strrchr(full_name.short_name,'\\');
1339           *q =0;
1340         }
1341     if (full_name.short_name[namelen-1]=='.')
1342         full_name.short_name[(namelen--)-1] =0;
1343     if (!driveletter)
1344       if (full_name.short_name[namelen-1]=='\\')
1345         full_name.short_name[(namelen--)-1] =0;
1346     TRACE("got %s\n",full_name.short_name);
1347
1348     /* If the lpBuffer buffer is too small, the return value is the 
1349     size of the buffer, in characters, required to hold the path 
1350     plus the terminating \0 (tested against win95osr2, bon 001118)
1351     . */
1352     ret = strlen(full_name.short_name);
1353     if (ret >= len )
1354       {
1355         /* don't touch anything when the buffer is not large enough */
1356         SetLastError( ERROR_INSUFFICIENT_BUFFER );
1357         return ret+1;
1358       }
1359     if (result)
1360     {
1361         if (unicode)
1362             MultiByteToWideChar( CP_ACP, 0, full_name.short_name, -1, (LPWSTR)result, len );
1363         else
1364             lstrcpynA( result, full_name.short_name, len );
1365     }
1366
1367     TRACE("returning '%s'\n", full_name.short_name );
1368     return ret;
1369 }
1370
1371
1372 /***********************************************************************
1373  *           GetFullPathNameA   (KERNEL32.@)
1374  * NOTES
1375  *   if the path closed with '\', *lastpart is 0 
1376  */
1377 DWORD WINAPI GetFullPathNameA( LPCSTR name, DWORD len, LPSTR buffer,
1378                                  LPSTR *lastpart )
1379 {
1380     DWORD ret = DOSFS_DoGetFullPathName( name, len, buffer, FALSE );
1381     if (ret && (ret<=len) && buffer && lastpart)
1382     {
1383         LPSTR p = buffer + strlen(buffer);
1384
1385         if (*p != '\\')
1386         {
1387           while ((p > buffer + 2) && (*p != '\\')) p--;
1388           *lastpart = p + 1;
1389         }
1390         else *lastpart = NULL;
1391     }
1392     return ret;
1393 }
1394
1395
1396 /***********************************************************************
1397  *           GetFullPathNameW   (KERNEL32.@)
1398  */
1399 DWORD WINAPI GetFullPathNameW( LPCWSTR name, DWORD len, LPWSTR buffer,
1400                                  LPWSTR *lastpart )
1401 {
1402     LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
1403     DWORD ret = DOSFS_DoGetFullPathName( nameA, len, (LPSTR)buffer, TRUE );
1404     HeapFree( GetProcessHeap(), 0, nameA );
1405     if (ret && (ret<=len) && buffer && lastpart)
1406     {
1407         LPWSTR p = buffer + strlenW(buffer);
1408         if (*p != (WCHAR)'\\')
1409         {
1410             while ((p > buffer + 2) && (*p != (WCHAR)'\\')) p--;
1411             *lastpart = p + 1;
1412         }
1413         else *lastpart = NULL;  
1414     }
1415     return ret;
1416 }
1417
1418
1419 /***********************************************************************
1420  *           wine_get_unix_file_name (KERNEL32.@) Not a Windows API
1421  *
1422  * Return the full Unix file name for a given path.
1423  */
1424 BOOL WINAPI wine_get_unix_file_name( LPCSTR dos, LPSTR buffer, DWORD len )
1425 {
1426     BOOL ret;
1427     DOS_FULL_NAME path;
1428     if ((ret = DOSFS_GetFullName( dos, FALSE, &path ))) lstrcpynA( buffer, path.long_name, len );
1429     return ret;
1430 }
1431
1432
1433 /***********************************************************************
1434  *           DOSFS_FindNextEx
1435  */
1436 static int DOSFS_FindNextEx( FIND_FIRST_INFO *info, WIN32_FIND_DATAA *entry )
1437 {
1438     DWORD attr = info->attr | FA_UNUSED | FA_ARCHIVE | FA_RDONLY | FILE_ATTRIBUTE_SYMLINK;
1439     UINT flags = DRIVE_GetFlags( info->drive );
1440     char *p, buffer[MAX_PATHNAME_LEN];
1441     const char *drive_path;
1442     int drive_root;
1443     LPCSTR long_name, short_name;
1444     BY_HANDLE_FILE_INFORMATION fileinfo;
1445     char dos_name[13];
1446
1447     if ((info->attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
1448     {
1449         if (info->cur_pos) return 0;
1450         entry->dwFileAttributes  = FILE_ATTRIBUTE_LABEL;
1451         RtlSecondsSince1970ToTime( (time_t)0, &entry->ftCreationTime );
1452         RtlSecondsSince1970ToTime( (time_t)0, &entry->ftLastAccessTime );
1453         RtlSecondsSince1970ToTime( (time_t)0, &entry->ftLastWriteTime );
1454         entry->nFileSizeHigh     = 0;
1455         entry->nFileSizeLow      = 0;
1456         entry->dwReserved0       = 0;
1457         entry->dwReserved1       = 0;
1458         DOSFS_ToDosDTAFormat( DRIVE_GetLabel( info->drive ), entry->cFileName );
1459         strcpy( entry->cAlternateFileName, entry->cFileName ); 
1460         info->cur_pos++;
1461         TRACE("returning %s (%s) as label\n",
1462                entry->cFileName, entry->cAlternateFileName);
1463         return 1;
1464     }
1465
1466     drive_path = info->path + strlen(DRIVE_GetRoot( info->drive ));
1467     while ((*drive_path == '/') || (*drive_path == '\\')) drive_path++;
1468     drive_root = !*drive_path;
1469
1470     lstrcpynA( buffer, info->path, sizeof(buffer) - 1 );
1471     strcat( buffer, "/" );
1472     p = buffer + strlen(buffer);
1473
1474     while (DOSFS_ReadDir( info->dir, &long_name, &short_name ))
1475     {
1476         info->cur_pos++;
1477
1478         /* Don't return '.' and '..' in the root of the drive */
1479         if (drive_root && (long_name[0] == '.') &&
1480             (!long_name[1] || ((long_name[1] == '.') && !long_name[2])))
1481             continue;
1482
1483         /* Check the long mask */
1484
1485         if (info->long_mask)
1486         {
1487             if (!DOSFS_MatchLong( info->long_mask, long_name,
1488                                   flags & DRIVE_CASE_SENSITIVE )) continue;
1489         }
1490
1491         /* Check the short mask */
1492
1493         if (info->short_mask)
1494         {
1495             if (!short_name)
1496             {
1497                 DOSFS_Hash( long_name, dos_name, TRUE,
1498                             !(flags & DRIVE_CASE_SENSITIVE) );
1499                 short_name = dos_name;
1500             }
1501             if (!DOSFS_MatchShort( info->short_mask, short_name )) continue;
1502         }
1503
1504         /* Check the file attributes */
1505
1506         lstrcpynA( p, long_name, sizeof(buffer) - (int)(p - buffer) );
1507         if (!FILE_Stat( buffer, &fileinfo ))
1508         {
1509             WARN("can't stat %s\n", buffer);
1510             continue;
1511         }
1512         if ((fileinfo.dwFileAttributes & FILE_ATTRIBUTE_SYMLINK) &&
1513             (fileinfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
1514         {
1515             static int show_dir_symlinks = -1;
1516             if (show_dir_symlinks == -1)
1517                 show_dir_symlinks = PROFILE_GetWineIniBool("wine", "ShowDirSymlinks", 0);
1518             if (!show_dir_symlinks) continue;
1519         }
1520
1521         if (fileinfo.dwFileAttributes & ~attr) continue;
1522
1523         /* We now have a matching entry; fill the result and return */
1524
1525         entry->dwFileAttributes = fileinfo.dwFileAttributes;
1526         entry->ftCreationTime   = fileinfo.ftCreationTime;
1527         entry->ftLastAccessTime = fileinfo.ftLastAccessTime;
1528         entry->ftLastWriteTime  = fileinfo.ftLastWriteTime;
1529         entry->nFileSizeHigh    = fileinfo.nFileSizeHigh;
1530         entry->nFileSizeLow     = fileinfo.nFileSizeLow;
1531
1532         if (short_name)
1533             DOSFS_ToDosDTAFormat( short_name, entry->cAlternateFileName );
1534         else
1535             DOSFS_Hash( long_name, entry->cAlternateFileName, FALSE,
1536                         !(flags & DRIVE_CASE_SENSITIVE) );
1537
1538         lstrcpynA( entry->cFileName, long_name, sizeof(entry->cFileName) );
1539         if (!(flags & DRIVE_CASE_PRESERVING)) _strlwr( entry->cFileName );
1540         TRACE("returning %s (%s) %02lx %ld\n",
1541               entry->cFileName, entry->cAlternateFileName,
1542               entry->dwFileAttributes, entry->nFileSizeLow );
1543         return 1;
1544     }
1545     return 0;  /* End of directory */
1546 }
1547
1548 /***********************************************************************
1549  *           DOSFS_FindNext
1550  *
1551  * Find the next matching file. Return the number of entries read to find
1552  * the matching one, or 0 if no more entries.
1553  * 'short_mask' is the 8.3 mask (in FCB format), 'long_mask' is the long
1554  * file name mask. Either or both can be NULL.
1555  *
1556  * NOTE: This is supposed to be only called by the int21 emulation
1557  *       routines. Thus, we should own the Win16Mutex anyway.
1558  *       Nevertheless, we explicitly enter it to ensure the static
1559  *       directory cache is protected.
1560  */
1561 int DOSFS_FindNext( const char *path, const char *short_mask,
1562                     const char *long_mask, int drive, BYTE attr,
1563                     int skip, WIN32_FIND_DATAA *entry )
1564 {
1565     static FIND_FIRST_INFO info;
1566     LPCSTR short_name, long_name;
1567     int count;
1568
1569     _EnterWin16Lock();
1570
1571     /* Check the cached directory */
1572     if (!(info.dir && info.path == path && info.short_mask == short_mask
1573                    && info.long_mask == long_mask && info.drive == drive
1574                    && info.attr == attr && info.cur_pos <= skip))
1575     {  
1576         /* Not in the cache, open it anew */
1577         if (info.dir) DOSFS_CloseDir( info.dir );
1578
1579         info.path = (LPSTR)path;
1580         info.long_mask = (LPSTR)long_mask;
1581         info.short_mask = (LPSTR)short_mask;
1582         info.attr = attr;
1583         info.drive = drive;
1584         info.cur_pos = 0;
1585         info.dir = DOSFS_OpenDir( info.path );
1586     }
1587
1588     /* Skip to desired position */
1589     while (info.cur_pos < skip)
1590         if (info.dir && DOSFS_ReadDir( info.dir, &long_name, &short_name ))
1591             info.cur_pos++;
1592         else
1593             break;
1594
1595     if (info.dir && info.cur_pos == skip && DOSFS_FindNextEx( &info, entry ))
1596         count = info.cur_pos - skip;
1597     else
1598         count = 0;
1599
1600     if (!count)
1601     {
1602         if (info.dir) DOSFS_CloseDir( info.dir );
1603         memset( &info, '\0', sizeof(info) );
1604     }
1605
1606     _LeaveWin16Lock();
1607
1608     return count;
1609 }
1610
1611 /*************************************************************************
1612  *           FindFirstFileExA  (KERNEL32.@)
1613  */
1614 HANDLE WINAPI FindFirstFileExA(
1615         LPCSTR lpFileName,
1616         FINDEX_INFO_LEVELS fInfoLevelId,
1617         LPVOID lpFindFileData,
1618         FINDEX_SEARCH_OPS fSearchOp,
1619         LPVOID lpSearchFilter,
1620         DWORD dwAdditionalFlags)
1621 {
1622     DOS_FULL_NAME full_name;
1623     HGLOBAL handle;
1624     FIND_FIRST_INFO *info;
1625     
1626     if ((fSearchOp != FindExSearchNameMatch) || (dwAdditionalFlags != 0))
1627     {
1628         FIXME("options not implemented 0x%08x 0x%08lx\n", fSearchOp, dwAdditionalFlags );
1629         return INVALID_HANDLE_VALUE;
1630     }
1631
1632     switch(fInfoLevelId)
1633     {
1634       case FindExInfoStandard:
1635         {
1636           WIN32_FIND_DATAA * data = (WIN32_FIND_DATAA *) lpFindFileData;
1637           data->dwReserved0 = data->dwReserved1 = 0x0;
1638           if (!lpFileName) return 0;
1639           if (!DOSFS_GetFullName( lpFileName, FALSE, &full_name )) break;
1640           if (!(handle = GlobalAlloc(GMEM_MOVEABLE, sizeof(FIND_FIRST_INFO)))) break;
1641           info = (FIND_FIRST_INFO *)GlobalLock( handle );
1642           info->path = HeapAlloc( GetProcessHeap(), 0, strlen(full_name.long_name)+1 );
1643           strcpy( info->path, full_name.long_name );
1644           info->long_mask = strrchr( info->path, '/' );
1645           *(info->long_mask++) = '\0';
1646           info->short_mask = NULL;
1647           info->attr = 0xff;
1648           if (lpFileName[0] && (lpFileName[1] == ':'))
1649               info->drive = FILE_toupper(*lpFileName) - 'A';
1650           else info->drive = DRIVE_GetCurrentDrive();
1651           info->cur_pos = 0;
1652
1653           info->dir = DOSFS_OpenDir( info->path );
1654
1655           GlobalUnlock( handle );
1656           if (!FindNextFileA( handle, data ))
1657           {
1658               FindClose( handle );
1659               SetLastError( ERROR_NO_MORE_FILES );
1660               break;
1661           }
1662           return handle;
1663         }
1664         break;
1665       default:
1666         FIXME("fInfoLevelId 0x%08x not implemented\n", fInfoLevelId );
1667     }
1668     return INVALID_HANDLE_VALUE;
1669 }
1670
1671 /*************************************************************************
1672  *           FindFirstFileA   (KERNEL32.@)
1673  */
1674 HANDLE WINAPI FindFirstFileA(
1675         LPCSTR lpFileName,
1676         WIN32_FIND_DATAA *lpFindData )
1677 {
1678     return FindFirstFileExA(lpFileName, FindExInfoStandard, lpFindData,
1679                             FindExSearchNameMatch, NULL, 0);
1680 }
1681
1682 /*************************************************************************
1683  *           FindFirstFileExW   (KERNEL32.@)
1684  */
1685 HANDLE WINAPI FindFirstFileExW(
1686         LPCWSTR lpFileName,
1687         FINDEX_INFO_LEVELS fInfoLevelId,
1688         LPVOID lpFindFileData,
1689         FINDEX_SEARCH_OPS fSearchOp,
1690         LPVOID lpSearchFilter,
1691         DWORD dwAdditionalFlags)
1692 {
1693     HANDLE handle;
1694     WIN32_FIND_DATAA dataA;
1695     LPVOID _lpFindFileData;
1696     LPSTR pathA;
1697
1698     switch(fInfoLevelId)
1699     {
1700       case FindExInfoStandard:
1701         {
1702           _lpFindFileData = &dataA;
1703         }
1704         break;
1705       default:
1706         FIXME("fInfoLevelId 0x%08x not implemented\n", fInfoLevelId );
1707         return INVALID_HANDLE_VALUE;
1708     }
1709
1710     pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
1711     handle = FindFirstFileExA(pathA, fInfoLevelId, _lpFindFileData, fSearchOp, lpSearchFilter, dwAdditionalFlags);
1712     HeapFree( GetProcessHeap(), 0, pathA );
1713     if (handle == INVALID_HANDLE_VALUE) return handle;
1714     
1715     switch(fInfoLevelId)
1716     {
1717       case FindExInfoStandard:
1718         {
1719           WIN32_FIND_DATAW *dataW = (WIN32_FIND_DATAW*) lpFindFileData;
1720           dataW->dwFileAttributes = dataA.dwFileAttributes;
1721           dataW->ftCreationTime   = dataA.ftCreationTime;
1722           dataW->ftLastAccessTime = dataA.ftLastAccessTime;
1723           dataW->ftLastWriteTime  = dataA.ftLastWriteTime;
1724           dataW->nFileSizeHigh    = dataA.nFileSizeHigh;
1725           dataW->nFileSizeLow     = dataA.nFileSizeLow;
1726           MultiByteToWideChar( CP_ACP, 0, dataA.cFileName, -1,
1727                                dataW->cFileName, sizeof(dataW->cFileName)/sizeof(WCHAR) );
1728           MultiByteToWideChar( CP_ACP, 0, dataA.cAlternateFileName, -1,
1729                                dataW->cAlternateFileName,
1730                                sizeof(dataW->cAlternateFileName)/sizeof(WCHAR) );
1731         }
1732         break;
1733       default:
1734         FIXME("fInfoLevelId 0x%08x not implemented\n", fInfoLevelId );
1735         return INVALID_HANDLE_VALUE;
1736     }
1737     return handle;
1738 }
1739
1740 /*************************************************************************
1741  *           FindFirstFileW   (KERNEL32.@)
1742  */
1743 HANDLE WINAPI FindFirstFileW( LPCWSTR lpFileName, WIN32_FIND_DATAW *lpFindData )
1744 {
1745     return FindFirstFileExW(lpFileName, FindExInfoStandard, lpFindData,
1746                             FindExSearchNameMatch, NULL, 0);
1747 }
1748
1749 /*************************************************************************
1750  *           FindNextFileA   (KERNEL32.@)
1751  */
1752 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
1753 {
1754     FIND_FIRST_INFO *info;
1755
1756     if ((handle == INVALID_HANDLE_VALUE) || 
1757        !(info = (FIND_FIRST_INFO *)GlobalLock( handle )))
1758     {
1759         SetLastError( ERROR_INVALID_HANDLE );
1760         return FALSE;
1761     }
1762     GlobalUnlock( handle );
1763     if (!info->path || !info->dir)
1764     {
1765         SetLastError( ERROR_NO_MORE_FILES );
1766         return FALSE;
1767     }
1768     if (!DOSFS_FindNextEx( info, data ))
1769     {
1770         DOSFS_CloseDir( info->dir ); info->dir = NULL;
1771         HeapFree( GetProcessHeap(), 0, info->path );
1772         info->path = info->long_mask = NULL;
1773         SetLastError( ERROR_NO_MORE_FILES );
1774         return FALSE;
1775     }
1776     return TRUE;
1777 }
1778
1779
1780 /*************************************************************************
1781  *           FindNextFileW   (KERNEL32.@)
1782  */
1783 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
1784 {
1785     WIN32_FIND_DATAA dataA;
1786     if (!FindNextFileA( handle, &dataA )) return FALSE;
1787     data->dwFileAttributes = dataA.dwFileAttributes;
1788     data->ftCreationTime   = dataA.ftCreationTime;
1789     data->ftLastAccessTime = dataA.ftLastAccessTime;
1790     data->ftLastWriteTime  = dataA.ftLastWriteTime;
1791     data->nFileSizeHigh    = dataA.nFileSizeHigh;
1792     data->nFileSizeLow     = dataA.nFileSizeLow;
1793     MultiByteToWideChar( CP_ACP, 0, dataA.cFileName, -1,
1794                          data->cFileName, sizeof(data->cFileName)/sizeof(WCHAR) );
1795     MultiByteToWideChar( CP_ACP, 0, dataA.cAlternateFileName, -1,
1796                          data->cAlternateFileName,
1797                          sizeof(data->cAlternateFileName)/sizeof(WCHAR) );
1798     return TRUE;
1799 }
1800
1801 /*************************************************************************
1802  *           FindClose   (KERNEL32.@)
1803  */
1804 BOOL WINAPI FindClose( HANDLE handle )
1805 {
1806     FIND_FIRST_INFO *info;
1807
1808     if ((handle == INVALID_HANDLE_VALUE) ||
1809         !(info = (FIND_FIRST_INFO *)GlobalLock( handle )))
1810     {
1811         SetLastError( ERROR_INVALID_HANDLE );
1812         return FALSE;
1813     }
1814     __TRY
1815     {
1816         if (info->dir) DOSFS_CloseDir( info->dir );
1817         if (info->path) HeapFree( GetProcessHeap(), 0, info->path );
1818     }
1819     __EXCEPT(page_fault)
1820     {
1821         WARN("Illegal handle %x\n", handle);
1822         SetLastError( ERROR_INVALID_HANDLE );
1823         return FALSE;
1824     }
1825     __ENDTRY
1826     GlobalUnlock( handle );
1827     GlobalFree( handle );
1828     return TRUE;
1829 }
1830
1831 /***********************************************************************
1832  *           DOSFS_UnixTimeToFileTime
1833  *
1834  * Convert a Unix time to FILETIME format.
1835  * The FILETIME structure is a 64-bit value representing the number of
1836  * 100-nanosecond intervals since January 1, 1601, 0:00.
1837  * 'remainder' is the nonnegative number of 100-ns intervals
1838  * corresponding to the time fraction smaller than 1 second that
1839  * couldn't be stored in the time_t value.
1840  */
1841 void DOSFS_UnixTimeToFileTime( time_t unix_time, FILETIME *filetime,
1842                                DWORD remainder )
1843 {
1844     /* NOTES:
1845
1846        CONSTANTS: 
1847        The time difference between 1 January 1601, 00:00:00 and
1848        1 January 1970, 00:00:00 is 369 years, plus the leap years
1849        from 1604 to 1968, excluding 1700, 1800, 1900.
1850        This makes (1968 - 1600) / 4 - 3 = 89 leap days, and a total
1851        of 134774 days.
1852
1853        Any day in that period had 24 * 60 * 60 = 86400 seconds.
1854
1855        The time difference is 134774 * 86400 * 10000000, which can be written
1856        116444736000000000
1857        27111902 * 2^32 + 3577643008
1858        413 * 2^48 + 45534 * 2^32 + 54590 * 2^16 + 32768
1859
1860        If you find that these constants are buggy, please change them in all
1861        instances in both conversion functions.
1862
1863        VERSIONS:
1864        There are two versions, one of them uses long long variables and
1865        is presumably faster but not ISO C. The other one uses standard C
1866        data types and operations but relies on the assumption that negative
1867        numbers are stored as 2's complement (-1 is 0xffff....). If this
1868        assumption is violated, dates before 1970 will not convert correctly.
1869        This should however work on any reasonable architecture where WINE
1870        will run.
1871
1872        DETAILS:
1873        
1874        Take care not to remove the casts. I have tested these functions
1875        (in both versions) for a lot of numbers. I would be interested in
1876        results on other compilers than GCC.
1877
1878        The operations have been designed to account for the possibility
1879        of 64-bit time_t in future UNICES. Even the versions without
1880        internal long long numbers will work if time_t only is 64 bit.
1881        A 32-bit shift, which was necessary for that operation, turned out
1882        not to work correctly in GCC, besides giving the warning. So I
1883        used a double 16-bit shift instead. Numbers are in the ISO version
1884        represented by three limbs, the most significant with 32 bit, the
1885        other two with 16 bit each.
1886
1887        As the modulo-operator % is not well-defined for negative numbers,
1888        negative divisors have been avoided in DOSFS_FileTimeToUnixTime.
1889
1890        There might be quicker ways to do this in C. Certainly so in
1891        assembler.
1892
1893        Claus Fischer, fischer@iue.tuwien.ac.at
1894        */
1895
1896 #if SIZEOF_LONG_LONG >= 8
1897 #  define USE_LONG_LONG 1
1898 #else
1899 #  define USE_LONG_LONG 0
1900 #endif
1901
1902 #if USE_LONG_LONG               /* gcc supports long long type */
1903
1904     long long int t = unix_time;
1905     t *= 10000000;
1906     t += 116444736000000000LL;
1907     t += remainder;
1908     filetime->dwLowDateTime  = (UINT)t;
1909     filetime->dwHighDateTime = (UINT)(t >> 32);
1910
1911 #else  /* ISO version */
1912
1913     UINT a0;                    /* 16 bit, low    bits */
1914     UINT a1;                    /* 16 bit, medium bits */
1915     UINT a2;                    /* 32 bit, high   bits */
1916
1917     /* Copy the unix time to a2/a1/a0 */
1918     a0 =  unix_time & 0xffff;
1919     a1 = (unix_time >> 16) & 0xffff;
1920     /* This is obsolete if unix_time is only 32 bits, but it does not hurt.
1921        Do not replace this by >> 32, it gives a compiler warning and it does
1922        not work. */
1923     a2 = (unix_time >= 0 ? (unix_time >> 16) >> 16 :
1924           ~((~unix_time >> 16) >> 16));
1925
1926     /* Multiply a by 10000000 (a = a2/a1/a0)
1927        Split the factor into 10000 * 1000 which are both less than 0xffff. */
1928     a0 *= 10000;
1929     a1 = a1 * 10000 + (a0 >> 16);
1930     a2 = a2 * 10000 + (a1 >> 16);
1931     a0 &= 0xffff;
1932     a1 &= 0xffff;
1933
1934     a0 *= 1000;
1935     a1 = a1 * 1000 + (a0 >> 16);
1936     a2 = a2 * 1000 + (a1 >> 16);
1937     a0 &= 0xffff;
1938     a1 &= 0xffff;
1939
1940     /* Add the time difference and the remainder */
1941     a0 += 32768 + (remainder & 0xffff);
1942     a1 += 54590 + (remainder >> 16   ) + (a0 >> 16);
1943     a2 += 27111902                     + (a1 >> 16);
1944     a0 &= 0xffff;
1945     a1 &= 0xffff;
1946
1947     /* Set filetime */
1948     filetime->dwLowDateTime  = (a1 << 16) + a0;
1949     filetime->dwHighDateTime = a2;
1950 #endif
1951 }
1952
1953
1954 /***********************************************************************
1955  *           DOSFS_FileTimeToUnixTime
1956  *
1957  * Convert a FILETIME format to Unix time.
1958  * If not NULL, 'remainder' contains the fractional part of the filetime,
1959  * in the range of [0..9999999] (even if time_t is negative).
1960  */
1961 time_t DOSFS_FileTimeToUnixTime( const FILETIME *filetime, DWORD *remainder )
1962 {
1963     /* Read the comment in the function DOSFS_UnixTimeToFileTime. */
1964 #if USE_LONG_LONG
1965
1966     long long int t = filetime->dwHighDateTime;
1967     t <<= 32;
1968     t += (UINT)filetime->dwLowDateTime;
1969     t -= 116444736000000000LL;
1970     if (t < 0)
1971     {
1972         if (remainder) *remainder = 9999999 - (-t - 1) % 10000000;
1973         return -1 - ((-t - 1) / 10000000);
1974     }
1975     else
1976     {
1977         if (remainder) *remainder = t % 10000000;
1978         return t / 10000000;
1979     }
1980
1981 #else  /* ISO version */
1982
1983     UINT a0;                    /* 16 bit, low    bits */
1984     UINT a1;                    /* 16 bit, medium bits */
1985     UINT a2;                    /* 32 bit, high   bits */
1986     UINT r;                     /* remainder of division */
1987     unsigned int carry;         /* carry bit for subtraction */
1988     int negative;               /* whether a represents a negative value */
1989
1990     /* Copy the time values to a2/a1/a0 */
1991     a2 =  (UINT)filetime->dwHighDateTime;
1992     a1 = ((UINT)filetime->dwLowDateTime ) >> 16;
1993     a0 = ((UINT)filetime->dwLowDateTime ) & 0xffff;
1994
1995     /* Subtract the time difference */
1996     if (a0 >= 32768           ) a0 -=             32768        , carry = 0;
1997     else                        a0 += (1 << 16) - 32768        , carry = 1;
1998
1999     if (a1 >= 54590    + carry) a1 -=             54590 + carry, carry = 0;
2000     else                        a1 += (1 << 16) - 54590 - carry, carry = 1;
2001
2002     a2 -= 27111902 + carry;
2003     
2004     /* If a is negative, replace a by (-1-a) */
2005     negative = (a2 >= ((UINT)1) << 31);
2006     if (negative)
2007     {
2008         /* Set a to -a - 1 (a is a2/a1/a0) */
2009         a0 = 0xffff - a0;
2010         a1 = 0xffff - a1;
2011         a2 = ~a2;
2012     }
2013
2014     /* Divide a by 10000000 (a = a2/a1/a0), put the rest into r.
2015        Split the divisor into 10000 * 1000 which are both less than 0xffff. */
2016     a1 += (a2 % 10000) << 16;
2017     a2 /=       10000;
2018     a0 += (a1 % 10000) << 16;
2019     a1 /=       10000;
2020     r   =  a0 % 10000;
2021     a0 /=       10000;
2022
2023     a1 += (a2 % 1000) << 16;
2024     a2 /=       1000;
2025     a0 += (a1 % 1000) << 16;
2026     a1 /=       1000;
2027     r  += (a0 % 1000) * 10000;
2028     a0 /=       1000;
2029
2030     /* If a was negative, replace a by (-1-a) and r by (9999999 - r) */
2031     if (negative)
2032     {
2033         /* Set a to -a - 1 (a is a2/a1/a0) */
2034         a0 = 0xffff - a0;
2035         a1 = 0xffff - a1;
2036         a2 = ~a2;
2037
2038         r  = 9999999 - r;
2039     }
2040
2041     if (remainder) *remainder = r;
2042
2043     /* Do not replace this by << 32, it gives a compiler warning and it does
2044        not work. */
2045     return ((((time_t)a2) << 16) << 16) + (a1 << 16) + a0;
2046 #endif
2047 }
2048
2049
2050 /***********************************************************************
2051  *           MulDiv   (KERNEL32.@)
2052  * RETURNS
2053  *      Result of multiplication and division
2054  *      -1: Overflow occurred or Divisor was 0
2055  */
2056 INT WINAPI MulDiv(
2057              INT nMultiplicand, 
2058              INT nMultiplier,
2059              INT nDivisor)
2060 {
2061 #if SIZEOF_LONG_LONG >= 8
2062     long long ret;
2063
2064     if (!nDivisor) return -1;
2065
2066     /* We want to deal with a positive divisor to simplify the logic. */
2067     if (nDivisor < 0)
2068     {
2069       nMultiplicand = - nMultiplicand;
2070       nDivisor = -nDivisor;
2071     }
2072
2073     /* If the result is positive, we "add" to round. else, we subtract to round. */
2074     if ( ( (nMultiplicand <  0) && (nMultiplier <  0) ) ||
2075          ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
2076       ret = (((long long)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
2077     else
2078       ret = (((long long)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
2079
2080     if ((ret > 2147483647) || (ret < -2147483647)) return -1;
2081     return ret;
2082 #else
2083     if (!nDivisor) return -1;
2084
2085     /* We want to deal with a positive divisor to simplify the logic. */
2086     if (nDivisor < 0)
2087     {
2088       nMultiplicand = - nMultiplicand;
2089       nDivisor = -nDivisor;
2090     }
2091
2092     /* If the result is positive, we "add" to round. else, we subtract to round. */
2093     if ( ( (nMultiplicand <  0) && (nMultiplier <  0) ) ||
2094          ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
2095       return ((nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
2096  
2097     return ((nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
2098     
2099 #endif
2100 }
2101
2102
2103 /***********************************************************************
2104  *           DosDateTimeToFileTime   (KERNEL32.@)
2105  */
2106 BOOL WINAPI DosDateTimeToFileTime( WORD fatdate, WORD fattime, LPFILETIME ft)
2107 {
2108     struct tm newtm;
2109
2110     newtm.tm_sec  = (fattime & 0x1f) * 2;
2111     newtm.tm_min  = (fattime >> 5) & 0x3f;
2112     newtm.tm_hour = (fattime >> 11);
2113     newtm.tm_mday = (fatdate & 0x1f);
2114     newtm.tm_mon  = ((fatdate >> 5) & 0x0f) - 1;
2115     newtm.tm_year = (fatdate >> 9) + 80;
2116     RtlSecondsSince1970ToTime( mktime( &newtm ), ft );
2117     return TRUE;
2118 }
2119
2120
2121 /***********************************************************************
2122  *           FileTimeToDosDateTime   (KERNEL32.@)
2123  */
2124 BOOL WINAPI FileTimeToDosDateTime( const FILETIME *ft, LPWORD fatdate,
2125                                      LPWORD fattime )
2126 {
2127     time_t unixtime = DOSFS_FileTimeToUnixTime( ft, NULL );
2128     struct tm *tm = localtime( &unixtime );
2129     if (fattime)
2130         *fattime = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
2131     if (fatdate)
2132         *fatdate = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5)
2133                    + tm->tm_mday;
2134     return TRUE;
2135 }
2136
2137
2138 /***********************************************************************
2139  *           LocalFileTimeToFileTime   (KERNEL32.@)
2140  */
2141 BOOL WINAPI LocalFileTimeToFileTime( const FILETIME *localft,
2142                                        LPFILETIME utcft )
2143 {
2144     struct tm *xtm;
2145     DWORD remainder;
2146
2147     /* convert from local to UTC. Perhaps not correct. FIXME */
2148     time_t unixtime = DOSFS_FileTimeToUnixTime( localft, &remainder );
2149     xtm = gmtime( &unixtime );
2150     DOSFS_UnixTimeToFileTime( mktime(xtm), utcft, remainder );
2151     return TRUE; 
2152 }
2153
2154
2155 /***********************************************************************
2156  *           FileTimeToLocalFileTime   (KERNEL32.@)
2157  */
2158 BOOL WINAPI FileTimeToLocalFileTime( const FILETIME *utcft,
2159                                        LPFILETIME localft )
2160 {
2161     DWORD remainder;
2162     /* convert from UTC to local. Perhaps not correct. FIXME */
2163     time_t unixtime = DOSFS_FileTimeToUnixTime( utcft, &remainder );
2164 #ifdef HAVE_TIMEGM
2165     struct tm *xtm = localtime( &unixtime );
2166     time_t localtime;
2167
2168     localtime = timegm(xtm);
2169     DOSFS_UnixTimeToFileTime( localtime, localft, remainder );
2170
2171 #else
2172     struct tm *xtm,*gtm;
2173     time_t time1,time2;
2174
2175     xtm = localtime( &unixtime );
2176     gtm = gmtime( &unixtime );
2177     time1 = mktime(xtm);
2178     time2 = mktime(gtm);
2179     DOSFS_UnixTimeToFileTime( 2*time1-time2, localft, remainder );
2180 #endif
2181     return TRUE; 
2182 }
2183
2184
2185 /***********************************************************************
2186  *           FileTimeToSystemTime   (KERNEL32.@)
2187  */
2188 BOOL WINAPI FileTimeToSystemTime( const FILETIME *ft, LPSYSTEMTIME syst )
2189 {
2190     struct tm *xtm;
2191     DWORD remainder;
2192     time_t xtime = DOSFS_FileTimeToUnixTime( ft, &remainder );
2193     xtm = gmtime(&xtime);
2194     syst->wYear         = xtm->tm_year+1900;
2195     syst->wMonth        = xtm->tm_mon + 1;
2196     syst->wDayOfWeek    = xtm->tm_wday;
2197     syst->wDay          = xtm->tm_mday;
2198     syst->wHour         = xtm->tm_hour;
2199     syst->wMinute       = xtm->tm_min;
2200     syst->wSecond       = xtm->tm_sec;
2201     syst->wMilliseconds = remainder / 10000;
2202     return TRUE; 
2203 }
2204
2205 /***********************************************************************
2206  *           QueryDosDeviceA   (KERNEL32.@)
2207  *
2208  * returns array of strings terminated by \0, terminated by \0
2209  */
2210 DWORD WINAPI QueryDosDeviceA(LPCSTR devname,LPSTR target,DWORD bufsize)
2211 {
2212     LPSTR s;
2213     char  buffer[200];
2214
2215     TRACE("(%s,...)\n", devname ? devname : "<null>");
2216     if (!devname) {
2217         /* return known MSDOS devices */
2218         static const char devices[24] = "CON\0COM1\0COM2\0LPT1\0NUL\0\0";
2219         memcpy( target, devices, min(bufsize,sizeof(devices)) );
2220         return min(bufsize,sizeof(devices));
2221     }
2222     /* In theory all that are possible and have been defined.
2223      * Now just those below, since mirc uses it to check for special files.
2224      *
2225      * (It is more complex, and supports netmounted stuff, and \\.\ stuff, 
2226      *  but currently we just ignore that.)
2227      */
2228 #define CHECK(x) (strstr(devname,#x)==devname)
2229     if (CHECK(con) || CHECK(com) || CHECK(lpt) || CHECK(nul)) {
2230         strcpy(buffer,"\\DEV\\");
2231         strcat(buffer,devname);
2232         if ((s=strchr(buffer,':'))) *s='\0';
2233         lstrcpynA(target,buffer,bufsize);
2234         return strlen(buffer)+1;
2235     } else {
2236         if (strchr(devname,':') || devname[0]=='\\') {
2237             /* This might be a DOS device we do not handle yet ... */
2238             FIXME("(%s) not detected as DOS device!\n",devname);
2239         }
2240         SetLastError(ERROR_DEV_NOT_EXIST);
2241         return 0;
2242     }
2243
2244 }
2245
2246
2247 /***********************************************************************
2248  *           QueryDosDeviceW   (KERNEL32.@)
2249  *
2250  * returns array of strings terminated by \0, terminated by \0
2251  */
2252 DWORD WINAPI QueryDosDeviceW(LPCWSTR devname,LPWSTR target,DWORD bufsize)
2253 {
2254     LPSTR devnameA = devname?HEAP_strdupWtoA(GetProcessHeap(),0,devname):NULL;
2255     LPSTR targetA = (LPSTR)HeapAlloc(GetProcessHeap(),0,bufsize);
2256     DWORD ret = QueryDosDeviceA(devnameA,targetA,bufsize);
2257
2258     ret = MultiByteToWideChar( CP_ACP, 0, targetA, ret, target, bufsize );
2259     if (devnameA) HeapFree(GetProcessHeap(),0,devnameA);
2260     if (targetA) HeapFree(GetProcessHeap(),0,targetA);
2261     return ret;
2262 }
2263
2264
2265 /***********************************************************************
2266  *           SystemTimeToFileTime   (KERNEL32.@)
2267  */
2268 BOOL WINAPI SystemTimeToFileTime( const SYSTEMTIME *syst, LPFILETIME ft )
2269 {
2270 #ifdef HAVE_TIMEGM
2271     struct tm xtm;
2272     time_t utctime;
2273 #else
2274     struct tm xtm,*local_tm,*utc_tm;
2275     time_t localtim,utctime;
2276 #endif
2277
2278     xtm.tm_year = syst->wYear-1900;
2279     xtm.tm_mon  = syst->wMonth - 1;
2280     xtm.tm_wday = syst->wDayOfWeek;
2281     xtm.tm_mday = syst->wDay;
2282     xtm.tm_hour = syst->wHour;
2283     xtm.tm_min  = syst->wMinute;
2284     xtm.tm_sec  = syst->wSecond; /* this is UTC */
2285     xtm.tm_isdst = -1;
2286 #ifdef HAVE_TIMEGM
2287     utctime = timegm(&xtm);
2288     DOSFS_UnixTimeToFileTime( utctime, ft, 
2289                               syst->wMilliseconds * 10000 );
2290 #else
2291     localtim = mktime(&xtm);    /* now we've got local time */
2292     local_tm = localtime(&localtim);
2293     utc_tm = gmtime(&localtim);
2294     utctime = mktime(utc_tm);
2295     DOSFS_UnixTimeToFileTime( 2*localtim -utctime, ft, 
2296                               syst->wMilliseconds * 10000 );
2297 #endif
2298     return TRUE; 
2299 }
2300
2301 /***********************************************************************
2302  *           DefineDosDeviceA       (KERNEL32.@)
2303  */
2304 BOOL WINAPI DefineDosDeviceA(DWORD flags,LPCSTR devname,LPCSTR targetpath) {
2305         FIXME("(0x%08lx,%s,%s),stub!\n",flags,devname,targetpath);
2306         SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2307         return FALSE;
2308 }
2309
2310 /*
2311    --- 16 bit functions ---
2312 */
2313
2314 /*************************************************************************
2315  *           FindFirstFile   (KERNEL.413)
2316  */
2317 HANDLE16 WINAPI FindFirstFile16( LPCSTR path, WIN32_FIND_DATAA *data )
2318 {
2319     DOS_FULL_NAME full_name;
2320     HGLOBAL16 handle;
2321     FIND_FIRST_INFO *info;
2322
2323     data->dwReserved0 = data->dwReserved1 = 0x0;
2324     if (!path) return 0;
2325     if (!DOSFS_GetFullName( path, FALSE, &full_name ))
2326         return INVALID_HANDLE_VALUE16;
2327     if (!(handle = GlobalAlloc16( GMEM_MOVEABLE, sizeof(FIND_FIRST_INFO) )))
2328         return INVALID_HANDLE_VALUE16;
2329     info = (FIND_FIRST_INFO *)GlobalLock16( handle );
2330     info->path = HeapAlloc( GetProcessHeap(), 0, strlen(full_name.long_name)+1 );
2331     strcpy( info->path, full_name.long_name );
2332     info->long_mask = strrchr( info->path, '/' );
2333     if (info->long_mask )
2334         *(info->long_mask++) = '\0';
2335     info->short_mask = NULL;
2336     info->attr = 0xff;
2337     if (path[0] && (path[1] == ':')) info->drive = FILE_toupper(*path) - 'A';
2338     else info->drive = DRIVE_GetCurrentDrive();
2339     info->cur_pos = 0;
2340
2341     info->dir = DOSFS_OpenDir( info->path );
2342
2343     GlobalUnlock16( handle );
2344     if (!FindNextFile16( handle, data ))
2345     {
2346         FindClose16( handle );
2347         SetLastError( ERROR_NO_MORE_FILES );
2348         return INVALID_HANDLE_VALUE16;
2349     }
2350     return handle;
2351 }
2352
2353 /*************************************************************************
2354  *           FindNextFile   (KERNEL.414)
2355  */
2356 BOOL16 WINAPI FindNextFile16( HANDLE16 handle, WIN32_FIND_DATAA *data )
2357 {
2358     FIND_FIRST_INFO *info;
2359
2360     if ((handle == INVALID_HANDLE_VALUE16) ||
2361        !(info = (FIND_FIRST_INFO *)GlobalLock16( handle )))
2362     {
2363         SetLastError( ERROR_INVALID_HANDLE );
2364         return FALSE;
2365     }
2366     GlobalUnlock16( handle );
2367     if (!info->path || !info->dir)
2368     {
2369         SetLastError( ERROR_NO_MORE_FILES );
2370         return FALSE;
2371     }
2372     if (!DOSFS_FindNextEx( info, data ))
2373     {
2374         DOSFS_CloseDir( info->dir ); info->dir = NULL;
2375         HeapFree( GetProcessHeap(), 0, info->path );
2376         info->path = info->long_mask = NULL;
2377         SetLastError( ERROR_NO_MORE_FILES );
2378         return FALSE;
2379     }
2380     return TRUE;
2381 }
2382
2383 /*************************************************************************
2384  *           FindClose   (KERNEL.415)
2385  */
2386 BOOL16 WINAPI FindClose16( HANDLE16 handle )
2387 {
2388     FIND_FIRST_INFO *info;
2389
2390     if ((handle == INVALID_HANDLE_VALUE16) ||
2391         !(info = (FIND_FIRST_INFO *)GlobalLock16( handle )))
2392     {
2393         SetLastError( ERROR_INVALID_HANDLE );
2394         return FALSE;
2395     }
2396     if (info->dir) DOSFS_CloseDir( info->dir );
2397     if (info->path) HeapFree( GetProcessHeap(), 0, info->path );
2398     GlobalUnlock16( handle );
2399     GlobalFree16( handle );
2400     return TRUE;
2401 }
2402