#include #include #ifndef SYSCONFDIR #define SYSCONFDIR "/etc" #endif #ifndef DATADIR #define DATADIR "/usr/share" #endif static int find_device_by_model(char *conffile, char *model) { // Check config/%model.conf in the current working directory sprintf(conffile, "config/%s.conf", model); if (access(conffile, F_OK) != -1) { return 1; } // Check user overridden /etc/megapixels/config/%model.conf sprintf(conffile, "%s/megapixels/config/%s.conf", SYSCONFDIR, model); if (access(conffile, F_OK) != -1) { return 1; } // Check packaged /usr/share/megapixels/config/%model.conf sprintf(conffile, "%s/megapixels/config/%s.conf", DATADIR, model); if (access(conffile, F_OK) != -1) { return 1; } return 0; } int libmegapixels_find_config(char *configfile) { char model[512]; FILE *fp; if (access("/proc/device-tree/compatible", F_OK) == -1) { return -1; } fp = fopen("/proc/device-tree/compatible", "r"); char *modelptr = model; while (1) { int c = fgetc(fp); if (c == EOF) { *(modelptr) = '\0'; return find_device_by_model(configfile, model); } *(modelptr++) = (char)c; if (c == 0) { if (find_device_by_model(configfile, model)) { return 0; } modelptr = model; } } return -1; }