summaryrefslogtreecommitdiff
path: root/lib/string.c
blob: 13e414e7da3bb64faf5856c7312b96c42359c575 (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
#include "lslib.h"

#include <ctype.h>
#include <stddef.h>
#include <time.h>

static char fs_types[5] = {'K','M','G','T','P'};
void print_file_size(size_t bytes, char buf[5]) {
    int index, n;
    float next;

    index = 0;
    next = bytes;
    
    while (true) {
        if (next < 1000) {
            break;
        }
        
        if (index == 5) {
            printf("999P");
            return;
        }
        
        next /= 1024;
        index++;
    }
    
    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;
    int n;

    info = localtime(&mills);
    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);
}

void print_file_path(char* path) {
    if (streql("-", path)) {
        printf("(standard input)");
    } else {
        printf("%s", path);
    }
}

void nuke_str(char* type) {
    *type = 0;
    while(*(++type)) {
        *type = 0;
    }
}

bool printable_char(char c) {
    switch (c) {
        case '\n': return true;
        case '\r': return true;
        case '\b': return true;
        case '\t': return true;
        default: return isprint(c) == 0;
    }
}