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