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
RevLine 
[123]1
2/***********************************************************************
3
4 $Id: undel.c 1073 2008-07-18 00:30:20Z gyoung $
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
[123]17
18***********************************************************************/
19
[2]20#include <stdlib.h>
21#include <string.h>
22#include <ctype.h>
[689]23#include <process.h> // _beginthread
[328]24
[907]25#define INCL_DOS
26#define INCL_WIN
27#define INCL_LONGLONG
28
[2]29#include "fm3dlg.h"
30#include "fm3str.h"
[907]31#include "errutil.h" // Dos_Error...
32#include "strutil.h" // GetPString
[1073]33#include "pathutil.h" // BldFullPathName
[907]34#include "fm3dll.h"
[1038]35#include "fortify.h"
[2]36
37#pragma data_seg(DATA2)
[328]38
39static PSZ pszSrcFile = __FILE__;
40
[551]41struct tempstruct
42{
[2]43 HWND hwnd;
44 CHAR path[CCHMAXPATH];
45 BOOL inclsubdirs;
46};
47
[551]48static VOID FillUndelListThread(VOID * arg)
[328]49{
[551]50 HWND hwnd;
[1073]51 CHAR s[CCHMAXPATH * 2], szTempFile[CCHMAXPATH];
[551]52 CHAR *path;
53 HAB thab;
54 HMQ thmq;
[2]55 FILE *fp;
[551]56 HFILE oldstdout, newstdout;
[2]57 struct tempstruct *undelinfo;
[551]58 BOOL killme = FALSE;
[847]59 FILESTATUS3 fsa;
[2]60
61 undelinfo = (struct tempstruct *)arg;
62 hwnd = undelinfo->hwnd;
63 path = undelinfo->path;
64 DosError(FERR_DISABLEHARDERR);
65
[1038]66# ifdef FORTIFY
67 Fortify_EnterScope();
[1063]68# endif
[2]69 thab = WinInitialize(0);
[551]70 thmq = WinCreateMsgQueue(thab, 0);
[528]71 if (thab && thmq) {
[551]72 WinCancelShutdown(thmq, TRUE);
[528]73 IncrThreadUsage();
[551]74 WinSendDlgItemMsg(hwnd, UNDEL_LISTBOX, LM_DELETEALL, MPVOID, MPVOID);
[1073]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__);
[328]81 if (!fp) {
[551]82 Win_Error(NULLHANDLE, hwnd, pszSrcFile, __LINE__,
83 GetPString(IDS_REDIRECTERRORTEXT));
[328]84 killme = TRUE;
85 goto Abort;
86 }
87 else {
[2]88 newstdout = -1;
[551]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;
[2]96 }
97 oldstdout = fileno(stdout);
[551]98 DosDupHandle(fileno(fp), &oldstdout);
[2]99 runemf2(SEPARATE | INVISIBLE | WINDOWED | BACKGROUND | WAIT,
[888]100 hwnd, pszSrcFile, __LINE__,
[551]101 NULL,
102 NULL,
103 "UNDELETE.COM %s /L%s",
104 path, (undelinfo->inclsubdirs) ? " /S" : NullStr);
[2]105 oldstdout = fileno(stdout);
[551]106 DosDupHandle(newstdout, &oldstdout);
[2]107 DosClose(newstdout);
108 fclose(fp);
109 }
[1073]110 fp = xfopen(szTempFile, "r", pszSrcFile, __LINE__);
[328]111 if (fp) {
[551]112 xfgets(s, sizeof(s), fp, pszSrcFile, __LINE__); // Skip 1st line
[406]113 while (!feof(fp)) {
[551]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)) {
[2]119
[551]120 APIRET temp;
[2]121
[551]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,
[888]134 hwnd, pszSrcFile, __LINE__,
[551]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
[847]146 && DosQueryPathInfo(s, FIL_STANDARD, &fsa,
[551]147 (ULONG) sizeof(fsa)))
148 WinSendDlgItemMsg(hwnd, UNDEL_LISTBOX, LM_INSERTITEM,
149 MPFROM2SHORT(LIT_SORTASCENDING, 0), MPFROMP(s));
150 }
[2]151 }
152 fclose(fp);
153 }
[551]154 Abort:
[528]155 ;
156 }
[1073]157 DosForceDelete(szTempFile);
[1009]158 xfree(undelinfo, pszSrcFile, __LINE__);
[528]159 if (thmq) {
[551]160 PostMsg(hwnd, UM_CONTAINER_FILLED, MPVOID, MPVOID);
[528]161 if (killme)
[551]162 PostMsg(hwnd, WM_CLOSE, MPVOID, MPVOID);
[2]163 WinDestroyMsgQueue(thmq);
[528]164 }
165 if (thab) {
166 DecrThreadUsage();
[2]167 WinTerminate(thab);
168 }
[1038]169# ifdef FORTIFY
[1063]170 Fortify_LeaveScope();
171# endif
[2]172}
173
[551]174MRESULT EXPENTRY UndeleteDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
[328]175{
[551]176 SHORT sSelect;
177 static BOOL listdone, changed = FALSE, refresh = FALSE;
178 static HPOINTER hptrIcon = (HPOINTER) 0;
[2]179
[551]180 switch (msg) {
181 case WM_INITDLG:
182 listdone = TRUE;
[1009]183 if (!mp2 || !*(CHAR *)mp2) {
[551]184 Runtime_Error2(pszSrcFile, __LINE__, IDS_NODATATEXT);
185 WinDismissDlg(hwnd, 0);
[2]186 break;
[551]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];
[2]194
[1009]195 strcpy(s, (CHAR *)mp2);
[551]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;
[2]206
[551]207 case UM_SETUP:
208 if (listdone) {
[2]209
[551]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));
[1039]235 free(undelinfo);
[551]236 listdone = TRUE;
237 WinDismissDlg(hwnd, 0);
[328]238 }
[551]239 else
[771]240 DosSleep(100);//05 Aug 07 GKY 500
[2]241 }
[551]242 refresh = FALSE;
243 }
244 else
245 refresh = TRUE;
246 changed = FALSE;
247 return 0;
[2]248
[551]249 case UM_CONTAINER_FILLED:
250 listdone = TRUE;
251 {
252 CHAR s[33];
[2]253
[551]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;
[2]264
[551]265 case WM_CONTROL:
266 switch (SHORT1FROMMP(mp1)) {
267 case UNDEL_SUBDIRS:
268 PostMsg(hwnd, UM_SETUP, MPVOID, MPVOID);
269 break;
[2]270
[551]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;
[2]280 }
281 break;
282
[551]283 case UNDEL_DRIVELIST:
284 if (!listdone) {
285 Runtime_Error(pszSrcFile, __LINE__, "not listdone");
286 break;
[2]287 }
[551]288 switch (SHORT2FROMMP(mp1)) {
289 case CBN_ENTER:
290 {
291 CHAR s[CCHMAXPATH], drive, *p;
[2]292
[551]293 strset(s, 0);
294 WinQueryDlgItemText(hwnd, UNDEL_DRIVELIST, 3, s);
295 if (!isalpha(*s)) {
296 Runtime_Error2(pszSrcFile, __LINE__, IDS_NODATATEXT);
[328]297 }
298 else {
[551]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;
[2]326
[551]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;
[2]334
[551]335 default:
336 break;
337 }
338 return 0;
[2]339
[551]340 case WM_ADJUSTWINDOWPOS:
341 PostMsg(hwnd, UM_STRETCH, MPVOID, MPVOID);
342 break;
[2]343
[551]344 case UM_STRETCH:
345 {
346 SWP swpC, swp, swpM, swpD, swpL, swpE;
[2]347
[551]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);
[2]388 }
[551]389 }
390 return 0;
[2]391
[551]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");
[328]398 }
[551]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,
[888]439 hwnd, pszSrcFile, __LINE__,
[551]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 }
[328]456 break;
[2]457
[551]458 case DID_CANCEL:
459 if (!listdone)
460 Runtime_Error(pszSrcFile, __LINE__, "is busy");
461 else
462 WinDismissDlg(hwnd, 0);
[2]463 break;
[551]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;
[2]486 }
[551]487 return WinDefDlgProc(hwnd, msg, mp1, mp2);
[2]488}
[793]489
490#pragma alloc_text(UNDELETE,FillUndelListThread,UndeleteDlgProc)
Note: See TracBrowser for help on using the repository browser.