79 if (!dir || !name)
return NULL;
81 size_t dl = strlen(dir);
82 size_t nl = strlen(name);
88 char last = dir[dl - 1];
89 if (last ==
'/' || last ==
'\\') need_sep = 0;
93 size_t out_len = dl + (need_sep ? 1 : 0) + nl + 1;
94 char* out = (
char*)malloc(out_len);
95 if (!out)
return NULL;
98 snprintf(out, out_len,
"%s/%s", dir, name);
100 snprintf(out, out_len,
"%s%s", dir, name);
147 if (!file || !st)
return;
149#if defined(__linux__) || defined(__sun)
150 file->
filetime = (int64_t)st->st_mtim.tv_sec;
153 file->
filetime = (int64_t)st->st_mtime;
182 const struct dirent* entry = NULL;
191 n_log(
LOG_ERR,
"cannot open directory: %s, %s", dir, strerror(error));
196 while ((entry = readdir(dp)) != NULL) {
197 const char* name = entry->d_name;
199 if (strcmp(name,
".") == 0 || strcmp(name,
"..") == 0) {
206 n_log(
LOG_ERR,
"malloc failed building path for %s/%s", dir, name);
212 if (stat(fullpath, &st) == -1) {
214 n_log(
LOG_ERR,
"unable to stat %s, %s", fullpath, strerror(error));
220 if (S_ISDIR(st.st_mode)) {
221 if (recurse != FALSE) {
222 if (
n_scan_dir(fullpath, result, recurse) == FALSE) {
223 n_log(
LOG_ERR,
"error while recursively scanning %s", fullpath);
226 }
else if (S_ISREG(st.st_mode)) {
232 file->
name = strdup(fullpath);
247 n_log(
LOG_ERR,
"readdir failed for %s: %s", dir, strerror(errno));
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
#define __n_assert(__ptr, __ret)
macro to assert things
int list_push_sorted(LIST *list, void *ptr, int(*comparator)(const void *a, const void *b), void(*destructor)(void *ptr))
Add a pointer sorted in the list , starting by the end of the list.
Structure of a generic LIST container.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
time_t filetime
file creation time
int n_scan_dir(const char *dir, LIST *result, const int recurse)
Scan a directory and append only regular files into a sorted list (oldest first).
void n_free_file_info(void *ptr)
Cross-platform directory scanning (Linux / Solaris / Windows via MinGW) WITHOUT chdir().
void n_set_time_from_stat(N_FILE_INFO *file, const struct stat *st)
Fill filetime and filetime_nsec from a struct stat (mtime).
char * n_path_join(const char *dir, const char *name)
Build "dir + '/' + name" (or without extra slash if dir already ends with '/' or '\').
int n_comp_file_info(const void *a, const void *b)
Comparison function for sorting N_FILE_INFO by time (oldest first).
Files configuration header.