msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / kernel32 / oldconfig.c
blob20b6eaaeff511fd8f55f53426214dd6e891c493e
1 /*
2 * Support for converting from old configuration format
4 * Copyright 2005 Alexandre Julliard
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
20 * NOTES
21 * This file should be removed after a suitable transition period.
24 #include "config.h"
25 #include "wine/port.h"
27 #include <stdarg.h>
28 #include <sys/types.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
33 #endif
34 #include <fcntl.h>
35 #ifdef HAVE_DIRENT_H
36 # include <dirent.h>
37 #endif
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
40 #endif
41 #ifdef HAVE_LINUX_HDREG_H
42 # include <linux/hdreg.h>
43 #endif
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
47 #include "windef.h"
48 #include "winbase.h"
49 #include "winternl.h"
50 #include "ntddscsi.h"
51 #include "wine/library.h"
52 #include "wine/unicode.h"
53 #include "wine/debug.h"
54 #include "kernel_private.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(reg);
59 /* registry initialisation, allocates some default keys. */
60 static ULONG allocate_default_keys(void)
62 static const WCHAR StatDataW[] = {'D','y','n','D','a','t','a','\\',
63 'P','e','r','f','S','t','a','t','s','\\',
64 'S','t','a','t','D','a','t','a',0};
65 static const WCHAR ConfigManagerW[] = {'D','y','n','D','a','t','a','\\',
66 'C','o','n','f','i','g',' ','M','a','n','a','g','e','r','\\',
67 'E','n','u','m',0};
68 HANDLE hkey;
69 ULONG dispos;
70 OBJECT_ATTRIBUTES attr;
71 UNICODE_STRING nameW;
73 attr.Length = sizeof(attr);
74 attr.RootDirectory = 0;
75 attr.ObjectName = &nameW;
76 attr.Attributes = 0;
77 attr.SecurityDescriptor = NULL;
78 attr.SecurityQualityOfService = NULL;
80 RtlInitUnicodeString( &nameW, StatDataW );
81 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, &dispos )) NtClose( hkey );
82 if (dispos == REG_OPENED_EXISTING_KEY)
83 return dispos; /* someone else already loaded the registry */
85 RtlInitUnicodeString( &nameW, ConfigManagerW );
86 if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) NtClose( hkey );
88 return dispos;
91 /******************************************************************
92 * create_scsi_entry
94 * Initializes registry to contain scsi info about the cdrom in NT.
95 * All devices (even not real scsi ones) have this info in NT.
96 * NOTE: programs usually read these registry entries after sending the
97 * IOCTL_SCSI_GET_ADDRESS ioctl to the cdrom
99 static void create_scsi_entry( PSCSI_ADDRESS scsi_addr, LPCSTR lpDriver, UINT uDriveType,
100 LPSTR lpDriveName, LPSTR lpUnixDeviceName )
102 static UCHAR uCdromNumber = 0;
103 static UCHAR uTapeNumber = 0;
105 OBJECT_ATTRIBUTES attr;
106 UNICODE_STRING nameW;
107 WCHAR dataW[50];
108 DWORD sizeW;
109 char buffer[40];
110 DWORD value;
111 const char *data;
112 HANDLE scsiKey;
113 HANDLE portKey;
114 HANDLE busKey;
115 HANDLE targetKey;
116 HANDLE lunKey;
117 DWORD disp;
119 attr.Length = sizeof(attr);
120 attr.RootDirectory = 0;
121 attr.ObjectName = &nameW;
122 attr.Attributes = 0;
123 attr.SecurityDescriptor = NULL;
124 attr.SecurityQualityOfService = NULL;
126 /* Ensure there is Scsi key */
127 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, "Machine\\HARDWARE\\DEVICEMAP\\Scsi" ) ||
128 NtCreateKey( &scsiKey, KEY_ALL_ACCESS, &attr, 0,
129 NULL, REG_OPTION_VOLATILE, &disp ))
131 ERR("Cannot create DEVICEMAP\\Scsi registry key\n" );
132 return;
134 RtlFreeUnicodeString( &nameW );
136 snprintf(buffer,sizeof(buffer),"Scsi Port %d",scsi_addr->PortNumber);
137 attr.RootDirectory = scsiKey;
138 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
139 NtCreateKey( &portKey, KEY_ALL_ACCESS, &attr, 0,
140 NULL, REG_OPTION_VOLATILE, &disp ))
142 ERR("Cannot create DEVICEMAP\\Scsi Port registry key\n" );
143 return;
145 RtlFreeUnicodeString( &nameW );
147 RtlCreateUnicodeStringFromAsciiz( &nameW, "Driver" );
148 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpDriver, strlen(lpDriver)+1);
149 NtSetValueKey( portKey, &nameW, 0, REG_SZ, (BYTE*)dataW, sizeW );
150 RtlFreeUnicodeString( &nameW );
151 value = 10;
152 RtlCreateUnicodeStringFromAsciiz( &nameW, "FirstBusTimeScanInMs" );
153 NtSetValueKey( portKey,&nameW, 0, REG_DWORD, (BYTE *)&value, sizeof(DWORD));
154 RtlFreeUnicodeString( &nameW );
156 value = 0;
157 if (strcmp(lpDriver, "atapi") == 0)
159 #ifdef HDIO_GET_DMA
160 int fd, dma;
162 fd = open(lpUnixDeviceName, O_RDONLY|O_NONBLOCK);
163 if (fd != -1)
165 if (ioctl(fd, HDIO_GET_DMA, &dma) != -1) value = dma;
166 close(fd);
168 #endif
169 RtlCreateUnicodeStringFromAsciiz( &nameW, "DMAEnabled" );
170 NtSetValueKey( portKey,&nameW, 0, REG_DWORD, (BYTE *)&value, sizeof(DWORD));
171 RtlFreeUnicodeString( &nameW );
174 snprintf(buffer, sizeof(buffer),"Scsi Bus %d", scsi_addr->PathId);
175 attr.RootDirectory = portKey;
176 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
177 NtCreateKey( &busKey, KEY_ALL_ACCESS, &attr, 0,
178 NULL, REG_OPTION_VOLATILE, &disp ))
180 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus registry key\n" );
181 return;
183 RtlFreeUnicodeString( &nameW );
185 attr.RootDirectory = busKey;
186 /* FIXME: get real controller Id for SCSI */
187 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, "Initiator Id 255" ) ||
188 NtCreateKey( &targetKey, KEY_ALL_ACCESS, &attr, 0,
189 NULL, REG_OPTION_VOLATILE, &disp ))
191 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus\\Initiator Id 255 registry key\n" );
192 return;
194 RtlFreeUnicodeString( &nameW );
195 NtClose( targetKey );
197 snprintf(buffer, sizeof(buffer),"Target Id %d", scsi_addr->TargetId);
198 attr.RootDirectory = busKey;
199 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
200 NtCreateKey( &targetKey, KEY_ALL_ACCESS, &attr, 0,
201 NULL, REG_OPTION_VOLATILE, &disp ))
203 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\n" );
204 return;
206 RtlFreeUnicodeString( &nameW );
208 snprintf(buffer, sizeof(buffer),"Logical Unit Id %d", scsi_addr->Lun);
209 attr.RootDirectory = targetKey;
210 if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
211 NtCreateKey( &lunKey, KEY_ALL_ACCESS, &attr, 0,
212 NULL, REG_OPTION_VOLATILE, &disp ))
214 ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\\Logical Unit Id\n" );
215 return;
217 RtlFreeUnicodeString( &nameW );
219 switch (uDriveType)
221 case DRIVE_NO_ROOT_DIR:
222 default:
223 data = "OtherPeripheral"; break;
224 case DRIVE_FIXED:
225 data = "DiskPeripheral"; break;
226 case DRIVE_REMOVABLE:
227 data = "TapePeripheral";
228 snprintf(buffer, sizeof(buffer), "Tape%d", uTapeNumber++);
229 break;
230 case DRIVE_CDROM:
231 data = "CdRomPeripheral";
232 snprintf(buffer, sizeof(buffer), "Cdrom%d", uCdromNumber++);
233 break;
235 RtlCreateUnicodeStringFromAsciiz( &nameW, "Type" );
236 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, data, strlen(data)+1);
237 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, (BYTE*)dataW, sizeW );
238 RtlFreeUnicodeString( &nameW );
240 RtlCreateUnicodeStringFromAsciiz( &nameW, "Identifier" );
241 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpDriveName, strlen(lpDriveName)+1);
242 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, (BYTE*)dataW, sizeW );
243 RtlFreeUnicodeString( &nameW );
245 if (uDriveType == DRIVE_CDROM || uDriveType == DRIVE_REMOVABLE)
247 RtlCreateUnicodeStringFromAsciiz( &nameW, "DeviceName" );
248 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, buffer, strlen(buffer)+1);
249 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, (BYTE*)dataW, sizeW );
250 RtlFreeUnicodeString( &nameW );
253 RtlCreateUnicodeStringFromAsciiz( &nameW, "UnixDeviceName" );
254 RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpUnixDeviceName, strlen(lpUnixDeviceName)+1);
255 NtSetValueKey( lunKey, &nameW, 0, REG_SZ, (BYTE*)dataW, sizeW );
256 RtlFreeUnicodeString( &nameW );
258 NtClose( lunKey );
259 NtClose( targetKey );
260 NtClose( busKey );
261 NtClose( portKey );
262 NtClose( scsiKey );
265 struct LinuxProcScsiDevice
267 int host;
268 int channel;
269 int target;
270 int lun;
271 char vendor[9];
272 char model[17];
273 char rev[5];
274 char type[33];
275 int ansirev;
279 * we need to declare white spaces explicitly via %*1[ ],
280 * as there are very dainbread CD-ROM devices out there
281 * which have their manufacturer name blanked out via spaces,
282 * which confuses fscanf's parsing (skips all blank spaces)
284 static int SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
286 int result;
288 result = fscanf( procfile,
289 "Host:%*1[ ]scsi%d%*1[ ]Channel:%*1[ ]%d%*1[ ]Id:%*1[ ]%d%*1[ ]Lun:%*1[ ]%d\n",
290 &dev->host,
291 &dev->channel,
292 &dev->target,
293 &dev->lun );
294 if( result == EOF )
296 /* "end of entries" return, so no TRACE() here */
297 return EOF;
299 if( result != 4 )
301 ERR("bus id line scan count error (fscanf returns %d, expected 4)\n", result);
302 return 0;
304 result = fscanf( procfile,
305 " Vendor:%*1[ ]%8c%*1[ ]Model:%*1[ ]%16c%*1[ ]Rev:%*1[ ]%4c\n",
306 dev->vendor,
307 dev->model,
308 dev->rev );
309 if( result != 3 )
311 ERR("model line scan count error (fscanf returns %d, expected 3)\n", result);
312 return 0;
315 result = fscanf( procfile,
316 " Type:%*3[ ]%32c%*1[ ]ANSI SCSI%*1[ ]revision:%*1[ ]%x\n",
317 dev->type,
318 &dev->ansirev );
319 if( result != 2 )
321 ERR("SCSI type line scan count error (fscanf returns %d, expected 2)\n", result);
322 return 0;
324 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
325 dev->vendor[8] = 0;
326 dev->model[16] = 0;
327 dev->rev[4] = 0;
328 dev->type[32] = 0;
330 return 1;
334 /* create the hardware registry branch */
335 static void create_hardware_branch(void)
337 /* The following mostly will work on Linux, but should not cause
338 * problems on other systems. */
339 static const char procname_ide_media[] = "/proc/ide/%s/media";
340 static const char procname_ide_model[] = "/proc/ide/%s/model";
341 static const char procname_scsi[] = "/proc/scsi/scsi";
342 DIR *idedir;
343 struct dirent *dent = NULL;
344 FILE *procfile = NULL;
345 char cStr[40], cDevModel[40], cUnixDeviceName[40], read1[10] = "\0", read2[10] = "\0";
346 SCSI_ADDRESS scsi_addr;
347 UINT nType;
348 struct LinuxProcScsiDevice dev;
349 int result = 0, nSgNumber = 0;
350 UCHAR uFirstSCSIPort = 0;
352 /* Enumerate all ide devices first */
353 idedir = opendir("/proc/ide");
354 if (idedir)
356 while ((dent = readdir(idedir)))
358 if (strncmp(dent->d_name, "hd", 2) == 0)
360 sprintf(cStr, procname_ide_media, dent->d_name);
361 procfile = fopen(cStr, "r");
362 if (!procfile)
364 ERR("Could not open %s\n", cStr);
365 continue;
366 } else {
367 fgets(cStr, sizeof(cStr), procfile);
368 fclose(procfile);
369 nType = DRIVE_UNKNOWN;
370 if (strncasecmp(cStr, "disk", 4) == 0) nType = DRIVE_FIXED;
371 if (strncasecmp(cStr, "cdrom", 5) == 0) nType = DRIVE_CDROM;
373 if (nType == DRIVE_UNKNOWN) continue;
376 sprintf(cStr, procname_ide_model, dent->d_name);
377 procfile = fopen(cStr, "r");
378 if (!procfile)
380 ERR("Could not open %s\n", cStr);
381 switch (nType)
383 case DRIVE_FIXED: strcpy(cDevModel, "Wine harddisk"); break;
384 case DRIVE_CDROM: strcpy(cDevModel, "Wine CDROM"); break;
386 } else {
387 fgets(cDevModel, sizeof(cDevModel), procfile);
388 fclose(procfile);
389 cDevModel[strlen(cDevModel) - 1] = 0;
392 sprintf(cUnixDeviceName, "/dev/%s", dent->d_name);
393 scsi_addr.PortNumber = (dent->d_name[2] - 'a') / 2;
394 scsi_addr.PathId = 0;
395 scsi_addr.TargetId = (dent->d_name[2] - 'a') % 2;
396 scsi_addr.Lun = 0;
397 if (scsi_addr.PortNumber + 1 > uFirstSCSIPort)
398 uFirstSCSIPort = scsi_addr.PortNumber + 1;
400 create_scsi_entry(&scsi_addr, "atapi", nType, cDevModel, cUnixDeviceName);
403 closedir(idedir);
406 /* Now goes SCSI */
407 procfile = fopen(procname_scsi, "r");
408 if (!procfile)
410 TRACE("Could not open %s\n", procname_scsi);
411 return;
413 fgets(cStr, 40, procfile);
414 sscanf(cStr, "Attached %9s %9s", read1, read2);
416 if (strcmp(read1, "devices:") != 0)
418 WARN("Incorrect %s format\n", procname_scsi);
419 fclose( procfile );
420 return;
422 if (strcmp(read2, "none") == 0)
424 TRACE("No SCSI devices found in %s.\n", procname_scsi);
425 fclose( procfile );
426 return;
429 /* Read info for one device */
430 while ((result = SCSI_getprocentry(procfile, &dev)) > 0)
432 scsi_addr.PortNumber = dev.host;
433 scsi_addr.PathId = dev.channel;
434 scsi_addr.TargetId = dev.target;
435 scsi_addr.Lun = dev.lun;
437 scsi_addr.PortNumber += uFirstSCSIPort;
438 if (strncmp(dev.type, "Direct-Access", 13) == 0) nType = DRIVE_FIXED;
439 else if (strncmp(dev.type, "Sequential-Access", 17) == 0) nType = DRIVE_REMOVABLE;
440 else if (strncmp(dev.type, "CD-ROM", 6) == 0) nType = DRIVE_CDROM;
441 else if (strncmp(dev.type, "Processor", 9) == 0) nType = DRIVE_NO_ROOT_DIR;
442 else continue;
444 strcpy(cDevModel, dev.vendor);
445 strcat(cDevModel, dev.model);
446 strcat(cDevModel, dev.rev);
447 sprintf(cUnixDeviceName, "/dev/sg%d", nSgNumber++);
448 /* FIXME: get real driver name */
449 create_scsi_entry(&scsi_addr, "WINE SCSI", nType, cDevModel, cUnixDeviceName);
451 if( result != EOF )
452 WARN("Incorrect %s format\n", procname_scsi);
453 fclose( procfile );
457 /***********************************************************************
458 * convert_old_config
460 void convert_old_config(void)
462 if (allocate_default_keys() == REG_OPENED_EXISTING_KEY)
463 return; /* someone else already loaded the registry */
465 /* create some hardware keys (FIXME: should not be done here) */
466 create_hardware_branch();