Repository navigation
Expand file tree
/
Copy patherr.c
More file actions
214 lines (184 loc) · 4.63 KB
/
Copy patherr.c
File metadata and controls
214 lines (184 loc) · 4.63 KB
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
/*
* err.c - shell and utility error-handling routines
*/
/*-
* Copyright (c) 2004-2011
* Jeffrey Allen Neitzel <jan (at) v6shell (dot) org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY JEFFREY ALLEN NEITZEL ``AS IS'', AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL JEFFREY ALLEN NEITZEL BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @(#)$Id$
*/
#include "defs.h"
#include "err.h"
#define FMTMAX SBUFMM(2)
#define MSGMAX (FMTMAX + LINEMAX)
#define MYNAME "unknown"
static void (*myerrexit)(int) = NULL;
/*@observer@*/
static const char *myname = NULL;
static pid_t mypid = -1;
static void wmsg(int, const char *, va_list);
/*
* Handle all errors for the calling process. This includes printing
* any specified non-NULL message to the standard error and calling
* the error exit function pointed to by the global myerrexit.
* This function may or may not return.
*/
void
err(int es, const char *msgfmt, ...)
{
va_list va;
if (msgfmt != NULL) {
va_start(va, msgfmt);
wmsg(FD2, msgfmt, va);
va_end(va);
}
if (myerrexit == NULL) {
fd_print(FD2, FMT1S, "err: Invalid myerrexit function pointer");
abort();
}
(*myerrexit)(es);
}
/*
* Print any specified non-NULL message to the file descriptor pfd.
*/
void
fd_print(int pfd, const char *msgfmt, ...)
{
va_list va;
if (msgfmt != NULL) {
va_start(va, msgfmt);
wmsg(pfd, msgfmt, va);
va_end(va);
}
}
/*
* Return a pointer to the global myname on success.
* Return a pointer to "(null)" on error.
*
* NOTE: Must call setmyname(argv[0]) first.
*/
const char *
getmyname(void)
{
if (myname == NULL)
return "(null)";
return myname;
}
/*
* Return the global mypid on success.
* Return 0 on error.
*
* NOTE: Must call setmypid(getpid()) first.
*/
pid_t
getmypid(void)
{
if (mypid == -1)
return 0;
return mypid;
}
/*
* Set the global myerrexit to the function pointed to by func.
*/
void
setmyerrexit(void (*func)(int))
{
if (func == NULL)
return;
myerrexit = func;
}
/*
* Set the global myname to the basename of the string pointed to by s.
*/
void
setmyname(const char *s)
{
const char *p;
if (s != NULL && *s != EOS) {
if ((p = strrchr(s, SLASH)) != NULL)
p++;
else
p = s;
if (*p == HYPHEN && *(p + 1) != EOS)
p++;
else if (*p == EOS)
/* should never (but can) be true */
p = MYNAME;
} else
/* should never (but can) be true */
p = MYNAME;
myname = p;
}
/*
* Set the global mypid to the process ID p.
*/
void
setmypid(const pid_t p)
{
if (mypid != -1)
return;
mypid = p;
}
/*
* Cause all shell utility processes to exit appropriately on error.
* This function is called by err() and never returns.
*/
void
ut_errexit(int es)
{
#ifdef DEBUG
fd_print(FD2, "ut_errexit: es == %d: Call %s(%d);\n",
es, (getpid() == getmypid()) ? "exit" : "_exit", es);
#endif
EXIT(es);
}
/*
* Write the specified message to the file descriptor wfd.
* A diagnostic is written to FD2 on error.
*/
static void
wmsg(int wfd, const char *msgfmt, va_list va)
{
char msg[MSGMAX];
char fmt[FMTMAX];
struct iovec ev[4];
int i;
i = snprintf(fmt, sizeof(fmt), "%s", msgfmt);
if (i >= 1 && i < (int)sizeof(fmt)) {
i = vsnprintf(msg, sizeof(msg), fmt, va);
if (i >= 0 && i < (int)sizeof(msg)) {
if (write(wfd, msg, strlen(msg)) == -1) {
ev[0].iov_base = (char *)getmyname();
ev[0].iov_len = strlen(getmyname());
ev[1].iov_base = ": ";
ev[1].iov_len = (size_t)2;
ev[2].iov_base = ERR_WRITE;
ev[2].iov_len = strlen(ERR_WRITE);
ev[3].iov_base = "\n";
ev[3].iov_len = (size_t)1;
(void)writev(FD2, ev, 4);
}
}
}
}