source: trunk/dll/undel.c@ 1036

Last change on this file since 1036 was 1009, checked in by Steven Levine, 17 years ago

Add xfree xstrdup Fortify support
Add MT capable Fortify scope logic

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