source: trunk/dll/undel.c@ 1038

Last change on this file since 1038 was 1038, checked in by Gregg Young, 17 years ago

More fortify cleanup

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