blob: 150d5bf66e020091ff510478f6206aaa5e506300 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
#include <lib/dir.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
//-----------------------------------------------
// Is directory
bool_t is_directory(char* dirname) {
struct stat st;
if (stat(dirname, &st) == 0) {
if (S_ISDIR(st.st_mode)) {
return TRUE;
}
}
return FALSE;
}
//-----------------------------------------------
// List directory
char** lsdir(char* dirname) {
DIR *d;
struct dirent *dir;
d = opendir(dirname);
char** file_names = (char**)malloc(sizeof(char*));
if (!file_names) {
return NULL;
}
file_names[0] = NULL;
size_t file_count = 0;
if (d) {
while ((dir = readdir(d)) != NULL) {
if (dir->d_type == DT_REG) {
// When a regular file is reached
/// Create space for it in the list
file_names = realloc(file_names, (file_count+1+1)*sizeof(char*));
/// Create space for the name
file_names[file_count] = calloc(strlen(dir->d_name)+1, sizeof(char));
/// Copy the name
strcpy(file_names[file_count], dir->d_name);
/// Mark the end of the file list with a NULL entry
file_names[++file_count] = NULL;
}
}
return file_names;
}
return NULL;
}
|