Skip to content

Commit 6e71596

Browse files
committed
Update stringList_t code
1 parent 1988d88 commit 6e71596

5 files changed

Lines changed: 42 additions & 35 deletions

File tree

src/libnffile/flist.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ static int GetFileList(stringlist_t *source_dirs, flist_t *flist) {
476476
s[MAXPATHLEN - 1] = '\0';
477477
last_file_ptr = strdup(s);
478478
levels_last_file = dirlevels(last_file_ptr);
479+
free(r);
479480
}
480481
}
481482
}
@@ -968,7 +969,6 @@ static void *FileLister_thr(void *arg) {
968969

969970
// stringlist of all directories
970971
stringlist_t source_dirs = {0};
971-
InitStringlist(&source_dirs, 16);
972972

973973
if (flist->multiple_dirs) {
974974
char *expanded = ExpandWildcard(flist->multiple_dirs);

src/libnffile/nffile.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -730,23 +730,21 @@ static nffile_t *NewFile(uint32_t num_workers) {
730730
} // End of NewFile
731731

732732
static nffile_t *OpenFileStatic(const char *filename, unsigned workerSlots) {
733-
struct stat stat_buf;
734-
int fd = 0;
735-
736733
if (filename == NULL) return NULL;
737734

738-
// regular file
735+
// check regular file
739736
if (access(filename, F_OK) < 0) {
740737
LogError("access() '%s': %s", filename, strerror(errno));
741738
return NULL;
742739
}
743740

744-
fd = open(filename, O_RDONLY);
741+
int fd = open(filename, O_RDONLY);
745742
if (fd < 0) {
746743
LogError("Error open file: %s", strerror(errno));
747744
return NULL;
748745
}
749746

747+
struct stat stat_buf;
750748
if (fstat(fd, &stat_buf) < 0) {
751749
LogError("fstat() '%s': %s", filename, strerror(errno));
752750
return NULL;

src/libnffile/util.c

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -647,37 +647,43 @@ char *DurationString(uint64_t duration) {
647647
return s;
648648
} // End of DurationString
649649

650-
void InitStringlist(stringlist_t *list, uint32_t capacity) {
651-
list->list = NULL;
652-
list->num_strings = 0;
653-
list->capacity = capacity;
654-
655-
} // End of InitStringlist
656-
657-
void InsertString(stringlist_t *list, char *string) {
658-
if (!list->list) {
659-
// default if not yet initialised
660-
if (list->capacity == 0) list->capacity = 8;
661-
list->num_strings = 0;
662-
list->list = (char **)malloc(list->capacity * sizeof(char *));
663-
if (!list->list) {
664-
LogError("malloc() error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
665-
exit(250);
666-
}
650+
stringlist_t *NewStringlist(stringlist_t *list, uint32_t capacity) {
651+
stringlist_t *sl = calloc(1, sizeof(stringlist_t));
652+
if (!sl) {
653+
LogError("calloc() error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
654+
return NULL;
667655
}
668-
list->list[list->num_strings++] = string ? strdup(string) : NULL;
656+
return sl;
657+
} // End of NewStringlist
669658

670-
// if all slots used, double capacity
671-
if (list->num_strings == list->capacity) {
672-
list->capacity += list->capacity;
673-
list->list = (char **)realloc(list->list, list->capacity * sizeof(char *));
674-
if (!list->list) {
659+
void InsertString(stringlist_t *sl, const char *s) {
660+
if (sl->num_strings == sl->capacity) {
661+
sl->capacity = sl->capacity ? sl->capacity * 2 : 16;
662+
sl->list = (char **)realloc(sl->list, sl->capacity * sizeof(char *));
663+
if (!sl->list) {
675664
LogError("realloc() error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
676-
exit(250);
665+
exit(EXIT_FAILURE);
677666
}
678667
}
679668

680-
} // End of InsertString
669+
if (s) {
670+
sl->list[sl->num_strings++] = strdup(s);
671+
} else {
672+
/* allow explicit NULL sentinel */
673+
sl->list[sl->num_strings++] = NULL;
674+
}
675+
} // // End of InsertString
676+
677+
void ClearStringList(stringlist_t *sl) {
678+
if (sl->list) free(sl->list);
679+
memset(sl, 0, sizeof(stringlist_t));
680+
} // End of ClearStringList
681+
682+
void FreeStringList(stringlist_t *sl) {
683+
if (sl == NULL) return;
684+
ClearStringList(sl);
685+
free(sl);
686+
} // End of ClearStringList
681687

682688
void format_number(uint64_t num, numStr s, int plain, int fixed_width) {
683689
double f = num;

src/libnffile/util.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,13 @@ void LogInfo(char *format, ...);
125125

126126
void LogVerbose(char *format, ...);
127127

128-
void InitStringlist(stringlist_t *list, uint32_t capacity);
128+
stringlist_t *NewStringlist(stringlist_t *list, uint32_t capacity);
129129

130-
void InsertString(stringlist_t *list, char *string);
130+
void ClearStringList(stringlist_t *sl);
131+
132+
void FreeStringList(stringlist_t *sl);
133+
134+
void InsertString(stringlist_t *sl, const char *s);
131135

132136
timeWindow_t *ScanTimeFrame(char *tstring);
133137

src/nfexpire/nfexpire.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,11 @@ void CheckDataDir(char *datadir) {
9797

9898
channel_t *GetChannelList(char *datadir, int is_profile, int do_rescan) {
9999
channel_t **c, *channel;
100-
stringlist_t dirlist;
100+
stringlist_t dirlist = {0};
101101
struct stat stat_buf;
102102
int i;
103103

104104
// Generate list of directories
105-
InitStringlist(&dirlist, 32);
106105
if (is_profile) {
107106
DIR *PDIR = opendir(datadir);
108107
struct dirent *entry;

0 commit comments

Comments
 (0)