helpers.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "helpers.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h> // access
  6. #include <sys/types.h>
  7. #include <dirent.h>
  8. #include <errno.h> // errno
  9. #include <libgen.h> // dirname
  10. bool light_file_read_uint64(char const *filename, uint64_t *val)
  11. {
  12. FILE *fp;
  13. uint64_t data;
  14. fp = fopen(filename, "r");
  15. if(!fp)
  16. {
  17. LIGHT_PERMERR("reading");
  18. return false;
  19. }
  20. if(fscanf(fp, "%lu", &data) != 1)
  21. {
  22. LIGHT_ERR("Couldn't parse an unsigned integer from '%s'", filename);
  23. fclose(fp);
  24. return false;
  25. }
  26. *val = data;
  27. fclose(fp);
  28. return true;
  29. }
  30. bool light_file_write_uint64(char const *filename, uint64_t val)
  31. {
  32. FILE *fp;
  33. fp = fopen(filename, "w");
  34. if(!fp)
  35. {
  36. LIGHT_PERMERR("writing");
  37. return false;
  38. }
  39. if(fprintf(fp, "%lu", val) < 0)
  40. {
  41. LIGHT_ERR("fprintf failed");
  42. fclose(fp);
  43. return false;
  44. }
  45. fclose(fp);
  46. return true;
  47. }
  48. bool light_file_exists (char const *filename)
  49. {
  50. return access( filename, F_OK ) != -1;
  51. }
  52. /* Returns true if file is writable, false otherwise */
  53. bool light_file_is_writable(char const *filename)
  54. {
  55. FILE *fp;
  56. fp = fopen(filename, "r+");
  57. if(!fp)
  58. {
  59. LIGHT_PERMWARN("writing");
  60. return false;
  61. }
  62. fclose(fp);
  63. return true;
  64. }
  65. /* Returns true if file is readable, false otherwise */
  66. bool light_file_is_readable(char const *filename)
  67. {
  68. FILE *fp;
  69. fp = fopen(filename, "r");
  70. if(!fp)
  71. {
  72. LIGHT_PERMWARN("reading");
  73. return false;
  74. }
  75. fclose(fp);
  76. return true;
  77. }
  78. /* Prints a notice about a value which was below `x` and was adjusted to it */
  79. uint64_t light_log_clamp_min(uint64_t min)
  80. {
  81. LIGHT_NOTE("too small value, adjusting to minimum %lu (raw)", min);
  82. return min;
  83. }
  84. /* Prints a notice about a value which was above `x` and was adjusted to it */
  85. uint64_t light_log_clamp_max(uint64_t max)
  86. {
  87. LIGHT_NOTE("too large value, adjusting to maximum %lu (raw)", max);
  88. return max;
  89. }
  90. /* Clamps the `percent` value between 0% and 100% */
  91. double light_percent_clamp(double val)
  92. {
  93. if(val < 0.0)
  94. {
  95. LIGHT_WARN("specified value %g%% is not valid, adjusting it to 0%%", val);
  96. return 0.0;
  97. }
  98. if(val > 100.0)
  99. {
  100. LIGHT_WARN("specified value %g%% is not valid, adjusting it to 100%%", val);
  101. return 100.0;
  102. }
  103. return val;
  104. }
  105. int light_mkpath(char *dir, mode_t mode)
  106. {
  107. struct stat sb;
  108. if(!dir)
  109. {
  110. errno = EINVAL;
  111. return -1;
  112. }
  113. if(!stat(dir, &sb))
  114. return 0;
  115. char *tempdir = strdup(dir);
  116. light_mkpath(dirname(tempdir), mode);
  117. free(tempdir);
  118. return mkdir(dir, mode);
  119. }