File: carp.c

package info (click to toggle)
validns 0.8%2Bgit20230810.a7e6b4f-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,260 kB
  • sloc: ansic: 7,496; perl: 308; makefile: 163
file content (118 lines) | stat: -rw-r--r-- 2,515 bytes parent folder | download
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
/*
 * Part of DNS zone file validator `validns`.
 *
 * Copyright 2011-2014 Anton Berezin <tobez@tobez.org>
 * Modified BSD license.
 * (See LICENSE file in the distribution.)
 *
 */
#include <stdarg.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/param.h>

#include "common.h"
#include "carp.h"

static void v(int is_croak, int is_x, int exit_code, const char *fmt, va_list ap);

void
croak(int exit_code, const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    v(1, errno, exit_code, fmt, ap);
    va_end(ap);
}

void
croakx(int exit_code, const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    v(1, -1, exit_code, fmt, ap);
    va_end(ap);
}

void *
bitch(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    if (!G.opt.no_output) {
        fprintf(stderr, "%s:%d: ", file_info->name, file_info->line);
        if (fmt != NULL) {
            vfprintf(stderr, fmt, ap);
        }
        fprintf(stderr, "\n");
    }
    va_end(ap);
    G.exit_code = 1;
    G.stats.error_count++;
    file_info->paren_mode = 0;
    if (G.opt.die_on_first_error)
        exit(1);
    return NULL;
}

void *
moan(char *file_name, int line, const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    if (!G.opt.no_output) {
        fprintf(stderr, "%s:%d: ", file_name, line);
        if (fmt != NULL) {
            vfprintf(stderr, fmt, ap);
        }
        fprintf(stderr, "\n");
    }
    va_end(ap);
    G.exit_code = 1;
    G.stats.error_count++;
    if (G.opt.die_on_first_error)
        exit(1);
    return NULL;
}

void
v(int is_croak, int use_errno, int exit_code, const char *fmt, va_list ap)
{
    fprintf(stderr, "%s: ", thisprogname());
    if (fmt != NULL) {
        vfprintf(stderr, fmt, ap);
        if (use_errno >= 0)
            fprintf(stderr, ": ");
    }
    if (use_errno >= 0)
        fprintf(stderr, "%s\n", strerror(use_errno));
    else
        fprintf(stderr, "\n");
    if (is_croak)
        exit(exit_code);
}

#if defined(__linux__)
static char proggy[MAXPATHLEN];
#endif

const char *thisprogname(void)
{
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
    return getprogname();
#elif defined(__APPLE__)
    return getprogname();
#elif defined(__sun__)
    return getexecname();
#elif defined(__linux__)
    if (readlink("/proc/self/exe", proggy, MAXPATHLEN) != -1)
        return proggy;
    return "";
#else
#error "unsupported OS"
#endif
}