mirror of
https://github.com/troglobit/finit.git
synced 2026-10-28 12:30:44 +00:00
tmpfiles: refactor code into new is_dir_empty function
The existing implementation seemed useful enough to warrant a new helper function. Care was taken to ensure the revised code no longer suffers any memory leaks.
This commit is contained in:
1 parent
2957246092
commit
5993b4bb5d
2 files changed
+19
-12
No files matched your search
@@ -26,6 +26,7 @@
|
||||
#define FINIT_HELPERS_H_
|
||||
|
||||
#include <ctype.h>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
@@ -206,6 +207,22 @@ static inline char *fgetval(const char *line, const char *key, char *sep)
|
||||
return realloc(copy, len);
|
||||
}
|
||||
|
||||
static inline int is_dir_empty(const char *path)
|
||||
{
|
||||
struct dirent **namelist;
|
||||
int num;
|
||||
|
||||
num = scandir(path, &namelist, NULL, NULL);
|
||||
if (num < 0)
|
||||
return 0;
|
||||
|
||||
for (int i = 0; i < num; i++)
|
||||
free(namelist[i]);
|
||||
free(namelist);
|
||||
|
||||
return num >= 3;
|
||||
}
|
||||
|
||||
#endif /* FINIT_HELPERS_H_ */
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user