source: trunk/dll/undel.c@ 1120

Last change on this file since 1120 was 1075, checked in by Gregg Young, 17 years ago

Debulked TMP code; Added comments;

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