source: trunk/dll/undel.c@ 1194

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

Ticket 187: Draft 2: Move remaining function declarations

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