source: trunk/dll/makelist.c@ 1077

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

Enhance Fortify infrastructure
Add Fortify_SetOwner Fortify_ChangeOwner Fortify_ChangeScope
Add FORTIFY_VERBOSE_SCOPE_ENTER_EXIT support
Add more fm/2 Fortify tooling and rework existing tooling for correct nesting
Still lots to do for cross-thread allocations
Add misc.h
Add walkem.h

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.5 KB
Line 
1
2/***********************************************************************
3
4 $Id: makelist.c 1077 2008-07-18 18:11:54Z stevenhl $
5
6 Make file lists
7
8 Copyright (c) 1993-98 M. Kimes
9 Copyright (c) 2003, 2008 Steven H.Levine
10
11 12 Feb 03 SHL AddToFileList: standardize EA math
12 22 Jul 06 SHL Use Runtime_Error
13 22 Jul 06 SHL AddToList optimize
14 06 Apr 07 GKY Work around PM DragInfo and DrgFreeDISH limits
15 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
16 29 Feb 08 GKY Use xfree where appropriate
17 17 Jul 08 SHL Add SetListOwner for Fortify support
18
19***********************************************************************/
20
21#include <stdlib.h>
22#include <string.h>
23
24#define INCL_DOS
25#define INCL_WIN
26#define INCL_LONGLONG
27
28#include "fm3str.h"
29#include "makelist.h"
30#include "errutil.h" // Dos_Error...
31#include "strutil.h" // GetPString
32#include "dircnrs.h"
33#include "fm3dll.h" // 05 Jan 08 SHL fixme to be gone
34
35#include "fortify.h" // 06 May 08 SHL
36
37static PSZ pszSrcFile = __FILE__;
38
39VOID SortList(LISTINFO *li)
40{
41 /* bubble-sort entries by size, descending */
42
43 UINT x;
44 CHAR *s;
45 ULONG l;
46 BOOL swapped;
47
48 if (li && li->list && li->list[0] && li->cbFile) {
49 do {
50 swapped = FALSE;
51 for (x = 0; li->list[x] && li->list[x + 1]; x++) {
52 if (li->cbFile[x] < li->cbFile[x + 1]) {
53 s = li->list[x];
54 li->list[x] = li->list[x + 1];
55 li->list[x + 1] = s;
56 l = li->cbFile[x];
57 li->cbFile[x] = li->cbFile[x + 1];
58 li->cbFile[x + 1] = l;
59 if (li->ulitemID) {
60 l = li->ulitemID[x];
61 li->ulitemID[x] = li->ulitemID[x + 1];
62 li->ulitemID[x + 1] = l;
63 }
64 swapped = TRUE;
65 }
66 }
67 } while (swapped);
68 }
69}
70
71VOID FreeListInfo(LISTINFO *li)
72{
73 if (li) {
74 xfree(li->ulitemID, pszSrcFile, __LINE__);
75 xfree(li->cbFile, pszSrcFile, __LINE__);
76 if (li->list)
77 FreeList(li->list);
78 free(li);
79 }
80}
81
82VOID FreeList(CHAR **list)
83{
84 UINT x;
85
86 if (list) {
87 for (x = 0; list[x]; x++) {
88#ifdef __DEBUG_ALLOC__
89 _heap_check();
90#endif
91 free(list[x]);
92 }
93#ifdef __DEBUG_ALLOC__
94 _heap_check();
95#endif
96 free(list);
97 }
98 DosPostEventSem(CompactSem);
99}
100
101INT AddToFileList(CHAR *string, FILEFINDBUF4L *ffb4, FILELIST ***list,
102 UINT *pnumfiles, UINT *pnumalloced)
103{
104 FILELIST *pfl;
105
106 if (string && ffb4) {
107 // Ensure room for NULL entry
108 if (((*pnumfiles) + 3) > *pnumalloced) {
109 FILELIST **pflArray;
110
111 // Use plain realloc for speed
112 // 06 Aug 07 SHL fixme to know why + 6
113# ifdef FORTIFY
114 Fortify_EnterScope();
115# endif
116 pflArray = realloc(*list, (*pnumalloced + 6) * sizeof(FILELIST *));
117 if (!pflArray) {
118 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
119 return 1;
120 }
121 (*pnumalloced) += 6;
122 *list = pflArray;
123 }
124 // Use plain malloc for speed
125 pfl = malloc(sizeof(FILELIST) + strlen(string));
126 if (!pfl) {
127 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
128 return 2;
129 }
130 pfl->attrFile = ffb4->attrFile;
131 pfl->date = ffb4->fdateLastWrite;
132 pfl->time = ffb4->ftimeLastWrite;
133 pfl->ladate = ffb4->fdateLastAccess;
134 pfl->latime = ffb4->ftimeLastAccess;
135 pfl->crdate = ffb4->fdateCreation;
136 pfl->crtime = ffb4->ftimeCreation;
137 pfl->cbFile = ffb4->cbFile;
138 pfl->easize = CBLIST_TO_EASIZE(ffb4->cbList);
139 strcpy(pfl->fname, string);
140 (*list)[*pnumfiles] = pfl;
141 (*pnumfiles)++;
142 // Ensure list always ends with two NULL entries
143 // 06 Aug 07 SHL fixme to know why
144 (*list)[*pnumfiles] = NULL;
145 (*list)[(*pnumfiles) + 1] = NULL;
146#ifdef __DEBUG_ALLOC__
147 _heap_check();
148#endif
149 }
150 return 0;
151}
152
153/**
154 * Add string to string list
155 * Enlarges as needed
156 * Ensures 2 NULL end markers exist
157 */
158
159INT AddToList(CHAR *string, CHAR ***list, UINT *pnumfiles, UINT *pnumalloced)
160{
161 CHAR **ppsz;
162 PSZ psz;
163
164 if (string) {
165 if (((*pnumfiles) + 3) > *pnumalloced) {
166 // Use plain realloc for speed
167# ifdef FORTIFY
168 Fortify_EnterScope();
169# endif
170 ppsz = realloc(*list, (*pnumalloced + 6) * sizeof(CHAR *));
171 if (!ppsz) {
172 Runtime_Error(pszSrcFile, __LINE__, "realloc");
173 return 1;
174 }
175 (*pnumalloced) += 6;
176 *list = ppsz;
177 }
178 // Use plain malloc for speed
179 psz = malloc(strlen(string) + 1);
180 if (!psz) {
181 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
182 return 2;
183 }
184 (*list)[*pnumfiles] = psz;
185 strcpy((*list)[*pnumfiles], string); // Add entry
186 (*pnumfiles)++;
187 (*list)[*pnumfiles] = NULL; // Add end marker
188 (*list)[(*pnumfiles) + 1] = NULL; // Add 2nd end marker - fixme to know why?
189#ifdef __DEBUG_ALLOC__
190 _heap_check();
191#endif
192 }
193 return 0;
194}
195
196CHAR **BuildList(HWND hwndCnr)
197{
198 PCNRITEM pci;
199 CHAR **list = NULL, **test;
200 UINT numfiles = 0, numalloc = 0;
201 INT error = 0, attribute = CRA_CURSORED;
202
203 pci = (PCNRITEM) CurrentRecord(hwndCnr);
204 if (pci && (INT) pci != -1 && !(pci->flags & RECFLAGS_ENV)) {
205 if (pci->rc.flRecordAttr & CRA_SELECTED) {
206 attribute = CRA_SELECTED;
207 pci = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMLONG(CMA_FIRST),
208 MPFROMSHORT(attribute));
209 }
210 }
211 while (pci && (INT) pci != -1 && !error) {
212 if (!(pci->rc.flRecordAttr & CRA_FILTERED))
213 error = AddToList(pci->pszFileName, &list, &numfiles, &numalloc);
214 pci = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMP(pci),
215 MPFROMSHORT(attribute));
216 }
217 if (numalloc > numfiles + 1) {
218 // Use plain realloc for speed
219
220# ifdef FORTIFY
221 Fortify_EnterScope();
222# endif
223 test = realloc(list, sizeof(CHAR *) * (numfiles + 1));
224 if (!test)
225 Runtime_Error(pszSrcFile, __LINE__, GetPString(IDS_OUTOFMEMORY));
226 else
227 list = test;
228 } // while
229 return list;
230}
231
232CHAR **BuildArcList(HWND hwndCnr)
233{
234 PARCITEM pai;
235 CHAR **list = NULL;
236 UINT numfiles = 0, numalloc = 0;
237 INT error = 0, attribute = CRA_CURSORED;
238
239 pai = (PARCITEM) CurrentRecord(hwndCnr);
240 if (pai && (INT) pai != -1) {
241 if (pai->rc.flRecordAttr & CRA_SELECTED) {
242 attribute = CRA_SELECTED;
243 pai = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMLONG(CMA_FIRST),
244 MPFROMSHORT(attribute));
245 }
246 }
247 while (pai && (INT) pai != -1 && !error) {
248 if (!(pai->rc.flRecordAttr & CRA_FILTERED))
249 error = AddToList(pai->pszFileName, &list, &numfiles, &numalloc);
250 pai = WinSendMsg(hwndCnr, CM_QUERYRECORDEMPHASIS, MPFROMP(pai),
251 MPFROMSHORT(attribute));
252 }
253 return list;
254}
255
256CHAR **RemoveFromList(CHAR **list, CHAR *item)
257{
258 UINT x, y;
259
260 if (list && list[0] && item) {
261 for (x = 0; list[x]; x++) {
262 if (item == list[x]) {
263 free(list[x]);
264 list[x] = NULL;
265 for (y = x;; y++) {
266 if (y != x && !list[y])
267 break;
268 list[y] = list[y + 1];
269 }
270 if (!list[0]) {
271 FreeList(list);
272 list = NULL;
273 }
274# ifdef FORTIFY
275 Fortify_LeaveScope();
276# endif
277 break;
278 }
279 }
280 }
281 return list;
282}
283
284CHAR **CombineLists(CHAR **prime, CHAR **add)
285{
286 UINT x;
287 UINT numalloc, numfiles = 0;
288
289 if (add && add[0]) {
290 if (prime) {
291 for (x = 0; prime[x]; x++)
292 numfiles++;
293 }
294 numalloc = numfiles;
295 for (x = 0; add[x]; x++) {
296 if (*add[x])
297 AddToList(add[x], &prime, &numfiles, &numalloc);
298 }
299 FreeList(add);
300 }
301 return prime;
302}
303
304#ifdef FORTIFY
305
306VOID SetListOwner(LISTINFO *li)
307{
308 if (li) {
309 CHAR **list = li->list;
310 if (list) {
311 UINT x;
312 for (x = 0; list[x]; x++)
313 Fortify_ChangeOwner(list[x]);
314 }
315 Fortify_ChangeOwner(li);
316 }
317}
318
319#endif // FORTIFY
320
321#pragma alloc_text(MAKELIST,AddToList,AddToFileList,BuildList,FreeListInfo,FreeList)
322#pragma alloc_text(MAKELIST,SortList,BuildArcList,RemoveFromList,CombineLists)
323#ifdef FORTIFY
324#pragma alloc_text(MAKELIST,SetListOwner)
325#endif // FORTIFY
Note: See TracBrowser for help on using the repository browser.