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