source: trunk/dll/undel.c@ 1214

Last change on this file since 1214 was 1214, checked in by John Small, 17 years ago

Ticket 187: Move data declarations/definitions out of fm3dll.h

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