MCPcopy Create free account
hub / github.com/F-Stack/f-stack / UTIL_prepareFileList

Function UTIL_prepareFileList

freebsd/contrib/zstd/programs/util.c:506–567  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

504
505#ifdef _WIN32
506static int UTIL_prepareFileList(const char* dirName,
507 char** bufStart, size_t* pos,
508 char** bufEnd, int followLinks)
509{
510 char* path;
511 size_t dirLength, pathLength;
512 int nbFiles = 0;
513 WIN32_FIND_DATAA cFile;
514 HANDLE hFile;
515
516 dirLength = strlen(dirName);
517 path = (char*) malloc(dirLength + 3);
518 if (!path) return 0;
519
520 memcpy(path, dirName, dirLength);
521 path[dirLength] = '\\';
522 path[dirLength+1] = '*';
523 path[dirLength+2] = 0;
524
525 hFile=FindFirstFileA(path, &cFile);
526 if (hFile == INVALID_HANDLE_VALUE) {
527 UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s'\n", dirName);
528 return 0;
529 }
530 free(path);
531
532 do {
533 size_t const fnameLength = strlen(cFile.cFileName);
534 path = (char*) malloc(dirLength + fnameLength + 2);
535 if (!path) { FindClose(hFile); return 0; }
536 memcpy(path, dirName, dirLength);
537 path[dirLength] = '\\';
538 memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
539 pathLength = dirLength+1+fnameLength;
540 path[pathLength] = 0;
541 if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
542 if ( strcmp (cFile.cFileName, "..") == 0
543 || strcmp (cFile.cFileName, ".") == 0 )
544 continue;
545 /* Recursively call "UTIL_prepareFileList" with the new path. */
546 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks);
547 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
548 } else if ( (cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
549 || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)
550 || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) ) {
551 if (*bufStart + *pos + pathLength >= *bufEnd) {
552 ptrdiff_t const newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
553 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
554 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
555 *bufEnd = *bufStart + newListSize;
556 }
557 if (*bufStart + *pos + pathLength < *bufEnd) {
558 memcpy(*bufStart + *pos, path, pathLength+1 /* include final \0 */);
559 *pos += pathLength + 1;
560 nbFiles++;
561 } }
562 free(path);
563 } while (FindNextFileA(hFile, &cFile));

Callers 1

UTIL_createExpandedFNTFunction · 0.85

Calls 10

mallocFunction · 0.85
strcmpFunction · 0.85
UTIL_reallocFunction · 0.85
opendirFunction · 0.85
readdirFunction · 0.85
closedirFunction · 0.85
UTIL_isLinkFunction · 0.85
UTIL_isDirectoryFunction · 0.85
memcpyFunction · 0.50
freeFunction · 0.50

Tested by

no test coverage detected