summaryrefslogtreecommitdiff
path: root/src/util/shared.c
blob: 2e0c98a650b5243ae683996a7fb3c1d0192b87e1 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "shared.h"

#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <paths.h>

extern char* cmd;

void error_s(const char *format, ...) {
    va_list list;
    va_start(list, format);

    fprintf(stderr, "%s: ", cmd);
    vfprintf(stderr, format, list);
    fprintf(stderr, "\n");
}

void error(const char *format, ...) {
    va_list list;
    va_start(list, format);

    fprintf(stderr, "%s: ", cmd);
    vfprintf(stderr, format, list);
    fprintf(stderr, "\n");
    exit(EXIT_FAILURE);
}

void output(const char *format, ...) {
    va_list list;
    va_start(list, format);

    fprintf(stdout, "%s: ", cmd);
    vfprintf(stdout, format, list);
    fprintf(stdout, "\n");
}

FILE* get_file_s(const char* path, const char* type) {
    struct stat s;
    FILE* file;

    if (streql("-", path) && type[0] == 'r') {
        clearerr(stdin);
        fflush(stdin);
        return stdin;
    }
    
    if (lstat(path, &s) < 0) {
        if (type[0] != 'r') goto read;
        error_s("failed to read %s: %s", path, strerror(errno));
        return NULL;
    }
    
    if (S_ISDIR(s.st_mode)) {
        error_s("%s is a directory", path);
        return NULL;
    }

read:
    
    file = fopen(path, type);
    
    if (file == NULL) {
        error_s("failed to %s file %s: %s", type[0] == 'r' ? "read" : "write", 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("%s is not a valid number", text);
    }
    return n;
}

long int get_blkm(const char* text) {
    char* end;
    long int n = strtol(text, &end, 10);
    if (text == end) {
        error("%s is not a valid bkm", text);
    }
    if (*end == '\0') return n;
    switch (*end) {
        case 'K':
        case 'k':
            return n * 1024;
        case 'B':
        case 'b':
            return n * 512;
        case 'M':
        case 'm':
            return n * 1024 * 1204;
        default:
            error("invalid bkm type %c", *end);
    }
    
    /* shouldnt get here anyways */
    return 0;
}

mode_t get_mode(const char* next) {
    char* end = NULL;
    mode_t mode = (mode_t)strtol(next, &end, 8);
    if (!end) return 0;
    while(isspace(*end)) end++;
    if (*end != '\0' || (unsigned) mode < 010000) {
        error("invalid file mode: `%s`", next);
    }
    return mode;
}

bool streql(const char* a, const char* b) {
    int n = 0;

    if (*a != *b) return false;
    
    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, 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);
    }
}

#ifndef MAJOR
#define MAJOR 0
#endif

#ifndef MINOR
#define MINOR 0
#endif

#ifndef PATCH
#define PATCH 0
#endif

void check_arg (char* arg) {
    if (arg == NULL) {
        error("expected another argument after option");
    }
}

void global_help(void (*help)(void)) {
    printf("LazySphere v%d.%d.%d multi-call binary.\n\n", MAJOR, MINOR, PATCH);
    help();
    exit(EXIT_SUCCESS);
}

void parse_help(int argc, char** argv, void (*help)(void)) {
    int i;

    if (argc < 1) return;

    for (i = 0; i < argc; i++) {
        if (!prefix("-", argv[i]) || streql("-", argv[i])) break;
        if (help != NULL && streql("--help", argv[i])) global_help(help);
    }
}

int parse_args(int argc, char** argv, void (*help)(void), int (*short_arg)(char, char*), int (*long_arg)(char*, char*)) {
    int start, i, current;
    char* next_arg;

    if (argc < 1) return 0;

    start = 0;
    for (i = 0; i < argc; i++) {
        
        if (!prefix("-", argv[i]) || streql("-", argv[i])) break;
        if (help != NULL && streql("--help", argv[i])) global_help(help);

        if (i + 1 == argc) {
            next_arg = NULL;
        } else {
            next_arg = argv[i+1];
        }

        current = i;

        if (prefix("--", argv[i])) {
            int r;

            if (long_arg == NULL) {
                goto exit;
            }
            
            r = long_arg(argv[current], next_arg);
            
            if (r == ARG_USED) {
                i++;
                start++;
            } else if (r == ARG_IGNORE) {
                goto exit;
            } else if (r == ARG_INVALID) {
                error("invalid argument %s", argv[current]);
            
            }
        } else {
            size_t j;
            int r;

            if (short_arg == NULL) {
                goto exit;
            }

            for (j = 1; j < strlen(argv[current]); j++) {
                
                r = short_arg(argv[current][j], next_arg);
                
                if (r == ARG_USED) {
                    i++;
                    start++;
                } else if (r == ARG_IGNORE) {
                    goto exit;
                } else if (r == ARG_INVALID) {
                    error("invalid argument -%c", argv[current][j]);
                }
            }
        }

        start++;
    }

exit:

    return start;
}

int get_tty (void) {
    int fd = open(_PATH_TTY, O_RDONLY);
    if (fd < 0) error("failed to get tty: %s", strerror(errno));
    return fd;
}

FILE* get_tty_stream(char* type) {
    int fd = get_tty();
    FILE* file = fdopen(fd, type);
    if (file == NULL) {
        error("failed to open tty stream: %s", strerror(errno));
    }
    return file;
}

static int push_path_buffer_b(char* buf, int* index, const char* string) {
    int save, string_len;

    save = *index;
    if (*index > 1 || (*index == 1 && buf[0] != '/')) {
        buf[(*index)++] = '/';
    }

    string_len = strlen(string);
    memcpy(buf + *index, string, string_len + 1);
    
    *index += string_len;
    
    return save;
}

static void pop_path_buffer_b(char* buf, int* index, int i) {
    *index = i;
    buf[*index] = '\0';
}

static char path_buffer[PATH_MAX + 1];
static int path_buffer_index = 0;

char* get_path_buffer(void) {
    return path_buffer;
}

int push_path_buffer(const char* string) {
    return push_path_buffer_b(path_buffer, &path_buffer_index, string);
}

void pop_path_buffer(int i) {
    pop_path_buffer_b(path_buffer, &path_buffer_index, i);
}

static char path_buffer_2[PATH_MAX + 1];
static int path_buffer_index_2 = 0;

char* get_path_buffer_2(void) {
    return path_buffer_2;
}

int push_path_buffer_2(const char* string) {
    return push_path_buffer_b(path_buffer_2, &path_buffer_index_2, string);
}

void pop_path_buffer_2(int i) {
    pop_path_buffer_b(path_buffer_2, &path_buffer_index_2, i);
}

bool is_dot_dir(const char* path) {
    return streql(path, ".") || streql(path, "..");
}