Merge commit 'cc543d0f9e35a75cc302a4cb152756d233299564'
[unleashed.git] / usr / src / lib / libadm / common / ckrange.c
blobbf3710c7a6f026aaf927711fa482edbbac72b43f
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
27 * Copyright (c) 1997, by Sun Microsystems, Inc.
28 * All rights reserved.
31 #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.3 */
32 /*LINTLIBRARY*/
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"
41 #define MSGSIZ 256
42 #define PROMPT10 "Enter an integer between %ld and %ld"
43 #define PROMPT "Enter a base %d integer between %ld and %ld"
44 #define MESG10 "Please enter an integer between %ld and %ld."
45 #define MESG "Please enter a base %d integer between %ld and %ld."
47 static void
48 setmsg(char *msg, long lower, long upper, int base)
50 if ((base == 10) || (base == 0))
51 (void) sprintf(msg, MESG10, lower, upper);
52 else
53 (void) sprintf(msg, MESG, base, lower, upper);
56 void
57 ckrange_err(long lower, long upper, int base, char *error)
59 char defmesg[MSGSIZ];
61 setmsg(defmesg, lower, upper, base);
62 puterror(stdout, defmesg, error);
65 void
66 ckrange_hlp(long lower, long upper, int base, char *help)
68 char defmesg[MSGSIZ];
70 setmsg(defmesg, lower, upper, base);
71 puthelp(stdout, defmesg, help);
74 int
75 ckrange_val(long lower, long upper, int base, char *input)
77 char *ptr;
78 long value;
80 value = strtol(input, &ptr, base);
81 if ((*ptr != '\0') || (value < lower) || (value > upper))
82 return (1);
83 return (0);
86 int
87 ckrange(long *rngval, long lower, long upper, short base, char *defstr,
88 char *error, char *help, char *prompt)
90 int valid, n;
91 long value;
92 char *ptr;
93 char input[MAX_INPUT];
94 char defmesg[MSGSIZ];
95 char defpmpt[128];
96 char buffer[64];
97 char *choices[2];
99 if (lower >= upper)
100 return (2);
102 (void) sprintf(buffer, "%ld-%ld", lower, upper);
104 if (base == 0)
105 base = 10;
107 if (!prompt) {
108 if (base == 10)
109 (void) sprintf(defpmpt, PROMPT10, lower, upper);
110 else
111 (void) sprintf(defpmpt, PROMPT, base, lower, upper);
112 prompt = defpmpt;
115 setmsg(defmesg, lower, upper, base);
116 choices[0] = buffer;
117 choices[1] = NULL;
119 start:
120 putprmpt(stderr, prompt, choices, defstr);
121 if (getinput(input))
122 return (1);
124 n = (int)strlen(input);
125 if (n == 0) {
126 if (defstr) {
127 *rngval = strtol(defstr, NULL, base);
128 return (0);
130 puterror(stderr, defmesg, error);
131 goto start;
133 if (strcmp(input, "?") == 0) {
134 puthelp(stderr, defmesg, help);
135 goto start;
137 if (ckquit && (strcmp(input, "q") == 0))
138 return (3);
140 value = strtol(input, &ptr, base);
141 if (*ptr == '\0')
142 valid = ((value >= lower) && (value <= upper));
143 else
144 valid = 0;
145 if (!valid) {
146 puterror(stderr, defmesg, error);
147 goto start;
149 *rngval = value;
150 return (0);