source: trunk/dll/makelist.c@ 1032

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

Fortify updates including a leave scope wrapper which frees and in some cases reloads commands, archivers, association, etc.

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