use syscall for doing syswrite stuff -- more portable?
[unix-history] / usr / src / local / sccscmds / sccscmds.2 / util / fatal.c
CommitLineData
9ba5b3ae 1static char Sccsid[] = "@(#)fatal.c 1.4 %G%";
455d17bb 2
82bd445e
JL
3# include "../hdr/macros.h"
4# include "../hdr/fatal.h"
9ba5b3ae
SL
5#include <sys/syscall.h>
6#define syswrite(a,b,c) syscall(SYS_write,a,b,c)
82bd445e
JL
7
8/*
9 General purpose error handler.
10 Typically, low level subroutines which detect error conditions
11 (an open or create routine, for example) return the
12 value of calling fatal with an appropriate error message string.
13 E.g., return(fatal("can't do it"));
14 Higher level routines control the execution of fatal
15 via the global word Fflags.
16 The macros FSAVE and FRSTR in <fatal.h> can be used by higher
17 level subroutines to save and restore the Fflags word.
18
19 The argument to fatal is a pointer to an error message string.
20 The action of this routine is driven completely from
21 the "Fflags" global word (see <fatal.h>).
22 The following discusses the interpretation of the various bits
23 of Fflags.
24
25 The FTLMSG bit controls the writing of the error
26 message on file descriptor 2. The message is preceded
27 by the string "ERROR: ", unless the global character pointer
28 "Ffile" is non-zero, in which case the message is preceded
29 by the string "ERROR [<Ffile>]: ". A newline is written
30 after the user supplied message.
31
32 If the FTLCLN bit is on, clean_up is called with an
33 argument of 0 (see clean.c).
34
35 If the FTLFUNC bit is on, the function pointed to by the global
36 function pointer "Ffunc" is called with the user supplied
37 error message pointer as argument.
38 (This feature can be used to log error messages).
39
40 The FTLACT bits determine how fatal should return.
41 If the FTLJMP bit is on longjmp(Fjmp) is
37ad916e 42 called (Fjmp is a global vector of n words, see
82bd445e
JL
43 setjmp, longjmp documentation).
44
45 If the FTLEXIT bit is on the value of userexit(1) is
46 passed as an argument to exit(II)
47 (see userexit.c).
48
49 If none of the FTLACT bits are on
50 (the default value for Fflags is 0), the global word
51 "Fvalue" (initialized to -1) is returned.
52
53 If all fatal globals have their default values, fatal simply
54 returns -1.
55*/
56
57int Fcnt;
58int Fflags;
59char *Ffile;
455d17bb 60int Fvalue = -1;
82bd445e 61int (*Ffunc)();
37ad916e 62jmp_buf Fjmp;
82bd445e
JL
63
64
65fatal(msg)
66char *msg;
67{
68 ++Fcnt;
69 if (Fflags & FTLMSG) {
70 syswrite(2,"ERROR",5);
71 if (Ffile) {
72 syswrite(2," [",2);
73 syswrite(2,Ffile,length(Ffile));
74 syswrite(2,"]",1);
75 }
76 syswrite(2,": ",2);
77 syswrite(2,msg,length(msg));
78 syswrite(2,"\n",1);
79 }
80 if (Fflags & FTLCLN)
81 clean_up(0);
82 if (Fflags & FTLFUNC)
83 (*Ffunc)(msg);
84 switch (Fflags & FTLACT) {
85 case FTLJMP:
86 longjmp(Fjmp, 1);
87 case FTLEXIT:
88 exit(userexit(1));
89 case FTLRET:
90 return(Fvalue);
91 }
92}