summaryrefslogtreecommitdiff
path: root/sources.c
blob: 3defa73478ecbd895ef8fb743e3c148ce175a0bc (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
#define _XOPEN_SOURCE 700
#include <ftw.h>
#include <string.h>
#include <stdlib.h>

#include "maje.h"

static char **filelist = NULL;
static size_t nfiles = 0;

static int addfile(const char *path, const struct stat *st, int flags, struct FTW *ft)
{
	(void)st; (void)flags; (void)ft;

	if (path[0] == '.' && path[1] == '/') {
		path += 2;
	}

	if (strcmp(path + strlen(path) - 2, ".c") == 0) {
		char **tmp = realloc(filelist, sizeof(*filelist) * (nfiles + 2));
		if (tmp == NULL) {
			return 1;
		}
		filelist = tmp;

		filelist[nfiles] = strdup(path);
		filelist[nfiles + 1] = NULL;
		nfiles++;
	}

	return 0;
}

char ** find_source_files(const char *dir)
{
	nftw(dir, addfile, -1, 0);
	return filelist;
}