util.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 MS2S(MS) ((MS) / 1000) /* milliseconds to seconds */
  33. #define MS2NS(MS) (((MS) % 1000) * 1E6) /* milliseconds to nanoseconds */
  34. #define MS2US(MS) (((MS) % 1000) * 1000) /* milliseconds to microseconds */
  35. #define ERRRET(B) \
  36. do { \
  37. *(B) = '\0'; \
  38. return; \
  39. } while (0)
  40. typedef uint8_t percent_t;
  41. #define PRIperc PRIu8
  42. /* buffer printf */
  43. void bprintf(char *, const char *, ...);
  44. void fmt_human(char *, uintmax_t);
  45. /*
  46. * get fd of sysfs file
  47. * example:
  48. * sysfs_fd("/sys/class/power_supply", "BAT0", "capacity")
  49. *
  50. * if last arg is NULL,
  51. * then return fd of directory (/sys/class/power_supply/BAT0/)
  52. * sysfs_fd("/sys/class/power_supply", "BAT0", NULL)
  53. */
  54. #define sysfs_fd(path, device, property) \
  55. _sysfs_fd(__func__, (path), (device), (property))
  56. int _sysfs_fd(const char *func,
  57. const char *path,
  58. const char *device,
  59. const char *property);
  60. /* same as sysfs_fd, but rewind if already opened */
  61. #define sysfs_fd_or_rewind(fd_ptr, path, device, property) \
  62. _sysfs_fd_or_rewind(__func__, (fd_ptr), (path), (device), (property))
  63. uint8_t _sysfs_fd_or_rewind(const char *func,
  64. int * fd,
  65. const char *path,
  66. const char *device,
  67. const char *property);
  68. /*
  69. * if function has prefix `e`: then it will print warning on error
  70. * if return type is uint8_t: then 1 on success 0 on error,
  71. * otherwise return original return value from function without `e` prefix
  72. */
  73. #define eopen(ret_fd, path, flags) \
  74. ((ret_fd = _eopen(__func__, (path), (flags), #flags)) != -1)
  75. int _eopen(const char *func, const char *path, int flags, const char *sflags);
  76. #define eopenat(ret_fd, fd, path, flags) \
  77. ((ret_fd = _eopenat(__func__, (fd), (path), (flags), #flags)) != -1)
  78. int _eopenat(
  79. const char *func, int fd, const char *path, int flags, const char *sflags);
  80. #define elseek(fd, offset, whence) _elseek(__func__, fd, offset, whence)
  81. off_t _elseek(const char *func, int fd, off_t offset, int whence);
  82. #define fd_rewind(fd) _fd_rewind(__func__, (fd))
  83. uint8_t _fd_rewind(const char *func, int fd);
  84. #define eread_ret(ret, fd, ...) _eread_macro(ret =, fd, __VA_ARGS__)
  85. #define eread(fd, ...) _eread_macro(, fd, __VA_ARGS__)
  86. #define _eread_macro(ret, fd, ...) \
  87. ((ret _eread(__func__, (fd), __VA_ARGS__)) != -1)
  88. ssize_t _eread(const char *func, int fd, void *buf, size_t size);
  89. #define eclose(fd) _eclose(__func__, (fd))
  90. uint8_t _eclose(const char *func, int fd);
  91. #define esscanf(count, str, fmt, ...) \
  92. _esscanf(__func__, (count), (str), (fmt), __VA_ARGS__)
  93. uint8_t _esscanf(const char *func,
  94. int count,
  95. const char *restrict str,
  96. const char *restrict fmt,
  97. ...);
  98. void fd_cleanup(void *ptr);
  99. #endif /* _UTIL_H */