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
Line 
1
2/***********************************************************************
3
4 $Id: undel.c 1214 2008-09-13 06:53:34Z jbs $
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 "fm3dll.h"
31#include "init.h" // Data declaration(s)
32#include "mainwnd.h" // Data declaration(s)
33#include "fm3dlg.h"
34#include "fm3str.h"
35#include "errutil.h" // Dos_Error...
36#include "strutil.h" // GetPString
37#include "pathutil.h" // BldFullPathName
38#include "walkem.h" // FillPathListBox
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
47#include "fortify.h"
48
49#pragma data_seg(DATA2)
50
51static PSZ pszSrcFile = __FILE__;
52
53struct tempstruct
54{
55 HWND hwnd;
56 CHAR path[CCHMAXPATH];
57 BOOL inclsubdirs;
58};
59
60static VOID FillUndelListThread(VOID * arg)
61{
62 HWND hwnd;
63 CHAR s[CCHMAXPATH * 2], szTempFile[CCHMAXPATH];
64 CHAR *path;
65 HAB thab;
66 HMQ thmq;
67 FILE *fp;
68 HFILE oldstdout, newstdout;
69 struct tempstruct *undelinfo;
70 BOOL killme = FALSE;
71 FILESTATUS3 fsa;
72
73 undelinfo = (struct tempstruct *)arg;
74 hwnd = undelinfo->hwnd;
75 path = undelinfo->path;
76 DosError(FERR_DISABLEHARDERR);
77
78# ifdef FORTIFY
79 Fortify_EnterScope();
80# endif
81 thab = WinInitialize(0);
82 thmq = WinCreateMsgQueue(thab, 0);
83 if (thab && thmq) {
84 WinCancelShutdown(thmq, TRUE);
85 IncrThreadUsage();
86 WinSendDlgItemMsg(hwnd, UNDEL_LISTBOX, LM_DELETEALL, MPVOID, MPVOID);
87 BldFullPathName(szTempFile, pTmpDir, "$UDELETE.#$#");
88 unlinkf("%s", szTempFile);
89 fp = xfopen(szTempFile, "w", pszSrcFile, __LINE__);
90 if (!fp) {
91 Win_Error(NULLHANDLE, hwnd, pszSrcFile, __LINE__,
92 GetPString(IDS_REDIRECTERRORTEXT));
93 killme = TRUE;
94 goto Abort;
95 }
96 else {
97 newstdout = -1;
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;
105 }
106 oldstdout = fileno(stdout);
107 DosDupHandle(fileno(fp), &oldstdout);
108 runemf2(SEPARATE | INVISIBLE | WINDOWED | BACKGROUND | WAIT,
109 hwnd, pszSrcFile, __LINE__,
110 NULL,
111 NULL,
112 "UNDELETE.COM %s /L%s",
113 path, (undelinfo->inclsubdirs) ? " /S" : NullStr);
114 oldstdout = fileno(stdout);
115 DosDupHandle(newstdout, &oldstdout);
116 DosClose(newstdout);
117 fclose(fp);
118 }
119 fp = xfopen(szTempFile, "r", pszSrcFile, __LINE__);
120 if (fp) {
121 xfgets(s, sizeof(s), fp, pszSrcFile, __LINE__); // Skip 1st line
122 while (!feof(fp)) {
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)) {
128
129 APIRET temp;
130
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,
143 hwnd, pszSrcFile, __LINE__,
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
155 && DosQueryPathInfo(s, FIL_STANDARD, &fsa,
156 (ULONG) sizeof(fsa)))
157 WinSendDlgItemMsg(hwnd, UNDEL_LISTBOX, LM_INSERTITEM,
158 MPFROM2SHORT(LIT_SORTASCENDING, 0), MPFROMP(s));
159 }
160 }
161 fclose(fp);
162 }
163 Abort:
164 ;
165 }
166 DosForceDelete(szTempFile);
167 xfree(undelinfo, pszSrcFile, __LINE__);
168 if (thmq) {
169 PostMsg(hwnd, UM_CONTAINER_FILLED, MPVOID, MPVOID);
170 if (killme)
171 PostMsg(hwnd, WM_CLOSE, MPVOID, MPVOID);
172 WinDestroyMsgQueue(thmq);
173 }
174 if (thab) {
175 DecrThreadUsage();
176 WinTerminate(thab);
177 }
178# ifdef FORTIFY
179 Fortify_LeaveScope();
180# endif
181}
182
183MRESULT EXPENTRY UndeleteDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
184{
185 SHORT sSelect;
186 static BOOL listdone, changed = FALSE, refresh = FALSE;
187 static HPOINTER hptrIcon = (HPOINTER) 0;
188
189 switch (msg) {
190 case WM_INITDLG:
191 listdone = TRUE;
192 if (!mp2 || !*(CHAR *)mp2) {
193 Runtime_Error2(pszSrcFile, __LINE__, IDS_NODATATEXT);
194 WinDismissDlg(hwnd, 0);
195 break;
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];
203
204 strcpy(s, (CHAR *)mp2);
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;
215
216 case UM_SETUP:
217 if (listdone) {
218
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));
244 free(undelinfo);
245 listdone = TRUE;
246 WinDismissDlg(hwnd, 0);
247 }
248 else
249 DosSleep(100);//05 Aug 07 GKY 500
250 }
251 refresh = FALSE;
252 }
253 else
254 refresh = TRUE;
255 changed = FALSE;
256 return 0;
257
258 case UM_CONTAINER_FILLED:
259 listdone = TRUE;
260 {
261 CHAR s[33];
262
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;
273
274 case WM_CONTROL:
275 switch (SHORT1FROMMP(mp1)) {
276 case UNDEL_SUBDIRS:
277 PostMsg(hwnd, UM_SETUP, MPVOID, MPVOID);
278 break;
279
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;
289 }
290 break;
291
292 case UNDEL_DRIVELIST:
293 if (!listdone) {
294 Runtime_Error(pszSrcFile, __LINE__, "not listdone");
295 break;
296 }
297 switch (SHORT2FROMMP(mp1)) {
298 case CBN_ENTER:
299 {
300 CHAR s[CCHMAXPATH], drive, *p;
301
302 strset(s, 0);
303 WinQueryDlgItemText(hwnd, UNDEL_DRIVELIST, 3, s);
304 if (!isalpha(*s)) {
305 Runtime_Error2(pszSrcFile, __LINE__, IDS_NODATATEXT);
306 }
307 else {
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;
335
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;
343
344 default:
345 break;
346 }
347 return 0;
348
349 case WM_ADJUSTWINDOWPOS:
350 PostMsg(hwnd, UM_STRETCH, MPVOID, MPVOID);
351 break;
352
353 case UM_STRETCH:
354 {
355 SWP swpC, swp, swpM, swpD, swpL, swpE;
356
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);
397 }
398 }
399 return 0;
400
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");
407 }
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,
448 hwnd, pszSrcFile, __LINE__,
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 }
465 break;
466
467 case DID_CANCEL:
468 if (!listdone)
469 Runtime_Error(pszSrcFile, __LINE__, "is busy");
470 else
471 WinDismissDlg(hwnd, 0);
472 break;
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;
495 }
496 return WinDefDlgProc(hwnd, msg, mp1, mp2);
497}
498
499#pragma alloc_text(UNDELETE,FillUndelListThread,UndeleteDlgProc)
Note: See TracBrowser for help on using the repository browser.