netapi32/tests: Fix a failing test on localized Windows.
[wine/hramrach.git] / programs / winecfg / drivedetect.c
blob8b9911a3c852088869d0c11787f0c81024fa8930
1 /*
2 * Drive autodetection code
4 * Copyright 2004 Mike Hearn
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #ifdef HAVE_MNTENT_H
27 #include <mntent.h>
28 #endif
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <sys/stat.h>
33 #include <windef.h>
34 #include <winbase.h>
35 #include <wine/debug.h>
36 #include <wine/library.h>
38 #include "winecfg.h"
39 #include "resource.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
44 BOOL gui_mode = TRUE;
45 static long working_mask = 0;
47 typedef struct
49 const char *szNode;
50 int nType;
51 } DEV_NODES;
53 #ifdef HAVE_MNTENT_H
55 static const DEV_NODES sDeviceNodes[] = {
56 {"/dev/fd", DRIVE_REMOVABLE},
57 {"/dev/pf", DRIVE_REMOVABLE},
58 {"/dev/aztcd", DRIVE_CDROM},
59 {"/dev/bpcd", DRIVE_CDROM},
60 {"/dev/cdrom", DRIVE_CDROM},
61 {"/dev/cdu535", DRIVE_CDROM},
62 {"/dev/cdwriter", DRIVE_CDROM},
63 {"/dev/cm205cd", DRIVE_CDROM},
64 {"/dev/cm206cd", DRIVE_CDROM},
65 {"/dev/gscd", DRIVE_CDROM},
66 {"/dev/hitcd", DRIVE_CDROM},
67 {"/dev/iseries/vcd", DRIVE_CDROM},
68 {"/dev/lmscd", DRIVE_CDROM},
69 {"/dev/mcd", DRIVE_CDROM},
70 {"/dev/optcd", DRIVE_CDROM},
71 {"/dev/pcd", DRIVE_CDROM},
72 {"/dev/sbpcd", DRIVE_CDROM},
73 {"/dev/scd", DRIVE_CDROM},
74 {"/dev/sjcd", DRIVE_CDROM},
75 {"/dev/sonycd", DRIVE_CDROM},
76 {"/dev/sr", DRIVE_CDROM},
77 {"",0}
80 static const char * const ignored_fstypes[] = {
81 "devpts",
82 "tmpfs",
83 "proc",
84 "sysfs",
85 "swap",
86 "usbdevfs",
87 "rpc_pipefs",
88 "binfmt_misc",
89 NULL
92 static const char * const ignored_mnt_dirs[] = {
93 "/boot",
94 NULL
97 static int try_dev_node(char *dev)
99 const DEV_NODES *pDevNodes = sDeviceNodes;
101 while(pDevNodes->szNode[0])
103 if(!strncmp(dev,pDevNodes->szNode,strlen(pDevNodes->szNode)))
104 return pDevNodes->nType;
105 ++pDevNodes;
108 return DRIVE_FIXED;
111 static BOOL should_ignore_fstype(char *type)
113 const char * const *s;
115 for (s = ignored_fstypes; *s; s++)
116 if (!strcmp(*s, type)) return TRUE;
118 return FALSE;
121 static BOOL should_ignore_mnt_dir(char *dir)
123 const char * const *s;
125 for (s = ignored_mnt_dirs; *s; s++)
126 if (!strcmp(*s, dir)) return TRUE;
128 return FALSE;
131 static BOOL is_drive_defined(char *path)
133 int i;
135 for (i = 0; i < 26; i++)
136 if (drives[i].in_use && !strcmp(drives[i].unixpath, path)) return TRUE;
138 return FALSE;
141 /* returns Z + 1 if there are no more available letters */
142 static char allocate_letter(int type)
144 char letter, start;
146 if (type == DRIVE_REMOVABLE)
147 start = 'A';
148 else
149 start = 'C';
151 for (letter = start; letter <= 'Z'; letter++)
152 if ((DRIVE_MASK_BIT(letter) & working_mask) != 0) break;
154 return letter;
156 #endif
158 #define FSTAB_OPEN 1
159 #define NO_MORE_LETTERS 2
160 #define NO_ROOT 3
161 #define NO_DRIVE_C 4
162 #define NO_HOME 5
164 static void report_error(int code)
166 char *buffer;
167 int len;
169 switch (code)
171 case FSTAB_OPEN:
172 if (gui_mode)
174 static const char s[] = "Could not open your mountpoint description table.\n\nOpening of /etc/fstab failed: %s";
175 len = snprintf(NULL, 0, s, strerror(errno));
176 buffer = HeapAlloc(GetProcessHeap(), 0, len + 1);
177 snprintf(buffer, len, s, strerror(errno));
178 MessageBox(NULL, s, "", MB_OK | MB_ICONEXCLAMATION);
179 HeapFree(GetProcessHeap(), 0, buffer);
181 else
183 fprintf(stderr, "winecfg: could not open fstab: %s\n", strerror(errno));
185 break;
187 case NO_MORE_LETTERS:
188 if (gui_mode) MessageBox(NULL, "No more letters are available to auto-detect available drives with.", "", MB_OK | MB_ICONEXCLAMATION);
189 fprintf(stderr, "winecfg: no more available letters while scanning /etc/fstab\n");
190 break;
192 case NO_ROOT:
193 if (gui_mode) MessageBox(NULL, "Could not ensure that the root directory was mapped.\n\n"
194 "This can happen if you run out of drive letters. "
195 "It's important to have the root directory mapped, otherwise Wine"
196 "will not be able to always find the programs you want to run. "
197 "Try unmapping a drive letter then trying again.", "",
198 MB_OK | MB_ICONEXCLAMATION);
199 else fprintf(stderr, "winecfg: unable to map root drive\n");
200 break;
202 case NO_DRIVE_C:
203 if (gui_mode)
204 MessageBox(NULL, "No virtual drive C mapped\n\nTry running wineprefixcreate", "", MB_OK | MB_ICONEXCLAMATION);
205 else
206 fprintf(stderr, "winecfg: no drive_c directory\n");
208 case NO_HOME:
209 if (gui_mode)
210 MessageBox(NULL, "Could not ensure that your home directory was mapped.\n\n"
211 "This can happen if you run out of drive letters. "
212 "Try unmapping a drive letter then try again.", "",
213 MB_OK | MB_ICONEXCLAMATION);
214 else
215 fprintf(stderr, "winecfg: unable to map home drive\n");
216 break;
220 static void ensure_root_is_mapped(void)
222 int i;
223 BOOL mapped = FALSE;
225 for (i = 0; i < 26; i++)
226 if (drives[i].in_use && !strcmp(drives[i].unixpath, "/")) mapped = TRUE;
228 if (!mapped)
230 /* work backwards from Z, trying to map it */
231 char letter;
233 for (letter = 'Z'; letter >= 'A'; letter--)
235 if (!drives[letter - 'A'].in_use)
237 add_drive(letter, "/", NULL, 0, DRIVE_FIXED);
238 WINE_TRACE("allocated drive %c as the root drive\n", letter);
239 break;
243 if (letter == ('A' - 1)) report_error(NO_ROOT);
247 static void ensure_home_is_mapped(void)
249 int i;
250 BOOL mapped = FALSE;
251 char *home = getenv("HOME");
253 if (!home) return;
255 for (i = 0; i < 26; i++)
256 if (drives[i].in_use && !strcmp(drives[i].unixpath, home)) mapped = TRUE;
258 if (!mapped)
260 char letter;
262 for (letter = 'H'; letter <= 'Z'; letter++)
264 if (!drives[letter - 'A'].in_use)
266 add_drive(letter, home, NULL, 0, DRIVE_FIXED);
267 WINE_TRACE("allocated drive %c as the user's home directory\n", letter);
268 break;
271 if (letter == ('Z' + 1)) report_error(NO_HOME);
275 static void ensure_drive_c_is_mapped(void)
277 struct stat buf;
278 const char *configdir = wine_get_config_dir();
279 int len;
280 char *drive_c_dir;
282 if (drives[2].in_use) return;
284 len = snprintf(NULL, 0, "%s/../drive_c", configdir);
285 drive_c_dir = HeapAlloc(GetProcessHeap(), 0, len);
286 snprintf(drive_c_dir, len, "%s/../drive_c", configdir);
287 HeapFree(GetProcessHeap(), 0, drive_c_dir);
289 if (stat(drive_c_dir, &buf) == 0)
291 WCHAR label[64];
292 LoadStringW (GetModuleHandle (NULL), IDS_SYSTEM_DRIVE_LABEL, label,
293 sizeof(label)/sizeof(label[0]));
294 add_drive('C', "../drive_c", label, 0, DRIVE_FIXED);
296 else
298 report_error(NO_DRIVE_C);
302 int autodetect_drives(void)
304 #ifdef HAVE_MNTENT_H
305 struct mntent *ent;
306 FILE *fstab;
307 #endif
309 /* we want to build a list of autodetected drives, then ensure each entry
310 exists in the users setup. so, we superimpose the autodetected drives
311 onto whatever is pre-existing.
313 for now let's just rummage around inside the fstab.
316 load_drives();
318 working_mask = drive_available_mask('\0');
320 #ifdef HAVE_MNTENT_H
321 fstab = fopen("/etc/fstab", "r");
322 if (!fstab)
324 report_error(FSTAB_OPEN);
325 return FALSE;
328 while ((ent = getmntent(fstab)))
330 char letter;
331 int type;
333 WINE_TRACE("ent->mnt_dir=%s\n", ent->mnt_dir);
335 if (should_ignore_fstype(ent->mnt_type)) continue;
336 if (should_ignore_mnt_dir(ent->mnt_dir)) continue;
337 if (is_drive_defined(ent->mnt_dir)) continue;
339 if (!strcmp(ent->mnt_type, "nfs")) type = DRIVE_REMOTE;
340 else if (!strcmp(ent->mnt_type, "nfs4")) type = DRIVE_REMOTE;
341 else if (!strcmp(ent->mnt_type, "smbfs")) type = DRIVE_REMOTE;
342 else if (!strcmp(ent->mnt_type, "cifs")) type = DRIVE_REMOTE;
343 else if (!strcmp(ent->mnt_type, "coda")) type = DRIVE_REMOTE;
344 else if (!strcmp(ent->mnt_type, "iso9660")) type = DRIVE_CDROM;
345 else if (!strcmp(ent->mnt_type, "ramfs")) type = DRIVE_RAMDISK;
346 else type = try_dev_node(ent->mnt_fsname);
348 /* allocate a drive for it */
349 letter = allocate_letter(type);
350 if (letter == ']')
352 report_error(NO_MORE_LETTERS);
353 fclose(fstab);
354 return FALSE;
357 WINE_TRACE("adding drive %c for %s, type %s\n", letter, ent->mnt_dir, ent->mnt_type);
358 add_drive(letter, ent->mnt_dir, NULL, 0, type);
360 /* working_mask is a map of the drive letters still available. */
361 working_mask &= ~DRIVE_MASK_BIT(letter);
364 fclose(fstab);
365 #endif
367 ensure_root_is_mapped();
369 ensure_drive_c_is_mapped();
371 ensure_home_is_mapped();
373 return TRUE;