Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
13 verr(const char *fmt
, va_list ap
)
15 if (argv0
&& strncmp(fmt
, "usage", sizeof("usage") - 1)) {
16 fprintf(stderr
, "%s: ", argv0
);
19 vfprintf(stderr
, fmt
, ap
);
21 if (fmt
[0] && fmt
[strlen(fmt
) - 1] == ':') {
30 warn(const char *fmt
, ...)
40 die(const char *fmt
, ...)
52 bprintf(const char *fmt
, ...)
58 if ((ret
= vsnprintf(buf
, sizeof(buf
), fmt
, ap
)) < 0) {
60 } else if ((size_t)ret
>= sizeof(buf
)) {
61 warn("vsnprintf: Output truncated");
69 fmt_scaled(size_t bytes
)
73 const char *units
[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB",
77 for (i
= 0; i
< LEN(units
) && scaled
>= 1024; i
++) {
81 return bprintf("%.1f%s", scaled
, units
[i
]);
85 pscanf(const char *path
, const char *fmt
, ...)
91 if (!(fp
= fopen(path
, "r"))) {
92 warn("fopen '%s':", path
);
96 n
= vfscanf(fp
, fmt
, ap
);
100 return (n
== EOF
) ? -1 : n
;