File: error.c

package info (click to toggle)
memdump 1.01-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 208 kB
  • sloc: ansic: 402; makefile: 78
file content (163 lines) | stat: -rw-r--r-- 3,658 bytes parent folder | download | duplicates (6)
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
/*++
/* NAME
/*	error 3
/* SUMMARY
/*	diagnostics handlers
/* SYNOPSIS
/*	#include <error.h>
/*
/*	void	error(format, ...)
/*	char	*format;
/*
/*	void	remark(format, ...)
/*	char	*format;
/*
/*	void	panic(format, ...)
/*	char	*format;
/*
/*	char	*progname;
/*	int	verbose;
/* DESCRIPTION
/*	This module reports diagnostics. Each routine produces a one-line
/*	record with the program name and a caller-provided informative
/*	message. In the format string, %m is replaced by the text that
/*	corresponds to the present \fBerrno\fR value.
/*
/*	error() writes a message to the standard error stream and
/*	terminates the process with a non-zero exit status.
/*
/*	remark() writes a message to the standard error stream.
/*
/*	panic() writes a message to the standard error stream and
/*	forces a core dump.
/*
/*	progname is a global variable that the application should
/*	assign the program name. The initial value is a pointer to
/*	the string \fB"unknown"\fR.
/*
/*	verbose is a global variable (initially, zero), that exists
/*	solely for the convenience of the application. Typical usage
/*	is like:
/*
/* .ti +5
/*	if (verbose) remark(...);
/* SEE ALSO
/*	errno(2) error numbers
/* HISTORY
/*	error() and remark() appear in "Software Tools" by B.W. Kernighan
/*	and P.J. Plaugher.
/* LICENSE
/*	This software is distributed under the IBM Public License.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*--*/

/* System library. */

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

#ifdef __STDC__
#include <stdarg.h>
#define VARARGS(func,type,arg) func(type arg, ...)
#define VASTART(ap,type,name)  va_start(ap,name)
#define VAEND(ap)              va_end(ap)
#else
#include <varargs.h>
#define VARARGS(func,type,arg) func(va_alist) va_dcl
#define VASTART(ap,type,name)  {type name; va_start(ap); name = va_arg(ap, type)
#define VAEND(ap)              va_end(ap);}
#endif

/* Utility library. */

#include "error.h"

char   *progname = "unknown";
int     verbose = 0;

/* percentm - replace %m by error message associated with value in err */

char   *percentm(buf, str, err)
char   *buf;
char   *str;
int     err;
{
    char   *ip = str;
    char   *op = buf;

    while (*ip) {
	switch (*ip) {
	case '%':
	    switch (ip[1]) {
	    case '\0':				/* don't fall off end */
		*op++ = *ip++;
		break;
	    case 'm':				/* replace %m */
		strcpy(op, strerror(err));
		op += strlen(op);
		ip += 2;
		break;
	    default:				/* leave %<any> alone */
		*op++ = *ip++, *op++ = *ip++;
		break;
	    }
	default:
	    *op++ = *ip++;
	}
    }
    *op = 0;
    return (buf);
}

/* error - print warning on stderr and terminate */

void    VARARGS(error, char *, fmt)
{
    va_list ap;
    int     err = errno;
    char    buf[BUFSIZ];

    VASTART(ap, char *, fmt);
    fprintf(stderr, "%s: ", progname);
    vfprintf(stderr, percentm(buf, fmt, err), ap);
    fprintf(stderr, "\n");
    VAEND(ap);
    exit(1);
}

/* remark - print warning on stderr and continue */

void    VARARGS(remark, char *, fmt)
{
    va_list ap;
    int     err = errno;
    char    buf[BUFSIZ];

    VASTART(ap, char *, fmt);
    fprintf(stderr, "%s: ", progname);
    vfprintf(stderr, percentm(buf, fmt, err), ap);
    fprintf(stderr, "\n");
    VAEND(ap);
}

/* panic - print warning on stderr and dump core */

void    VARARGS(panic, char *, fmt)
{
    va_list ap;
    int     err = errno;
    char    buf[BUFSIZ];

    VASTART(ap, char *, fmt);
    fprintf(stderr, "%s: ", progname);
    vfprintf(stderr, percentm(buf, fmt, err), ap);
    fprintf(stderr, "\n");
    VAEND(ap);
    abort();
}