matt/src/util.c

31 lines
634 B
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
bool startswith(char *str, char *sub) {
size_t strl = strlen(str);
size_t subl = strlen(sub);
if (subl > strl)
return false;
return strncmp(sub, str, subl) == 0;
}
void size_to_human(char *buf, long size) {
char *suffix[] = {"B", "KB", "MB", "GB", "TB"};
char length = sizeof(suffix) / sizeof(suffix[0]);
int i = 0;
double cursize = size;
if (size > 1024) {
for (i = 0; (size / 1024) > 0 && i < length - 1; i++, size /= 1024)
cursize = size / 1024.0;
}
sprintf(buf, "%.02lf%s", cursize, suffix[i]);
}