xref: /titanic_50/usr/src/lib/libadm/common/ckint.c (revision 015a6ef6781cc3ceba8ad3bfbae98449b6002a1f)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright (c) 1997, by Sun Microsystems, Inc.
28  * All rights reserved.
29  */
30 
31 /*LINTLIBRARY*/
32 #pragma	ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.3 */
33 
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <limits.h>
38 #include <sys/types.h>
39 #include "libadm.h"
40 
41 static void
42 setmsg(char *msg, short base)
43 {
44 	if ((base == 0) || (base == 10))
45 		(void) sprintf(msg, "Please enter an integer.");
46 	else
47 		(void) sprintf(msg, "Please enter a base %d integer.", base);
48 }
49 
50 static void
51 setprmpt(char *prmpt, short base)
52 {
53 	if ((base == 0) || (base == 10))
54 		(void) sprintf(prmpt, "Enter an integer.");
55 	else
56 		(void) sprintf(prmpt, "Enter a base %d integer.", base);
57 }
58 
59 int
60 ckint_val(char *value, short base)
61 {
62 	char	*ptr;
63 
64 	(void) strtol(value, &ptr, (int)base);
65 	if (*ptr == '\0')
66 		return (0);
67 	return (1);
68 }
69 
70 void
71 ckint_err(short base, char *error)
72 {
73 	char	defmesg[64];
74 
75 	setmsg(defmesg, base);
76 	puterror(stdout, defmesg, error);
77 }
78 
79 void
80 ckint_hlp(short base, char *help)
81 {
82 	char	defmesg[64];
83 
84 	setmsg(defmesg, base);
85 	puthelp(stdout, defmesg, help);
86 }
87 
88 int
89 ckint(long *intval, short base, char *defstr, char *error, char *help,
90 	char *prompt)
91 {
92 	long	value;
93 	char	*ptr,
94 		input[MAX_INPUT],
95 		defmesg[64],
96 		temp[64];
97 
98 	if (!prompt) {
99 		setprmpt(temp, base);
100 		prompt = temp;
101 	}
102 	setmsg(defmesg, base);
103 
104 start:
105 	putprmpt(stderr, prompt, NULL, defstr);
106 	if (getinput(input))
107 		return (1);
108 
109 	if (strlen(input) == 0) {
110 		if (defstr) {
111 			*intval = strtol(defstr, NULL, (int)base);
112 			return (0);
113 		}
114 		puterror(stderr, defmesg, error);
115 		goto start;
116 	} else if (strcmp(input, "?") == 0) {
117 		puthelp(stderr, defmesg, help);
118 		goto start;
119 	} else if (ckquit && (strcmp(input, "q") == 0))
120 		return (3);
121 
122 	value = strtol(input, &ptr, (int)base);
123 	if (*ptr != '\0') {
124 		puterror(stderr, defmesg, error);
125 		goto start;
126 	}
127 	*intval = value;
128 	return (0);
129 }
130