libthingplugins/src/plugins.c (view raw)
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 |
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int readplugins(char **plugins) {
struct dirent *de;
DIR *dr = opendir("/usr/share/thing/plugins/");
if (dr == NULL) {
perror("Could not open plugin capability directory "
"(/usr/share/thing/plugins/)");
return 1;
}
int i = 0; // plugin counter
while ((de = readdir(dr)) != NULL) {
// find actual name end, as d_name is fixed length 256 bytes
char *nend = strchr(de->d_name, '\0');
if (nend - de->d_name < 7)
continue;
// only match toml files
char ext[6];
strncpy(ext, nend - 5, 6);
if ((strcmp(ext, ".toml")))
continue;
// retrieve only plugin name from filename
// add it to plugins[]
int nlen = nend - de->d_name - 5;
plugins[i] = malloc(nlen);
strncpy(plugins[i], de->d_name, nlen);
i++;
}
closedir(dr);
// add end signal
if (i < 256)
plugins[i] = "";
return 0;
}
|