source: trunk/dll/undel.c@ 1880

Last change on this file since 1880 was 1880, checked in by Gregg Young, 10 years ago

Remove dead code and comments from remaining c files. #if 0 and #if NEVER were not addressed

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