findconfig.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #ifndef SYSCONFDIR
  4. #define SYSCONFDIR "/etc"
  5. #endif
  6. #ifndef DATADIR
  7. #define DATADIR "/usr/share"
  8. #endif
  9. static int
  10. find_device_by_model(char *conffile, char *model)
  11. {
  12. // Check config/%model.conf in the current working directory
  13. sprintf(conffile, "config/%s.conf", model);
  14. if (access(conffile, F_OK) != -1) {
  15. return 1;
  16. }
  17. // Check user overridden /etc/megapixels/config/%model.conf
  18. sprintf(conffile, "%s/megapixels/config/%s.conf", SYSCONFDIR, model);
  19. if (access(conffile, F_OK) != -1) {
  20. return 1;
  21. }
  22. // Check packaged /usr/share/megapixels/config/%model.conf
  23. sprintf(conffile, "%s/megapixels/config/%s.conf", DATADIR, model);
  24. if (access(conffile, F_OK) != -1) {
  25. return 1;
  26. }
  27. return 0;
  28. }
  29. int
  30. libmegapixels_find_config(char *configfile)
  31. {
  32. char model[512];
  33. FILE *fp;
  34. if (access("/proc/device-tree/compatible", F_OK) == -1) {
  35. return -1;
  36. }
  37. fp = fopen("/proc/device-tree/compatible", "r");
  38. char *modelptr = model;
  39. while (1) {
  40. int c = fgetc(fp);
  41. if (c == EOF) {
  42. *(modelptr) = '\0';
  43. return find_device_by_model(configfile, model);
  44. }
  45. *(modelptr++) = (char)c;
  46. if (c == 0) {
  47. if (find_device_by_model(configfile, model)) {
  48. return 0;
  49. }
  50. modelptr = model;
  51. }
  52. }
  53. return -1;
  54. }