xref: /titanic_44/usr/src/lib/libadm/common/puterror.c (revision bde3d612a7c090234c60e6e4578821237a5db135)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
24 /*	  All Rights Reserved  	*/
25 
26 
27 /*
28  * Copyright (c) 1997, by Sun Microsystems, Inc.
29  * All rights reserved.
30  */
31 
32 /*LINTLIBRARY*/
33 #pragma	ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1 */
34 
35 #include <sys/types.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include "libadm.h"
40 
41 #define	DEFMSG	"ERROR: "
42 #define	MS	sizeof (DEFMSG)
43 #define	INVINP	"invalid input"
44 
45 void
46 puterror(FILE *fp, char *defmesg, char *error)
47 {
48 	char	*tmp;
49 	size_t	n;
50 
51 	if (error == NULL) {
52 		/* use default message since no error was provided */
53 		n = (defmesg ?  strlen(defmesg) : strlen(INVINP));
54 		tmp = calloc(MS+n+1, sizeof (char));
55 		(void) strcpy(tmp, DEFMSG);
56 		(void) strcat(tmp, defmesg ? defmesg : INVINP);
57 
58 	} else if (defmesg != NULL) {
59 		n = strlen(error);
60 		if (error[0] == '~') {
61 			/* prepend default message */
62 			tmp = calloc(MS+n+strlen(defmesg)+2, sizeof (char));
63 			(void) strcpy(tmp, DEFMSG);
64 			(void) strcat(tmp, defmesg);
65 			(void) strcat(tmp, "\n");
66 			++error;
67 			(void) strcat(tmp, error);
68 		} else if (n && (error[n-1] == '~')) {
69 			/* append default message */
70 			tmp = calloc(MS+n+strlen(defmesg)+2, sizeof (char));
71 			(void) strcpy(tmp, DEFMSG);
72 			(void) strcat(tmp, error);
73 			/* first -1 'cuz sizeof (DEFMSG) includes terminator */
74 			tmp[MS-1+n-1] = '\0';
75 			(void) strcat(tmp, "\n");
76 			(void) strcat(tmp, defmesg);
77 		} else {
78 			tmp = calloc(MS+n+1, sizeof (char));
79 			(void) strcpy(tmp, DEFMSG);
80 			(void) strcat(tmp, error);
81 		}
82 	} else {
83 		n = strlen(error);
84 		tmp = calloc(MS+n+1, sizeof (char));
85 		(void) strcpy(tmp, DEFMSG);
86 		(void) strcat(tmp, error);
87 	}
88 	(void) puttext(fp, tmp, ckindent, ckwidth);
89 	(void) fputc('\n', fp);
90 	free(tmp);
91 }
92