util.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #ifndef _UTIL_H
  2. #define _UTIL_H
  3. #include <err.h> /* warn */
  4. #include <stdio.h> /* fseek */
  5. #include <errno.h>
  6. #include <unistd.h> /* lseek */
  7. #include <stdint.h> /* uintmax_t, uint8_t */
  8. #include <stdarg.h> /* ... */
  9. #include <inttypes.h> /* PRI* */
  10. #define BUFF_SZ 96 /* buffer size for one commponent */
  11. #define INT_STR_SIZE \
  12. (10 /* max string size size of int32_t (unsigned and signed) */ \
  13. + 1 /* '\n' */)
  14. #define JU_STR_SIZE \
  15. (20 /* max string size size of %ju (uintmax_t) */ + 1 /* '\n' */)
  16. #define __unused __attribute__((__unused__))
  17. #define typeof_field(S, F) __typeof__(((S *)0)->F)
  18. #define TWICE(V) V, V
  19. /*
  20. * #define X SOMETHING
  21. * STR(X)
  22. * => "SOMETHING"
  23. */
  24. #define STRINGIFY_AUX(X) #X
  25. #define STR(X) STRINGIFY_AUX(X)
  26. #define WITH_STR(X) X, #X
  27. #define STR_SIZE(S) (LEN(S) - 1) /* minus null byte */
  28. #define WITH_SSIZE(S) S, STR_SIZE(S)
  29. #define QUOTED(S) "\"" S "\""
  30. #define LEN(S) (sizeof(S) / sizeof *(S))
  31. #define WITH_LEN(S) S, LEN(S)
  32. #define SAFE_ASSIGN(TO, FROM) ((TO) = (__typeof__((TO)))(FROM))
  33. #define MS2S(MS) ((MS) / 1000) /* milliseconds to seconds */
  34. #define MS2NS(MS) (((MS) % 1000) * 1E6) /* milliseconds to nanoseconds */
  35. #define MS2US(MS) (((MS) % 1000) * 1000) /* milliseconds to microseconds */
  36. #define ERRRET(B) \
  37. do { \
  38. *(B) = '\0'; \
  39. return; \
  40. } while (0)
  41. typedef uint8_t percent_t;
  42. #define PRIperc PRIu8
  43. /* buffer printf */
  44. void bprintf(char *, const char *, ...);
  45. void fmt_human(char *, uintmax_t);
  46. /*
  47. * get fd of sysfs file
  48. * example:
  49. * sysfs_fd("/sys/class/power_supply", "BAT0", "capacity")
  50. *
  51. * if last arg is NULL,
  52. * then return fd of directory (/sys/class/power_supply/BAT0/)
  53. * sysfs_fd("/sys/class/power_supply", "BAT0", NULL)
  54. */
  55. #define sysfs_fd(path, device, property) \
  56. _sysfs_fd(__func__, (path), (device), (property))
  57. int _sysfs_fd(const char *func,
  58. const char *path,
  59. const char *device,
  60. const char *property);
  61. /* same as sysfs_fd, but rewind if already opened */
  62. #define sysfs_fd_or_rewind(fd_ptr, path, device, property) \
  63. _sysfs_fd_or_rewind(__func__, (fd_ptr), (path), (device), (property))
  64. uint8_t _sysfs_fd_or_rewind(const char *func,
  65. int * fd,
  66. const char *path,
  67. const char *device,
  68. const char *property);
  69. /*
  70. * if function has prefix `e`: then it will print warning on error
  71. * if return type is uint8_t: then 1 on success 0 on error,
  72. * otherwise return original return value from function without `e` prefix
  73. */
  74. #define eopen(ret_fd, path, flags) \
  75. ((ret_fd = _eopen(__func__, (path), (flags), #flags)) != -1)
  76. int _eopen(const char *func, const char *path, int flags, const char *sflags);
  77. #define eopenat(ret_fd, fd, path, flags) \
  78. ((ret_fd = _eopenat(__func__, (fd), (path), (flags), #flags)) != -1)
  79. int _eopenat(
  80. const char *func, int fd, const char *path, int flags, const char *sflags);
  81. #define elseek(fd, offset, whence) _elseek(__func__, fd, offset, whence)
  82. off_t _elseek(const char *func, int fd, off_t offset, int whence);
  83. #define fd_rewind(fd) _fd_rewind(__func__, (fd))
  84. uint8_t _fd_rewind(const char *func, int fd);
  85. #define eread_ret(ret, fd, ...) _eread_macro(ret = (size_t), fd, __VA_ARGS__)
  86. #define eread(fd, ...) _eread_macro(, fd, __VA_ARGS__)
  87. #define _eread_macro(ret, fd, ...) \
  88. ((ssize_t)(ret _eread(__func__, (fd), __VA_ARGS__)) != -1)
  89. ssize_t _eread(const char *func, int fd, void *buf, size_t size);
  90. #define eclose(fd) _eclose(__func__, (fd))
  91. uint8_t _eclose(const char *func, int fd);
  92. #define esscanf(count, str, fmt, ...) \
  93. _esscanf(__func__, (count), (str), (fmt), __VA_ARGS__)
  94. uint8_t _esscanf(const char *func,
  95. int count,
  96. const char *restrict str,
  97. const char *restrict fmt,
  98. ...);
  99. void fd_cleanup(void *ptr);
  100. #endif /* _UTIL_H */