util.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* See LICENSE file for copyright and license details. */
  2. #include <errno.h>
  3. #include <stdarg.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "util.h"
  9. char *argv0;
  10. static void
  11. verr(const char *fmt, va_list ap)
  12. {
  13. vfprintf(stderr, fmt, ap);
  14. if (fmt[0] && fmt[strlen(fmt) - 1] == ':') {
  15. fputc(' ', stderr);
  16. perror(NULL);
  17. } else {
  18. fputc('\n', stderr);
  19. }
  20. }
  21. void
  22. warn(const char *fmt, ...)
  23. {
  24. va_list ap;
  25. va_start(ap, fmt);
  26. verr(fmt, ap);
  27. va_end(ap);
  28. }
  29. void
  30. die(const char *fmt, ...)
  31. {
  32. va_list ap;
  33. va_start(ap, fmt);
  34. verr(fmt, ap);
  35. va_end(ap);
  36. exit(1);
  37. }
  38. static int
  39. evsnprintf(char *str, size_t size, const char *fmt, va_list ap)
  40. {
  41. int ret;
  42. ret = vsnprintf(str, size, fmt, ap);
  43. if (ret < 0) {
  44. warn("vsnprintf:");
  45. return -1;
  46. } else if ((size_t)ret >= size) {
  47. warn("vsnprintf: Output truncated");
  48. return -1;
  49. }
  50. return ret;
  51. }
  52. int
  53. esnprintf(char *str, size_t size, const char *fmt, ...)
  54. {
  55. va_list ap;
  56. int ret;
  57. va_start(ap, fmt);
  58. ret = evsnprintf(str, size, fmt, ap);
  59. va_end(ap);
  60. return ret;
  61. }
  62. const char *
  63. bprintf(const char *fmt, ...)
  64. {
  65. va_list ap;
  66. int ret;
  67. va_start(ap, fmt);
  68. ret = evsnprintf(buf, sizeof(buf), fmt, ap);
  69. va_end(ap);
  70. return (ret < 0) ? NULL : buf;
  71. }
  72. const char *
  73. fmt_human(uintmax_t num, int base)
  74. {
  75. double scaled;
  76. size_t i, prefixlen;
  77. const char **prefix;
  78. const char *prefix_1000[] = { "", "k", "M", "G", "T", "P", "E", "Z",
  79. "Y" };
  80. const char *prefix_1024[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei",
  81. "Zi", "Yi" };
  82. switch (base) {
  83. case 1000:
  84. prefix = prefix_1000;
  85. prefixlen = LEN(prefix_1000);
  86. break;
  87. case 1024:
  88. prefix = prefix_1024;
  89. prefixlen = LEN(prefix_1024);
  90. break;
  91. default:
  92. warn("fmt_human: Invalid base");
  93. return NULL;
  94. }
  95. scaled = num;
  96. for (i = 0; i < prefixlen && scaled >= base; i++)
  97. scaled /= base;
  98. return bprintf("%.1f%s", scaled, prefix[i]);
  99. }
  100. int
  101. pscanf(const char *path, const char *fmt, ...)
  102. {
  103. FILE *fp;
  104. va_list ap;
  105. int n;
  106. if (!(fp = fopen(path, "r"))) {
  107. warn("fopen '%s':", path);
  108. return -1;
  109. }
  110. va_start(ap, fmt);
  111. n = vfscanf(fp, fmt, ap);
  112. va_end(ap);
  113. fclose(fp);
  114. return (n == EOF) ? -1 : n;
  115. }