Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

518 lines
13 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. vdm.c
  5. Abstract:
  6. Routines to configure the MS-DOS subsystem.
  7. Author:
  8. Ted Miller (tedm) 27-Apr-1995
  9. Revision History:
  10. --*/
  11. #include "setupp.h"
  12. #pragma hdrstop
  13. //
  14. // Keywords whose presence in config.sys indicate OS/2.
  15. //
  16. PCSTR Os2ConfigSysKeywords[] = { "DISKCACHE", "LIBPATH", "PAUSEONERROR",
  17. "RMSIZE", "RUN", "SWAPPATH",
  18. "IOPL", "MAXWAIT", "MEMMAN",
  19. "PRIORITY", "PROTSHELL", "PROTECTONLY",
  20. "THREADS", "TIMESLICE", "TRACE",
  21. "TRACEBUF", "DEVINFO", NULL
  22. };
  23. //
  24. // Keywords we migrate from the user's existing DOS config.sys
  25. // into config.nt.
  26. //
  27. #define NUM_DOS_KEYWORDS 4
  28. PCSTR DosConfigSysKeywords[NUM_DOS_KEYWORDS] = { "FCBS","BREAK","LASTDRIVE","FILES" };
  29. BOOL
  30. DosConfigSysExists(
  31. IN PCWSTR Filename
  32. );
  33. BOOL
  34. CreateConfigNt(
  35. IN PCWSTR ConfigDos,
  36. IN PCWSTR ConfigTmp,
  37. IN PCWSTR ConfigNt
  38. );
  39. PSTR
  40. IsolateFirstField(
  41. IN PSTR Line,
  42. OUT PSTR *End,
  43. OUT PCHAR Terminator
  44. );
  45. BOOL
  46. ConfigureMsDosSubsystem(
  47. VOID
  48. )
  49. /*++
  50. Routine Description:
  51. Configure the 16-bit MS-DOS subsystem.
  52. Currently this means creating config.nt and autoexec.nt.
  53. It also means creating empty config.sys, autoexec.bat, io.sys and msdos.sys
  54. if these files don't already exist.
  55. On the upgrade, the only thing that we do is to create the empty files,
  56. if they don't already exist.
  57. Arguments:
  58. None.
  59. Return Value:
  60. Boolean value indicating outcome.
  61. --*/
  62. {
  63. WCHAR ConfigDos[] = L"?:\\CONFIG.SYS";
  64. WCHAR ConfigTmp[MAX_PATH];
  65. WCHAR ConfigNt[MAX_PATH];
  66. WCHAR AutoexecDos[] = L"?:\\AUTOEXEC.BAT";
  67. WCHAR AutoexecTmp[MAX_PATH];
  68. WCHAR AutoexecNt[MAX_PATH];
  69. WCHAR IoSysFile[] = L"?:\\IO.SYS";
  70. WCHAR MsDosSysFile[] = L"?:\\MSDOS.SYS";
  71. WCHAR ControlIniFile[MAX_PATH];
  72. BOOL b;
  73. DWORD Result;
  74. ULONG i;
  75. HANDLE FileHandle;
  76. PWSTR DosFileNames[] = {
  77. ConfigDos,
  78. AutoexecDos,
  79. IoSysFile,
  80. MsDosSysFile,
  81. ControlIniFile
  82. };
  83. DWORD DosFileAttributes[] = {
  84. FILE_ATTRIBUTE_NORMAL,
  85. FILE_ATTRIBUTE_NORMAL,
  86. FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY,
  87. FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY,
  88. FILE_ATTRIBUTE_NORMAL
  89. };
  90. //
  91. // Fill in drive letter of system partition.
  92. //
  93. #ifdef _X86_
  94. ConfigDos[0] = x86SystemPartitionDrive;
  95. AutoexecDos[0] = x86SystemPartitionDrive;
  96. IoSysFile[0] = x86SystemPartitionDrive;
  97. MsDosSysFile[0] = x86SystemPartitionDrive;
  98. #else
  99. ConfigDos[0] = L'C';
  100. AutoexecDos[0] = L'C';
  101. IoSysFile[0] = L'C';
  102. MsDosSysFile[0] = L'C';
  103. #endif
  104. //
  105. // Build path to control.ini file
  106. //
  107. Result = GetWindowsDirectory(ControlIniFile, MAX_PATH - ARRAYSIZE(L"control.ini"));
  108. if( Result == 0) {
  109. MYASSERT(FALSE);
  110. return FALSE;
  111. }
  112. pSetupConcatenatePaths(ControlIniFile,L"control.ini",MAX_PATH,NULL);
  113. //
  114. // Create empty config.sys, autoexec.bat, io.sys, msdos.sys and control.ini
  115. // if they don't exist. This is because some 16-bit apps depend on
  116. // these files and SudeepB wants this moved from VDM to setup.
  117. //
  118. for( i = 0; i < sizeof( DosFileNames ) / sizeof( PWSTR ); i++ ) {
  119. FileHandle = CreateFile( DosFileNames[i],
  120. GENERIC_READ | GENERIC_WRITE,
  121. 0, // File is not to be shared
  122. NULL, // No security attributes
  123. CREATE_NEW, // Create only if it doesn't exist
  124. DosFileAttributes[i],
  125. NULL ); // No extended attributes
  126. if( FileHandle != INVALID_HANDLE_VALUE ) {
  127. CloseHandle( FileHandle );
  128. }
  129. }
  130. if( Upgrade ) {
  131. return( TRUE );
  132. }
  133. //
  134. // Form filenames.
  135. //
  136. if(!GetSystemDirectory(ConfigTmp,MAX_PATH)){
  137. MYASSERT(FALSE);
  138. }
  139. lstrcpy(ConfigNt,ConfigTmp);
  140. lstrcpy(AutoexecNt,ConfigTmp);
  141. lstrcpy(AutoexecTmp,ConfigTmp);
  142. pSetupConcatenatePaths(ConfigTmp,L"CONFIG.TMP",MAX_PATH,NULL);
  143. pSetupConcatenatePaths(ConfigNt,L"CONFIG.NT",MAX_PATH,NULL);
  144. pSetupConcatenatePaths(AutoexecTmp,L"AUTOEXEC.TMP",MAX_PATH,NULL);
  145. pSetupConcatenatePaths(AutoexecNt,L"AUTOEXEC.NT",MAX_PATH,NULL);
  146. //
  147. // If the temp files don't exist, we're done.
  148. // If they do, set their attributes so that we can delete them later.
  149. //
  150. if(!FileExists(ConfigTmp,NULL) || !FileExists(AutoexecTmp,NULL)) {
  151. return(TRUE);
  152. }
  153. SetFileAttributes(ConfigTmp,FILE_ATTRIBUTE_NORMAL);
  154. SetFileAttributes(AutoexecTmp,FILE_ATTRIBUTE_NORMAL);
  155. //
  156. // Get rid of any existing nt config files. We don't support
  157. // merging/upgrading them; we support only ooverwriting them.
  158. //
  159. SetFileAttributes(ConfigNt,FILE_ATTRIBUTE_NORMAL);
  160. SetFileAttributes(AutoexecNt,FILE_ATTRIBUTE_NORMAL);
  161. DeleteFile(ConfigNt);
  162. DeleteFile(AutoexecNt);
  163. //
  164. // If a DOS config.sys exists, merge the template config.sys
  165. // and the dos config.sys to form the nt config.sys.
  166. // Otherwise move the config.sys template over to be the
  167. // nt config file.
  168. //
  169. if(DosConfigSysExists(ConfigDos)) {
  170. b = CreateConfigNt(ConfigDos,ConfigTmp,ConfigNt);
  171. } else {
  172. b = MoveFile(ConfigTmp,ConfigNt);
  173. }
  174. //
  175. // We don't do anything special with autoexec.bat.
  176. // Just move the template over to be the nt file.
  177. //
  178. if(!MoveFile(AutoexecTmp,AutoexecNt)) {
  179. b = FALSE;
  180. }
  181. return(b);
  182. }
  183. BOOL
  184. DosConfigSysExists(
  185. IN PCWSTR Filename
  186. )
  187. /*++
  188. Routine Description:
  189. Determine whether a given file is a DOS config.sys.
  190. Arguments:
  191. Filename - supplies name of file to check.
  192. Return Value:
  193. TRUE if the file exists and is not an OS/2 config.sys.
  194. FALSE if the file does not exist or is an OS/2 config.sys.
  195. --*/
  196. {
  197. BOOL b;
  198. FILE *f;
  199. CHAR Line[512];
  200. UINT i;
  201. PCHAR p;
  202. CHAR c;
  203. PSTR End;
  204. PSTR filename;
  205. filename = pSetupUnicodeToAnsi(Filename);
  206. if(!filename) {
  207. return(FALSE);
  208. }
  209. b = FALSE;
  210. if(FileExists(Filename,NULL)) {
  211. b = TRUE;
  212. if(f = fopen(filename,"rt")) {
  213. while(b && fgets(Line, ARRAYSIZE(Line),f)) {
  214. if(p = IsolateFirstField(Line,&End,&c)) {
  215. for(i=0; b && Os2ConfigSysKeywords[i]; i++) {
  216. if(!lstrcmpiA(p,Os2ConfigSysKeywords[i])) {
  217. b = FALSE;
  218. }
  219. }
  220. }
  221. }
  222. fclose(f);
  223. }
  224. }
  225. MyFree(filename);
  226. return(b);
  227. }
  228. BOOL
  229. CreateConfigNt(
  230. IN PCWSTR ConfigDos,
  231. IN PCWSTR ConfigTmp,
  232. IN PCWSTR ConfigNt
  233. )
  234. /*++
  235. Routine Description:
  236. Create config.nt. This is done by merging config.tmp (copied during setup)
  237. and the user's existing DOS config.sys. We migrate certain lines from
  238. the DOS config.sys into config.nt.
  239. Arguments:
  240. ConfigDos - supplies filename of DOS config.sys.
  241. ConfigTmp - supplies filename of template config.sys.
  242. ConfigNt - supplies filename of config.nt to be created.
  243. Return Value:
  244. Boolean value indicating outcome.
  245. --*/
  246. {
  247. FILE *DosFile;
  248. FILE *NtFile;
  249. FILE *TmpFile;
  250. BOOL b;
  251. CHAR Line[512];
  252. PCHAR p;
  253. BOOL Found;
  254. BOOL SawKeyword[NUM_DOS_KEYWORDS];
  255. PCSTR FoundKeyword[NUM_DOS_KEYWORDS];
  256. PSTR FoundLine[NUM_DOS_KEYWORDS];
  257. UINT KeywordsFound;
  258. CHAR c;
  259. PSTR End;
  260. PCSTR configDos,configTmp,configNt;
  261. UINT i;
  262. //
  263. // Open the dos file for reading.
  264. // Create the nt file for writing.
  265. // Open the template file for reading.
  266. //
  267. b = FALSE;
  268. if(configDos = pSetupUnicodeToAnsi(ConfigDos)) {
  269. DosFile = fopen(configDos,"rt");
  270. MyFree(configDos);
  271. if(!DosFile) {
  272. goto err0;
  273. }
  274. } else {
  275. goto err0;
  276. }
  277. if(configNt = pSetupUnicodeToAnsi(ConfigNt)) {
  278. NtFile = fopen(configNt,"wt");
  279. MyFree(configNt);
  280. if(!NtFile) {
  281. goto err1;
  282. }
  283. } else {
  284. goto err1;
  285. }
  286. if(configTmp = pSetupUnicodeToAnsi(ConfigTmp)) {
  287. TmpFile = fopen(configTmp,"rt");
  288. MyFree(configTmp);
  289. if(!TmpFile) {
  290. goto err2;
  291. }
  292. } else {
  293. goto err2;
  294. }
  295. //
  296. // Process the DOS file. Read each line and see if it's one
  297. // we care about. If so, save it for later.
  298. //
  299. ZeroMemory(SawKeyword,sizeof(SawKeyword));
  300. KeywordsFound = 0;
  301. while(fgets(Line,ARRAYSIZE(Line),DosFile)) {
  302. //
  303. // Isolate the first field.
  304. //
  305. if(p = IsolateFirstField(Line,&End,&c)) {
  306. //
  307. // See if we care about this line.
  308. //
  309. for(i=0; i<NUM_DOS_KEYWORDS; i++) {
  310. if(!SawKeyword[i] && !lstrcmpiA(p,DosConfigSysKeywords[i])) {
  311. //
  312. // Remember that we saw this line and save away
  313. // the rest of the line for later.
  314. //
  315. *End = c;
  316. SawKeyword[i] = TRUE;
  317. FoundKeyword[KeywordsFound] = DosConfigSysKeywords[i];
  318. FoundLine[KeywordsFound] = MyMalloc(lstrlenA(p)+1);
  319. if(!FoundLine[KeywordsFound]) {
  320. goto err3;
  321. }
  322. lstrcpyA(FoundLine[KeywordsFound],p);
  323. KeywordsFound++;
  324. break;
  325. }
  326. }
  327. }
  328. }
  329. //
  330. // Look at each line in the template file.
  331. // If it's a line with a value we respect, make sure the line
  332. // does not exist in the DOS file. If it exists in the DOS file
  333. // use the DOS value instead.
  334. //
  335. while(fgets(Line,ARRAYSIZE(Line),TmpFile)) {
  336. //
  337. // Isolate the first field in the template line and
  338. // check against those we found in the DOS file.
  339. //
  340. Found = FALSE;
  341. if(p = IsolateFirstField(Line,&End,&c)) {
  342. for(i=0; i<KeywordsFound; i++) {
  343. if(!lstrcmpiA(FoundKeyword[i],p)) {
  344. Found = TRUE;
  345. break;
  346. }
  347. }
  348. }
  349. *End = c;
  350. if(Found) {
  351. //
  352. // Use value we found in the dos file.
  353. //
  354. fputs(FoundLine[i],NtFile);
  355. } else {
  356. //
  357. // Use line from template file as-is.
  358. //
  359. fputs(Line,NtFile);
  360. }
  361. }
  362. b = TRUE;
  363. err3:
  364. for(i=0; i<KeywordsFound; i++) {
  365. MyFree(FoundLine[i]);
  366. }
  367. fclose(TmpFile);
  368. err2:
  369. fclose(NtFile);
  370. err1:
  371. fclose(DosFile);
  372. err0:
  373. return(b);
  374. }
  375. PSTR
  376. IsolateFirstField(
  377. IN PSTR Line,
  378. OUT PSTR *End,
  379. OUT PCHAR Terminator
  380. )
  381. /*++
  382. Routine Description:
  383. Isolate the first token in a line of config.sys. The first field
  384. starts at the first non-space/tab character, and is terminated
  385. by a space/tab, newline, or equals.
  386. Arguments:
  387. Line - supplies pointer to line whose first field is desired.
  388. End - receives a pointer to the character that termianted the first
  389. field. That character will have been overwritten with a nul byte.
  390. Terminator - receives the character that terminated the first field,
  391. before we overwrote it with a nul byte.
  392. Return Value:
  393. Pointer to the first field. If the line is blank, the return value
  394. will be NULL.
  395. --*/
  396. {
  397. PSTR p,q;
  398. //
  399. // Get start of first field.
  400. //
  401. p = Line;
  402. while((*p == ' ') || (*p == '\t')) {
  403. p++;
  404. }
  405. //
  406. // If line is empty or bogus, we're done.
  407. //
  408. if((*p == 0) || (*p == '\r') || (*p == '\n') || (*p == '=')) {
  409. return(NULL);
  410. }
  411. //
  412. // Find end of field.
  413. //
  414. q = p;
  415. while(*q && !strchr("\r\n \t=",*q)) {
  416. q++;
  417. }
  418. *End = q;
  419. *Terminator = *q;
  420. *q = 0;
  421. return(p);
  422. }