source: trunk/dll/undel.c@ 1544

Last change on this file since 1544 was 1544, checked in by Gregg Young, 15 years ago

Changes to fopen and _fsopen to allow FM2 to be loaded in high memory

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.3 KB
RevLine 
[123]1
2/***********************************************************************
3
4 $Id: undel.c 1544 2010-09-30 13:00:59Z gyoung $
5
6 Copyright (c) 1993-98 M. Kimes
[1335]7 Copyright (c) 2004, 2008 Steven H. Levine
[123]8
[145]9 01 Aug 04 SHL Rework lstrip/rstrip usage
10 24 May 05 SHL Rework Win_Error usage
[328]11 17 Jul 06 SHL Use Runtime_Error
[406]12 29 Jul 06 SHL Use xfgets_bstripcr
[528]13 03 Nov 06 SHL Count thread usage
[775]14 06 Aug 07 GKY Reduce DosSleep times (ticket 148)
[793]15 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
[985]16 29 Feb 08 GKY Use xfree where appropriate
[1075]17 16 JUL 08 GKY Use TMP directory for temp files
[1335]18 10 Dec 08 SHL Integrate exception handler support
[1402]19 08 Mar 09 GKY Removed variable aurguments from docopyf and unlinkf (not used)
20 08 Mar 09 GKY Additional strings move to PCSZs
[1438]21 28 Jun 09 GKY Added AddBackslashToPath() to remove repeatative code.
[123]22
23***********************************************************************/
24
[2]25#include <stdlib.h>
26#include <string.h>
27#include <ctype.h>
[1335]28// #include <process.h> // _beginthread
[328]29
[907]30#define INCL_DOS
31#define INCL_WIN
32#define INCL_LONGLONG
33
[1185]34#include "fm3dll.h"
[1227]35#include "fm3dll2.h" // #define's for UM_*, control id's, etc.
36#include "undel.h"
[1214]37#include "init.h" // Data declaration(s)
38#include "mainwnd.h" // Data declaration(s)
[2]39#include "fm3dlg.h"
40#include "fm3str.h"
[907]41#include "errutil.h" // Dos_Error...
42#include "strutil.h" // GetPString
[1335]43#include "pathutil.h" // BldFullPathName
[1161]44#include "walkem.h" // FillPathListBox
[1185]45#include "common.h" // DecrThreadUsage, IncrThreadUsage
46#include "valid.h" // MakeFullName
47#include "copyf.h" // unlinkf
48#include "wrappers.h" // xfgets
49#include "strips.h" // bstrip
50#include "misc.h" // GetCmdSpec
51#include "systemf.h" // runemf2
[1038]52#include "fortify.h"
[1335]53#include "excputil.h" // xbeginthread
[2]54
55#pragma data_seg(DATA2)
[328]56
57static PSZ pszSrcFile = __FILE__;
58
[551]59struct tempstruct
60{
[2]61 HWND hwnd;
62 CHAR path[CCHMAXPATH];
63 BOOL inclsubdirs;
64};
65
[551]66static VOID FillUndelListThread(VOID * arg)
[328]67{
[551]68 HWND hwnd;
[1073]69 CHAR s[CCHMAXPATH * 2], szTempFile[CCHMAXPATH];
[551]70 CHAR *path;
71 HAB thab;
72 HMQ thmq;
[2]73 FILE *fp;
[551]74 HFILE oldstdout, newstdout;
[2]75 struct tempstruct *undelinfo;
[551]76 BOOL killme = FALSE;
[847]77 FILESTATUS3 fsa;
[1544]78 CHAR *mode = "w";
[2]79
80 undelinfo = (struct tempstruct *)arg;
81 hwnd = undelinfo->hwnd;
82 path = undelinfo->path;
83 DosError(FERR_DISABLEHARDERR);
84
[1038]85# ifdef FORTIFY
86 Fortify_EnterScope();
[1063]87# endif
[2]88 thab = WinInitialize(0);
[551]89 thmq = WinCreateMsgQueue(thab, 0);
[528]90 if (thab && thmq) {
[551]91 WinCancelShutdown(thmq, TRUE);
[528]92 IncrThreadUsage();
[551]93 WinSendDlgItemMsg(hwnd, UNDEL_LISTBOX, LM_DELETEALL, MPVOID, MPVOID);
[1075]94 BldFullPathName(szTempFile, pTmpDir, "$UDELETE.#$#");
[1402]95 unlinkf(szTempFile);
[1544]96 fp = xfopen(szTempFile, mode, pszSrcFile, __LINE__, FALSE);
[328]97 if (!fp) {
[551]98 Win_Error(NULLHANDLE, hwnd, pszSrcFile, __LINE__,
99 GetPString(IDS_REDIRECTERRORTEXT));
[328]100 killme = TRUE;
101 goto Abort;
102 }
103 else {
[2]104 newstdout = -1;
[551]105 if (DosDupHandle(fileno(stdout), &newstdout)) {
106 saymsg(MB_CANCEL,
107 hwnd,
108 GetPString(IDS_MAYDAYTEXT), GetPString(IDS_REDIRECTERRORTEXT));
109 fclose(fp);
110 killme = TRUE;
111 goto Abort;
[2]112 }
113 oldstdout = fileno(stdout);
[551]114 DosDupHandle(fileno(fp), &oldstdout);
[2]115 runemf2(SEPARATE | INVISIBLE | WINDOWED | BACKGROUND | WAIT,
[888]116 hwnd, pszSrcFile, __LINE__,
[551]117 NULL,
118 NULL,
119 "UNDELETE.COM %s /L%s",
120 path, (undelinfo->inclsubdirs) ? " /S" : NullStr);
[2]121 oldstdout = fileno(stdout);
[551]122 DosDupHandle(newstdout, &oldstdout);
[2]123 DosClose(newstdout);
124 fclose(fp);
125 }
[1544]126 mode = "r";
127 fp = xfopen(szTempFile, mode, pszSrcFile, __LINE__, FALSE);
[328]128 if (fp) {
[551]129 xfgets(s, sizeof(s), fp, pszSrcFile, __LINE__); // Skip 1st line
[406]130 while (!feof(fp)) {
[551]131 strset(s, 0);
132 if (!xfgets_bstripcr(s, CCHMAXPATH + 2, fp, pszSrcFile, __LINE__))
133 break;
134 if (*s) {
135 if (!strnicmp(s, "SYS3194: ", 9)) {
[2]136
[551]137 APIRET temp;
[2]138
[551]139 strcat(s, " ");
140 xfgets(&s[strlen(s)], CCHMAXPATH + 128 - strlen(s), fp,
141 pszSrcFile, __LINE__);
142 fclose(fp);
143 s[CCHMAXPATH + 128] = 0;
144 stripcr(s);
145 rstrip(s);
146 strcat(s, GetPString(IDS_ASKABOUTUNDELETEHELPTEXT));
147 temp = saymsg(MB_YESNOCANCEL | MB_ICONEXCLAMATION,
[1402]148 hwnd, GetPString(IDS_ERRORTEXT), s);
[551]149 if (temp == MBID_YES)
150 runemf2(BACKGROUND | INVISIBLE | SEPARATE | WINDOWED,
[888]151 hwnd, pszSrcFile, __LINE__,
[551]152 NULL, NULL, "%s /C HELP UNDELETE", GetCmdSpec(FALSE));
153 if (temp == MBID_CANCEL)
154 killme = TRUE;
155 goto Abort;
156 }
157 else if (s[1] != ':')
158 continue;
159 else if ((SHORT)
160 WinSendDlgItemMsg(hwnd, UNDEL_LISTBOX, LM_SEARCHSTRING,
161 MPFROM2SHORT(0, LIT_FIRST),
162 MPFROMP(s)) < 0
[847]163 && DosQueryPathInfo(s, FIL_STANDARD, &fsa,
[551]164 (ULONG) sizeof(fsa)))
165 WinSendDlgItemMsg(hwnd, UNDEL_LISTBOX, LM_INSERTITEM,
166 MPFROM2SHORT(LIT_SORTASCENDING, 0), MPFROMP(s));
167 }
[2]168 }
169 fclose(fp);
170 }
[551]171 Abort:
[528]172 ;
173 }
[1073]174 DosForceDelete(szTempFile);
[1009]175 xfree(undelinfo, pszSrcFile, __LINE__);
[528]176 if (thmq) {
[551]177 PostMsg(hwnd, UM_CONTAINER_FILLED, MPVOID, MPVOID);
[528]178 if (killme)
[551]179 PostMsg(hwnd, WM_CLOSE, MPVOID, MPVOID);
[2]180 WinDestroyMsgQueue(thmq);
[528]181 }
182 if (thab) {
183 DecrThreadUsage();
[2]184 WinTerminate(thab);
185 }
[1038]186# ifdef FORTIFY
[1063]187 Fortify_LeaveScope();
188# endif
[2]189}
190
[551]191MRESULT EXPENTRY UndeleteDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
[328]192{
[551]193 SHORT sSelect;
194 static BOOL listdone, changed = FALSE, refresh = FALSE;
195 static HPOINTER hptrIcon = (HPOINTER) 0;
[2]196
[551]197 switch (msg) {
198 case WM_INITDLG:
199 listdone = TRUE;
[1009]200 if (!mp2 || !*(CHAR *)mp2) {
[1398]201 Runtime_Error(pszSrcFile, __LINE__, NULL);
[551]202 WinDismissDlg(hwnd, 0);
[2]203 break;
[551]204 }
205 hptrIcon = WinLoadPointer(HWND_DESKTOP, FM3ModHandle, UNDEL_FRAME);
206 WinDefDlgProc(hwnd, WM_SETICON, MPFROMLONG(hptrIcon), MPVOID);
207 WinSendDlgItemMsg(hwnd, UNDEL_ENTRY, EM_SETTEXTLIMIT,
208 MPFROM2SHORT(CCHMAXPATH, 0), MPVOID);
209 {
210 CHAR s[CCHMAXPATH];
[2]211
[1009]212 strcpy(s, (CHAR *)mp2);
[1438]213 AddBackslashToPath(s);
214 //if (s[strlen(s) - 1] != '\\')
215 // strcat(s, "\\");
[551]216 strcat(s, "*");
217 WinSetDlgItemText(hwnd, UNDEL_ENTRY, s);
218 WinCheckButton(hwnd, UNDEL_SUBDIRS, TRUE);
219 FillPathListBox(hwnd, WinWindowFromID(hwnd, UNDEL_DRIVELIST), (HWND) 0,
220 s, TRUE);
221 }
222 PostMsg(hwnd, UM_SETUP, MPVOID, MPVOID);
223 break;
[2]224
[551]225 case UM_SETUP:
226 if (listdone) {
[2]227
[551]228 struct tempstruct *undelinfo;
229 CHAR s[CCHMAXPATH];
230
231 listdone = FALSE;
232 undelinfo = xmallocz(sizeof(struct tempstruct), pszSrcFile, __LINE__);
233 if (!undelinfo) {
234 listdone = TRUE;
235 WinDismissDlg(hwnd, 0);
236 }
237 else {
238 undelinfo->hwnd = hwnd;
239 WinQueryDlgItemText(hwnd,
240 UNDEL_ENTRY,
241 sizeof(undelinfo->path), undelinfo->path);
242 bstrip(undelinfo->path);
243 MakeFullName(undelinfo->path);
244 undelinfo->inclsubdirs = WinQueryButtonCheckstate(hwnd,
245 UNDEL_SUBDIRS);
246 sprintf(s,
247 GetPString(IDS_UNDELETETITLETEXT), toupper(*undelinfo->path));
248 WinSetWindowText(hwnd, s);
[1335]249 if (xbeginthread(FillUndelListThread,
250 65536,
251 undelinfo,
252 pszSrcFile,
253 __LINE__) == -1)
254 {
[1039]255 free(undelinfo);
[551]256 listdone = TRUE;
257 WinDismissDlg(hwnd, 0);
[328]258 }
[551]259 else
[771]260 DosSleep(100);//05 Aug 07 GKY 500
[2]261 }
[551]262 refresh = FALSE;
263 }
264 else
265 refresh = TRUE;
266 changed = FALSE;
267 return 0;
[2]268
[551]269 case UM_CONTAINER_FILLED:
270 listdone = TRUE;
271 {
272 CHAR s[33];
[2]273
[551]274 sSelect = (SHORT) WinSendDlgItemMsg(hwnd,
275 UNDEL_LISTBOX,
276 LM_QUERYITEMCOUNT, MPVOID, MPVOID);
277 sprintf(s, "%d", sSelect);
278 WinSetDlgItemText(hwnd, UNDEL_COUNT, s);
279 if (refresh)
280 PostMsg(hwnd, UM_SETUP, MPVOID, MPVOID);
281 refresh = FALSE;
282 }
283 return 0;
[2]284
[551]285 case WM_CONTROL:
286 switch (SHORT1FROMMP(mp1)) {
287 case UNDEL_SUBDIRS:
288 PostMsg(hwnd, UM_SETUP, MPVOID, MPVOID);
289 break;
[2]290
[551]291 case UNDEL_ENTRY:
292 switch (SHORT2FROMMP(mp1)) {
293 case EN_CHANGE:
294 changed = TRUE;
295 break;
296 case EN_KILLFOCUS:
297 if (changed)
298 PostMsg(hwnd, UM_SETUP, MPVOID, MPVOID);
299 break;
[2]300 }
301 break;
302
[551]303 case UNDEL_DRIVELIST:
304 if (!listdone) {
305 Runtime_Error(pszSrcFile, __LINE__, "not listdone");
306 break;
[2]307 }
[551]308 switch (SHORT2FROMMP(mp1)) {
309 case CBN_ENTER:
310 {
311 CHAR s[CCHMAXPATH], drive, *p;
[2]312
[551]313 strset(s, 0);
314 WinQueryDlgItemText(hwnd, UNDEL_DRIVELIST, 3, s);
315 if (!isalpha(*s)) {
[1398]316 Runtime_Error(pszSrcFile, __LINE__, NULL);
[328]317 }
318 else {
[551]319 drive = toupper(*s);
320 WinQueryDlgItemText(hwnd, UNDEL_ENTRY, sizeof(s), s);
321 *s = drive;
322 s[1] = ':';
323 s[2] = '\\';
324 p = strrchr(s + 2, '\\');
325 if (p) {
326 p++;
327 if (*p)
328 memmove(s + 3, p, strlen(p) + 1);
329 else {
330 s[3] = '*';
331 s[4] = 0;
332 }
333 }
334 else {
335 s[3] = '*';
336 s[4] = 0;
337 }
338 s[CCHMAXPATH - 1] = 0;
339 WinSetDlgItemText(hwnd, UNDEL_ENTRY, s);
340 PostMsg(hwnd, UM_SETUP, MPVOID, MPVOID);
341 }
342 }
343 break;
344 }
345 break;
[2]346
[551]347 case UNDEL_LISTBOX:
348 switch (SHORT2FROMMP(mp2)) {
349 case LN_ENTER:
350 WinSendDlgItemMsg(hwnd, DID_OK, BM_CLICK, MPFROMSHORT(TRUE), MPVOID);
351 break;
352 }
353 break;
[2]354
[551]355 default:
356 break;
357 }
358 return 0;
[2]359
[551]360 case WM_ADJUSTWINDOWPOS:
361 PostMsg(hwnd, UM_STRETCH, MPVOID, MPVOID);
362 break;
[2]363
[551]364 case UM_STRETCH:
365 {
366 SWP swpC, swp, swpM, swpD, swpL, swpE;
[2]367
[551]368 WinQueryWindowPos(hwnd, &swp);
369 if (!(swp.fl & (SWP_HIDE | SWP_MINIMIZE))) {
370 WinQueryWindowPos(WinWindowFromID(hwnd, UNDEL_LISTBOX), &swpC);
371 WinQueryWindowPos(WinWindowFromID(hwnd, UNDEL_MASKHDR), &swpM);
372 WinQueryWindowPos(WinWindowFromID(hwnd, UNDEL_DRVHDR), &swpD);
373 WinQueryWindowPos(WinWindowFromID(hwnd, UNDEL_DRIVELIST), &swpL);
374 WinQueryWindowPos(WinWindowFromID(hwnd, UNDEL_ENTRY), &swpE);
375 WinSetWindowPos(WinWindowFromID(hwnd, UNDEL_LISTBOX), HWND_TOP,
376 SysVal(SV_CXSIZEBORDER),
377 swpC.y,
378 swp.cx - (SysVal(SV_CXSIZEBORDER) * 2),
379 (swp.cy - swpC.y) - (SysVal(SV_CYTITLEBAR) +
380 SysVal(SV_CYSIZEBORDER) +
381 swpM.cy + 16),
382 SWP_MOVE | SWP_SIZE);
383 WinSetWindowPos(WinWindowFromID(hwnd, UNDEL_MASKHDR), HWND_TOP,
384 swpM.x,
385 (swp.cy - swpM.cy) - (SysVal(SV_CYTITLEBAR) +
386 SysVal(SV_CYSIZEBORDER) + 8),
387 swpM.cx, swpM.cy, SWP_MOVE | SWP_SIZE);
388 WinSetWindowPos(WinWindowFromID(hwnd, UNDEL_DRVHDR), HWND_TOP,
389 swpD.x,
390 (swp.cy - swpM.cy) - (SysVal(SV_CYTITLEBAR) +
391 SysVal(SV_CYSIZEBORDER) + 8),
392 swpD.cx, swpM.cy, SWP_MOVE | SWP_SIZE);
393 WinSetWindowPos(WinWindowFromID(hwnd, UNDEL_DRIVELIST), HWND_TOP,
394 swpL.x,
395 SysVal(SV_CYSIZEBORDER),
396 swpL.cx,
397 swp.cy - (SysVal(SV_CYTITLEBAR) +
398 (SysVal(SV_CYSIZEBORDER) * 2) + 6),
399 SWP_MOVE | SWP_SIZE);
400 WinSetWindowPos(WinWindowFromID(hwnd, UNDEL_ENTRY), HWND_TOP,
401 swpM.x + swpM.cx + 4,
402 (swp.cy - swpM.cy) - (SysVal(SV_CYTITLEBAR) +
403 SysVal(SV_CYSIZEBORDER) + 8),
404 swp.cx - ((swpM.x + swpM.cx + 4) +
405 (SysVal(SV_CXSIZEBORDER) + 8)),
406 swpM.cy + 2, SWP_MOVE | SWP_SIZE);
407 WinInvalidateRect(WinWindowFromID(hwnd, UNDEL_ENTRY), NULL, FALSE);
[2]408 }
[551]409 }
410 return 0;
[2]411
[551]412 case WM_COMMAND:
413 switch (SHORT1FROMMP(mp1)) {
414 case UNDEL_DEL:
415 case DID_OK:
416 if (changed || !listdone) {
417 Runtime_Error(pszSrcFile, __LINE__, "not done");
[328]418 }
[551]419 else {
420 sSelect = (USHORT) WinSendDlgItemMsg(hwnd, UNDEL_LISTBOX,
421 LM_QUERYSELECTION,
422 MPFROMSHORT(LIT_FIRST), MPVOID);
423 if (sSelect >= 0) {
424
425 FILE *fp;
[1544]426 CHAR s[CCHMAXPATH + 1];
427 CHAR *modew = "w";
[551]428
429 DosForceDelete("\\FMUNDEL.CMD");
[1544]430 fp = xfopen("\\FMUNDEL.CMD", modew, pszSrcFile, __LINE__, FALSE);
[551]431 if (fp) {
432 while (sSelect >= 0) {
433 *s = 0;
434 WinSendDlgItemMsg(hwnd, UNDEL_LISTBOX, LM_QUERYITEMTEXT,
435 MPFROM2SHORT(sSelect, CCHMAXPATH),
436 MPFROMP(s));
437 if (SHORT1FROMMP(mp1) == UNDEL_DEL)
438 WinSendDlgItemMsg(hwnd, UNDEL_LISTBOX, LM_DELETEITEM,
439 MPFROM2SHORT(sSelect, 0), MPVOID);
440 if (*s) {
441 if (SHORT1FROMMP(mp1) == DID_OK)
442 fprintf(fp,
443 "IF NOT EXIST \"%s\" UNDELETE.COM \"%s\" /A\n",
444 s, s);
445 else
446 fprintf(fp, "UNDELETE.COM \"%s\" /F /A\n", s);
447 }
448 sSelect = (USHORT) WinSendDlgItemMsg(hwnd,
449 UNDEL_LISTBOX,
450 LM_QUERYSELECTION,
451 (SHORT1FROMMP(mp1) ==
452 DID_OK) ?
453 MPFROMSHORT(sSelect) :
454 MPFROMSHORT(LIT_FIRST),
455 MPVOID);
456 }
457 fprintf(fp, "DEL \\FMUNDEL.CMD /F\n");
458 fclose(fp);
459 runemf2(WINDOWED | BACKGROUND | SEPARATE | INVISIBLE,
[888]460 hwnd, pszSrcFile, __LINE__,
[551]461 NULL, NULL, "%s /C \\FMUNDEL.CMD", GetCmdSpec(FALSE));
462 }
463 }
464 if (SHORT1FROMMP(mp1) == DID_OK)
465 WinDismissDlg(hwnd, 0);
466 {
467 CHAR s[33];
468
469 sSelect = (SHORT) WinSendDlgItemMsg(hwnd,
470 UNDEL_LISTBOX,
471 LM_QUERYITEMCOUNT,
472 MPVOID, MPVOID);
473 sprintf(s, "%d", sSelect);
474 WinSetDlgItemText(hwnd, UNDEL_COUNT, s);
475 }
476 }
[328]477 break;
[2]478
[551]479 case DID_CANCEL:
480 if (!listdone)
481 Runtime_Error(pszSrcFile, __LINE__, "is busy");
482 else
483 WinDismissDlg(hwnd, 0);
[2]484 break;
[551]485
486 case IDM_HELP:
487 saymsg(MB_ENTER | MB_ICONASTERISK,
488 hwnd,
489 GetPString(IDS_UNDELETEHELPTITLETEXT),
490 GetPString(IDS_UNDELETEHELPTEXT));
491 break;
492 }
493 return 0;
494
495 case WM_CLOSE:
496 if (!listdone) {
497 Runtime_Error(pszSrcFile, __LINE__, "not listdone");
498 return 0;
499 }
500 break;
501
502 case WM_DESTROY:
503 if (hptrIcon)
504 WinDestroyPointer(hptrIcon);
505 hptrIcon = (HPOINTER) 0;
506 break;
[2]507 }
[551]508 return WinDefDlgProc(hwnd, msg, mp1, mp2);
[2]509}
[793]510
511#pragma alloc_text(UNDELETE,FillUndelListThread,UndeleteDlgProc)
Note: See TracBrowser for help on using the repository browser.