XDG Basedirs interface in a single header file
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.5KB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "../simple-xdg-bdirs.h"
  4. /* a simple example which attempts to read a value from a configuration file
  5. * and write it back to a cache file */
  6. int main(int argc, char *argv[])
  7. {
  8. char *s, *wdir, **rdirs, **cur;
  9. /* alloc a null-terminated array of directories in which to search for
  10. * configuration files */
  11. rdirs = simple_xdg_bdirs_read_dirs(SIMPLE_XDG_BDIRS_CONFIG);
  12. /* in case of error, errno is set, so perror works */
  13. if (rdirs == NULL) {
  14. perror(NULL);
  15. return 1;
  16. }
  17. /* search in the config directories for the given relative path. if not
  18. * found in the first, the second will be checked as a fallback etc */
  19. s = simple_xdg_bdirs_fullpath_read("xdg_bdirs_test/config.conf", rdirs);
  20. /* element strings of rdirs must also be freed */
  21. for (cur = rdirs; *cur != NULL; cur++)
  22. free(*cur);
  23. free(rdirs);
  24. /* check for errors again */
  25. if (s == NULL) {
  26. perror(NULL);
  27. return 1;
  28. }
  29. /* print the fullpath of the file that was found */
  30. puts(s);
  31. free(s);
  32. /* locate the directory into which runtime files should be written
  33. * (things like sockets or lock files) */
  34. wdir = simple_xdg_bdirs_write_dir(SIMPLE_XDG_BDIRS_RUNTIME);
  35. if (wdir == NULL) {
  36. perror(NULL);
  37. return 1;
  38. }
  39. /* a convenience function that builds a fullpath from a relative path and
  40. * write directory */
  41. s = simple_xdg_bdirs_fullpath_write("xdg_bdirs_test.lock", wdir);
  42. free(wdir);
  43. if (s == NULL) {
  44. perror(NULL);
  45. return 1;
  46. }
  47. puts(s);
  48. free(s);
  49. return 0;
  50. }