File: err.c

package info (click to toggle)
gfsecret 0.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,964 kB
  • sloc: sh: 5,079; ansic: 3,118; makefile: 41; sed: 16
file content (110 lines) | stat: -rw-r--r-- 2,304 bytes parent folder | download | duplicates (3)
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
/*
 * err - Incenp.org Notch Library: (v)(err|warn)(x) replacement
 * Copyright (C) 2011 Damien Goutte-Gattat
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

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

#define ERR_EXIT_ON_ERROR   0x01
#define ERR_PRINT_ERRNO     0x02

const char *getprogname(void);

static void
do_error(int opt, int eval, const char *fmt, va_list ap)
{
    fprintf(stderr, "%s: ", getprogname());
    if ( fmt )
        vfprintf(stderr, fmt, ap);
    if ( opt & ERR_PRINT_ERRNO )
        fprintf(stderr, ": %s", strerror(errno));
    fputc('\n', stderr);

    if ( opt & ERR_EXIT_ON_ERROR )
        exit(eval);
}

void
err(int eval, const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    do_error(ERR_EXIT_ON_ERROR | ERR_PRINT_ERRNO, eval, fmt, ap);
    va_end(ap);
}

void
errx(int eval, const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    do_error(ERR_EXIT_ON_ERROR, eval, fmt, ap);
    va_end(ap);
}

void
verr(int eval, const char *fmt, va_list ap)
{
    do_error(ERR_EXIT_ON_ERROR | ERR_PRINT_ERRNO, eval, fmt, ap);
}

void
verrx(int eval, const char *fmt, va_list ap)
{
    do_error(ERR_EXIT_ON_ERROR, eval, fmt, ap);
}

void
warn(const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    do_error(ERR_PRINT_ERRNO, 0, fmt, ap);
    va_end(ap);
}

void
warnx(const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    do_error(0, 0, fmt, ap);
    va_end(ap);
}

void
vwarn(const char *fmt, va_list ap)
{
    do_error(ERR_PRINT_ERRNO, 0, fmt, ap);
}

void
vwarnx(const char *fmt, va_list ap)
{
    do_error(0, 0, fmt, ap);
}