summaryrefslogtreecommitdiff
path: root/src/shared.c
blob: 8d0dcca1dc47cb16acd4d866beebc8acf2cb7b83 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "shared.h"

#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>

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_s(const char* path, const char* type) {
    struct stat s;
    if (stat(path, &s) < 0) {
        fprintf(stderr, "error: failed to read %s: %s\n", path, strerror(errno));
        return NULL;
    }
    if (!S_ISREG(s.st_mode)) {
        fprintf(stderr, "error: %s is not a file\n", path);
        return NULL;
    }
    FILE* file = fopen(path, type);
    if (file == NULL) {
        fprintf(stderr, "error: failed to open file %s: %s\n", path, strerror(errno));
    }
    return file;
}

FILE* get_file(const char* path, const char* type) {
    FILE* file = get_file_s(path, type);
    if (file == NULL) exit(EXIT_FAILURE);
    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, char buf[5]) {
    int index = 0;
    float next = bytes;
    while (true) {
        if (next < 1000) {
            break;
        }
        if (index == 5) {
            printf("999P");
            return;
        };
        next /= 1024;
        index++;
    }
    
    int n = snprintf(buf, 4, "%u", (int)(next+.5));
    if (index > 0) {
        buf[n] = (fs_types[index - 1]);
        n++;
    }
    buf[n] = '\0';
}

static char* months[12] = 
    {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
void print_date_time(time_t mills, char buf[13]) {
    struct tm* info;
    info = localtime(&mills);
    int n = snprintf(buf, 5, "%s ", months[info->tm_mon]);

    if (info->tm_mday < 10) {
        buf[n] = ' ';
        n++;
    }

    snprintf(buf + n, 13 - n, "%d %02d:%02d ", info->tm_mday, info->tm_hour, info->tm_sec);
}