source: trunk/dll/undel.c@ 1505

Last change on this file since 1505 was 1438, checked in by Gregg Young, 16 years ago

Improved drivebar changes; Added AddBackslashToPath() to remove repeatative code. replaced "
" with PCSZ variable; ANY_OBJ added the DosAlloc... (experimental)

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