source: trunk/dll/undel.c@ 1073

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

Fm2 uses TMP/TEMP directory if set for temporary files and directories. Ticket 20

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