#include "shared.h" #include #include #include #include #include void error(const char* format, ...) { va_list list; va_start(list, format); vfprintf(stderr, format, list); fprintf(stderr, "\n"); exit(EXIT_FAILURE); } FILE* get_file(const char* path, const char* type) { FILE* file = fopen(path, type); if (file == NULL) { error("error: failed to open file %s: %s", path, strerror(errno)); } return file; } long int get_number(const char* text) { char* end; long int n = strtol(text, &end, 10); if (text == end) { error("error: %s is not a valid number", text); } return n; } bool streql(const char* a, const char* b) { if (*a != *b) return false; int n = 0; while (true) { if (*(a+n) != *(b+n)) return false; if (*(a+n) == '\0') return true; ++n; } } bool prefix(const char* pre, const char* str) { return strncmp(pre, str, strlen(pre)) == 0; } static char fs_types[5] = {'K','M','G','T','P'}; void print_file_size(size_t bytes) { int index = 0; float next = bytes; while (true) { if (next < 1000) { break; } if (index == 5) { printf("999P"); return; }; next /= 1024; index++; } if (next/100 < 1) putchar(' '); if (next/10 < 1) putchar(' '); if (index == 0) putchar(' '); printf("%u", (int)(next+.5)); if (index > 0) { putchar(fs_types[index - 1]); } putchar(' '); } static char* months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; void print_date_time(time_t mills) { struct tm* info; info = localtime(&mills); printf("%s ", months[info->tm_mon]); if (info->tm_mday < 10) printf(" "); printf("%d %02d:%02d ", info->tm_mday, info->tm_hour, info->tm_sec); }