Cygwin: pinfo: use stpcpy where appropriate
[newlib-cygwin.git] / winsup / cygwin / fhandler / cygdrive.cc
blob1ac1d5d4f3a61b47ad7da596e97c82542d9e23fb
1 /* fhandler_cygdrive.cc
3 This file is part of Cygwin.
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
7 details. */
9 #include "winsup.h"
10 #include <lm.h>
11 #include <sys/statvfs.h>
12 #include "cygerrno.h"
13 #include "security.h"
14 #include "path.h"
15 #include "fhandler.h"
16 #include "dtable.h"
17 #include "cygheap.h"
18 #include "shared_info.h"
20 #define _LIBC
21 #include <dirent.h>
23 fhandler_cygdrive::fhandler_cygdrive () :
24 fhandler_disk_file ()
28 int
29 fhandler_cygdrive::open (int flags, mode_t mode)
31 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
33 set_errno (EEXIST);
34 return 0;
36 if (flags & O_WRONLY)
38 set_errno (EISDIR);
39 return 0;
41 /* Open a fake handle to \\Device\\Null */
42 return open_null (flags);
45 int
46 fhandler_cygdrive::fstat (struct stat *buf)
48 fhandler_base::fstat (buf);
49 buf->st_ino = 2;
50 buf->st_mode = S_IFDIR | STD_RBITS | STD_XBITS;
51 buf->st_nlink = 1;
52 return 0;
55 int
56 fhandler_cygdrive::fstatvfs (struct statvfs *sfs)
58 /* Virtual file system. Just return an empty buffer with a few values
59 set to something useful. Just as on Linux. */
60 memset (sfs, 0, sizeof (*sfs));
61 sfs->f_bsize = sfs->f_frsize = 4096;
62 sfs->f_flag = ST_RDONLY;
63 sfs->f_namemax = NAME_MAX;
64 return 0;
67 #define MAX_DRIVE_BUF_LEN (sizeof ("x:\\") * 26 + 2)
69 struct __DIR_drives
71 char *pdrive;
72 char pbuf[MAX_DRIVE_BUF_LEN];
75 #define d_drives(d) ((__DIR_drives *) (d)->__d_internal)
77 DIR *
78 fhandler_cygdrive::opendir (int fd)
80 DIR *dir;
82 dir = fhandler_disk_file::opendir (fd);
83 if (dir)
85 dir->__d_internal = (uintptr_t) new __DIR_drives;
86 GetLogicalDriveStrings (MAX_DRIVE_BUF_LEN, d_drives(dir)->pbuf);
87 d_drives(dir)->pdrive = d_drives(dir)->pbuf;
90 return dir;
93 int
94 fhandler_cygdrive::readdir (DIR *dir, dirent *de)
96 WCHAR drive[] = L"X:";
98 while (true)
100 if (!d_drives(dir)->pdrive || !*d_drives(dir)->pdrive)
102 if (!(dir->__flags & dirent_saw_dot))
104 de->d_name[0] = '.';
105 de->d_name[1] = '\0';
106 de->d_ino = 2;
108 return ENMFILE;
110 disk_type dt = get_disk_type ((drive[0] = *d_drives(dir)->pdrive, drive));
111 if (dt == DT_SHARE_SMB)
113 /* Calling NetUseGetInfo on SMB drives allows to fetch the
114 current state of the drive without trying to open a file
115 descriptor on the share (GetFileAttributes). This avoids
116 waiting for SMB timeouts. Of course, there's a downside:
117 If a drive becomes availabe again, it can take a couple of
118 minutes to recognize it. As long as this didn't happen,
119 the drive will not show up in the cygdrive dir. */
120 PUSE_INFO_1 pui1;
121 DWORD status;
123 if (NetUseGetInfo (NULL, drive, 1, (PBYTE *) &pui1) == NERR_Success)
125 status = pui1->ui1_status;
126 NetApiBufferFree (pui1);
127 if (status == USE_OK)
128 break;
131 else if (dt != DT_FLOPPY
132 && GetFileAttributes (d_drives(dir)->pdrive) != INVALID_FILE_ATTRIBUTES)
133 break;
134 d_drives(dir)->pdrive = strchr (d_drives(dir)->pdrive, '\0') + 1;
136 *de->d_name = cyg_tolower (*d_drives(dir)->pdrive);
137 de->d_name[1] = '\0';
138 user_shared->warned_msdos = true;
139 de->d_ino = readdir_get_ino (d_drives(dir)->pdrive, false);
140 dir->__d_position++;
141 d_drives(dir)->pdrive = strchr (d_drives(dir)->pdrive, '\0') + 1;
142 syscall_printf ("%p = readdir (%p) (%s)", &de, dir, de->d_name);
143 return 0;
146 void
147 fhandler_cygdrive::rewinddir (DIR *dir)
149 d_drives(dir)->pdrive = d_drives(dir)->pbuf;
150 dir->__d_position = 0;
154 fhandler_cygdrive::closedir (DIR *dir)
156 delete d_drives(dir);
157 return 0;