123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #include <stdio.h>
- #include <unistd.h>
- #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;
- }
|