2021-02-22 01:42:18 -06:00
|
|
|
#include <dirent.h>
|
2021-02-22 01:57:40 -06:00
|
|
|
#include <stdlib.h>
|
2021-02-22 01:42:18 -06:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <fnmatch.h>
|
|
|
|
|
|
|
|
#include "ep.h"
|
|
|
|
|
|
|
|
enum lang_index {
|
|
|
|
c_lang,
|
|
|
|
//cpp_lang,
|
|
|
|
//python_lang,
|
|
|
|
//go_lang,
|
|
|
|
lang_index_n
|
|
|
|
};
|
|
|
|
static int c_lang_check(const char *, unsigned char);
|
|
|
|
|
|
|
|
struct lang_check {
|
|
|
|
int (*check)(const char *, unsigned char);
|
|
|
|
char display[8];
|
|
|
|
};
|
|
|
|
|
2021-02-22 01:57:40 -06:00
|
|
|
const struct lang_check l[] = {
|
2021-02-22 01:42:18 -06:00
|
|
|
[c_lang] = { .check = c_lang_check, .display = " C" },
|
|
|
|
};
|
|
|
|
|
|
|
|
/* bitmap of 1<<lang_index */
|
2021-02-22 01:57:40 -06:00
|
|
|
void print_lang(uint64_t mask) {
|
2021-02-22 01:42:18 -06:00
|
|
|
for (int i = 0; i < lang_index_n; i++) {
|
|
|
|
if (mask & (1 << i)) {
|
|
|
|
p(l[i].display);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *lang_thread(void *arg)
|
|
|
|
{
|
2021-02-22 01:57:40 -06:00
|
|
|
/* scan current dir if arg is NULL */
|
2021-02-22 01:42:18 -06:00
|
|
|
char *path = arg ? arg : ".";
|
2021-02-22 01:57:40 -06:00
|
|
|
uint64_t *mask = calloc(1, sizeof(mask));
|
|
|
|
if (!mask)
|
|
|
|
return NULL;
|
2021-02-22 01:42:18 -06:00
|
|
|
|
|
|
|
DIR *d = opendir(path);
|
|
|
|
if (!d)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
struct dirent *item;
|
|
|
|
while ((item = readdir(d))) {
|
|
|
|
for (int i = 0; i < lang_index_n; i++) {
|
|
|
|
/* skip if language has already been detected */
|
|
|
|
if (*mask & (1 << i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
*mask |= l[i].check(item->d_name, item->d_type) << i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 01:57:40 -06:00
|
|
|
return mask;
|
2021-02-22 01:42:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int isfile(unsigned char t) { return t & (DT_REG | DT_LNK); }
|
|
|
|
|
|
|
|
static int c_lang_check(const char *s, unsigned char t)
|
|
|
|
{
|
|
|
|
return isfile(t) && !fnmatch("*.c", s, 0);
|
|
|
|
}
|