source: trunk/dll/undel.c@ 1161

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

Ticket 187: Draft 1: Functions only

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