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

Function UTIL_createExpandedFNT

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

Source from the content-addressed store, hash-verified

954}
955
956FileNamesTable*
957UTIL_createExpandedFNT(const char** inputNames, size_t nbIfns, int followLinks)
958{
959 unsigned nbFiles;
960 char* buf = (char*)malloc(LIST_SIZE_INCREASE);
961 char* bufend = buf + LIST_SIZE_INCREASE;
962
963 if (!buf) return NULL;
964
965 { size_t ifnNb, pos;
966 for (ifnNb=0, pos=0, nbFiles=0; ifnNb<nbIfns; ifnNb++) {
967 if (!UTIL_isDirectory(inputNames[ifnNb])) {
968 size_t const len = strlen(inputNames[ifnNb]);
969 if (buf + pos + len >= bufend) {
970 ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
971 assert(newListSize >= 0);
972 buf = (char*)UTIL_realloc(buf, (size_t)newListSize);
973 if (!buf) return NULL;
974 bufend = buf + newListSize;
975 }
976 if (buf + pos + len < bufend) {
977 memcpy(buf+pos, inputNames[ifnNb], len+1); /* including final \0 */
978 pos += len + 1;
979 nbFiles++;
980 }
981 } else {
982 nbFiles += (unsigned)UTIL_prepareFileList(inputNames[ifnNb], &buf, &pos, &bufend, followLinks);
983 if (buf == NULL) return NULL;
984 } } }
985
986 /* note : even if nbFiles==0, function returns a valid, though empty, FileNamesTable* object */
987
988 { size_t ifnNb, pos;
989 size_t const fntCapacity = nbFiles + 1; /* minimum 1, allows adding one reference, typically stdin */
990 const char** const fileNamesTable = (const char**)malloc(fntCapacity * sizeof(*fileNamesTable));
991 if (!fileNamesTable) { free(buf); return NULL; }
992
993 for (ifnNb = 0, pos = 0; ifnNb < nbFiles; ifnNb++) {
994 fileNamesTable[ifnNb] = buf + pos;
995 if (buf + pos > bufend) { free(buf); free((void*)fileNamesTable); return NULL; }
996 pos += strlen(fileNamesTable[ifnNb]) + 1;
997 }
998 return UTIL_assembleFileNamesTable2(fileNamesTable, nbFiles, fntCapacity, buf);
999 }
1000}
1001
1002
1003void UTIL_expandFNT(FileNamesTable** fnt, int followLinks)

Callers 1

UTIL_expandFNTFunction · 0.85

Calls 7

mallocFunction · 0.85
UTIL_isDirectoryFunction · 0.85
UTIL_reallocFunction · 0.85
UTIL_prepareFileListFunction · 0.85
memcpyFunction · 0.50
freeFunction · 0.50

Tested by

no test coverage detected