xref: /titanic_51/usr/src/lib/libadm/common/puthelp.c (revision 450396635f70344c58b6b1e4db38cf17ff34445c)
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 <stdio.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <sys/types.h>
39 #include "libadm.h"
40 
41 void
42 puthelp(FILE *fp, char *defmesg, char *help)
43 {
44 	char	*tmp;
45 	size_t	n;
46 
47 	tmp = NULL;
48 	if (help == NULL) {
49 		/* use default message since no help was provided */
50 		help = defmesg ? defmesg : "No help available.";
51 	} else if (defmesg != NULL) {
52 		n = strlen(help);
53 		if (help[0] == '~') {
54 			/* prepend default message */
55 			tmp = calloc(n+strlen(defmesg)+1, sizeof (char));
56 			(void) strcpy(tmp, defmesg);
57 			(void) strcat(tmp, "\n");
58 			++help;
59 			(void) strcat(tmp, help);
60 			help = tmp;
61 		} else if (n && (help[n-1] == '~')) {
62 			/* append default message */
63 			tmp = calloc(n+strlen(defmesg)+2, sizeof (char));
64 			(void) strcpy(tmp, help);
65 			tmp[n-1] = '\0';
66 			(void) strcat(tmp, "\n");
67 			(void) strcat(tmp, defmesg);
68 			help = tmp;
69 		}
70 	}
71 	(void) puttext(fp, help, ckindent, ckwidth);
72 	(void) fputc('\n', fp);
73 	if (tmp)
74 		free(tmp);
75 }
76