1*b7275c88Smartynas /* $OpenBSD: mtherr.c,v 1.1 2011/07/02 18:11:01 martynas Exp $ */
2*b7275c88Smartynas
3*b7275c88Smartynas /*
4*b7275c88Smartynas * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
5*b7275c88Smartynas *
6*b7275c88Smartynas * Permission to use, copy, modify, and distribute this software for any
7*b7275c88Smartynas * purpose with or without fee is hereby granted, provided that the above
8*b7275c88Smartynas * copyright notice and this permission notice appear in all copies.
9*b7275c88Smartynas *
10*b7275c88Smartynas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*b7275c88Smartynas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*b7275c88Smartynas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*b7275c88Smartynas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*b7275c88Smartynas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*b7275c88Smartynas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*b7275c88Smartynas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*b7275c88Smartynas */
18*b7275c88Smartynas
19*b7275c88Smartynas /* mtherr.c
20*b7275c88Smartynas *
21*b7275c88Smartynas * Library common error handling routine
22*b7275c88Smartynas *
23*b7275c88Smartynas *
24*b7275c88Smartynas *
25*b7275c88Smartynas * SYNOPSIS:
26*b7275c88Smartynas *
27*b7275c88Smartynas * char *fctnam;
28*b7275c88Smartynas * int code;
29*b7275c88Smartynas * int mtherr();
30*b7275c88Smartynas *
31*b7275c88Smartynas * mtherr( fctnam, code );
32*b7275c88Smartynas *
33*b7275c88Smartynas *
34*b7275c88Smartynas *
35*b7275c88Smartynas * DESCRIPTION:
36*b7275c88Smartynas *
37*b7275c88Smartynas * This routine may be called to report one of the following
38*b7275c88Smartynas * error conditions (in the include file mconf.h).
39*b7275c88Smartynas *
40*b7275c88Smartynas * Mnemonic Value Significance
41*b7275c88Smartynas *
42*b7275c88Smartynas * DOMAIN 1 argument domain error
43*b7275c88Smartynas * SING 2 function singularity
44*b7275c88Smartynas * OVERFLOW 3 overflow range error
45*b7275c88Smartynas * UNDERFLOW 4 underflow range error
46*b7275c88Smartynas * TLOSS 5 total loss of precision
47*b7275c88Smartynas * PLOSS 6 partial loss of precision
48*b7275c88Smartynas * EDOM 33 Unix domain error code
49*b7275c88Smartynas * ERANGE 34 Unix range error code
50*b7275c88Smartynas *
51*b7275c88Smartynas * The default version of the file prints the function name,
52*b7275c88Smartynas * passed to it by the pointer fctnam, followed by the
53*b7275c88Smartynas * error condition. The display is directed to the standard
54*b7275c88Smartynas * output device. The routine then returns to the calling
55*b7275c88Smartynas * program. Users may wish to modify the program to abort by
56*b7275c88Smartynas * calling exit() under severe error conditions such as domain
57*b7275c88Smartynas * errors.
58*b7275c88Smartynas *
59*b7275c88Smartynas * Since all error conditions pass control to this function,
60*b7275c88Smartynas * the display may be easily changed, eliminated, or directed
61*b7275c88Smartynas * to an error logging device.
62*b7275c88Smartynas *
63*b7275c88Smartynas * SEE ALSO:
64*b7275c88Smartynas *
65*b7275c88Smartynas * mconf.h
66*b7275c88Smartynas *
67*b7275c88Smartynas */
68*b7275c88Smartynas
69*b7275c88Smartynas #include <stdio.h>
70*b7275c88Smartynas #include "mconf.h"
71*b7275c88Smartynas
72*b7275c88Smartynas int merror = 0;
73*b7275c88Smartynas
74*b7275c88Smartynas /* Notice: the order of appearance of the following
75*b7275c88Smartynas * messages is bound to the error codes defined
76*b7275c88Smartynas * in mconf.h.
77*b7275c88Smartynas */
78*b7275c88Smartynas static char *ermsg[7] = {
79*b7275c88Smartynas "unknown", /* error code 0 */
80*b7275c88Smartynas "domain", /* error code 1 */
81*b7275c88Smartynas "singularity", /* et seq. */
82*b7275c88Smartynas "overflow",
83*b7275c88Smartynas "underflow",
84*b7275c88Smartynas "total loss of precision",
85*b7275c88Smartynas "partial loss of precision"
86*b7275c88Smartynas };
87*b7275c88Smartynas
88*b7275c88Smartynas
mtherr(name,code)89*b7275c88Smartynas int mtherr( name, code )
90*b7275c88Smartynas char *name;
91*b7275c88Smartynas int code;
92*b7275c88Smartynas {
93*b7275c88Smartynas
94*b7275c88Smartynas /* Display string passed by calling program,
95*b7275c88Smartynas * which is supposed to be the name of the
96*b7275c88Smartynas * function in which the error occurred:
97*b7275c88Smartynas */
98*b7275c88Smartynas printf( "\n%s ", name );
99*b7275c88Smartynas
100*b7275c88Smartynas /* Set global error message word */
101*b7275c88Smartynas merror = code;
102*b7275c88Smartynas
103*b7275c88Smartynas /* Display error message defined
104*b7275c88Smartynas * by the code argument.
105*b7275c88Smartynas */
106*b7275c88Smartynas if( (code <= 0) || (code >= 7) )
107*b7275c88Smartynas code = 0;
108*b7275c88Smartynas printf( "%s error\n", ermsg[code] );
109*b7275c88Smartynas
110*b7275c88Smartynas /* Return to calling
111*b7275c88Smartynas * program
112*b7275c88Smartynas */
113*b7275c88Smartynas return( 0 );
114*b7275c88Smartynas }
115