source: trunk/dll/grep.c@ 837

Last change on this file since 837 was 837, checked in by Gregg Young, 18 years ago

Fix for #144 crash when file names exceed max path; Initial xDosFindFirst/Next wrapper for performacne testing

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 35.6 KB
Line 
1
2/***********************************************************************
3
4 $Id: grep.c 837 2007-09-21 19:17:59Z gyoung $
5
6 grep tools
7
8 Copyright (c) 1993-98 M. Kimes
9 Copyright (c) 2001, 2007 Steven H. Levine
10
11 12 Feb 03 SHL insert_grepfile: standardize EA math
12 12 Feb 03 SHL doonefile: standardize EA math
13 25 May 05 SHL Rework for ULONGLONG
14 25 May 05 SHL Rework for FillInRecordFromFFB
15 06 Jun 05 SHL Drop unused code
16 24 Oct 05 SHL dononefile: do not free EA list twice
17 22 Jul 06 SHL Use Runtime_Error
18 26 Jul 06 SHL Check more run time errors
19 19 Oct 06 SHL Correct . and .. detect
20 03 Nov 06 SHL Count thread usage
21 03 Aug 07 GKY Enlarged and made setable everywhere Findbuf (speed file loading)
22 06 Aug 07 GKY Reduce DosSleep times (ticket 148)
23 13 Aug 07 SHL Avoid pointer errors; sanitize code
24 13 Aug 07 SHL Move #pragma alloc_text to end for OpenWatcom compat
25 15 Aug 07 SHL Use FilesToGet directly
26 26 Aug 07 GKY Improved performance of FillDups
27 26 Aug 07 GKY DosSleep(1) in loops changed to (0)
28 21 Sep 07 GKY Fix trap on search that includes filenames that exceed maxpath
29
30***********************************************************************/
31
32#define INCL_DOS
33#define INCL_WIN
34#define INCL_DOSERRORS
35#define INCL_LONGLONG
36#include <os2.h>
37
38#include <stdlib.h>
39#include <string.h>
40#include <ctype.h>
41#include <stdio.h>
42#include <share.h>
43
44#include "fm3dll.h"
45#include "fm3str.h"
46#include "grep.h"
47
48#pragma data_seg(DATA2)
49
50static PSZ pszSrcFile = __FILE__;
51
52static VOID doallsubdirs(GREP *grep, CHAR *searchPath, BOOL recursing,
53 char **fle, int numfls);
54static INT domatchingfiles(GREP *grep, CHAR *path, char **fle, int numfls);
55static BOOL doonefile(GREP *grep, CHAR *fileName, FILEFINDBUF4 *pffb);
56static BOOL doinsertion(GREP *grep);
57static BOOL InsertDupe(GREP *grep, CHAR *dir, FILEFINDBUF4 *pffb);
58static VOID FillDupes(GREP *grep);
59static VOID FreeDupes(GREP *grep);
60
61#define GREPCHARS "*?[] \\"
62
63#define isleap(year) ((((year%4)==0) && ((year%100)!=0)) || \
64 ((year%400)==0))
65
66static INT monthdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
67
68ULONG SecsSince1980(FDATE * date, FTIME * time)
69{
70 ULONG total = 0L;
71 register int x;
72
73 for (x = 1980; x < date->year + 1980; x++) {
74 if (isleap(x))
75 total += (366L * (24L * 60L * 60L));
76 else
77 total += (365L * (24L * 60L * 60L));
78 }
79 for (x = 1; x < date->month; x++) {
80 if (x == 2 && isleap(date->year + 1980))
81 total += (29L * (24L * 60L * 60L));
82 else
83 total += ((long)monthdays[x - 1] * (24L * 60L * 60L));
84 }
85 total += (((long)date->day - 1L) * (24L * 60L * 60L));
86 total += ((long)time->hours * (60L * 60L));
87 total += ((long)time->minutes * 60L);
88 total += ((long)time->twosecs * 2L);
89 return total;
90}
91
92/*
93 * this function originally from C_ECHO's Snippets -- modified
94 * brute force methodology
95 */
96
97static BOOL m_match(CHAR * string, CHAR * pattern, BOOL absolute, BOOL ignore,
98 LONG len)
99{
100 // return TRUE if pattern found in string
101 register CHAR *tn = pattern;
102 register LONG len2 = 0;
103 LONG lastlen = 0;
104 CHAR lo, hi;
105
106 if (len && string && pattern) {
107 if (absolute) // no pattern matching
108 return (findstring(pattern, strlen(pattern), string, len,
109 (ignore == FALSE)) != NULL);
110
111 while (*tn && len2 < len) {
112 switch (*tn) {
113 case ' ':
114 while (*tn == ' ')
115 tn++;
116 while (len2 < len && isspace(string[len2]))
117 len2++;
118 break;
119
120 case '*':
121 while (*tn == '*' || *tn == '?')
122 tn++;
123 if (!*tn)
124 return TRUE;
125 if (ignore) {
126 while (len2 < len && string[len2] != *tn)
127 len2++;
128 }
129 else {
130 while (len2 < len && toupper(string[len2] != *tn))
131 len2++;
132 }
133 break;
134
135 case '[':
136 tn++;
137 if (!*tn)
138 return FALSE;
139 lo = *tn;
140 tn++;
141 if (*tn != '-')
142 return FALSE;
143 tn++;
144 if (!*tn)
145 return FALSE;
146 hi = *tn;
147 tn++;
148 if (*tn != ']')
149 return FALSE;
150 tn++;
151 if (ignore) {
152 if ((toupper(string[len2]) >= toupper(lo)) &&
153 (toupper(string[len2]) <= toupper(hi)))
154 len2++;
155 else {
156 tn = pattern;
157 len2 = lastlen = lastlen + 1;
158 }
159 }
160 else {
161 if ((string[len2] >= lo) && (string[len2] <= hi))
162 len2++;
163 else {
164 tn = pattern;
165 len2 = lastlen = lastlen + 1;
166 }
167 }
168 break;
169
170 case '?':
171 tn++;
172 len2++;
173 break;
174
175 case '\\':
176 tn++;
177 if (!*tn)
178 return FALSE;
179 // else intentional fallthru
180 default:
181 if (ignore) {
182 if (toupper(*tn) == toupper(string[len2])) {
183 tn++;
184 len2++;
185 }
186 else {
187 tn = pattern;
188 len2 = lastlen = lastlen + 1;
189 }
190 }
191 else {
192 if (*tn == string[len2]) {
193 tn++;
194 len2++;
195 }
196 else {
197 tn = pattern;
198 len2 = lastlen = lastlen + 1;
199 }
200 }
201 break;
202 }
203 }
204 while (*tn == '*')
205 tn++;
206
207 if (!*tn)
208 return TRUE;
209 }
210 return FALSE;
211}
212
213static BOOL match(CHAR * string, CHAR * patterns, BOOL absolute, BOOL ignore,
214 LONG len, ULONG numlines, CHAR * matched, BOOL matchall)
215{
216 BOOL ret = FALSE;
217 register CHAR *p;
218 register ULONG x = 0;
219
220 p = patterns;
221 while (!ret && *p) {
222 ret = m_match(string, p, absolute, ignore, len);
223 if (matchall && ret)
224 break;
225 if (matched && ret && x < numlines)
226 matched[x] = 1;
227 p += strlen(p); // check each pattern in 0-terminated list
228 p++;
229 x++;
230 }
231 return ret;
232}
233
234VOID GrepThread(VOID * arg)
235{
236 HAB ghab;
237 HMQ ghmq;
238 GREP grep;
239 register INT x, numfls;
240 static CHAR *fle[512];
241 CHAR *p, *pp, searchPath[CCHMAXPATH * 2];
242
243 if (!arg) {
244 Runtime_Error(pszSrcFile, __LINE__, "no data");
245 return;
246 }
247
248 grep = *(GREP *)arg;
249 *grep.stopflag = 0; // reset thread-killing flag
250 DosError(FERR_DISABLEHARDERR);
251 priority_normal();
252
253 ghab = WinInitialize(0);
254 if (ghab) {
255 grep.ghab = ghab;
256 ghmq = WinCreateMsgQueue(ghab, 0);
257 if (ghmq) {
258 WinCancelShutdown(ghmq, TRUE);
259 IncrThreadUsage();
260 DosSleep(100); //05 Aug 07 GKY 128
261 WinSetWindowText(grep.hwndCurFile,
262 GetPString((grep.finddupes) ?
263 IDS_GREPDUPETEXT : IDS_GREPSCANTEXT));
264
265 pp = grep.searchPattern;
266 while (*pp) {
267 if (!grep.absFlag) {
268 p = GREPCHARS; // see if any sense in pattern matching
269 while (*p) {
270 if (strchr(pp, *p))
271 break;
272 p++;
273 }
274 if (!*p) // nope, turn it off
275 grep.absFlag = TRUE;
276 }
277 pp = pp + strlen(pp) + 1;
278 }
279
280 grep.attrFile &= (~FILE_DIRECTORY);
281 grep.antiattr &= (~FILE_DIRECTORY);
282 if (grep.antiattr & FILE_READONLY)
283 grep.antiattr |= MUST_HAVE_READONLY;
284 if (grep.antiattr & FILE_HIDDEN)
285 grep.antiattr |= MUST_HAVE_HIDDEN;
286 if (grep.antiattr & FILE_SYSTEM)
287 grep.antiattr |= MUST_HAVE_SYSTEM;
288 if (grep.antiattr & FILE_ARCHIVED)
289 grep.antiattr |= MUST_HAVE_ARCHIVED;
290
291 grep.anyexcludes = FALSE;
292 numfls = x = 0;
293 fle[numfls++] = strtok(grep.tosearch, ";");
294 while ((fle[numfls] = strtok(NULL, ";")) != NULL && numfls < 511) {
295 if (*fle[numfls] == '/')
296 grep.anyexcludes = TRUE;
297 numfls++;
298 }
299
300 while (x < numfls) { // loop through search masks
301
302 if (*fle[x] == '/') // is an exclude mask only
303 goto ExcludeSkip;
304
305 // first, separate any path from mask
306
307 p = (char *)(fle[x] + (strlen(fle[x]) - 1));
308 while (*p != '\\' && *p != ':' && p != fle[x])
309 --p;
310
311 if (p == fle[x]) { // no path
312 strcpy(searchPath, grep.curdir);
313 strncpy(grep.fileMask, fle[x], CCHMAXPATH);
314 grep.fileMask[CCHMAXPATH - 1] = 0;
315 }
316 else { // got to deal with a path
317 if (*p == ':') { // just a drive, start in root dir
318 *p = 0;
319 p++;
320 strncpy(searchPath, fle[x], CCHMAXPATH - 2);
321 searchPath[CCHMAXPATH - 3] = 0;
322 strcat(searchPath, ":\\");
323 strcpy(grep.fileMask, p);
324 }
325 if (*p == '\\') { // got a 'full' path
326
327 CHAR temp;
328
329 p++;
330 temp = *p;
331 *p = 0;
332 strncpy(searchPath, fle[x], CCHMAXPATH);
333 searchPath[CCHMAXPATH - 1] = 0;
334 *p = temp;
335 strcpy(grep.fileMask, p);
336 }
337 if (!*grep.fileMask)
338 strcpy(grep.fileMask, "*");
339 }
340 if (*grep.stopflag)
341 break;
342 // do single directory
343 domatchingfiles(&grep, searchPath, fle, numfls);
344 if (grep.dirFlag) // do subdirs
345 doallsubdirs(&grep, searchPath, FALSE, fle, numfls);
346 ExcludeSkip:
347 if (*grep.stopflag)
348 break;
349 x++;
350 if (WinIsWindow(grep.ghab, grep.hwndFiles))
351 doinsertion(&grep); // insert any remaining objects
352 } // while
353
354 if (WinIsWindow(grep.ghab, grep.hwndFiles))
355 doinsertion(&grep); // insert any remaining objects
356
357 if (WinIsWindow(grep.ghab, grep.hwndFiles) && grep.finddupes &&
358 !*grep.stopflag)
359 FillDupes(&grep);
360
361 if (!PostMsg(grep.hwndFiles, UM_CONTAINER_FILLED, MPVOID, MPVOID)) // tell window we're done
362 WinSendMsg(grep.hwndFiles, UM_CONTAINER_FILLED, MPVOID, MPVOID);
363 WinDestroyMsgQueue(ghmq);
364 }
365 DecrThreadUsage();
366 WinTerminate(ghab);
367 }
368 if (!ghmq || !ghab)
369 WinPostMsg(grep.hwndFiles, UM_CONTAINER_FILLED, MPVOID, MPVOID);
370 if (grep.dupehead)
371 FreeDupes(&grep);
372 if (grep.numlines && grep.matched)
373 free(grep.matched);
374 DosPostEventSem(CompactSem);
375}
376
377static BOOL IsExcluded(char *name, char **fle, int numfls)
378{
379 int x;
380 char *n;
381
382 n = strrchr(name, '\\');
383 if (!n)
384 n = strrchr(name, ':');
385 if (n)
386 n++;
387 else
388 n = name;
389 for (x = 0; x < numfls; x++) {
390 if (*fle[x] == '/' &&
391 wildcard((strchr(fle[x], '\\') ||
392 strchr(fle[x], ':')) ? name : n, fle[x] + 1, FALSE))
393 return TRUE;
394 }
395 return FALSE;
396}
397
398static VOID doallsubdirs(GREP * grep, CHAR * searchPath, BOOL recursing,
399 char **fle, int numfls)
400{
401 // process all subdirectories
402
403 FILEFINDBUF4 ffb;
404 HDIR findHandle = HDIR_CREATE;
405 LONG ulFindCnt = 1;
406 CHAR *p = NULL;
407
408 // add a mask to search path
409 if (searchPath[strlen(searchPath) - 1] != '\\')
410 strcat(searchPath, "\\");
411 strcat(searchPath, "*");
412 // step through all subdirectories
413 DosError(FERR_DISABLEHARDERR);
414 if (!xDosFindFirst(searchPath, &findHandle, (MUST_HAVE_DIRECTORY |
415 FILE_ARCHIVED | FILE_SYSTEM | FILE_HIDDEN | FILE_READONLY),
416 &ffb, (ULONG) sizeof(ffb),
417 (PULONG) & ulFindCnt, FIL_QUERYEASIZE)) {
418
419 // get rid of mask portion, save end-of-directory
420
421 p = strrchr(searchPath, '\\');
422 if (p)
423 p++;
424 else
425 p = searchPath;
426 do { // Process each directory that matches the mask
427 priority_normal();
428 if (*grep->stopflag)
429 break;
430 // Skip . and ..
431 if (ffb.achName[0] != '.' ||
432 (ffb.achName[1] &&
433 (ffb.achName[1] != '.' || ffb.achName[2]))) {
434 strcpy(p, ffb.achName);
435 if (!grep->anyexcludes || !IsExcluded(searchPath, fle, numfls)) {
436 domatchingfiles(grep, searchPath, fle, numfls);
437 doallsubdirs(grep, searchPath, TRUE, fle, numfls);
438 DosSleep(0); //26 Aug 07 GKY 1
439 }
440 }
441 ulFindCnt = 1;
442 } while (!xDosFindNext(findHandle,
443 &ffb,
444 sizeof(ffb), (PULONG) & ulFindCnt));
445 DosFindClose(findHandle);
446 priority_normal();
447 }
448 if (p) // strip off last directory addition
449 *p = 0;
450}
451
452static INT domatchingfiles(GREP * grep, CHAR * path, char **fle, int numfls)
453{
454 // process all matching files in a directory
455
456 PFILEFINDBUF4 pffbArray;
457 PFILEFINDBUF4 pffbFile;
458 ULONG x;
459 HDIR findHandle = HDIR_CREATE;
460 ULONG ulFindCnt;
461 CHAR szFindPath[CCHMAXPATH];
462 PSZ p;
463 APIRET rc;
464 ULONG ulBufBytes = FilesToGet * sizeof(FILEFINDBUF4);
465
466 pffbArray = xmalloc(ulBufBytes, pszSrcFile, __LINE__);
467 if (!pffbArray)
468 return 0;
469
470 // build filemask
471 BldFullPathName(szFindPath, path, grep->fileMask);
472 // sprintf(szFindPath,
473 // "%s%s%s",
474 // path,
475 // (path[strlen(path) - 1] == '\\') ? NullStr : "\\", grep->fileMask);
476
477 MakeFullName(szFindPath);
478
479 // find and save end-of-dir position
480 p = strrchr(szFindPath, '\\');
481 if (p)
482 p++;
483 else
484 p = szFindPath;
485
486 // step through matching files
487 DosError(FERR_DISABLEHARDERR);
488 ulFindCnt = FilesToGet;
489 rc = xDosFindFirst(szFindPath,
490 &findHandle,
491 FILE_NORMAL | grep->attrFile | grep->antiattr,
492 pffbArray,
493 ulBufBytes,
494 &ulFindCnt,
495 FIL_QUERYEASIZE);
496 if (!rc) {
497 do {
498 // Process each file that matches the mask
499 priority_normal();
500 pffbFile = pffbArray;
501 for (x = 0; x < ulFindCnt; x++) {
502 if (*grep->stopflag)
503 break;
504 if (*pffbFile->achName != '.' ||
505 (pffbFile->achName[1] && pffbFile->achName[1] != '.')) {
506 strcpy(p, pffbFile->achName); // build filename
507 if (strlen(szFindPath) > 256){ //21 Sep GKY check for pathnames that exceed maxpath
508 DosFindClose(findHandle);
509 free(pffbArray);
510 return 1;
511 }
512 if (!grep->anyexcludes || !IsExcluded(szFindPath, fle, numfls)) {
513 if (!grep->finddupes)
514 doonefile(grep, szFindPath, pffbFile);
515 else if (!InsertDupe(grep, szFindPath, pffbFile)) {
516 DosFindClose(findHandle);
517 free(pffbArray);
518 return 1;
519 }
520 }
521 }
522 if (!pffbFile->oNextEntryOffset)
523 break;
524 pffbFile = (PFILEFINDBUF4)((PBYTE)pffbFile + pffbFile->oNextEntryOffset);
525 } // for
526 if (*grep->stopflag)
527 break;
528 DosSleep(0); //26 Aug 07 GKY 1
529 ulFindCnt = FilesToGet;
530 rc = xDosFindNext(findHandle, pffbArray, ulBufBytes, &ulFindCnt);
531 } while (!rc);
532
533 DosFindClose(findHandle);
534 priority_normal();
535 } // if
536
537 if (rc && rc != ERROR_NO_MORE_FILES) {
538 Dos_Error(MB_ENTER, rc, HWND_DESKTOP, pszSrcFile, __LINE__,
539 GetPString(IDS_CANTFINDDIRTEXT), szFindPath);
540 }
541
542 free(pffbArray);
543 return 0;
544}
545
546static VOID freegreplist(GREP * grep)
547{
548 INT x;
549
550 if (grep) {
551 if (grep->insertffb) {
552 for (x = 0; grep->insertffb[x]; x++)
553 free(grep->insertffb[x]);
554 free(grep->insertffb);
555 }
556 if (grep->dir) {
557 for (x = 0; grep->dir[x]; x++)
558 free(grep->dir[x]);
559 free(grep->dir);
560 }
561 grep->dir = NULL;
562 grep->insertffb = NULL;
563 grep->toinsert = 0L;
564 grep->insertedbytes = 0L;
565 }
566}
567
568static BOOL doinsertion(GREP * grep)
569{
570 RECORDINSERT ri;
571 DIRCNRDATA *dcd;
572 PCNRITEM pci, pciFirst;
573 INT x;
574
575 if (!grep || !grep->toinsert || !grep->insertffb || !grep->dir)
576 return FALSE;
577 pci = WinSendMsg(grep->hwndFiles,
578 CM_ALLOCRECORD,
579 MPFROMLONG(EXTRA_RECORD_BYTES),
580 MPFROMLONG(grep->toinsert));
581 if (pci) {
582 if (grep->sayfiles)
583 WinSetWindowText(grep->hwndCurFile, GetPString(IDS_GREPINSERTINGTEXT));
584 pciFirst = pci;
585 dcd = INSTDATA(grep->hwndFiles);
586 for (x = 0; grep->insertffb[x]; x++) {
587 FillInRecordFromFFB(grep->hwndFiles,
588 pci, grep->dir[x], grep->insertffb[x], FALSE, dcd);
589 pci = (PCNRITEM) pci->rc.preccNextRecord;
590 }
591 memset(&ri, 0, sizeof(RECORDINSERT));
592 ri.cb = sizeof(RECORDINSERT);
593 ri.pRecordOrder = (PRECORDCORE) CMA_END;
594 ri.pRecordParent = (PRECORDCORE) NULL;
595 ri.zOrder = (USHORT) CMA_TOP;
596 ri.cRecordsInsert = grep->toinsert;
597 ri.fInvalidateRecord = TRUE;
598 WinSendMsg(grep->hwndFiles,
599 CM_INSERTRECORD, MPFROMP(pciFirst), MPFROMP(&ri));
600 if (dcd) {
601 DosEnterCritSec();
602 dcd->ullTotalBytes += grep->insertedbytes;
603 DosExitCritSec();
604 }
605 if (grep->toinsert == FilesToGet)
606 DosSleep(0); //26 Aug 07 GKY 1
607 freegreplist(grep);
608 PostMsg(grep->hwndFiles, UM_RESCAN, MPVOID, MPVOID);
609 return TRUE;
610 }
611 freegreplist(grep);
612 return FALSE;
613}
614
615static BOOL insert_grepfile(GREP *grep, CHAR *filename, PFILEFINDBUF4 pffb)
616{
617 PSZ p;
618 CHAR szDirectory[CCHMAXPATH];
619
620 if (WinIsWindow(grep->ghab, grep->hwndFiles)) {
621 grep->numfiles++;
622 strcpy(szDirectory, filename);
623 p = strrchr(szDirectory, '\\');
624 if (p) {
625 if (p < szDirectory + 4)
626 p++;
627 *p = 0;
628 if (!grep->insertffb) {
629 // Allocate 1 extra for end marker?
630 grep->insertffb = xmallocz(sizeof(PFILEFINDBUF4) * (FilesToGet + 1),
631 pszSrcFile, __LINE__);
632 if (!grep->insertffb)
633 return FALSE;
634 grep->dir = xmallocz(sizeof(CHAR *) * (FilesToGet + 1),
635 pszSrcFile, __LINE__);
636 if (!grep->dir) {
637 free(grep->insertffb);
638 return FALSE;
639 }
640 }
641 grep->insertffb[grep->toinsert] =
642 xmalloc(sizeof(FILEFINDBUF4), pszSrcFile, __LINE__);
643 if (!grep->insertffb[grep->toinsert])
644 return FALSE;
645 memcpy(grep->insertffb[grep->toinsert], pffb, sizeof(FILEFINDBUF4));
646 grep->dir[grep->toinsert] = xstrdup(szDirectory, pszSrcFile, __LINE__);
647 if (!grep->dir) {
648 free(grep->insertffb[grep->toinsert]);
649 return FALSE;
650 }
651 grep->insertedbytes += pffb->cbFile + CBLIST_TO_EASIZE(pffb->cbList);
652 grep->toinsert++;
653 if (grep->toinsert == FilesToGet)
654 return doinsertion(grep);
655 return TRUE;
656 }
657 }
658 else
659 freegreplist(grep);
660 return FALSE;
661}
662
663static BOOL doonefile(GREP *grep, CHAR *filename, FILEFINDBUF4 * pffb)
664{
665 // process a single file
666 CHAR *input;
667 FILE *inputFile;
668 ULONG pos;
669 BOOL ret = FALSE, strmatch = FALSE;
670
671 grep->fileCount++;
672 if (grep->sayfiles)
673 WinSetWindowText(grep->hwndCurFile, filename);
674
675 if (grep->greaterthan || grep->lessthan) {
676
677 BOOL keep = TRUE;
678 ULONG adjsize;
679
680 adjsize = pffb->cbFile + (grep->searchEAs ? CBLIST_TO_EASIZE(pffb->cbList) : 0);
681 if (grep->greaterthan) {
682 if (adjsize < grep->greaterthan)
683 keep = FALSE;
684 }
685 if (keep && grep->lessthan) {
686 if (adjsize > grep->lessthan)
687 keep = FALSE;
688 }
689 if (!keep)
690 return ret;
691 }
692
693 if (grep->newerthan || grep->olderthan) {
694
695 BOOL keep = TRUE;
696 ULONG numsecs;
697
698 numsecs = SecsSince1980(&pffb->fdateLastWrite, &pffb->ftimeLastWrite);
699 if (grep->newerthan) {
700 if (numsecs < grep->newerthan)
701 keep = FALSE;
702 }
703 if (keep && grep->olderthan) {
704 if (numsecs > grep->olderthan)
705 keep = FALSE;
706 }
707 if (!keep)
708 return ret;
709 }
710
711 if ((!grep->searchEAs && !grep->searchFiles) || !*grep->searchPattern) // just a find
712 return insert_grepfile(grep, filename, pffb);
713
714 if (grep->searchEAs) {
715
716 HOLDFEA *head, *info;
717 USHORT type, len;
718 BOOL alltext;
719 CHAR *data, temp;
720
721 head = GetFileEAs(filename, FALSE, TRUE);
722 if (head) {
723 info = head;
724 while (info && !strmatch) {
725 alltext = TRUE;
726 switch (*(USHORT *) info->value) {
727 case EAT_ASCII:
728 if (match(info->value + (sizeof(USHORT) * 2),
729 grep->searchPattern, grep->absFlag,
730 (grep->caseFlag == FALSE),
731 info->cbValue - (sizeof(USHORT) * 2),
732 grep->numlines, grep->matched, !grep->findifany)) {
733 strmatch = TRUE;
734 }
735 break;
736 case EAT_MVST:
737 type = *(USHORT *) (info->value + (sizeof(USHORT) * 3));
738 if (type == EAT_ASCII) {
739 data = info->value + (sizeof(USHORT) * 4);
740 len = *(USHORT *) data;
741 data += sizeof(USHORT);
742 while ((data - info->value) + len <= info->cbValue) {
743 temp = *(data + len);
744 *(data + len) = 0;
745 if (match(data,
746 grep->searchPattern,
747 grep->absFlag,
748 (grep->caseFlag == FALSE),
749 len,
750 grep->numlines, grep->matched, !grep->findifany)) {
751 strmatch = TRUE;
752 break;
753 }
754 data += len;
755 if (data - info->value >= info->cbValue)
756 break;
757 *data = temp;
758 len = *(USHORT *) data;
759 data += sizeof(USHORT);
760 }
761 }
762 break;
763 case EAT_MVMT:
764 data = info->value + (sizeof(USHORT) * 3);
765 type = *(USHORT *) data;
766 data += sizeof(USHORT);
767 len = *(USHORT *) data;
768 data += sizeof(USHORT);
769 while ((data - info->value) - len <= info->cbValue) {
770 if (type != EAT_ASCII) {
771 alltext = FALSE;
772 break;
773 }
774 data += len;
775 if (data - info->value >= info->cbValue)
776 break;
777 type = *(USHORT *) data;
778 data += sizeof(USHORT);
779 len = *(USHORT *) data;
780 data += sizeof(USHORT);
781 }
782 if (alltext) {
783 data = info->value + (sizeof(USHORT) * 3);
784 type = *(USHORT *) data;
785 data += sizeof(USHORT);
786 len = *(USHORT *) data;
787 data += sizeof(USHORT);
788 while ((data - info->value) - len <= info->cbValue) {
789 temp = *(data + len);
790 *(data + len) = 0;
791 if (match(data,
792 grep->searchPattern,
793 grep->absFlag,
794 (grep->caseFlag == FALSE),
795 len,
796 grep->numlines, grep->matched, !grep->findifany)) {
797 strmatch = TRUE;
798 break;
799 }
800 data += len;
801 *data = temp;
802 if (data - info->value >= info->cbValue)
803 break;
804 type = *(USHORT *) data;
805 data += sizeof(USHORT);
806 len = *(USHORT *) data;
807 data += sizeof(USHORT);
808 }
809 }
810 break;
811 default:
812 break;
813 }
814 info = info->next;
815 } // while
816 Free_FEAList(head);
817 DosSleep(1);
818 }
819 }
820
821 if (grep->searchFiles) {
822 input = xmalloc(65537, pszSrcFile, __LINE__);
823 if (input) {
824 LONG len;
825
826 inputFile = _fsopen(filename, "rb", SH_DENYNO);
827 if (inputFile) {
828 pos = ftell(inputFile);
829 while (!feof(inputFile)) {
830 if (pos)
831 fseek(inputFile, pos - 1024, SEEK_SET);
832 len = fread(input, 1, 65536, inputFile);
833 if (len >= 0) {
834 if (*grep->stopflag)
835 break;
836 if (match(input,
837 grep->searchPattern,
838 grep->absFlag,
839 (grep->caseFlag == FALSE),
840 len, grep->numlines, grep->matched, !grep->findifany)) {
841 strmatch = TRUE;
842 break;
843 }
844 }
845 else
846 break;
847 }
848 fclose(inputFile);
849 }
850 free(input);
851 DosSleep(1);
852 }
853 } // if
854
855 if (strmatch)
856 ret = insert_grepfile(grep, filename, pffb);
857 return ret;
858}
859
860static LONG cr3tab[] = { // CRC polynomial 0xEDB88320
861
862 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
863 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
864 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
865 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
866 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
867 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
868 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
869 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
870 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
871 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
872 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
873 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
874 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
875 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
876 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
877 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
878 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
879 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
880 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
881 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
882 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
883 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
884 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
885 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
886 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
887 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
888 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
889 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
890 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
891 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
892 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
893 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
894 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
895 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
896 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
897 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
898 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
899 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
900 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
901 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
902 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
903 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
904 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
905 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
906 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
907 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
908 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
909 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
910 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
911 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
912 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
913 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
914 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
915 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
916 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
917 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
918 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
919 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
920 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
921 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
922 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
923 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
924 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
925 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
926};
927
928LONG CRCBlock(register CHAR * str, register INT blklen, register LONG crc)
929{
930 while (blklen--) {
931 crc =
932 cr3tab[((INT) crc ^ *str) & 0xff] ^ (((ULONG) crc >> 8) & 0x00FFFFFF);
933 str++;
934 }
935 return crc;
936}
937
938LONG CRCFile(CHAR * filename, INT * error)
939{
940 LONG CRC = -1L, len;
941 FILE *fp;
942 CHAR *buffer;
943
944 *error = 0;
945 buffer = xmalloc(65535, pszSrcFile, __LINE__);
946 if (!buffer)
947 *error = -1;
948 else {
949 fp = _fsopen(filename, "rb", SH_DENYNO);
950 if (!fp)
951 *error = -2;
952 else {
953 while (!feof(fp)) {
954 len = fread(buffer, 1, 65535, fp);
955 if (len && len < 65536L)
956 CRC = CRCBlock(buffer, len, CRC);
957 else
958 break;
959 DosSleep(0); //26 Aug 07 GKY 1
960 }
961 fclose(fp);
962 DosSleep(1);
963 }
964 free(buffer);
965 }
966 return CRC;
967}
968
969static VOID FreeDupes(GREP *grep)
970{
971 DUPES *i, *next;
972
973 i = grep->dupehead;
974 while (i) {
975 next = i->next;
976 if (i->name)
977 free(i->name);
978 free(i);
979 i = next;
980 }
981 grep->dupehead = grep->dupelast = NULL;
982 if (grep->dupenames)
983 free(grep->dupenames);
984 if (grep->dupesizes)
985 free(grep->dupesizes);
986 grep->dupesizes = grep->dupenames = NULL;
987}
988
989int comparenamesq(const void *v1, const void *v2)
990{
991 DUPES *d1 = *(DUPES **) v1;
992 DUPES *d2 = *(DUPES **) v2;
993 CHAR *p1, *p2;
994
995 p1 = strrchr(d1->name, '\\');
996 if (p1)
997 p1++;
998 else
999 p1 = d1->name;
1000 p2 = strrchr(d2->name, '\\');
1001 if (p2)
1002 p2++;
1003 else
1004 p2 = d2->name;
1005 return stricmp(p1, p2);
1006}
1007
1008int comparenamesqe(const void *v1, const void *v2)
1009{
1010 DUPES *d1 = *(DUPES **) v1;
1011 DUPES *d2 = *(DUPES **) v2;
1012 CHAR *p1, *p2, *p1e, *p2e, e1, e2;
1013 int ret;
1014
1015 p1 = strrchr(d1->name, '\\');
1016 if (p1)
1017 p1++;
1018 else
1019 p1 = d1->name;
1020 p1e = strrchr(p1, '.');
1021 if (p1e) {
1022 e1 = *p1e;
1023 *p1e = 0;
1024 }
1025 p2 = strrchr(d2->name, '\\');
1026 if (p2)
1027 p2++;
1028 else
1029 p2 = d2->name;
1030 p2e = strrchr(p2, '.');
1031 if (p2e) {
1032 e2 = *p2e;
1033 *p2e = 0;
1034 }
1035 ret = stricmp(p1, p2);
1036 if (p1e)
1037 *p1e = e1;
1038 if (p2e)
1039 *p2e = e2;
1040 return ret;
1041}
1042
1043int comparesizesq(const void *v1, const void *v2)
1044{
1045 DUPES *d1 = *(DUPES **) v1;
1046 DUPES *d2 = *(DUPES **) v2;
1047
1048 return (d1->size > d2->size) ? 1 : (d1->size == d2->size) ? 0 : -1;
1049}
1050
1051int comparenamesb(const void *v1, const void *v2)
1052{
1053 DUPES *d1 = (DUPES *) v1;
1054 DUPES *d2 = *(DUPES **) v2;
1055 CHAR *p1, *p2;
1056
1057 p1 = strrchr(d1->name, '\\');
1058 if (p1)
1059 p1++;
1060 else
1061 p1 = d1->name;
1062 p2 = strrchr(d2->name, '\\');
1063 if (p2)
1064 p2++;
1065 else
1066 p2 = d2->name;
1067 return stricmp(p1, p2);
1068}
1069
1070int comparenamesbe(const void *v1, const void *v2)
1071{
1072 DUPES *d1 = (DUPES *) v1;
1073 DUPES *d2 = *(DUPES **) v2;
1074 CHAR *p1, *p2, *p1e, *p2e, e1, e2;
1075 int ret;
1076
1077 p1 = strrchr(d1->name, '\\');
1078 if (p1)
1079 p1++;
1080 else
1081 p1 = d1->name;
1082 p1e = strrchr(p1, '.');
1083 if (p1e) {
1084 e1 = *p1e;
1085 *p1e = 0;
1086 }
1087 p2 = strrchr(d2->name, '\\');
1088 if (p2)
1089 p2++;
1090 else
1091 p2 = d2->name;
1092 p2e = strrchr(p2, '.');
1093 if (p2e) {
1094 e2 = *p2e;
1095 *p2e = 0;
1096 }
1097 ret = stricmp(p1, p2);
1098 if (p1e)
1099 *p1e = e1;
1100 if (p2e)
1101 *p2e = e2;
1102 return ret;
1103}
1104
1105int comparesizesb(const void *v1, const void *v2)
1106{
1107 DUPES *d1 = (DUPES *) v1;
1108 DUPES *d2 = *(DUPES **) v2;
1109
1110 return (d1->size > d2->size) ? 1 : (d1->size == d2->size) ? 0 : -1;
1111}
1112
1113static VOID FillDupes(GREP * grep)
1114{
1115 DUPES *c, *i, **r;
1116 register CHAR *pc, *pi;
1117 CHAR **list = NULL;
1118 INT numfiles = 0, numalloced = 0, error;
1119 register ULONG x = 0, y = 0;
1120 ULONG cntr = 1000;
1121
1122 if (grep->CRCdupes)
1123 cntr = 100;
1124 i = grep->dupehead;
1125 while (i) {
1126 x++;
1127 i = i->next;
1128 }
1129 if (x) {
1130 WinSetWindowText(grep->hwndCurFile, GetPString(IDS_GREPDUPESORTINGTEXT));
1131 DosSleep(0); //26 Aug 07 GKY 1
1132 grep->dupenames = xmalloc(sizeof(DUPES *) * (x + 1), pszSrcFile, __LINE__);
1133 if (!grep->nosizedupes)
1134 grep->dupesizes = xmalloc(sizeof(DUPES *) * (x + 1), pszSrcFile, __LINE__);
1135 if (grep->dupenames && (grep->nosizedupes || grep->dupesizes)) {
1136 i = grep->dupehead;
1137 while (i) {
1138 grep->dupenames[y] = i;
1139 if (!grep->nosizedupes)
1140 grep->dupesizes[y] = i;
1141 i = i->next;
1142 y++;
1143 }
1144 grep->dupenames[y] = NULL;
1145 if (!grep->nosizedupes)
1146 grep->dupesizes[y] = NULL;
1147 DosSleep(0); //26 Aug 07 GKY 1
1148 qsort(grep->dupenames,
1149 x,
1150 sizeof(DUPES *),
1151 ((grep->ignoreextdupes) ? comparenamesqe : comparenamesq));
1152 DosSleep(0); //26 Aug 07 GKY 1
1153 if (!grep->nosizedupes) {
1154 qsort(grep->dupesizes, x, sizeof(DUPES *), comparesizesq);
1155 DosSleep(0); //26 Aug 07 GKY 1
1156 }
1157 WinSetWindowText(grep->hwndCurFile, GetPString(IDS_GREPDUPECOMPARINGTEXT));
1158
1159 i = grep->dupehead;
1160 y = 0;
1161 while (i) {
1162 if (*grep->stopflag)
1163 break;
1164 if (!(i->flags & GF_SKIPME)) {
1165 r = (DUPES **) bsearch(i, grep->dupenames, x, sizeof(DUPES *),
1166 ((grep->ignoreextdupes) ? comparenamesbe :
1167 comparenamesb));
1168 if (r) {
1169 while (r > grep->dupenames && ((grep->ignoreextdupes) ?
1170 !comparenamesqe((r - 1), &i) :
1171 !comparenamesq((r - 1), &i)))
1172 r--;
1173 while (*r && ((grep->ignoreextdupes) ?
1174 !comparenamesqe(r, &i) : !comparenamesq(r, &i))) {
1175 if (*r == i || ((*r)->flags & (GF_INSERTED | GF_SKIPME))) {
1176 r++;
1177 continue;
1178 }
1179 if (grep->CRCdupes) {
1180 if ((*r)->CRC == -1L) {
1181 (*r)->CRC = CRCFile((*r)->name, &error);
1182 if (error)
1183 (*r)->CRC = -1L;
1184 else if ((*r)->CRC == -1L)
1185 (*r)->CRC = 0L;
1186 }
1187 if (i->CRC == -1L) {
1188 i->CRC = CRCFile(i->name, &error);
1189 if (error)
1190 i->CRC = -1L;
1191 else if (i->CRC == -1L)
1192 i->CRC = 0L;
1193 }
1194 if (((*r)->size != i->size) || ((*r)->CRC != -1L &&
1195 i->CRC != -1L
1196 && (*r)->CRC != i->CRC)) {
1197 r++;
1198 continue;
1199 }
1200 }
1201 if (!AddToList((*r)->name, &list, &numfiles, &numalloced)) {
1202 (*r)->flags |= GF_INSERTED;
1203 if (grep->sayfiles)
1204 WinSetWindowText(grep->hwndFiles, (*r)->name);
1205 if ((*r)->size == i->size &&
1206 (i->date.year == (*r)->date.year &&
1207 i->date.month == (*r)->date.month &&
1208 i->date.day == (*r)->date.day &&
1209 i->time.hours == (*r)->time.hours &&
1210 i->time.minutes == (*r)->time.minutes &&
1211 i->time.twosecs == (*r)->time.twosecs))
1212 (*r)->flags |= GF_SKIPME;
1213 }
1214 if (!(i->flags & (GF_INSERTED | GF_SKIPME))) {
1215 if (!AddToList(i->name, &list, &numfiles, &numalloced)) {
1216 i->flags |= GF_INSERTED;
1217 if ((*r)->flags & GF_SKIPME)
1218 i->flags |= GF_SKIPME;
1219 }
1220 }
1221 r++;
1222 }
1223 }
1224 if (!grep->nosizedupes) {
1225 r = (DUPES **) bsearch(i,
1226 grep->dupesizes,
1227 x, sizeof(DUPES *), comparesizesb);
1228 if (r) {
1229 while (r > grep->dupesizes && !comparesizesq((r - 1), &i))
1230 r--;
1231 while (*r && !comparesizesq(r, &i)) {
1232 if (*r == i || ((*r)->flags & (GF_INSERTED | GF_SKIPME)) ||
1233 (i->date.year != (*r)->date.year ||
1234 i->date.month != (*r)->date.month ||
1235 i->date.day != (*r)->date.day ||
1236 i->time.hours != (*r)->time.hours ||
1237 i->time.minutes != (*r)->time.minutes ||
1238 i->time.twosecs != (*r)->time.twosecs)) {
1239 r++;
1240 continue;
1241 }
1242 if (grep->CRCdupes) {
1243 if ((*r)->CRC == -1L) {
1244 (*r)->CRC = CRCFile((*r)->name, &error);
1245 if (error)
1246 (*r)->CRC = -1L;
1247 else if ((*r)->CRC == -1L)
1248 (*r)->CRC = 0L;
1249 }
1250 if (i->CRC == -1L) {
1251 i->CRC = CRCFile(i->name, &error);
1252 if (error)
1253 i->CRC = -1L;
1254 else if (i->CRC == -1L)
1255 i->CRC = 0L;
1256 }
1257 if ((*r)->CRC != -1L && i->CRC != -1L &&
1258 (*r)->CRC != i->CRC) {
1259 *r += 1;
1260 continue;
1261 }
1262 }
1263 if (!AddToList((*r)->name, &list, &numfiles, &numalloced)) {
1264 if (grep->sayfiles)
1265 WinSetWindowText(grep->hwndCurFile, (*r)->name);
1266 (*r)->flags |= GF_INSERTED;
1267 if (((grep->ignoreextdupes) ?
1268 comparenamesqe(r, &i) : comparenamesq(r, &i)))
1269 (*r)->flags |= GF_SKIPME;
1270 }
1271 if (!(i->flags & (GF_INSERTED | GF_SKIPME))) {
1272 if (!AddToList(i->name, &list, &numfiles, &numalloced)) {
1273 i->flags |= GF_INSERTED;
1274 if ((*r)->flags & GF_SKIPME)
1275 i->flags |= GF_SKIPME;
1276 }
1277 }
1278 r++;
1279 }
1280 }
1281 }
1282 }
1283 i = i->next;
1284 y++;
1285 if (!(y % cntr)) {
1286
1287 CHAR s[44];
1288
1289 sprintf(s, GetPString(IDS_GREPDUPECHECKPROGTEXT), y, grep->numfiles);
1290 WinSetWindowText(grep->hwndCurFile, s);
1291 DosSleep(1); //05 Aug 07 GKY 128
1292 }
1293 DosSleep(0); //26 Aug 07 GKY 1
1294 }
1295 }
1296 else {
1297 // Insufficient memory - fall back
1298 DosBeep(50, 100);
1299 WinSetWindowText(grep->hwndCurFile, GetPString(IDS_GREPDUPECOMPARINGTEXT));
1300 x = y = 0;
1301 if (grep->dupenames) {
1302 free(grep->dupenames);
1303 grep->dupenames = NULL;
1304 }
1305 if (grep->dupesizes) {
1306 free(grep->dupesizes);
1307 grep->dupesizes = NULL;
1308 }
1309 i = grep->dupehead;
1310 while (i) {
1311 if (*grep->stopflag)
1312 break;
1313 if (!(i->flags & GF_SKIPME)) {
1314 if (!(y % cntr)) {
1315
1316 CHAR s[44];
1317
1318 sprintf(s, GetPString(IDS_GREPDUPECHECKPROGTEXT), y, grep->numfiles);
1319 WinSetWindowText(grep->hwndCurFile, s);
1320 DosSleep(0); //26 Aug 07 GKY 1
1321 }
1322 y++;
1323 pi = strrchr(i->name, '\\');
1324 if (pi)
1325 pi++;
1326 else
1327 pi = i->name;
1328 c = grep->dupehead;
1329 while (c) {
1330 if (*grep->stopflag)
1331 break;
1332 if (c != i && !(c->flags & (GF_INSERTED | GF_SKIPME))) {
1333 x++;
1334 pc = strrchr(c->name, '\\');
1335 if (pc)
1336 pc++;
1337 else
1338 pc = c->name;
1339 if ((!grep->nosizedupes && i->size == c->size && i->date.year == c->date.year && i->date.month == c->date.month && i->date.day == c->date.day && i->time.hours == c->time.hours && i->time.minutes == c->time.minutes && i->time.twosecs == c->time.twosecs) || !stricmp(pc, pi)) { // potential dupe
1340 if (grep->CRCdupes) {
1341 if (grep->CRCdupes) {
1342 if (c->CRC == -1L) {
1343 c->CRC = CRCFile(c->name, &error);
1344 if (error)
1345 c->CRC = -1L;
1346 else if (c->CRC == -1L)
1347 c->CRC = 0L;
1348 }
1349 if (i->CRC == -1L) {
1350 i->CRC = CRCFile(i->name, &error);
1351 if (error)
1352 i->CRC = -1L;
1353 else if (i->CRC == -1L)
1354 i->CRC = 0L;
1355 }
1356 if ((c->size != i->size) || (c->CRC != -1L &&
1357 i->CRC != -1L
1358 && c->CRC != i->CRC)) {
1359 c = c->next;
1360 continue;
1361 }
1362 }
1363 }
1364 if (AddToList(c->name, &list, &numfiles, &numalloced))
1365 goto BreakOut; // Failed
1366 if (!(i->flags & GF_INSERTED)) {
1367 if (AddToList(i->name, &list, &numfiles, &numalloced))
1368 goto BreakOut; // Failed
1369 }
1370 if (grep->sayfiles)
1371 WinSetWindowText(grep->hwndCurFile, pc);
1372 c->flags |= GF_INSERTED;
1373 i->flags |= GF_INSERTED;
1374 if (!stricmp(pc, pi)) {
1375 c->flags |= GF_SKIPME;
1376 i->flags |= GF_SKIPME;
1377 }
1378 }
1379 else if (!(x % 100))
1380 DosSleep(0); //26 Aug 07 GKY 1
1381 }
1382 c = c->next;
1383 }
1384 }
1385 i = i->next;
1386 }
1387 }
1388 }
1389BreakOut:
1390 FreeDupes(grep);
1391 if (numfiles && list) {
1392 if (!PostMsg(grep->hwndFiles,
1393 WM_COMMAND, MPFROM2SHORT(IDM_COLLECTOR, 0), MPFROMP(list)))
1394 FreeList(list);
1395 }
1396 else
1397 DosPostEventSem(CompactSem);
1398}
1399
1400static BOOL InsertDupe(GREP *grep, CHAR *dir, FILEFINDBUF4 *pffb)
1401{
1402 DUPES *info;
1403
1404 if (*dir) {
1405 info = xmallocz(sizeof(DUPES), pszSrcFile, __LINE__);
1406 if (!info)
1407 return FALSE;
1408
1409 info->name = xstrdup(dir, pszSrcFile, __LINE__);
1410 if (!info->name) {
1411 free(info);
1412 return FALSE;
1413 }
1414
1415 info->size = pffb->cbFile;
1416 info->date = pffb->fdateLastWrite;
1417 info->time = pffb->ftimeLastWrite;
1418 info->CRC = -1L;
1419 grep->numfiles++;
1420 if (!grep->dupehead)
1421 grep->dupehead = info;
1422 if (grep->dupelast)
1423 grep->dupelast->next = info;
1424 grep->dupelast = info;
1425 info->next = NULL;
1426 }
1427 return TRUE;
1428}
1429
1430#pragma alloc_text(GREP,insert_grepfile,doonefile,doinsertion,freegreplist)
1431#pragma alloc_text(GREP,SecsSince1980,match,mmatch,GrepThread)
1432#pragma alloc_text(GREP,doallsubdirs,domatchingfiles,InsertDupes,FreeDupes)
1433
1434#pragma alloc_text(DUPES,InsertDupe,FillDupes,FreeDupes,CRCFile,CRCBlock)
1435#pragma alloc_text(DUPES,comparenamesq,comparenamesqe,comparenamesb)
1436#pragma alloc_text(DUPES,comparenamesbe,comparesizesq,comparesizesb)
Note: See TracBrowser for help on using the repository browser.