Removed some unnecessary #includes and dll dependencies.
[wine/multimedia.git] / files / dos_fs.c
blob7b259463baf1bf595af07ddab758b0a417b3c949
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 "wingdi.h"
26 #include "winuser.h"
27 #include "wine/winbase16.h"
28 #include "winerror.h"
29 #include "drive.h"
30 #include "file.h"
31 #include "heap.h"
32 #include "msdos.h"
33 #include "syslevel.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] == ':') ? 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] = 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] = 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 static int DOSFS_MatchLong( const char *mask, const char *name,
293 int case_sensitive )
295 if (!strcmp( mask, "*.*" )) return 1;
296 while (*name && *mask)
298 if (*mask == '*')
300 mask++;
301 while (*mask == '*') mask++; /* Skip consecutive '*' */
302 if (!*mask) return 1;
303 if (case_sensitive) while (*name && (*name != *mask)) name++;
304 else while (*name && (toupper(*name) != toupper(*mask))) name++;
305 if (!*name) break;
307 else if (*mask != '?')
309 if (case_sensitive)
311 if (*mask != *name) return 0;
313 else if (toupper(*mask) != toupper(*name)) return 0;
315 mask++;
316 name++;
318 if (*mask == '.') mask++; /* Ignore trailing '.' in mask */
319 return (!*name && !*mask);
323 /***********************************************************************
324 * DOSFS_OpenDir
326 static DOS_DIR *DOSFS_OpenDir( LPCSTR path )
328 DOS_DIR *dir = HeapAlloc( GetProcessHeap(), 0, sizeof(*dir) );
329 if (!dir)
331 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
332 return NULL;
335 /* Treat empty path as root directory. This simplifies path split into
336 directory and mask in several other places */
337 if (!*path) path = "/";
339 #ifdef VFAT_IOCTL_READDIR_BOTH
341 /* Check if the VFAT ioctl is supported on this directory */
343 if ((dir->fd = open( path, O_RDONLY )) != -1)
345 if (ioctl( dir->fd, VFAT_IOCTL_READDIR_BOTH, (long)dir->dirent ) == -1)
347 close( dir->fd );
348 dir->fd = -1;
350 else
352 /* Set the file pointer back at the start of the directory */
353 lseek( dir->fd, 0, SEEK_SET );
354 dir->dir = NULL;
355 return dir;
358 #endif /* VFAT_IOCTL_READDIR_BOTH */
360 /* Now use the standard opendir/readdir interface */
362 if (!(dir->dir = opendir( path )))
364 HeapFree( GetProcessHeap(), 0, dir );
365 return NULL;
367 return dir;
371 /***********************************************************************
372 * DOSFS_CloseDir
374 static void DOSFS_CloseDir( DOS_DIR *dir )
376 #ifdef VFAT_IOCTL_READDIR_BOTH
377 if (dir->fd != -1) close( dir->fd );
378 #endif /* VFAT_IOCTL_READDIR_BOTH */
379 if (dir->dir) closedir( dir->dir );
380 HeapFree( GetProcessHeap(), 0, dir );
384 /***********************************************************************
385 * DOSFS_ReadDir
387 static BOOL DOSFS_ReadDir( DOS_DIR *dir, LPCSTR *long_name,
388 LPCSTR *short_name )
390 struct dirent *dirent;
392 #ifdef VFAT_IOCTL_READDIR_BOTH
393 if (dir->fd != -1)
395 if (ioctl( dir->fd, VFAT_IOCTL_READDIR_BOTH, (long)dir->dirent ) != -1) {
396 if (!dir->dirent[0].d_reclen) return FALSE;
397 if (!DOSFS_ToDosFCBFormat( dir->dirent[0].d_name, dir->short_name ))
398 dir->short_name[0] = '\0';
399 *short_name = dir->short_name;
400 if (dir->dirent[1].d_name[0]) *long_name = dir->dirent[1].d_name;
401 else *long_name = dir->dirent[0].d_name;
402 return TRUE;
405 #endif /* VFAT_IOCTL_READDIR_BOTH */
407 if (!(dirent = readdir( dir->dir ))) return FALSE;
408 *long_name = dirent->d_name;
409 *short_name = NULL;
410 return TRUE;
414 /***********************************************************************
415 * DOSFS_Hash
417 * Transform a Unix file name into a hashed DOS name. If the name is a valid
418 * DOS name, it is converted to upper-case; otherwise it is replaced by a
419 * hashed version that fits in 8.3 format.
420 * File name can be terminated by '\0', '\\' or '/'.
421 * 'buffer' must be at least 13 characters long.
423 static void DOSFS_Hash( LPCSTR name, LPSTR buffer, BOOL dir_format,
424 BOOL ignore_case )
426 static const char invalid_chars[] = INVALID_DOS_CHARS "~.";
427 static const char hash_chars[32] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
429 const char *p, *ext;
430 char *dst;
431 unsigned short hash;
432 int i;
434 if (dir_format) strcpy( buffer, " " );
436 if (DOSFS_ValidDOSName( name, ignore_case ))
438 /* Check for '.' and '..' */
439 if (*name == '.')
441 buffer[0] = '.';
442 if (!dir_format) buffer[1] = buffer[2] = '\0';
443 if (name[1] == '.') buffer[1] = '.';
444 return;
447 /* Simply copy the name, converting to uppercase */
449 for (dst = buffer; !IS_END_OF_NAME(*name) && (*name != '.'); name++)
450 *dst++ = toupper(*name);
451 if (*name == '.')
453 if (dir_format) dst = buffer + 8;
454 else *dst++ = '.';
455 for (name++; !IS_END_OF_NAME(*name); name++)
456 *dst++ = toupper(*name);
458 if (!dir_format) *dst = '\0';
459 return;
462 /* Compute the hash code of the file name */
463 /* If you know something about hash functions, feel free to */
464 /* insert a better algorithm here... */
465 if (ignore_case)
467 for (p = name, hash = 0xbeef; !IS_END_OF_NAME(p[1]); p++)
468 hash = (hash<<3) ^ (hash>>5) ^ tolower(*p) ^ (tolower(p[1]) << 8);
469 hash = (hash<<3) ^ (hash>>5) ^ tolower(*p); /* Last character*/
471 else
473 for (p = name, hash = 0xbeef; !IS_END_OF_NAME(p[1]); p++)
474 hash = (hash << 3) ^ (hash >> 5) ^ *p ^ (p[1] << 8);
475 hash = (hash << 3) ^ (hash >> 5) ^ *p; /* Last character */
478 /* Find last dot for start of the extension */
479 for (p = name+1, ext = NULL; !IS_END_OF_NAME(*p); p++)
480 if (*p == '.') ext = p;
481 if (ext && IS_END_OF_NAME(ext[1]))
482 ext = NULL; /* Empty extension ignored */
484 /* Copy first 4 chars, replacing invalid chars with '_' */
485 for (i = 4, p = name, dst = buffer; i > 0; i--, p++)
487 if (IS_END_OF_NAME(*p) || (p == ext)) break;
488 *dst++ = strchr( invalid_chars, *p ) ? '_' : toupper(*p);
490 /* Pad to 5 chars with '~' */
491 while (i-- >= 0) *dst++ = '~';
493 /* Insert hash code converted to 3 ASCII chars */
494 *dst++ = hash_chars[(hash >> 10) & 0x1f];
495 *dst++ = hash_chars[(hash >> 5) & 0x1f];
496 *dst++ = hash_chars[hash & 0x1f];
498 /* Copy the first 3 chars of the extension (if any) */
499 if (ext)
501 if (!dir_format) *dst++ = '.';
502 for (i = 3, ext++; (i > 0) && !IS_END_OF_NAME(*ext); i--, ext++)
503 *dst++ = strchr( invalid_chars, *ext ) ? '_' : toupper(*ext);
505 if (!dir_format) *dst = '\0';
509 /***********************************************************************
510 * DOSFS_FindUnixName
512 * Find the Unix file name in a given directory that corresponds to
513 * a file name (either in Unix or DOS format).
514 * File name can be terminated by '\0', '\\' or '/'.
515 * Return TRUE if OK, FALSE if no file name matches.
517 * 'long_buf' must be at least 'long_len' characters long. If the long name
518 * turns out to be larger than that, the function returns FALSE.
519 * 'short_buf' must be at least 13 characters long.
521 BOOL DOSFS_FindUnixName( LPCSTR path, LPCSTR name, LPSTR long_buf,
522 INT long_len, LPSTR short_buf, BOOL ignore_case)
524 DOS_DIR *dir;
525 LPCSTR long_name, short_name;
526 char dos_name[12], tmp_buf[13];
527 BOOL ret;
529 const char *p = strchr( name, '/' );
530 int len = p ? (int)(p - name) : strlen(name);
531 if ((p = strchr( name, '\\' ))) len = min( (int)(p - name), len );
532 /* Ignore trailing dots and spaces */
533 while (len > 1 && (name[len-1] == '.' || name[len-1] == ' ')) len--;
534 if (long_len < len + 1) return FALSE;
536 TRACE("%s,%s\n", path, name );
538 if (!DOSFS_ToDosFCBFormat( name, dos_name )) dos_name[0] = '\0';
540 if (!(dir = DOSFS_OpenDir( path )))
542 WARN("(%s,%s): can't open dir: %s\n",
543 path, name, strerror(errno) );
544 return FALSE;
547 while ((ret = DOSFS_ReadDir( dir, &long_name, &short_name )))
549 /* Check against Unix name */
550 if (len == strlen(long_name))
552 if (!ignore_case)
554 if (!strncmp( long_name, name, len )) break;
556 else
558 if (!lstrncmpiA( long_name, name, len )) break;
561 if (dos_name[0])
563 /* Check against hashed DOS name */
564 if (!short_name)
566 DOSFS_Hash( long_name, tmp_buf, TRUE, ignore_case );
567 short_name = tmp_buf;
569 if (!strcmp( dos_name, short_name )) break;
572 if (ret)
574 if (long_buf) strcpy( long_buf, long_name );
575 if (short_buf)
577 if (short_name)
578 DOSFS_ToDosDTAFormat( short_name, short_buf );
579 else
580 DOSFS_Hash( long_name, short_buf, FALSE, ignore_case );
582 TRACE("(%s,%s) -> %s (%s)\n",
583 path, name, long_name, short_buf ? short_buf : "***");
585 else
586 WARN("'%s' not found in '%s'\n", name, path);
587 DOSFS_CloseDir( dir );
588 return ret;
592 /***********************************************************************
593 * DOSFS_GetDevice
595 * Check if a DOS file name represents a DOS device and return the device.
597 const DOS_DEVICE *DOSFS_GetDevice( const char *name )
599 int i;
600 const char *p;
602 if (!name) return NULL; /* if FILE_DupUnixHandle was used */
603 if (name[0] && (name[1] == ':')) name += 2;
604 if ((p = strrchr( name, '/' ))) name = p + 1;
605 if ((p = strrchr( name, '\\' ))) name = p + 1;
606 for (i = 0; i < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0]); i++)
608 const char *dev = DOSFS_Devices[i].name;
609 if (!lstrncmpiA( dev, name, strlen(dev) ))
611 p = name + strlen( dev );
612 if (!*p || (*p == '.')) return &DOSFS_Devices[i];
615 return NULL;
619 /***********************************************************************
620 * DOSFS_GetDeviceByHandle
622 const DOS_DEVICE *DOSFS_GetDeviceByHandle( HFILE hFile )
624 struct get_file_info_request *req = get_req_buffer();
626 req->handle = hFile;
627 if (!server_call( REQ_GET_FILE_INFO ) && (req->type == FILE_TYPE_UNKNOWN))
629 if ((req->attr >= 0) &&
630 (req->attr < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0])))
631 return &DOSFS_Devices[req->attr];
633 return NULL;
637 /***********************************************************************
638 * DOSFS_OpenDevice
640 * Open a DOS device. This might not map 1:1 into the UNIX device concept.
642 HFILE DOSFS_OpenDevice( const char *name, DWORD access )
644 int i;
645 const char *p;
647 if (!name) return (HFILE)NULL; /* if FILE_DupUnixHandle was used */
648 if (name[0] && (name[1] == ':')) name += 2;
649 if ((p = strrchr( name, '/' ))) name = p + 1;
650 if ((p = strrchr( name, '\\' ))) name = p + 1;
651 for (i = 0; i < sizeof(DOSFS_Devices)/sizeof(DOSFS_Devices[0]); i++)
653 const char *dev = DOSFS_Devices[i].name;
654 if (!lstrncmpiA( dev, name, strlen(dev) ))
656 p = name + strlen( dev );
657 if (!*p || (*p == '.')) {
658 /* got it */
659 if (!strcmp(DOSFS_Devices[i].name,"NUL"))
660 return FILE_CreateFile( "/dev/null", access,
661 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
662 OPEN_EXISTING, 0, -1, TRUE );
663 if (!strcmp(DOSFS_Devices[i].name,"CON")) {
664 HFILE to_dup;
665 HFILE handle;
666 switch (access & (GENERIC_READ|GENERIC_WRITE)) {
667 case GENERIC_READ:
668 to_dup = GetStdHandle( STD_INPUT_HANDLE );
669 break;
670 case GENERIC_WRITE:
671 to_dup = GetStdHandle( STD_OUTPUT_HANDLE );
672 break;
673 default:
674 FIXME("can't open CON read/write\n");
675 return HFILE_ERROR;
676 break;
678 if (!DuplicateHandle( GetCurrentProcess(), to_dup, GetCurrentProcess(),
679 &handle, 0, FALSE, DUPLICATE_SAME_ACCESS ))
680 handle = HFILE_ERROR;
681 return handle;
683 if (!strcmp(DOSFS_Devices[i].name,"SCSIMGR$") ||
684 !strcmp(DOSFS_Devices[i].name,"HPSCAN"))
686 return FILE_CreateDevice( i, access, NULL );
689 HFILE r;
690 char devname[40];
691 PROFILE_GetWineIniString("serialports",name,"",devname,sizeof devname);
693 if(devname[0])
695 TRACE_(file)("DOSFS_OpenDevice %s is %s\n",
696 DOSFS_Devices[i].name,devname);
697 r = FILE_CreateFile( devname, access,
698 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
699 OPEN_EXISTING, 0, -1, TRUE );
700 TRACE_(file)("Create_File return %08X\n",r);
701 return r;
705 FIXME("device open %s not supported (yet)\n",DOSFS_Devices[i].name);
706 return HFILE_ERROR;
710 return HFILE_ERROR;
714 /***********************************************************************
715 * DOSFS_GetPathDrive
717 * Get the drive specified by a given path name (DOS or Unix format).
719 static int DOSFS_GetPathDrive( const char **name )
721 int drive;
722 const char *p = *name;
724 if (*p && (p[1] == ':'))
726 drive = toupper(*p) - 'A';
727 *name += 2;
729 else if (*p == '/') /* Absolute Unix path? */
731 if ((drive = DRIVE_FindDriveRoot( name )) == -1)
733 MESSAGE("Warning: %s not accessible from a DOS drive\n", *name );
734 /* Assume it really was a DOS name */
735 drive = DRIVE_GetCurrentDrive();
738 else drive = DRIVE_GetCurrentDrive();
740 if (!DRIVE_IsValid(drive))
742 SetLastError( ERROR_INVALID_DRIVE );
743 return -1;
745 return drive;
749 /***********************************************************************
750 * DOSFS_GetFullName
752 * Convert a file name (DOS or mixed DOS/Unix format) to a valid
753 * Unix name / short DOS name pair.
754 * Return FALSE if one of the path components does not exist. The last path
755 * component is only checked if 'check_last' is non-zero.
756 * The buffers pointed to by 'long_buf' and 'short_buf' must be
757 * at least MAX_PATHNAME_LEN long.
759 BOOL DOSFS_GetFullName( LPCSTR name, BOOL check_last, DOS_FULL_NAME *full )
761 BOOL found;
762 UINT flags;
763 char *p_l, *p_s, *root;
765 TRACE("%s (last=%d)\n", name, check_last );
767 if ((full->drive = DOSFS_GetPathDrive( &name )) == -1) return FALSE;
768 flags = DRIVE_GetFlags( full->drive );
770 lstrcpynA( full->long_name, DRIVE_GetRoot( full->drive ),
771 sizeof(full->long_name) );
772 if (full->long_name[1]) root = full->long_name + strlen(full->long_name);
773 else root = full->long_name; /* root directory */
775 strcpy( full->short_name, "A:\\" );
776 full->short_name[0] += full->drive;
778 if ((*name == '\\') || (*name == '/')) /* Absolute path */
780 while ((*name == '\\') || (*name == '/')) name++;
782 else /* Relative path */
784 lstrcpynA( root + 1, DRIVE_GetUnixCwd( full->drive ),
785 sizeof(full->long_name) - (root - full->long_name) - 1 );
786 if (root[1]) *root = '/';
787 lstrcpynA( full->short_name + 3, DRIVE_GetDosCwd( full->drive ),
788 sizeof(full->short_name) - 3 );
791 p_l = full->long_name[1] ? full->long_name + strlen(full->long_name)
792 : full->long_name;
793 p_s = full->short_name[3] ? full->short_name + strlen(full->short_name)
794 : full->short_name + 2;
795 found = TRUE;
797 while (*name && found)
799 /* Check for '.' and '..' */
801 if (*name == '.')
803 if (IS_END_OF_NAME(name[1]))
805 name++;
806 while ((*name == '\\') || (*name == '/')) name++;
807 continue;
809 else if ((name[1] == '.') && IS_END_OF_NAME(name[2]))
811 name += 2;
812 while ((*name == '\\') || (*name == '/')) name++;
813 while ((p_l > root) && (*p_l != '/')) p_l--;
814 while ((p_s > full->short_name + 2) && (*p_s != '\\')) p_s--;
815 *p_l = *p_s = '\0'; /* Remove trailing separator */
816 continue;
820 /* Make sure buffers are large enough */
822 if ((p_s >= full->short_name + sizeof(full->short_name) - 14) ||
823 (p_l >= full->long_name + sizeof(full->long_name) - 1))
825 SetLastError( ERROR_PATH_NOT_FOUND );
826 return FALSE;
829 /* Get the long and short name matching the file name */
831 if ((found = DOSFS_FindUnixName( full->long_name, name, p_l + 1,
832 sizeof(full->long_name) - (p_l - full->long_name) - 1,
833 p_s + 1, !(flags & DRIVE_CASE_SENSITIVE) )))
835 *p_l++ = '/';
836 p_l += strlen(p_l);
837 *p_s++ = '\\';
838 p_s += strlen(p_s);
839 while (!IS_END_OF_NAME(*name)) name++;
841 else if (!check_last)
843 *p_l++ = '/';
844 *p_s++ = '\\';
845 while (!IS_END_OF_NAME(*name) &&
846 (p_s < full->short_name + sizeof(full->short_name) - 1) &&
847 (p_l < full->long_name + sizeof(full->long_name) - 1))
849 *p_s++ = tolower(*name);
850 /* If the drive is case-sensitive we want to create new */
851 /* files in lower-case otherwise we can't reopen them */
852 /* under the same short name. */
853 if (flags & DRIVE_CASE_SENSITIVE) *p_l++ = tolower(*name);
854 else *p_l++ = *name;
855 name++;
857 /* Ignore trailing dots and spaces */
858 while(p_l[-1] == '.' || p_l[-1] == ' ') {
859 --p_l;
860 --p_s;
862 *p_l = *p_s = '\0';
864 while ((*name == '\\') || (*name == '/')) name++;
867 if (!found)
869 if (check_last)
871 SetLastError( ERROR_FILE_NOT_FOUND );
872 return FALSE;
874 if (*name) /* Not last */
876 SetLastError( ERROR_PATH_NOT_FOUND );
877 return FALSE;
880 if (!full->long_name[0]) strcpy( full->long_name, "/" );
881 if (!full->short_name[2]) strcpy( full->short_name + 2, "\\" );
882 TRACE("returning %s = %s\n", full->long_name, full->short_name );
883 return TRUE;
887 /***********************************************************************
888 * GetShortPathNameA (KERNEL32.271)
890 * NOTES
891 * observed:
892 * longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
893 * *longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
895 * more observations ( with NT 3.51 (WinDD) ):
896 * longpath <= 8.3 -> just copy longpath to shortpath
897 * longpath > 8.3 ->
898 * a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
899 * b) file does exist -> set the short filename.
900 * - trailing slashes are reproduced in the short name, even if the
901 * file is not a directory
902 * - the absolute/relative path of the short name is reproduced like found
903 * in the long name
904 * - longpath and shortpath may have the same adress
905 * Peter Ganten, 1999
907 DWORD WINAPI GetShortPathNameA( LPCSTR longpath, LPSTR shortpath,
908 DWORD shortlen )
910 DOS_FULL_NAME full_name;
911 LPSTR tmpshortpath;
912 DWORD sp = 0, lp = 0;
913 int tmplen, drive;
914 UINT flags;
916 TRACE("%s\n", debugstr_a(longpath));
918 if (!longpath) {
919 SetLastError(ERROR_INVALID_PARAMETER);
920 return 0;
922 if (!longpath[0]) {
923 SetLastError(ERROR_BAD_PATHNAME);
924 return 0;
927 if ( ( tmpshortpath = HeapAlloc ( GetProcessHeap(), 0, MAX_PATHNAME_LEN ) ) == NULL ) {
928 SetLastError ( ERROR_NOT_ENOUGH_MEMORY );
929 return 0;
932 /* check for drive letter */
933 if ( longpath[1] == ':' ) {
934 tmpshortpath[0] = longpath[0];
935 tmpshortpath[1] = ':';
936 sp = 2;
939 if ( ( drive = DOSFS_GetPathDrive ( &longpath )) == -1 ) return 0;
940 flags = DRIVE_GetFlags ( drive );
942 while ( longpath[lp] ) {
944 /* check for path delimiters and reproduce them */
945 if ( longpath[lp] == '\\' || longpath[lp] == '/' ) {
946 if (!sp || tmpshortpath[sp-1]!= '\\')
948 /* strip double "\\" */
949 tmpshortpath[sp] = '\\';
950 sp++;
952 tmpshortpath[sp]=0;/*terminate string*/
953 lp++;
954 continue;
957 tmplen = strcspn ( longpath + lp, "\\/" );
958 lstrcpynA ( tmpshortpath+sp, longpath + lp, tmplen+1 );
960 /* Check, if the current element is a valid dos name */
961 if ( DOSFS_ValidDOSName ( longpath + lp, !(flags & DRIVE_CASE_SENSITIVE) ) ) {
962 sp += tmplen;
963 lp += tmplen;
964 continue;
967 /* Check if the file exists and use the existing file name */
968 if ( DOSFS_GetFullName ( tmpshortpath, TRUE, &full_name ) ) {
969 lstrcpyA ( tmpshortpath+sp, strrchr ( full_name.short_name, '\\' ) + 1 );
970 sp += strlen ( tmpshortpath+sp );
971 lp += tmplen;
972 continue;
975 TRACE("not found!\n" );
976 SetLastError ( ERROR_FILE_NOT_FOUND );
977 return 0;
979 tmpshortpath[sp] = 0;
981 lstrcpynA ( shortpath, tmpshortpath, shortlen );
982 TRACE("returning %s\n", debugstr_a(shortpath) );
983 tmplen = strlen ( tmpshortpath );
984 HeapFree ( GetProcessHeap(), 0, tmpshortpath );
986 return tmplen;
990 /***********************************************************************
991 * GetShortPathNameW (KERNEL32.272)
993 DWORD WINAPI GetShortPathNameW( LPCWSTR longpath, LPWSTR shortpath,
994 DWORD shortlen )
996 LPSTR longpathA, shortpathA;
997 DWORD ret = 0;
999 longpathA = HEAP_strdupWtoA( GetProcessHeap(), 0, longpath );
1000 shortpathA = HeapAlloc ( GetProcessHeap(), 0, shortlen );
1002 ret = GetShortPathNameA ( longpathA, shortpathA, shortlen );
1003 lstrcpynAtoW ( shortpath, shortpathA, shortlen );
1005 HeapFree( GetProcessHeap(), 0, longpathA );
1006 HeapFree( GetProcessHeap(), 0, shortpathA );
1008 return ret;
1012 /***********************************************************************
1013 * GetLongPathNameA (KERNEL32.xxx)
1015 DWORD WINAPI GetLongPathNameA( LPCSTR shortpath, LPSTR longpath,
1016 DWORD longlen )
1018 DOS_FULL_NAME full_name;
1019 char *p, *r, *ll, *ss;
1021 if (!DOSFS_GetFullName( shortpath, TRUE, &full_name )) return 0;
1022 lstrcpynA( longpath, full_name.short_name, longlen );
1024 /* Do some hackery to get the long filename. */
1026 if (longpath) {
1027 ss=longpath+strlen(longpath);
1028 ll=full_name.long_name+strlen(full_name.long_name);
1029 p=NULL;
1030 while (ss>=longpath)
1032 /* FIXME: aren't we more paranoid, than needed? */
1033 while ((ss[0]=='\\') && (ss>=longpath)) ss--;
1034 p=ss;
1035 while ((ss[0]!='\\') && (ss>=longpath)) ss--;
1036 if (ss>=longpath)
1038 /* FIXME: aren't we more paranoid, than needed? */
1039 while ((ll[0]=='/') && (ll>=full_name.long_name)) ll--;
1040 while ((ll[0]!='/') && (ll>=full_name.long_name)) ll--;
1041 if (ll<full_name.long_name)
1043 ERR("Bad longname! (ss=%s ll=%s)\n This should never happen !\n"
1044 ,ss ,ll );
1045 return 0;
1050 /* FIXME: fix for names like "C:\\" (ie. with more '\'s) */
1051 if (p && p[2])
1053 p+=1;
1054 if ((p-longpath)>0) longlen -= (p-longpath);
1055 lstrcpynA( p, ll , longlen);
1057 /* Now, change all '/' to '\' */
1058 for (r=p; r<(p+longlen); r++ )
1059 if (r[0]=='/') r[0]='\\';
1060 return strlen(longpath) - strlen(p) + longlen;
1064 return strlen(longpath);
1068 /***********************************************************************
1069 * GetLongPathNameW (KERNEL32.269)
1071 DWORD WINAPI GetLongPathNameW( LPCWSTR shortpath, LPWSTR longpath,
1072 DWORD longlen )
1074 DOS_FULL_NAME full_name;
1075 DWORD ret = 0;
1076 LPSTR shortpathA = HEAP_strdupWtoA( GetProcessHeap(), 0, shortpath );
1078 /* FIXME: is it correct to always return a fully qualified short path? */
1079 if (DOSFS_GetFullName( shortpathA, TRUE, &full_name ))
1081 ret = strlen( full_name.short_name );
1082 lstrcpynAtoW( longpath, full_name.long_name, longlen );
1084 HeapFree( GetProcessHeap(), 0, shortpathA );
1085 return ret;
1089 /***********************************************************************
1090 * DOSFS_DoGetFullPathName
1092 * Implementation of GetFullPathNameA/W.
1094 * bon@elektron 000331:
1095 * A test for GetFullPathName with many patholotical case
1096 * gives now identical output for Wine and OSR2
1098 static DWORD DOSFS_DoGetFullPathName( LPCSTR name, DWORD len, LPSTR result,
1099 BOOL unicode )
1101 DWORD ret;
1102 DOS_FULL_NAME full_name;
1103 char *p,*q;
1104 const char * root;
1105 char drivecur[]="c:.";
1106 char driveletter=0;
1107 int namelen,drive=0;
1109 if ((strlen(name) >1)&& (name[1]==':'))
1110 /*drive letter given */
1112 driveletter = name[0];
1114 if ((strlen(name) >2)&& (name[1]==':') &&
1115 ((name[2]=='\\') || (name[2]=='/')))
1116 /*absolute path given */
1118 lstrcpynA(full_name.short_name,name,MAX_PATHNAME_LEN);
1119 drive = (int)toupper(name[0]) - 'A';
1121 else
1123 if (driveletter)
1124 drivecur[0]=driveletter;
1125 else
1126 strcpy(drivecur,".");
1127 if (!DOSFS_GetFullName( drivecur, FALSE, &full_name ))
1129 FIXME("internal: error getting drive/path\n");
1130 return 0;
1132 /* find path that drive letter substitutes*/
1133 drive = (int)toupper(full_name.short_name[0]) -0x41;
1134 root= DRIVE_GetRoot(drive);
1135 if (!root)
1137 FIXME("internal: error getting DOS Drive Root\n");
1138 return 0;
1140 p= full_name.long_name +strlen(root);
1141 /* append long name (= unix name) to drive */
1142 lstrcpynA(full_name.short_name+2,p,MAX_PATHNAME_LEN-3);
1143 /* append name to treat */
1144 namelen= strlen(full_name.short_name);
1145 p = (char*)name;
1146 if (driveletter)
1147 p += +2; /* skip drive name when appending */
1148 if (namelen +2 + strlen(p) > MAX_PATHNAME_LEN)
1150 FIXME("internal error: buffer too small\n");
1151 return 0;
1153 full_name.short_name[namelen++] ='\\';
1154 full_name.short_name[namelen] = 0;
1155 lstrcpynA(full_name.short_name +namelen,p,MAX_PATHNAME_LEN-namelen);
1157 /* reverse all slashes */
1158 for (p=full_name.short_name;
1159 p < full_name.short_name+strlen(full_name.short_name);
1160 p++)
1162 if ( *p == '/' )
1163 *p = '\\';
1165 /* Use memmove, as areas overlap*/
1166 /* Delete .. */
1167 while ((p = strstr(full_name.short_name,"\\..\\")))
1169 if (p > full_name.short_name+2)
1171 *p = 0;
1172 q = strrchr(full_name.short_name,'\\');
1173 memmove(q+1,p+4,strlen(p+4)+1);
1175 else
1177 memmove(full_name.short_name+3,p+4,strlen(p+4)+1);
1180 if ((full_name.short_name[2]=='.')&&(full_name.short_name[3]=='.'))
1182 /* This case istn't treated yet : c:..\test */
1183 memmove(full_name.short_name+2,full_name.short_name+4,
1184 strlen(full_name.short_name+4)+1);
1186 /* Delete . */
1187 while ((p = strstr(full_name.short_name,"\\.\\")))
1189 *(p+1) = 0;
1190 memmove(p+1,p+3,strlen(p+3)+1);
1192 if (!(DRIVE_GetFlags(drive) & DRIVE_CASE_PRESERVING))
1193 CharUpperA( full_name.short_name );
1194 namelen=strlen(full_name.short_name);
1195 if (!strcmp(full_name.short_name+namelen-3,"\\.."))
1197 /* one more starnge case: "c:\test\test1\.."
1198 return "c:\test"*/
1199 *(full_name.short_name+namelen-3)=0;
1200 q = strrchr(full_name.short_name,'\\');
1201 *q =0;
1203 if (full_name.short_name[namelen-1]=='.')
1204 full_name.short_name[(namelen--)-1] =0;
1205 if (!driveletter)
1206 if (full_name.short_name[namelen-1]=='\\')
1207 full_name.short_name[(namelen--)-1] =0;
1208 TRACE("got %s\n",full_name.short_name);
1210 /* If the lpBuffer buffer is too small, the return value is the
1211 size of the buffer, in characters, required to hold the path
1212 plus the terminating \0 (tested against win95osr, bon 001118)
1213 . */
1214 ret = strlen(full_name.short_name);
1215 if (ret >= len )
1217 /* don't touch anything when the buffer is not large enough */
1218 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1219 return ret+1;
1221 if (result)
1223 if (unicode)
1224 lstrcpynAtoW( (LPWSTR)result, full_name.short_name, len );
1225 else
1226 lstrcpynA( result, full_name.short_name, len );
1229 TRACE("returning '%s'\n", full_name.short_name );
1230 return ret;
1234 /***********************************************************************
1235 * GetFullPathNameA (KERNEL32.272)
1236 * NOTES
1237 * if the path closed with '\', *lastpart is 0
1239 DWORD WINAPI GetFullPathNameA( LPCSTR name, DWORD len, LPSTR buffer,
1240 LPSTR *lastpart )
1242 DWORD ret = DOSFS_DoGetFullPathName( name, len, buffer, FALSE );
1243 if (ret && (ret<=len) && buffer && lastpart)
1245 LPSTR p = buffer + strlen(buffer);
1247 if (*p != '\\')
1249 while ((p > buffer + 2) && (*p != '\\')) p--;
1250 *lastpart = p + 1;
1252 else *lastpart = NULL;
1254 return ret;
1258 /***********************************************************************
1259 * GetFullPathNameW (KERNEL32.273)
1261 DWORD WINAPI GetFullPathNameW( LPCWSTR name, DWORD len, LPWSTR buffer,
1262 LPWSTR *lastpart )
1264 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
1265 DWORD ret = DOSFS_DoGetFullPathName( nameA, len, (LPSTR)buffer, TRUE );
1266 HeapFree( GetProcessHeap(), 0, nameA );
1267 if (ret && (ret<=len) && buffer && lastpart)
1269 LPWSTR p = buffer + lstrlenW(buffer);
1270 if (*p != (WCHAR)'\\')
1272 while ((p > buffer + 2) && (*p != (WCHAR)'\\')) p--;
1273 *lastpart = p + 1;
1275 else *lastpart = NULL;
1277 return ret;
1280 /***********************************************************************
1281 * DOSFS_FindNextEx
1283 static int DOSFS_FindNextEx( FIND_FIRST_INFO *info, WIN32_FIND_DATAA *entry )
1285 BYTE attr = info->attr | FA_UNUSED | FA_ARCHIVE | FA_RDONLY;
1286 UINT flags = DRIVE_GetFlags( info->drive );
1287 char *p, buffer[MAX_PATHNAME_LEN];
1288 const char *drive_path;
1289 int drive_root;
1290 LPCSTR long_name, short_name;
1291 BY_HANDLE_FILE_INFORMATION fileinfo;
1292 char dos_name[13];
1294 if ((info->attr & ~(FA_UNUSED | FA_ARCHIVE | FA_RDONLY)) == FA_LABEL)
1296 if (info->cur_pos) return 0;
1297 entry->dwFileAttributes = FILE_ATTRIBUTE_LABEL;
1298 DOSFS_UnixTimeToFileTime( (time_t)0, &entry->ftCreationTime, 0 );
1299 DOSFS_UnixTimeToFileTime( (time_t)0, &entry->ftLastAccessTime, 0 );
1300 DOSFS_UnixTimeToFileTime( (time_t)0, &entry->ftLastWriteTime, 0 );
1301 entry->nFileSizeHigh = 0;
1302 entry->nFileSizeLow = 0;
1303 entry->dwReserved0 = 0;
1304 entry->dwReserved1 = 0;
1305 DOSFS_ToDosDTAFormat( DRIVE_GetLabel( info->drive ), entry->cFileName );
1306 strcpy( entry->cAlternateFileName, entry->cFileName );
1307 info->cur_pos++;
1308 return 1;
1311 drive_path = info->path + strlen(DRIVE_GetRoot( info->drive ));
1312 while ((*drive_path == '/') || (*drive_path == '\\')) drive_path++;
1313 drive_root = !*drive_path;
1315 lstrcpynA( buffer, info->path, sizeof(buffer) - 1 );
1316 strcat( buffer, "/" );
1317 p = buffer + strlen(buffer);
1319 while (DOSFS_ReadDir( info->dir, &long_name, &short_name ))
1321 info->cur_pos++;
1323 /* Don't return '.' and '..' in the root of the drive */
1324 if (drive_root && (long_name[0] == '.') &&
1325 (!long_name[1] || ((long_name[1] == '.') && !long_name[2])))
1326 continue;
1328 /* Check the long mask */
1330 if (info->long_mask)
1332 if (!DOSFS_MatchLong( info->long_mask, long_name,
1333 flags & DRIVE_CASE_SENSITIVE )) continue;
1336 /* Check the short mask */
1338 if (info->short_mask)
1340 if (!short_name)
1342 DOSFS_Hash( long_name, dos_name, TRUE,
1343 !(flags & DRIVE_CASE_SENSITIVE) );
1344 short_name = dos_name;
1346 if (!DOSFS_MatchShort( info->short_mask, short_name )) continue;
1349 /* Check the file attributes */
1351 lstrcpynA( p, long_name, sizeof(buffer) - (int)(p - buffer) );
1352 if (!FILE_Stat( buffer, &fileinfo ))
1354 WARN("can't stat %s\n", buffer);
1355 continue;
1357 if (fileinfo.dwFileAttributes & ~attr) continue;
1359 /* We now have a matching entry; fill the result and return */
1361 entry->dwFileAttributes = fileinfo.dwFileAttributes;
1362 entry->ftCreationTime = fileinfo.ftCreationTime;
1363 entry->ftLastAccessTime = fileinfo.ftLastAccessTime;
1364 entry->ftLastWriteTime = fileinfo.ftLastWriteTime;
1365 entry->nFileSizeHigh = fileinfo.nFileSizeHigh;
1366 entry->nFileSizeLow = fileinfo.nFileSizeLow;
1368 if (short_name)
1369 DOSFS_ToDosDTAFormat( short_name, entry->cAlternateFileName );
1370 else
1371 DOSFS_Hash( long_name, entry->cAlternateFileName, FALSE,
1372 !(flags & DRIVE_CASE_SENSITIVE) );
1374 lstrcpynA( entry->cFileName, long_name, sizeof(entry->cFileName) );
1375 if (!(flags & DRIVE_CASE_PRESERVING)) CharLowerA( entry->cFileName );
1376 TRACE("returning %s (%s) %02lx %ld\n",
1377 entry->cFileName, entry->cAlternateFileName,
1378 entry->dwFileAttributes, entry->nFileSizeLow );
1379 return 1;
1381 return 0; /* End of directory */
1384 /***********************************************************************
1385 * DOSFS_FindNext
1387 * Find the next matching file. Return the number of entries read to find
1388 * the matching one, or 0 if no more entries.
1389 * 'short_mask' is the 8.3 mask (in FCB format), 'long_mask' is the long
1390 * file name mask. Either or both can be NULL.
1392 * NOTE: This is supposed to be only called by the int21 emulation
1393 * routines. Thus, we should own the Win16Mutex anyway.
1394 * Nevertheless, we explicitly enter it to ensure the static
1395 * directory cache is protected.
1397 int DOSFS_FindNext( const char *path, const char *short_mask,
1398 const char *long_mask, int drive, BYTE attr,
1399 int skip, WIN32_FIND_DATAA *entry )
1401 static FIND_FIRST_INFO info = { NULL };
1402 LPCSTR short_name, long_name;
1403 int count;
1405 SYSLEVEL_EnterWin16Lock();
1407 /* Check the cached directory */
1408 if (!(info.dir && info.path == path && info.short_mask == short_mask
1409 && info.long_mask == long_mask && info.drive == drive
1410 && info.attr == attr && info.cur_pos <= skip))
1412 /* Not in the cache, open it anew */
1413 if (info.dir) DOSFS_CloseDir( info.dir );
1415 info.path = (LPSTR)path;
1416 info.long_mask = (LPSTR)long_mask;
1417 info.short_mask = (LPSTR)short_mask;
1418 info.attr = attr;
1419 info.drive = drive;
1420 info.cur_pos = 0;
1421 info.dir = DOSFS_OpenDir( info.path );
1424 /* Skip to desired position */
1425 while (info.cur_pos < skip)
1426 if (info.dir && DOSFS_ReadDir( info.dir, &long_name, &short_name ))
1427 info.cur_pos++;
1428 else
1429 break;
1431 if (info.dir && info.cur_pos == skip && DOSFS_FindNextEx( &info, entry ))
1432 count = info.cur_pos - skip;
1433 else
1434 count = 0;
1436 if (!count)
1438 if (info.dir) DOSFS_CloseDir( info.dir );
1439 memset( &info, '\0', sizeof(info) );
1442 SYSLEVEL_LeaveWin16Lock();
1444 return count;
1447 /*************************************************************************
1448 * FindFirstFileExA (KERNEL32)
1450 HANDLE WINAPI FindFirstFileExA(
1451 LPCSTR lpFileName,
1452 FINDEX_INFO_LEVELS fInfoLevelId,
1453 LPVOID lpFindFileData,
1454 FINDEX_SEARCH_OPS fSearchOp,
1455 LPVOID lpSearchFilter,
1456 DWORD dwAdditionalFlags)
1458 DOS_FULL_NAME full_name;
1459 HGLOBAL handle;
1460 FIND_FIRST_INFO *info;
1462 if ((fSearchOp != FindExSearchNameMatch) || (dwAdditionalFlags != 0))
1464 FIXME("options not implemented 0x%08x 0x%08lx\n", fSearchOp, dwAdditionalFlags );
1465 return INVALID_HANDLE_VALUE;
1468 switch(fInfoLevelId)
1470 case FindExInfoStandard:
1472 WIN32_FIND_DATAA * data = (WIN32_FIND_DATAA *) lpFindFileData;
1473 data->dwReserved0 = data->dwReserved1 = 0x0;
1474 if (!lpFileName) return 0;
1475 if (!DOSFS_GetFullName( lpFileName, FALSE, &full_name )) break;
1476 if (!(handle = GlobalAlloc(GMEM_MOVEABLE, sizeof(FIND_FIRST_INFO)))) break;
1477 info = (FIND_FIRST_INFO *)GlobalLock( handle );
1478 info->path = HEAP_strdupA( GetProcessHeap(), 0, full_name.long_name );
1479 info->long_mask = strrchr( info->path, '/' );
1480 *(info->long_mask++) = '\0';
1481 info->short_mask = NULL;
1482 info->attr = 0xff;
1483 if (lpFileName[0] && (lpFileName[1] == ':'))
1484 info->drive = toupper(*lpFileName) - 'A';
1485 else info->drive = DRIVE_GetCurrentDrive();
1486 info->cur_pos = 0;
1488 info->dir = DOSFS_OpenDir( info->path );
1490 GlobalUnlock( handle );
1491 if (!FindNextFileA( handle, data ))
1493 FindClose( handle );
1494 SetLastError( ERROR_NO_MORE_FILES );
1495 break;
1497 return handle;
1499 break;
1500 default:
1501 FIXME("fInfoLevelId 0x%08x not implemented\n", fInfoLevelId );
1503 return INVALID_HANDLE_VALUE;
1506 /*************************************************************************
1507 * FindFirstFileA (KERNEL32.123)
1509 HANDLE WINAPI FindFirstFileA(
1510 LPCSTR lpFileName,
1511 WIN32_FIND_DATAA *lpFindData )
1513 return FindFirstFileExA(lpFileName, FindExInfoStandard, lpFindData,
1514 FindExSearchNameMatch, NULL, 0);
1517 /*************************************************************************
1518 * FindFirstFileExW (KERNEL32)
1520 HANDLE WINAPI FindFirstFileExW(
1521 LPCWSTR lpFileName,
1522 FINDEX_INFO_LEVELS fInfoLevelId,
1523 LPVOID lpFindFileData,
1524 FINDEX_SEARCH_OPS fSearchOp,
1525 LPVOID lpSearchFilter,
1526 DWORD dwAdditionalFlags)
1528 HANDLE handle;
1529 WIN32_FIND_DATAA dataA;
1530 LPVOID _lpFindFileData;
1531 LPSTR pathA;
1533 switch(fInfoLevelId)
1535 case FindExInfoStandard:
1537 _lpFindFileData = &dataA;
1539 break;
1540 default:
1541 FIXME("fInfoLevelId 0x%08x not implemented\n", fInfoLevelId );
1542 return INVALID_HANDLE_VALUE;
1545 pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFileName );
1546 handle = FindFirstFileExA(pathA, fInfoLevelId, _lpFindFileData, fSearchOp, lpSearchFilter, dwAdditionalFlags);
1547 HeapFree( GetProcessHeap(), 0, pathA );
1548 if (handle == INVALID_HANDLE_VALUE) return handle;
1550 switch(fInfoLevelId)
1552 case FindExInfoStandard:
1554 WIN32_FIND_DATAW *dataW = (WIN32_FIND_DATAW*) lpFindFileData;
1555 dataW->dwFileAttributes = dataA.dwFileAttributes;
1556 dataW->ftCreationTime = dataA.ftCreationTime;
1557 dataW->ftLastAccessTime = dataA.ftLastAccessTime;
1558 dataW->ftLastWriteTime = dataA.ftLastWriteTime;
1559 dataW->nFileSizeHigh = dataA.nFileSizeHigh;
1560 dataW->nFileSizeLow = dataA.nFileSizeLow;
1561 lstrcpyAtoW( dataW->cFileName, dataA.cFileName );
1562 lstrcpyAtoW( dataW->cAlternateFileName, dataA.cAlternateFileName );
1564 break;
1565 default:
1566 FIXME("fInfoLevelId 0x%08x not implemented\n", fInfoLevelId );
1567 return INVALID_HANDLE_VALUE;
1569 return handle;
1572 /*************************************************************************
1573 * FindFirstFileW (KERNEL32.124)
1575 HANDLE WINAPI FindFirstFileW( LPCWSTR lpFileName, WIN32_FIND_DATAW *lpFindData )
1577 return FindFirstFileExW(lpFileName, FindExInfoStandard, lpFindData,
1578 FindExSearchNameMatch, NULL, 0);
1581 /*************************************************************************
1582 * FindNextFileA (KERNEL32.126)
1584 BOOL WINAPI FindNextFileA( HANDLE handle, WIN32_FIND_DATAA *data )
1586 FIND_FIRST_INFO *info;
1588 if ((handle == INVALID_HANDLE_VALUE) ||
1589 !(info = (FIND_FIRST_INFO *)GlobalLock( handle )))
1591 SetLastError( ERROR_INVALID_HANDLE );
1592 return FALSE;
1594 GlobalUnlock( handle );
1595 if (!info->path || !info->dir)
1597 SetLastError( ERROR_NO_MORE_FILES );
1598 return FALSE;
1600 if (!DOSFS_FindNextEx( info, data ))
1602 DOSFS_CloseDir( info->dir ); info->dir = NULL;
1603 HeapFree( GetProcessHeap(), 0, info->path );
1604 info->path = info->long_mask = NULL;
1605 SetLastError( ERROR_NO_MORE_FILES );
1606 return FALSE;
1608 return TRUE;
1612 /*************************************************************************
1613 * FindNextFileW (KERNEL32.127)
1615 BOOL WINAPI FindNextFileW( HANDLE handle, WIN32_FIND_DATAW *data )
1617 WIN32_FIND_DATAA dataA;
1618 if (!FindNextFileA( handle, &dataA )) return FALSE;
1619 data->dwFileAttributes = dataA.dwFileAttributes;
1620 data->ftCreationTime = dataA.ftCreationTime;
1621 data->ftLastAccessTime = dataA.ftLastAccessTime;
1622 data->ftLastWriteTime = dataA.ftLastWriteTime;
1623 data->nFileSizeHigh = dataA.nFileSizeHigh;
1624 data->nFileSizeLow = dataA.nFileSizeLow;
1625 lstrcpyAtoW( data->cFileName, dataA.cFileName );
1626 lstrcpyAtoW( data->cAlternateFileName, dataA.cAlternateFileName );
1627 return TRUE;
1630 /*************************************************************************
1631 * FindClose (KERNEL32.119)
1633 BOOL WINAPI FindClose( HANDLE handle )
1635 FIND_FIRST_INFO *info;
1637 if ((handle == INVALID_HANDLE_VALUE) ||
1638 !(info = (FIND_FIRST_INFO *)GlobalLock( handle )))
1640 SetLastError( ERROR_INVALID_HANDLE );
1641 return FALSE;
1643 if (info->dir) DOSFS_CloseDir( info->dir );
1644 if (info->path) HeapFree( GetProcessHeap(), 0, info->path );
1645 GlobalUnlock( handle );
1646 GlobalFree( handle );
1647 return TRUE;
1650 /***********************************************************************
1651 * DOSFS_UnixTimeToFileTime
1653 * Convert a Unix time to FILETIME format.
1654 * The FILETIME structure is a 64-bit value representing the number of
1655 * 100-nanosecond intervals since January 1, 1601, 0:00.
1656 * 'remainder' is the nonnegative number of 100-ns intervals
1657 * corresponding to the time fraction smaller than 1 second that
1658 * couldn't be stored in the time_t value.
1660 void DOSFS_UnixTimeToFileTime( time_t unix_time, FILETIME *filetime,
1661 DWORD remainder )
1663 /* NOTES:
1665 CONSTANTS:
1666 The time difference between 1 January 1601, 00:00:00 and
1667 1 January 1970, 00:00:00 is 369 years, plus the leap years
1668 from 1604 to 1968, excluding 1700, 1800, 1900.
1669 This makes (1968 - 1600) / 4 - 3 = 89 leap days, and a total
1670 of 134774 days.
1672 Any day in that period had 24 * 60 * 60 = 86400 seconds.
1674 The time difference is 134774 * 86400 * 10000000, which can be written
1675 116444736000000000
1676 27111902 * 2^32 + 3577643008
1677 413 * 2^48 + 45534 * 2^32 + 54590 * 2^16 + 32768
1679 If you find that these constants are buggy, please change them in all
1680 instances in both conversion functions.
1682 VERSIONS:
1683 There are two versions, one of them uses long long variables and
1684 is presumably faster but not ISO C. The other one uses standard C
1685 data types and operations but relies on the assumption that negative
1686 numbers are stored as 2's complement (-1 is 0xffff....). If this
1687 assumption is violated, dates before 1970 will not convert correctly.
1688 This should however work on any reasonable architecture where WINE
1689 will run.
1691 DETAILS:
1693 Take care not to remove the casts. I have tested these functions
1694 (in both versions) for a lot of numbers. I would be interested in
1695 results on other compilers than GCC.
1697 The operations have been designed to account for the possibility
1698 of 64-bit time_t in future UNICES. Even the versions without
1699 internal long long numbers will work if time_t only is 64 bit.
1700 A 32-bit shift, which was necessary for that operation, turned out
1701 not to work correctly in GCC, besides giving the warning. So I
1702 used a double 16-bit shift instead. Numbers are in the ISO version
1703 represented by three limbs, the most significant with 32 bit, the
1704 other two with 16 bit each.
1706 As the modulo-operator % is not well-defined for negative numbers,
1707 negative divisors have been avoided in DOSFS_FileTimeToUnixTime.
1709 There might be quicker ways to do this in C. Certainly so in
1710 assembler.
1712 Claus Fischer, fischer@iue.tuwien.ac.at
1715 #if SIZEOF_LONG_LONG >= 8
1716 # define USE_LONG_LONG 1
1717 #else
1718 # define USE_LONG_LONG 0
1719 #endif
1721 #if USE_LONG_LONG /* gcc supports long long type */
1723 long long int t = unix_time;
1724 t *= 10000000;
1725 t += 116444736000000000LL;
1726 t += remainder;
1727 filetime->dwLowDateTime = (UINT)t;
1728 filetime->dwHighDateTime = (UINT)(t >> 32);
1730 #else /* ISO version */
1732 UINT a0; /* 16 bit, low bits */
1733 UINT a1; /* 16 bit, medium bits */
1734 UINT a2; /* 32 bit, high bits */
1736 /* Copy the unix time to a2/a1/a0 */
1737 a0 = unix_time & 0xffff;
1738 a1 = (unix_time >> 16) & 0xffff;
1739 /* This is obsolete if unix_time is only 32 bits, but it does not hurt.
1740 Do not replace this by >> 32, it gives a compiler warning and it does
1741 not work. */
1742 a2 = (unix_time >= 0 ? (unix_time >> 16) >> 16 :
1743 ~((~unix_time >> 16) >> 16));
1745 /* Multiply a by 10000000 (a = a2/a1/a0)
1746 Split the factor into 10000 * 1000 which are both less than 0xffff. */
1747 a0 *= 10000;
1748 a1 = a1 * 10000 + (a0 >> 16);
1749 a2 = a2 * 10000 + (a1 >> 16);
1750 a0 &= 0xffff;
1751 a1 &= 0xffff;
1753 a0 *= 1000;
1754 a1 = a1 * 1000 + (a0 >> 16);
1755 a2 = a2 * 1000 + (a1 >> 16);
1756 a0 &= 0xffff;
1757 a1 &= 0xffff;
1759 /* Add the time difference and the remainder */
1760 a0 += 32768 + (remainder & 0xffff);
1761 a1 += 54590 + (remainder >> 16 ) + (a0 >> 16);
1762 a2 += 27111902 + (a1 >> 16);
1763 a0 &= 0xffff;
1764 a1 &= 0xffff;
1766 /* Set filetime */
1767 filetime->dwLowDateTime = (a1 << 16) + a0;
1768 filetime->dwHighDateTime = a2;
1769 #endif
1773 /***********************************************************************
1774 * DOSFS_FileTimeToUnixTime
1776 * Convert a FILETIME format to Unix time.
1777 * If not NULL, 'remainder' contains the fractional part of the filetime,
1778 * in the range of [0..9999999] (even if time_t is negative).
1780 time_t DOSFS_FileTimeToUnixTime( const FILETIME *filetime, DWORD *remainder )
1782 /* Read the comment in the function DOSFS_UnixTimeToFileTime. */
1783 #if USE_LONG_LONG
1785 long long int t = filetime->dwHighDateTime;
1786 t <<= 32;
1787 t += (UINT)filetime->dwLowDateTime;
1788 t -= 116444736000000000LL;
1789 if (t < 0)
1791 if (remainder) *remainder = 9999999 - (-t - 1) % 10000000;
1792 return -1 - ((-t - 1) / 10000000);
1794 else
1796 if (remainder) *remainder = t % 10000000;
1797 return t / 10000000;
1800 #else /* ISO version */
1802 UINT a0; /* 16 bit, low bits */
1803 UINT a1; /* 16 bit, medium bits */
1804 UINT a2; /* 32 bit, high bits */
1805 UINT r; /* remainder of division */
1806 unsigned int carry; /* carry bit for subtraction */
1807 int negative; /* whether a represents a negative value */
1809 /* Copy the time values to a2/a1/a0 */
1810 a2 = (UINT)filetime->dwHighDateTime;
1811 a1 = ((UINT)filetime->dwLowDateTime ) >> 16;
1812 a0 = ((UINT)filetime->dwLowDateTime ) & 0xffff;
1814 /* Subtract the time difference */
1815 if (a0 >= 32768 ) a0 -= 32768 , carry = 0;
1816 else a0 += (1 << 16) - 32768 , carry = 1;
1818 if (a1 >= 54590 + carry) a1 -= 54590 + carry, carry = 0;
1819 else a1 += (1 << 16) - 54590 - carry, carry = 1;
1821 a2 -= 27111902 + carry;
1823 /* If a is negative, replace a by (-1-a) */
1824 negative = (a2 >= ((UINT)1) << 31);
1825 if (negative)
1827 /* Set a to -a - 1 (a is a2/a1/a0) */
1828 a0 = 0xffff - a0;
1829 a1 = 0xffff - a1;
1830 a2 = ~a2;
1833 /* Divide a by 10000000 (a = a2/a1/a0), put the rest into r.
1834 Split the divisor into 10000 * 1000 which are both less than 0xffff. */
1835 a1 += (a2 % 10000) << 16;
1836 a2 /= 10000;
1837 a0 += (a1 % 10000) << 16;
1838 a1 /= 10000;
1839 r = a0 % 10000;
1840 a0 /= 10000;
1842 a1 += (a2 % 1000) << 16;
1843 a2 /= 1000;
1844 a0 += (a1 % 1000) << 16;
1845 a1 /= 1000;
1846 r += (a0 % 1000) * 10000;
1847 a0 /= 1000;
1849 /* If a was negative, replace a by (-1-a) and r by (9999999 - r) */
1850 if (negative)
1852 /* Set a to -a - 1 (a is a2/a1/a0) */
1853 a0 = 0xffff - a0;
1854 a1 = 0xffff - a1;
1855 a2 = ~a2;
1857 r = 9999999 - r;
1860 if (remainder) *remainder = r;
1862 /* Do not replace this by << 32, it gives a compiler warning and it does
1863 not work. */
1864 return ((((time_t)a2) << 16) << 16) + (a1 << 16) + a0;
1865 #endif
1869 /***********************************************************************
1870 * MulDiv (KERNEL32.391)
1871 * RETURNS
1872 * Result of multiplication and division
1873 * -1: Overflow occurred or Divisor was 0
1875 INT WINAPI MulDiv(
1876 INT nMultiplicand,
1877 INT nMultiplier,
1878 INT nDivisor)
1880 #if SIZEOF_LONG_LONG >= 8
1881 long long ret;
1883 if (!nDivisor) return -1;
1885 /* We want to deal with a positive divisor to simplify the logic. */
1886 if (nDivisor < 0)
1888 nMultiplicand = - nMultiplicand;
1889 nDivisor = -nDivisor;
1892 /* If the result is positive, we "add" to round. else, we subtract to round. */
1893 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
1894 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
1895 ret = (((long long)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
1896 else
1897 ret = (((long long)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
1899 if ((ret > 2147483647) || (ret < -2147483647)) return -1;
1900 return ret;
1901 #else
1902 if (!nDivisor) return -1;
1904 /* We want to deal with a positive divisor to simplify the logic. */
1905 if (nDivisor < 0)
1907 nMultiplicand = - nMultiplicand;
1908 nDivisor = -nDivisor;
1911 /* If the result is positive, we "add" to round. else, we subtract to round. */
1912 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
1913 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
1914 return ((nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
1916 return ((nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
1918 #endif
1922 /***********************************************************************
1923 * DosDateTimeToFileTime (KERNEL32.76)
1925 BOOL WINAPI DosDateTimeToFileTime( WORD fatdate, WORD fattime, LPFILETIME ft)
1927 struct tm newtm;
1929 newtm.tm_sec = (fattime & 0x1f) * 2;
1930 newtm.tm_min = (fattime >> 5) & 0x3f;
1931 newtm.tm_hour = (fattime >> 11);
1932 newtm.tm_mday = (fatdate & 0x1f);
1933 newtm.tm_mon = ((fatdate >> 5) & 0x0f) - 1;
1934 newtm.tm_year = (fatdate >> 9) + 80;
1935 DOSFS_UnixTimeToFileTime( mktime( &newtm ), ft, 0 );
1936 return TRUE;
1940 /***********************************************************************
1941 * FileTimeToDosDateTime (KERNEL32.111)
1943 BOOL WINAPI FileTimeToDosDateTime( const FILETIME *ft, LPWORD fatdate,
1944 LPWORD fattime )
1946 time_t unixtime = DOSFS_FileTimeToUnixTime( ft, NULL );
1947 struct tm *tm = localtime( &unixtime );
1948 if (fattime)
1949 *fattime = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
1950 if (fatdate)
1951 *fatdate = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5)
1952 + tm->tm_mday;
1953 return TRUE;
1957 /***********************************************************************
1958 * LocalFileTimeToFileTime (KERNEL32.373)
1960 BOOL WINAPI LocalFileTimeToFileTime( const FILETIME *localft,
1961 LPFILETIME utcft )
1963 struct tm *xtm;
1964 DWORD remainder;
1966 /* convert from local to UTC. Perhaps not correct. FIXME */
1967 time_t unixtime = DOSFS_FileTimeToUnixTime( localft, &remainder );
1968 xtm = gmtime( &unixtime );
1969 DOSFS_UnixTimeToFileTime( mktime(xtm), utcft, remainder );
1970 return TRUE;
1974 /***********************************************************************
1975 * FileTimeToLocalFileTime (KERNEL32.112)
1977 BOOL WINAPI FileTimeToLocalFileTime( const FILETIME *utcft,
1978 LPFILETIME localft )
1980 DWORD remainder;
1981 /* convert from UTC to local. Perhaps not correct. FIXME */
1982 time_t unixtime = DOSFS_FileTimeToUnixTime( utcft, &remainder );
1983 #ifdef HAVE_TIMEGM
1984 struct tm *xtm = localtime( &unixtime );
1985 time_t localtime;
1987 localtime = timegm(xtm);
1988 DOSFS_UnixTimeToFileTime( localtime, localft, remainder );
1990 #else
1991 struct tm *xtm,*gtm;
1992 time_t time1,time2;
1994 xtm = localtime( &unixtime );
1995 gtm = gmtime( &unixtime );
1996 time1 = mktime(xtm);
1997 time2 = mktime(gtm);
1998 DOSFS_UnixTimeToFileTime( 2*time1-time2, localft, remainder );
1999 #endif
2000 return TRUE;
2004 /***********************************************************************
2005 * FileTimeToSystemTime (KERNEL32.113)
2007 BOOL WINAPI FileTimeToSystemTime( const FILETIME *ft, LPSYSTEMTIME syst )
2009 struct tm *xtm;
2010 DWORD remainder;
2011 time_t xtime = DOSFS_FileTimeToUnixTime( ft, &remainder );
2012 xtm = gmtime(&xtime);
2013 syst->wYear = xtm->tm_year+1900;
2014 syst->wMonth = xtm->tm_mon + 1;
2015 syst->wDayOfWeek = xtm->tm_wday;
2016 syst->wDay = xtm->tm_mday;
2017 syst->wHour = xtm->tm_hour;
2018 syst->wMinute = xtm->tm_min;
2019 syst->wSecond = xtm->tm_sec;
2020 syst->wMilliseconds = remainder / 10000;
2021 return TRUE;
2024 /***********************************************************************
2025 * QueryDosDeviceA (KERNEL32.413)
2027 * returns array of strings terminated by \0, terminated by \0
2029 DWORD WINAPI QueryDosDeviceA(LPCSTR devname,LPSTR target,DWORD bufsize)
2031 LPSTR s;
2032 char buffer[200];
2034 TRACE("(%s,...)\n", devname ? devname : "<null>");
2035 if (!devname) {
2036 /* return known MSDOS devices */
2037 strcpy(buffer,"CON COM1 COM2 LPT1 NUL ");
2038 while ((s=strchr(buffer,' ')))
2039 *s='\0';
2041 lstrcpynA(target,buffer,bufsize);
2042 return strlen(buffer);
2044 strcpy(buffer,"\\DEV\\");
2045 strcat(buffer,devname);
2046 if ((s=strchr(buffer,':'))) *s='\0';
2047 lstrcpynA(target,buffer,bufsize);
2048 return strlen(buffer);
2052 /***********************************************************************
2053 * QueryDosDeviceW (KERNEL32.414)
2055 * returns array of strings terminated by \0, terminated by \0
2057 DWORD WINAPI QueryDosDeviceW(LPCWSTR devname,LPWSTR target,DWORD bufsize)
2059 LPSTR devnameA = devname?HEAP_strdupWtoA(GetProcessHeap(),0,devname):NULL;
2060 LPSTR targetA = (LPSTR)HeapAlloc(GetProcessHeap(),0,bufsize);
2061 DWORD ret = QueryDosDeviceA(devnameA,targetA,bufsize);
2063 lstrcpynAtoW(target,targetA,bufsize);
2064 if (devnameA) HeapFree(GetProcessHeap(),0,devnameA);
2065 if (targetA) HeapFree(GetProcessHeap(),0,targetA);
2066 return ret;
2070 /***********************************************************************
2071 * SystemTimeToFileTime (KERNEL32.526)
2073 BOOL WINAPI SystemTimeToFileTime( const SYSTEMTIME *syst, LPFILETIME ft )
2075 #ifdef HAVE_TIMEGM
2076 struct tm xtm;
2077 time_t utctime;
2078 #else
2079 struct tm xtm,*local_tm,*utc_tm;
2080 time_t localtim,utctime;
2081 #endif
2083 xtm.tm_year = syst->wYear-1900;
2084 xtm.tm_mon = syst->wMonth - 1;
2085 xtm.tm_wday = syst->wDayOfWeek;
2086 xtm.tm_mday = syst->wDay;
2087 xtm.tm_hour = syst->wHour;
2088 xtm.tm_min = syst->wMinute;
2089 xtm.tm_sec = syst->wSecond; /* this is UTC */
2090 xtm.tm_isdst = -1;
2091 #ifdef HAVE_TIMEGM
2092 utctime = timegm(&xtm);
2093 DOSFS_UnixTimeToFileTime( utctime, ft,
2094 syst->wMilliseconds * 10000 );
2095 #else
2096 localtim = mktime(&xtm); /* now we've got local time */
2097 local_tm = localtime(&localtim);
2098 utc_tm = gmtime(&localtim);
2099 utctime = mktime(utc_tm);
2100 DOSFS_UnixTimeToFileTime( 2*localtim -utctime, ft,
2101 syst->wMilliseconds * 10000 );
2102 #endif
2103 return TRUE;
2106 /***********************************************************************
2107 * DefineDosDeviceA (KERNEL32.182)
2109 BOOL WINAPI DefineDosDeviceA(DWORD flags,LPCSTR devname,LPCSTR targetpath) {
2110 FIXME("(0x%08lx,%s,%s),stub!\n",flags,devname,targetpath);
2111 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2112 return FALSE;
2116 --- 16 bit functions ---
2119 /*************************************************************************
2120 * FindFirstFile16 (KERNEL.413)
2122 HANDLE16 WINAPI FindFirstFile16( LPCSTR path, WIN32_FIND_DATAA *data )
2124 DOS_FULL_NAME full_name;
2125 HGLOBAL16 handle;
2126 FIND_FIRST_INFO *info;
2128 data->dwReserved0 = data->dwReserved1 = 0x0;
2129 if (!path) return 0;
2130 if (!DOSFS_GetFullName( path, FALSE, &full_name ))
2131 return INVALID_HANDLE_VALUE16;
2132 if (!(handle = GlobalAlloc16( GMEM_MOVEABLE, sizeof(FIND_FIRST_INFO) )))
2133 return INVALID_HANDLE_VALUE16;
2134 info = (FIND_FIRST_INFO *)GlobalLock16( handle );
2135 info->path = HEAP_strdupA( SystemHeap, 0, full_name.long_name );
2136 info->long_mask = strrchr( info->path, '/' );
2137 if (info->long_mask )
2138 *(info->long_mask++) = '\0';
2139 info->short_mask = NULL;
2140 info->attr = 0xff;
2141 if (path[0] && (path[1] == ':')) info->drive = toupper(*path) - 'A';
2142 else info->drive = DRIVE_GetCurrentDrive();
2143 info->cur_pos = 0;
2145 info->dir = DOSFS_OpenDir( info->path );
2147 GlobalUnlock16( handle );
2148 if (!FindNextFile16( handle, data ))
2150 FindClose16( handle );
2151 SetLastError( ERROR_NO_MORE_FILES );
2152 return INVALID_HANDLE_VALUE16;
2154 return handle;
2157 /*************************************************************************
2158 * FindNextFile16 (KERNEL.414)
2160 BOOL16 WINAPI FindNextFile16( HANDLE16 handle, WIN32_FIND_DATAA *data )
2162 FIND_FIRST_INFO *info;
2164 if ((handle == INVALID_HANDLE_VALUE16) ||
2165 !(info = (FIND_FIRST_INFO *)GlobalLock16( handle )))
2167 SetLastError( ERROR_INVALID_HANDLE );
2168 return FALSE;
2170 GlobalUnlock16( handle );
2171 if (!info->path || !info->dir)
2173 SetLastError( ERROR_NO_MORE_FILES );
2174 return FALSE;
2176 if (!DOSFS_FindNextEx( info, data ))
2178 DOSFS_CloseDir( info->dir ); info->dir = NULL;
2179 HeapFree( SystemHeap, 0, info->path );
2180 info->path = info->long_mask = NULL;
2181 SetLastError( ERROR_NO_MORE_FILES );
2182 return FALSE;
2184 return TRUE;
2187 /*************************************************************************
2188 * FindClose16 (KERNEL.415)
2190 BOOL16 WINAPI FindClose16( HANDLE16 handle )
2192 FIND_FIRST_INFO *info;
2194 if ((handle == INVALID_HANDLE_VALUE16) ||
2195 !(info = (FIND_FIRST_INFO *)GlobalLock16( handle )))
2197 SetLastError( ERROR_INVALID_HANDLE );
2198 return FALSE;
2200 if (info->dir) DOSFS_CloseDir( info->dir );
2201 if (info->path) HeapFree( SystemHeap, 0, info->path );
2202 GlobalUnlock16( handle );
2203 GlobalFree16( handle );
2204 return TRUE;