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