2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static char sccsid[] = "@(#)error.c 8.2 (Berkeley) 5/4/95";
41 static const char rcsid[] =
46 * Errors and exceptions.
62 * Code to handle exceptions in C.
65 struct jmploc *handler;
66 volatile sig_atomic_t exception;
67 volatile sig_atomic_t suppressint;
68 volatile sig_atomic_t intpending;
72 static void exverror __P((int, char *, va_list));
75 * Called to raise an exception. Since C doesn't include exceptions, we
76 * just do a longjmp to the exception handler. The type of exception is
77 * stored in the global variable "exception".
87 longjmp(handler->loc, 1);
92 * Called from trap.c when a SIGINT is received. (If the user specifies
93 * that SIGINT is to be trapped or ignored using the trap builtin, then
94 * this routine is not called.) Suppressint is nonzero when interrupts
95 * are held using the INTOFF macro. If SIGINTs are not suppressed and
96 * the shell is not a root shell, then we want to be terminated if we
97 * get here, as if we were terminated directly by a SIGINT. Arrange for
106 * The !in_dotrap here is safe. The only way we can arrive here
107 * with in_dotrap set is that a trap handler set SIGINT to SIG_DFL
111 if (suppressint && !in_dotrap) {
116 sigemptyset(&sigset);
117 sigprocmask(SIG_SETMASK, &sigset, NULL);
120 * This doesn't seem to be needed, since main() emits a newline.
123 if (tcgetpgrp(0) == getpid())
124 write(STDERR_FILENO, "\n", 1);
126 if (rootshell && iflag)
129 signal(SIGINT, SIG_DFL);
130 kill(getpid(), SIGINT);
136 * Exverror is called to raise the error exception. If the first argument
137 * is not NULL then error prints an error message using printf style
138 * formatting. It then raises the error exception.
141 exverror(cond, msg, ap)
151 TRACE(("exverror(%d, \"%s\") pid=%d\n", cond, msg, getpid()));
153 TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid()));
157 outfmt(&errout, "%s: ", commandname);
158 doformat(&errout, msg, ap);
168 error(char *msg, ...)
183 msg = va_arg(ap, char *);
185 exverror(EXERROR, msg, ap);
192 exerror(int cond, char *msg, ...)
208 cond = va_arg(ap, int);
209 msg = va_arg(ap, char *);
211 exverror(cond, msg, ap);
218 * Table of error messages.
222 short errcode; /* error number */
223 short action; /* operation which encountered the error */
224 char *msg; /* text describing the error */
228 #define ALL (E_OPEN|E_CREAT|E_EXEC)
230 STATIC const struct errname errormsg[] = {
231 { EINTR, ALL, "interrupted" },
232 { EACCES, ALL, "permission denied" },
233 { EIO, ALL, "I/O error" },
234 { ENOENT, E_OPEN, "no such file" },
235 { ENOENT, E_CREAT,"directory nonexistent" },
236 { ENOENT, E_EXEC, "not found" },
237 { ENOTDIR, E_OPEN, "no such file" },
238 { ENOTDIR, E_CREAT,"directory nonexistent" },
239 { ENOTDIR, E_EXEC, "not found" },
240 { EISDIR, ALL, "is a directory" },
242 { EMFILE, ALL, "too many open files" },
244 { ENFILE, ALL, "file table overflow" },
245 { ENOSPC, ALL, "file system full" },
247 { EDQUOT, ALL, "disk quota exceeded" },
250 { ENOSR, ALL, "no streams resources" },
252 { ENXIO, ALL, "no such device or address" },
253 { EROFS, ALL, "read-only file system" },
254 { ETXTBSY, ALL, "text busy" },
256 { EAGAIN, E_EXEC, "not enough memory" },
258 { ENOMEM, ALL, "not enough memory" },
260 { ENOLINK, ALL, "remote access failed" },
263 { EMULTIHOP, ALL, "remote access failed" },
266 { ECOMM, ALL, "remote access failed" },
269 { ESTALE, ALL, "remote access failed" },
272 { ETIMEDOUT, ALL, "remote access failed" },
275 { ELOOP, ALL, "symbolic link loop" },
277 { E2BIG, E_EXEC, "argument list too long" },
279 { ELIBACC, E_EXEC, "shared library missing" },
286 * Return a string describing an error. The returned string may be a
287 * pointer to a static buffer that will be overwritten on the next call.
288 * Action describes the operation that got the error.
296 struct errname const *ep;
299 for (ep = errormsg ; ep->errcode ; ep++) {
300 if (ep->errcode == e && (ep->action & action) != 0)
303 fmtstr(buf, sizeof buf, "error %d", e);