* Copyright (c) 1982 The Regents of the University of California.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 THE REGENTS OR CONTRIBUTORS 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
static char sccsid
[] = "@(#)library.c 5.3 (Berkeley) 4/16/91";
* General purpose routines.
#define nil(type) ((type) 0)
String cmdname
; /* name of command for error messages */
Filename errfilename
; /* current file associated with error */
short errlineno
; /* line number associated with error */
#define ERR_IGNORE ((INTFUNC *) 0)
#define ERR_CATCH ((INTFUNC *) 1)
public INTFUNC
*onsyserr();
* call, callv - call a program and wait for it, returning status
* backv - call a program and don't wait, returning process id
* The command's standard input and output are passed as FILE's.
#define MAXNARGS 100 /* unchecked upper limit on max num of arguments */
#define BADEXEC 127 /* exec fails */
#define ischild(pid) ((pid) == 0)
public int call(name
, in
, out
, args
)
while (*ap
!= nil(String
)) {
return callv(name
, in
, out
, argv
);
public int callv(name
, in
, out
, argv
)
pid
= backv(name
, in
, out
, argv
);
public int backv(name
, in
, out
, argv
)
if (ischild(pid
= fork())) {
(void) onsyserr(EACCES
, ERR_IGNORE
);
* Swap file numbers so as to redirect standard input and output.
private fswap(oldfd
, newfd
)
* Invoke a shell on a command line.
if ((sh
= getenv("SHELL")) == nil(String
)) {
call(sh
, stdin
, stdout
, "-c", s
, 0);
* Wait for a process the right way. We wait for a particular
* process and if any others come along in between, we remember them
* in case they are eventually waited for.
* This routine is not very efficient when the number of processes
* to be remembered is large.
private Pidlist
*pidlist
, *pfind();
public pwait(pid
, statusp
)
if (p
!= nil(Pidlist
*)) {
while ((pnum
= wait(&status
)) != pid
&& pnum
>= 0) {
if (p
== nil(Pidlist
*)) {
panic("pwait: pid %d not found", pid
);
* Look for the given process id on the pidlist.
* Unlink it from list if found.
private Pidlist
*pfind(pid
)
register Pidlist
*p
, *prev
;
for (p
= pidlist
; p
!= nil(Pidlist
*); p
= p
->next
) {
if (p
!= nil(Pidlist
*)) {
if (prev
== nil(Pidlist
*)) {
* System call error handler.
* The syserr routine is called when a system call is about to
* set the c-bit to report an error. Certain errors are caught
* and cause the process to print a message and immediately exit.
extern char *sys_errlist
[];
* Before calling syserr, the integer errno is set to contain the
private ERRINFO errinfo
[] ={
/* no error */ ERR_IGNORE
,
if (e
->func
== ERR_CATCH
) {
panic(sys_errlist
[errno
]);
panic("errno %d", errno
);
} else if (e
->func
!= ERR_IGNORE
) {
* Change the action on receipt of an error.
public INTFUNC
*onsyserr(n
, f
)
INTFUNC
*g
= errinfo
[n
].func
;
* Main driver of error message reporting.
private errmsg(errname
, shouldquit
, s
, a
, b
, c
, d
, e
, f
, g
, h
, i
, j
, k
, l
, m
)
if (shouldquit
and cmdname
!= nil(String
)) {
fprintf(stderr
, "%s: ", cmdname
);
if (errfilename
!= nil(Filename
)) {
fprintf(stderr
, "%s: ", errfilename
);
fprintf(stderr
, "%d: ", errlineno
);
if (errname
!= nil(String
)) {
fprintf(stderr
, "%s: ", errname
);
fprintf(stderr
, s
, a
, b
, c
, d
, e
, f
, g
, h
, i
, j
, k
, l
, m
);
* Errors are a little worse, they mean something is wrong,
* but not so bad that processing can't continue.
* The routine "erecover" is called to recover from the error,
* a default routine is provided that does nothing.
public error(s
, a
, b
, c
, d
, e
, f
, g
, h
, i
, j
, k
, l
, m
)
errmsg(nil(String
), FALSE
, s
, a
, b
, c
, d
, e
, f
, g
, h
, i
, j
, k
, l
, m
);
* Non-recoverable user error.
public fatal(s
, a
, b
, c
, d
, e
, f
, g
, h
, i
, j
, k
, l
, m
)
errmsg("fatal error", TRUE
, s
, a
, b
, c
, d
, e
, f
, g
, h
, i
, j
, k
, l
, m
);
* Panics indicate an internal program error.
public panic(s
, a
, b
, c
, d
, e
, f
, g
, h
, i
, j
, k
, l
, m
)
errmsg("panic", TRUE
, s
, a
, b
, c
, d
, e
, f
, g
, h
, i
, j
, k
, l
, m
);
* Compare n-byte areas pointed to by s1 and s2
* if n is 0 then compare up until one has a null byte.
public int cmp(s1
, s2
, n
)
if (s1
== nil(char *) || s2
== nil(char *)) {
panic("cmp: nil pointer");
return(*(s1
-1) - *(s2
-1));
* Move n bytes from src to dest.
* If n is 0 move until a null is found.
register char *src
, *dest
;
if (src
== nil(char *)) {
panic("mov: nil source");
if (dest
== nil(char *)) {
panic("mov: nil destination");
while ((*dest
++ = *src
++) != '\0');