2023-04-10 04:58:48 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2023-04-10 16:51:42 +00:00
|
|
|
#include <sys/ioctl.h>
|
2023-04-10 04:58:48 +00:00
|
|
|
|
|
|
|
void die(const char *message) {
|
|
|
|
perror(message);
|
2023-04-13 03:46:53 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2023-04-10 04:58:48 +00:00
|
|
|
}
|
2023-04-10 16:51:42 +00:00
|
|
|
|
2023-04-11 23:17:36 +00:00
|
|
|
char *formatbytes(size_t bytes, float *rounded) {
|
2023-04-10 16:51:42 +00:00
|
|
|
char *SIZES[] = {"bytes", "kB", "MB", "GB"};
|
|
|
|
|
2023-04-11 16:32:38 +00:00
|
|
|
size_t size = bytes;
|
|
|
|
size_t div = 0;
|
|
|
|
size_t rem = 0;
|
2023-04-10 16:51:42 +00:00
|
|
|
|
|
|
|
while (size >= 1024 && div < (sizeof SIZES / sizeof *SIZES)) {
|
|
|
|
rem = (size % 1024);
|
|
|
|
div++;
|
|
|
|
size /= 1024;
|
|
|
|
}
|
|
|
|
|
2023-04-11 00:14:18 +00:00
|
|
|
*rounded = (float)size + (float)rem / 1024.0;
|
2023-04-10 16:51:42 +00:00
|
|
|
|
|
|
|
return SIZES[div];
|
|
|
|
}
|
2023-04-10 18:36:45 +00:00
|
|
|
|
2023-04-11 16:32:38 +00:00
|
|
|
int intlen(size_t i) {
|
2023-04-10 18:36:45 +00:00
|
|
|
int l = 1;
|
|
|
|
|
|
|
|
while (i > 9) {
|
|
|
|
l++;
|
|
|
|
i /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|