aboutsummaryrefslogtreecommitdiff
path: root/src/lib.c
blob: b7ee4924b42d95038339ee4c680989c55628b5e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>

void die(const char *message) {
  perror(message);
  exit(1);
}

char *formatBytes(double *bytes) {
  char *SIZES[] = {"bytes", "kB", "MB", "GB"};

  size_t size = *bytes;
  size_t div = 0;
  size_t rem = 0;

  while (size >= 1024 && div < (sizeof SIZES / sizeof *SIZES)) {
    rem = (size % 1024);
    div++;
    size /= 1024;
  }

  *bytes = (float)size + (float)rem / 1024.0;

  return SIZES[div];
}

int intlen(unsigned i) {
  int l = 1;

  while (i > 9) {
    l++;
    i /= 10;
  }

  return l;
}