source: trunk/dll/undel.c@ 1323

Last change on this file since 1323 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
RevLine 
[123]1
2/***********************************************************************
3
4 $Id: undel.c 1227 2008-09-13 23:13:11Z jbs $
5
6 Copyright (c) 1993-98 M. Kimes
[328]7 Copyright (c) 2004, 2006 Steven H. Levine
[123]8
[145]9 01 Aug 04 SHL Rework lstrip/rstrip usage
10 24 May 05 SHL Rework Win_Error usage
[328]11 17 Jul 06 SHL Use Runtime_Error
[406]12 29 Jul 06 SHL Use xfgets_bstripcr
[528]13 03 Nov 06 SHL Count thread usage
[775]14 06 Aug 07 GKY Reduce DosSleep times (ticket 148)
[793]15 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
[985]16 29 Feb 08 GKY Use xfree where appropriate
[1075]17 16 JUL 08 GKY Use TMP directory for temp files
[123]18
19***********************************************************************/
20
[2]21#include <stdlib.h>
22#include <string.h>
23#include <ctype.h>
[689]24#include <process.h> // _beginthread
[328]25
[907]26#define INCL_DOS
27#define INCL_WIN
28#define INCL_LONGLONG
29
[1185]30#include "fm3dll.h"
[1227]31#include "fm3dll2.h" // #define's for UM_*, control id's, etc.
32#include "undel.h"
[1214]33#include "init.h" // Data declaration(s)
34#include "mainwnd.h" // Data declaration(s)
[2]35#include "fm3dlg.h"
36#include "fm3str.h"
[907]37#include "errutil.h" // Dos_Error...
38#include "strutil.h" // GetPString
[1073]39#include "pathutil.h" // BldFullPathName
[1161]40#include "walkem.h" // FillPathListBox
[1185]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
[1038]48#include "fortify.h"
[2]49
50#pragma data_seg(DATA2)
[328]51
52static PSZ pszSrcFile = __FILE__;
53
[551]54struct tempstruct
55{
[2]56 HWND hwnd;
57 CHAR path[CCHMAXPATH];
58 BOOL inclsubdirs;
59};
60
[551]61static VOID FillUndelListThread(VOID * arg)
[328]62{
[551]63 HWND hwnd;
[1073]64 CHAR s[CCHMAXPATH * 2], szTempFile[CCHMAXPATH];
[551]65 CHAR *path;
66 HAB thab;
67 HMQ thmq;
[2]68 FILE *fp;
[551]69 HFILE oldstdout, newstdout;
[2]70 struct tempstruct *undelinfo;
[551]71 BOOL killme = FALSE;
[847]72 FILESTATUS3 fsa;
[2]73
74 undelinfo = (struct tempstruct *)arg;
75 hwnd = undelinfo->hwnd;
76 path = undelinfo->path;
77 DosError(FERR_DISABLEHARDERR);
78
[1038]79# ifdef FORTIFY
80 Fortify_EnterScope();
[1063]81# endif
[2]82 thab = WinInitialize(0);
[551]83 thmq = WinCreateMsgQueue(thab, 0);
[528]84 if (thab && thmq) {
[551]85 WinCancelShutdown(thmq, TRUE);
[528]86 IncrThreadUsage();
[551]87 WinSendDlgItemMsg(hwnd, UNDEL_LISTBOX, LM_DELETEALL, MPVOID, MPVOID);
[1075]88 BldFullPathName(szTempFile, pTmpDir, "$UDELETE.#$#");
[1073]89 unlinkf("%s", szTempFile);
90 fp = xfopen(szTempFile, "w", pszSrcFile, __LINE__);
[328]91 if (!fp) {
[551]92 Win_Error(NULLHANDLE, hwnd, pszSrcFile, __LINE__,
93 GetPString(IDS_REDIRECTERRORTEXT));
[328]94 killme = TRUE;
95 goto Abort;
96 }
97 else {
[2]98 newstdout = -1;
[551]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;
[2]106 }
107 oldstdout = fileno(stdout);
[551]108 DosDupHandle(fileno(fp), &oldstdout);
[2]109 runemf2(SEPARATE | INVISIBLE | WINDOWED | BACKGROUND | WAIT,
[888]110 hwnd, pszSrcFile, __LINE__,
[551]111 NULL,
112 NULL,
113 "UNDELETE.COM %s /L%s",
114 path, (undelinfo->inclsubdirs) ? " /S" : NullStr);
[2]115 oldstdout = fileno(stdout);
[551]116 DosDupHandle(newstdout, &oldstdout);
[2]117 DosClose(newstdout);
118 fclose(fp);
119 }
[1073]120 fp = xfopen(szTempFile, "r", pszSrcFile, __LINE__);
[328]121 if (fp) {
[551]122 xfgets(s, sizeof(s), fp, pszSrcFile, __LINE__); // Skip 1st line
[406]123 while (!feof(fp)) {
[551]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)) {
[2]129
[551]130 APIRET temp;
[2]131
[551]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,
[888]144 hwnd, pszSrcFile, __LINE__,
[551]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
[847]156 && DosQueryPathInfo(s, FIL_STANDARD, &fsa,
[551]157 (ULONG) sizeof(fsa)))
158 WinSendDlgItemMsg(hwnd, UNDEL_LISTBOX, LM_INSERTITEM,
159 MPFROM2SHORT(LIT_SORTASCENDING, 0), MPFROMP(s));
160 }
[2]161 }
162 fclose(fp);
163 }
[551]164 Abort:
[528]165 ;
166 }
[1073]167 DosForceDelete(szTempFile);
[1009]168 xfree(undelinfo, pszSrcFile, __LINE__);
[528]169 if (thmq) {
[551]170 PostMsg(hwnd, UM_CONTAINER_FILLED, MPVOID, MPVOID);
[528]171 if (killme)
[551]172 PostMsg(hwnd, WM_CLOSE, MPVOID, MPVOID);
[2]173 WinDestroyMsgQueue(thmq);
[528]174 }
175 if (thab) {
176 DecrThreadUsage();
[2]177 WinTerminate(thab);
178 }
[1038]179# ifdef FORTIFY
[1063]180 Fortify_LeaveScope();
181# endif
[2]182}
183
[551]184MRESULT EXPENTRY UndeleteDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
[328]185{
[551]186 SHORT sSelect;
187 static BOOL listdone, changed = FALSE, refresh = FALSE;
188 static HPOINTER hptrIcon = (HPOINTER) 0;
[2]189
[551]190 switch (msg) {
191 case WM_INITDLG:
192 listdone = TRUE;
[1009]193 if (!mp2 || !*(CHAR *)mp2) {
[551]194 Runtime_Error2(pszSrcFile, __LINE__, IDS_NODATATEXT);
195 WinDismissDlg(hwnd, 0);
[2]196 break;
[551]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];
[2]204
[1009]205 strcpy(s, (CHAR *)mp2);
[551]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;
[2]216
[551]217 case UM_SETUP:
218 if (listdone) {
[2]219
[551]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));
[1039]245 free(undelinfo);
[551]246 listdone = TRUE;
247 WinDismissDlg(hwnd, 0);
[328]248 }
[551]249 else
[771]250 DosSleep(100);//05 Aug 07 GKY 500
[2]251 }
[551]252 refresh = FALSE;
253 }
254 else
255 refresh = TRUE;
256 changed = FALSE;
257 return 0;
[2]258
[551]259 case UM_CONTAINER_FILLED:
260 listdone = TRUE;
261 {
262 CHAR s[33];
[2]263
[551]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;
[2]274
[551]275 case WM_CONTROL:
276 switch (SHORT1FROMMP(mp1)) {
277 case UNDEL_SUBDIRS:
278 PostMsg(hwnd, UM_SETUP, MPVOID, MPVOID);
279 break;
[2]280
[551]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;
[2]290 }
291 break;
292
[551]293 case UNDEL_DRIVELIST:
294 if (!listdone) {
295 Runtime_Error(pszSrcFile, __LINE__, "not listdone");
296 break;
[2]297 }
[551]298 switch (SHORT2FROMMP(mp1)) {
299 case CBN_ENTER:
300 {
301 CHAR s[CCHMAXPATH], drive, *p;
[2]302
[551]303 strset(s, 0);
304 WinQueryDlgItemText(hwnd, UNDEL_DRIVELIST, 3, s);
305 if (!isalpha(*s)) {
306 Runtime_Error2(pszSrcFile, __LINE__, IDS_NODATATEXT);
[328]307 }
308 else {
[551]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;
[2]336
[551]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;
[2]344
[551]345 default:
346 break;
347 }
348 return 0;
[2]349
[551]350 case WM_ADJUSTWINDOWPOS:
351 PostMsg(hwnd, UM_STRETCH, MPVOID, MPVOID);
352 break;
[2]353
[551]354 case UM_STRETCH:
355 {
356 SWP swpC, swp, swpM, swpD, swpL, swpE;
[2]357
[551]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);
[2]398 }
[551]399 }
400 return 0;
[2]401
[551]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");
[328]408 }
[551]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,
[888]449 hwnd, pszSrcFile, __LINE__,
[551]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 }
[328]466 break;
[2]467
[551]468 case DID_CANCEL:
469 if (!listdone)
470 Runtime_Error(pszSrcFile, __LINE__, "is busy");
471 else
472 WinDismissDlg(hwnd, 0);
[2]473 break;
[551]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;
[2]496 }
[551]497 return WinDefDlgProc(hwnd, msg, mp1, mp2);
[2]498}
[793]499
500#pragma alloc_text(UNDELETE,FillUndelListThread,UndeleteDlgProc)
Note: See TracBrowser for help on using the repository browser.