summaryrefslogtreecommitdiff
path: root/lib/error.c
blob: 0b55edbd17741d499b7eb9569cdb7cc67b5c744b (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
#include "lslib.h"

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

extern char* cmd;

void die (void) {
    exit (EXIT_FAILURE);
}

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

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

    if (errno != 0) {
        fprintf(stderr, ": %s", strerror(errno));
        errno = 0;
    }

    fprintf(stderr, "\n");
}

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

    fprintf(stderr, "%s: ", cmd);
    vfprintf(stderr, format, list);
    
    if (errno != 0) {
        fprintf(stderr, ": %s", strerror(errno));
        errno = 0;
    }

    fprintf(stderr, "\n");
    
    die();
}

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

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