/*Windows headers*/ #define STRICT #include #pragma hdrstop #include #include /*Non portable functions*/ #include #include /*Standard headers*/ #include #include #include #include #include "lnx4win.h" BOOL CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); int SaveParameters(HWND hDlg); void BootLinux(void); void updatesize(HWND hDlg, int max, int min, int increment, int id); int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { /*Is it an installation?*/ if(strstr(lpCmdLine, "--install")) { /*Use Ctl3D*/ Ctl3dRegister(hInstance); Ctl3dAutoSubclass(hInstance); DialogBox(hInstance, "Maindialog", NULL, DlgProc); return (TRUE); } /*However it's a boot*/ BootLinux(); return (FALSE); } BOOL CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_INITDIALOG: CheckDlgButton(hDlg, IDC_BOOTMENU, 1); CheckDlgButton(hDlg, IDC_WARNONEXIT, 1); CheckDlgButton(hDlg, IDC_SHORTCUT, 1); CheckDlgButton(hDlg, IDC_CDROM, 1); break; case WM_SYSCOLORCHANGE: Ctl3dColorChange(); break; case WM_DESTROY: Ctl3dUnregister(hDlg); EndDialog(hDlg, 0); return 0 ; case WM_COMMAND: switch (GET_WM_COMMAND_ID(wParam, lParam)) { case ID_CANCEL: EndDialog(hDlg, 0); break; case ID_OK: if(SaveParameters(hDlg)) BootLinux(); break; case ID_HELP: WinHelp(hDlg, "Lnx4Win.hlp", HELP_INDEX, 0L ); break; case IDC_UPSIZE: updatesize(hDlg, 200, 2000, 20, IDC_LINUXSIZE); break; case IDC_DOWNSIZE: updatesize(hDlg, 200, 2000, -20, IDC_LINUXSIZE); break; case IDC_UPSWAPSIZE: updatesize(hDlg, 24, 400, 4, IDC_SWAPSIZE); break; case IDC_DOWNSWAPSIZE: updatesize(hDlg, 24, 400, -4, IDC_SWAPSIZE); break; } default: return (FALSE); } return TRUE; } /*Yeah! emulation of spinbox :)*/ void updatesize(HWND hDlg, int min, int max, int increment, int id) { char buf[4]; int size; GetDlgItemText(hDlg, id , buf, 5); size = atoi(buf); if(increment < 0) { if(size > min) size += increment; } else { if(size < max) size += increment; } sprintf(buf, "%d", size); SetDlgItemText(hDlg, id, buf); } /*Prepare a new installation*/ int SaveParameters(HWND hDlg) { FILE *f; char buf[4]; char oldpath[1024], path[1024]; int size, swapsize; struct diskfree_t free; long available; struct stat sb; size_t bytesread = 0; FILE *fp; char hd[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'}; if (_dos_getdiskfree(0, &free) != 0) { MessageBox(hDlg, "Error in getdiskfree call", "Internal error", MB_APPLMODAL|MB_ICONEXCLAMATION|MB_OK); return 0; } available = (long) free.avail_clusters * (long) free.bytes_per_sector * (long) free.sectors_per_cluster; GetDlgItemText(hDlg, IDC_LINUXSIZE , buf, 5); size = atoi(buf); GetDlgItemText(hDlg, IDC_SWAPSIZE , buf, 5); swapsize = atoi(buf); if((size + swapsize) > (available/1000000)) { MessageBox(hDlg, "You've not enough space on disk", "Invalid size", MB_APPLMODAL|MB_ICONEXCLAMATION|MB_OK); return 0; } sprintf(path, "%c:\\lnx4win\\size.txt", hd[getdisk()]); if((f = fopen(path, "w")) == NULL) { MessageBox(hDlg, "Can't save configuration file.", "File error!", MB_APPLMODAL|MB_ICONEXCLAMATION|MB_OK); return 0; } fprintf(f, "%d ", size); fclose(f); sprintf(path, "%c:\\lnx4win\\swapsize.txt", hd[getdisk()]); if((f = fopen(path, "w")) == NULL) { MessageBox(hDlg, "Can't save configuration file.", "File error!", MB_APPLMODAL|MB_ICONEXCLAMATION|MB_OK); return 0; } fprintf(f, "%d ", swapsize); fclose(f); /*If selected write new config.sys*/ if(IsDlgButtonChecked(hDlg, IDC_BOOTMENU)) { char buf[32000]; /*Allow for a *very* large config.sys! */ char temp[1024]; memset(buf,0,sizeof(buf)); if((f = fopen("C:\\config.sys","rb")) != NULL) { bytesread = fread(buf,1,sizeof(buf),f); fclose(f); } sprintf(temp,"shell=%c:\\lnx4win\\loadlin.exe %c:\\lnx4win\\vmlinuz initrd=%c:\\lnx4win\\initrd.gz lnx4win", hd[getdisk()], hd[getdisk()], hd[getdisk()]); if(strstr(buf,"[Menu]")) { if(strstr(buf,"menuitem=Windows") && strstr(buf,"menuitem=Linux") && strstr(buf,"menudefault=") && strstr(buf,"[Linux]") && strstr(buf,temp)) { /*Probably the menu has been added already!*/ MessageBox(hDlg, "Your config.sys file was not modified.Linux appears to be already installed on your boot menu.", "Config.sys", MB_APPLMODAL|MB_OK|MB_ICONINFORMATION); } else { /*Someone else's boot menu was already here.*/ MessageBox(hDlg, "Your config.sys file was NOT modified.A boot menu appears to be already installed.", "Config.sys",MB_APPLMODAL|MB_OK|MB_ICONINFORMATION); } } else { /*Hopefully we don't have a name conflict here. */ /*If we do, maybe we should generate a name. */ unlink("C:\\config.mdk"); // Remove mandrake config.sys backup rename("C:\\config.sys","C:\\config.mdk"); if((f = fopen("C:\\config.sys","wt")) != NULL) { /* We may duplicate menus here if a menu was installed but is different.*/ fprintf(f,"[Menu]\n"); fprintf(f,"menuitem=Windows\n"); fprintf(f,"menuitem=Linux\n"); fprintf(f,"menudefault=Windows,5\n"); fprintf(f,"\n"); fprintf(f,"[Linux]\n"); fprintf(f,"%s\n",temp); fprintf(f,"[Windows]\n"); fwrite(buf,1,bytesread,f); fclose(f); } MessageBox(hDlg, "Your C:\\config.sys file has been modified. The old one is saved as C:\\config.mdk","Config.sys",MB_OK); } } if(IsDlgButtonChecked(hDlg, IDC_SHORTCUT)) { /*Because all people haven't Windows English version*/ if(PRIMARYLANGID(GetSystemDefaultLangID()) == LANG_FRENCH) sprintf(path,"%s\\Bureau\\Linux4Win.url",getenv("WINDIR")); else sprintf(path,"%s\\Desktop\\Linux4Win.url",getenv("WINDIR")); /*Create an "Internet Shortcut" to the main program */ if((fp = fopen(path,"wt")) != NULL) { fprintf(fp,"[InternetShortcut]\n"); fprintf(fp,"URL=file:\\lnx4win\\Lnx4Win.exe\n"); fprintf(fp,"WorkingDirectory=%c:\\lnx4win\\\n", hd[getdisk()]); fprintf(fp,"IconFile=%c:\\lnx4win\\Lnx4Win.ico\n", hd[getdisk()]); fprintf(fp,"IconIndex=0\n"); fclose(fp); } } if(PRIMARYLANGID(GetSystemDefaultLangID()) == LANG_FRENCH) sprintf(path,"%s\\Menu Démarrer\\Linux4Win.url",getenv("WINDIR")); else sprintf(path,"%s\\Start Menu\\Linux4Win.url",getenv("WINDIR")); if((fp = fopen(path,"wt")) != NULL) { fprintf(fp,"[InternetShortcut]\n"); fprintf(fp,"URL=file:\\lnx4win\\Lnx4Win.exe\n"); fprintf(fp,"WorkingDirectory=%c:\\lnx4win\\\n", hd[getdisk()]); fprintf(fp,"IconFile=%c:\\lnx4win\\Lnx4Win.ico\n", hd[getdisk()]); fprintf(fp,"IconIndex=0\n"); fclose(fp); } /*Warn on exit if option selected*/ if(IsDlgButtonChecked(hDlg, IDC_WARNONEXIT)) WritePrivateProfileString("Lnx4Win", "WarnOnExit", "1", "Lnx4Win.ini"); else WritePrivateProfileString("Lnx4Win", "WarnOnExit", "0", "Lnx4Win.ini"); sprintf(path, "%c:\\lnx4win\\linux.bat", hd[getdisk()]); if((fp = fopen(path,"wt")) != NULL) { fprintf(fp, "@echo off\n"); fprintf(fp, "echo Linux Mandrake\n"); fprintf(fp, "smartdrv /C\n"); fprintf(fp, "loadlin vmlinuz initrd=initrd.gz ramdisk_size=32000 mdkinst lnx4win "); if(IsDlgButtonChecked(hDlg, IDC_HD)) fprintf(fp, "hd\n"); else if(IsDlgButtonChecked(hDlg, IDC_CDROM)) fprintf(fp, "cdrom\n"); else if(IsDlgButtonChecked(hDlg, IDC_NETWORK)) fprintf(fp, "network\n"); fprintf(fp, "echo Linux4Win failed to load.\n"); fprintf(fp, "pause\n"); fclose(fp); } /*If an previous installation was found...*/ sprintf(path, "%c:\\lnx4win\\vmlinuz", hd[getdisk()]); sprintf(oldpath, "%c:\\lnx4win\\vmlinuz.old", hd[getdisk()]); if(!stat(oldpath, &sb)) { remove(path); rename(oldpath, path); } return 1; } /*Boot Linux by using linux.bat batch*/ void BootLinux() { char bootcmdline[] = {"linux.bat"}; int rc; if(GetPrivateProfileInt("Lnx4Win", "WarnOnExit", 1, "Lnx4Win.ini")) { rc = MessageBox(NULL, "Are you ready to boot under Linux?", "Ready to Boot?", MB_APPLMODAL|MB_YESNO|MB_ICONQUESTION); if(rc == IDNO) return; } WinExec(bootcmdline, SW_HIDE); }