source: trunk/dll/undel.c@ 1227

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

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