xref: /titanic_44/usr/src/lib/libadm/common/cktime.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1997,1998 by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  * All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <stdio.h>
35*7c478bd9Sstevel@tonic-gate #include <string.h>
36*7c478bd9Sstevel@tonic-gate #include <ctype.h>
37*7c478bd9Sstevel@tonic-gate #include <limits.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
39*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
40*7c478bd9Sstevel@tonic-gate #include "libadm.h"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate static int	fmtcheck(char *);
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #define	PROMPT	"Enter the time of day"
45*7c478bd9Sstevel@tonic-gate #define	ERRMSG	"Please enter the time of day. Format is"
46*7c478bd9Sstevel@tonic-gate #define	DEFAULT	"%H:%M"
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #define	TLEN 3
49*7c478bd9Sstevel@tonic-gate #define	LH 00
50*7c478bd9Sstevel@tonic-gate #define	UH 23
51*7c478bd9Sstevel@tonic-gate #define	USH 12
52*7c478bd9Sstevel@tonic-gate #define	LM 00
53*7c478bd9Sstevel@tonic-gate #define	UM 59
54*7c478bd9Sstevel@tonic-gate #define	LS 00
55*7c478bd9Sstevel@tonic-gate #define	US 59
56*7c478bd9Sstevel@tonic-gate #define	DELIM1 ':'
57*7c478bd9Sstevel@tonic-gate #define	BLANK ' '
58*7c478bd9Sstevel@tonic-gate #define	TAB '	'
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate static void
61*7c478bd9Sstevel@tonic-gate setmsg(char *msg, char *fmt)
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate 	if (fmt == NULL)
64*7c478bd9Sstevel@tonic-gate 		fmt = DEFAULT;
65*7c478bd9Sstevel@tonic-gate 	(void) sprintf(msg, "%s <%s>.", ERRMSG, fmt);
66*7c478bd9Sstevel@tonic-gate }
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate static char *
69*7c478bd9Sstevel@tonic-gate p_ndig(char *string, int *value)
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	char *ptr;
72*7c478bd9Sstevel@tonic-gate 	int accum = 0;
73*7c478bd9Sstevel@tonic-gate 	int n = 2;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	if (!string)
76*7c478bd9Sstevel@tonic-gate 		return (0);
77*7c478bd9Sstevel@tonic-gate 	for (ptr = string; *ptr && n > 0; n--, ptr++) {
78*7c478bd9Sstevel@tonic-gate 		if (! isdigit((unsigned char)*ptr))
79*7c478bd9Sstevel@tonic-gate 			return (NULL);
80*7c478bd9Sstevel@tonic-gate 		accum = (10 * accum) + (*ptr - '0');
81*7c478bd9Sstevel@tonic-gate 		}
82*7c478bd9Sstevel@tonic-gate 	if (n)
83*7c478bd9Sstevel@tonic-gate 		return (NULL);
84*7c478bd9Sstevel@tonic-gate 	*value = accum;
85*7c478bd9Sstevel@tonic-gate 	return (ptr);
86*7c478bd9Sstevel@tonic-gate }
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate static char *
89*7c478bd9Sstevel@tonic-gate p_time(char *string, int llim, int ulim)
90*7c478bd9Sstevel@tonic-gate {
91*7c478bd9Sstevel@tonic-gate 	char *ptr;
92*7c478bd9Sstevel@tonic-gate 	int begin = -1;
93*7c478bd9Sstevel@tonic-gate 	if (!(ptr = p_ndig(string, &begin)))
94*7c478bd9Sstevel@tonic-gate 		return (NULL);
95*7c478bd9Sstevel@tonic-gate 	if (begin >= llim && begin <= ulim)
96*7c478bd9Sstevel@tonic-gate 		return (ptr);
97*7c478bd9Sstevel@tonic-gate 	else return (NULL);
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate /* p_meridian will parse the string for the meridian - AM/PM or am/pm */
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate static char *
103*7c478bd9Sstevel@tonic-gate p_meridian(char *string)
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate 	static char *middle[] = { "AM", "PM", "am", "pm" };
106*7c478bd9Sstevel@tonic-gate 	int legit, n;
107*7c478bd9Sstevel@tonic-gate 	char mid[TLEN];
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	legit = 0;
110*7c478bd9Sstevel@tonic-gate 	n = 0;
111*7c478bd9Sstevel@tonic-gate 	mid[2] = '\0';
112*7c478bd9Sstevel@tonic-gate 	(void) sscanf(string, "%2s", mid);
113*7c478bd9Sstevel@tonic-gate 	while (!(legit) && (n < 4)) {
114*7c478bd9Sstevel@tonic-gate 		if ((strncmp(mid, middle[n], 2)) == 0)
115*7c478bd9Sstevel@tonic-gate 			legit = 1;	/* found legitimate string */
116*7c478bd9Sstevel@tonic-gate 		n++;
117*7c478bd9Sstevel@tonic-gate 	}
118*7c478bd9Sstevel@tonic-gate 	if (legit)
119*7c478bd9Sstevel@tonic-gate 		return (string+2);
120*7c478bd9Sstevel@tonic-gate 	return (NULL);
121*7c478bd9Sstevel@tonic-gate }
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate static char *
124*7c478bd9Sstevel@tonic-gate p_delim(char *string, char dchoice)
125*7c478bd9Sstevel@tonic-gate {
126*7c478bd9Sstevel@tonic-gate 	char dlm;
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	if (! string)
129*7c478bd9Sstevel@tonic-gate 		return (NULL);
130*7c478bd9Sstevel@tonic-gate 	(void) sscanf(string, "%1c", &dlm);
131*7c478bd9Sstevel@tonic-gate 	return ((dlm == dchoice) ? string + 1 : NULL);
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate int
135*7c478bd9Sstevel@tonic-gate cktime_val(char *fmt, char *input)
136*7c478bd9Sstevel@tonic-gate {
137*7c478bd9Sstevel@tonic-gate 	char ltrl, dfl;
138*7c478bd9Sstevel@tonic-gate 	int valid = 1; 	/* time of day string is valid for format */
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	if ((fmt != NULL) && (fmtcheck(fmt) == 1))
141*7c478bd9Sstevel@tonic-gate 		return (4);
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	if (fmt == NULL)
144*7c478bd9Sstevel@tonic-gate 		fmt = DEFAULT;
145*7c478bd9Sstevel@tonic-gate 	ltrl = '\0';
146*7c478bd9Sstevel@tonic-gate 	while (*fmt && valid) {
147*7c478bd9Sstevel@tonic-gate 		if ((*fmt) == '%') {
148*7c478bd9Sstevel@tonic-gate 			switch (*++fmt) {
149*7c478bd9Sstevel@tonic-gate 			    case 'H':
150*7c478bd9Sstevel@tonic-gate 				input = p_time(input, LH, UH);
151*7c478bd9Sstevel@tonic-gate 				if (!input)
152*7c478bd9Sstevel@tonic-gate 					valid = 0;
153*7c478bd9Sstevel@tonic-gate 				break;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 			    case 'M':
156*7c478bd9Sstevel@tonic-gate 				input = p_time(input, LM, UM);
157*7c478bd9Sstevel@tonic-gate 				if (!input)
158*7c478bd9Sstevel@tonic-gate 					valid = 0;
159*7c478bd9Sstevel@tonic-gate 				break;
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 			    case 'S':
162*7c478bd9Sstevel@tonic-gate 				input = p_time(input, LS, US);
163*7c478bd9Sstevel@tonic-gate 				if (!input)
164*7c478bd9Sstevel@tonic-gate 					valid = 0;
165*7c478bd9Sstevel@tonic-gate 				break;
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 			    case 'T':
168*7c478bd9Sstevel@tonic-gate 				input = p_time(input, LH, UH);
169*7c478bd9Sstevel@tonic-gate 				if (!input) {
170*7c478bd9Sstevel@tonic-gate 					valid = 0;
171*7c478bd9Sstevel@tonic-gate 					break;
172*7c478bd9Sstevel@tonic-gate 				}
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 				input = p_delim(input, DELIM1);
175*7c478bd9Sstevel@tonic-gate 				if (!input) {
176*7c478bd9Sstevel@tonic-gate 					valid = 0;
177*7c478bd9Sstevel@tonic-gate 					break;
178*7c478bd9Sstevel@tonic-gate 				}
179*7c478bd9Sstevel@tonic-gate 				input = p_time(input, LM, UM);
180*7c478bd9Sstevel@tonic-gate 				if (!input) {
181*7c478bd9Sstevel@tonic-gate 					valid = 0;
182*7c478bd9Sstevel@tonic-gate 					break;
183*7c478bd9Sstevel@tonic-gate 				}
184*7c478bd9Sstevel@tonic-gate 				input = p_delim(input, DELIM1);
185*7c478bd9Sstevel@tonic-gate 				if (!input) {
186*7c478bd9Sstevel@tonic-gate 					valid = 0;
187*7c478bd9Sstevel@tonic-gate 					break;
188*7c478bd9Sstevel@tonic-gate 				}
189*7c478bd9Sstevel@tonic-gate 				input = p_time(input, LS, US);
190*7c478bd9Sstevel@tonic-gate 				if (!input)
191*7c478bd9Sstevel@tonic-gate 					valid = 0;
192*7c478bd9Sstevel@tonic-gate 				break;
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 			    case 'R':
195*7c478bd9Sstevel@tonic-gate 				input = p_time(input, LH, UH);
196*7c478bd9Sstevel@tonic-gate 				if (!input) {
197*7c478bd9Sstevel@tonic-gate 					valid = 0;
198*7c478bd9Sstevel@tonic-gate 					break;
199*7c478bd9Sstevel@tonic-gate 				}
200*7c478bd9Sstevel@tonic-gate 				input = p_delim(input, DELIM1);
201*7c478bd9Sstevel@tonic-gate 				if (!input) {
202*7c478bd9Sstevel@tonic-gate 					valid = 0;
203*7c478bd9Sstevel@tonic-gate 					break;
204*7c478bd9Sstevel@tonic-gate 				}
205*7c478bd9Sstevel@tonic-gate 				input = p_time(input, LM, UM);
206*7c478bd9Sstevel@tonic-gate 				if (!input) {
207*7c478bd9Sstevel@tonic-gate 					valid = 0;
208*7c478bd9Sstevel@tonic-gate 					break;
209*7c478bd9Sstevel@tonic-gate 				}
210*7c478bd9Sstevel@tonic-gate 				break;
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 			    case 'r':
213*7c478bd9Sstevel@tonic-gate 				input = p_time(input, LH, USH);
214*7c478bd9Sstevel@tonic-gate 				if (!input) {
215*7c478bd9Sstevel@tonic-gate 					valid = 0;
216*7c478bd9Sstevel@tonic-gate 					break;
217*7c478bd9Sstevel@tonic-gate 				}
218*7c478bd9Sstevel@tonic-gate 				input = p_delim(input, DELIM1);
219*7c478bd9Sstevel@tonic-gate 				if (!input) {
220*7c478bd9Sstevel@tonic-gate 					valid = 0;
221*7c478bd9Sstevel@tonic-gate 					break;
222*7c478bd9Sstevel@tonic-gate 				}
223*7c478bd9Sstevel@tonic-gate 				input = p_time(input, LM, UM);
224*7c478bd9Sstevel@tonic-gate 				if (!input) {
225*7c478bd9Sstevel@tonic-gate 					valid = 0;
226*7c478bd9Sstevel@tonic-gate 					break;
227*7c478bd9Sstevel@tonic-gate 				}
228*7c478bd9Sstevel@tonic-gate 				input = p_delim(input, DELIM1);
229*7c478bd9Sstevel@tonic-gate 				if (!input) {
230*7c478bd9Sstevel@tonic-gate 					valid = 0;
231*7c478bd9Sstevel@tonic-gate 					break;
232*7c478bd9Sstevel@tonic-gate 				}
233*7c478bd9Sstevel@tonic-gate 				input = p_time(input, LS, US);
234*7c478bd9Sstevel@tonic-gate 				if (!input) {
235*7c478bd9Sstevel@tonic-gate 					valid = 0;
236*7c478bd9Sstevel@tonic-gate 					break;
237*7c478bd9Sstevel@tonic-gate 				}
238*7c478bd9Sstevel@tonic-gate 				input = p_delim(input, BLANK);
239*7c478bd9Sstevel@tonic-gate 				if (!input) {
240*7c478bd9Sstevel@tonic-gate 					valid = 0;
241*7c478bd9Sstevel@tonic-gate 					break;
242*7c478bd9Sstevel@tonic-gate 				}
243*7c478bd9Sstevel@tonic-gate 				input = p_meridian(input);
244*7c478bd9Sstevel@tonic-gate 				if (!input)
245*7c478bd9Sstevel@tonic-gate 					valid = 0;
246*7c478bd9Sstevel@tonic-gate 				break;
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 			    case 'I':
249*7c478bd9Sstevel@tonic-gate 				input = p_time(input, LH, USH);
250*7c478bd9Sstevel@tonic-gate 				if (!input)
251*7c478bd9Sstevel@tonic-gate 					valid = 0;
252*7c478bd9Sstevel@tonic-gate 				break;
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 			    case 'p':
255*7c478bd9Sstevel@tonic-gate 				input = p_meridian(input);
256*7c478bd9Sstevel@tonic-gate 				if (!input)
257*7c478bd9Sstevel@tonic-gate 					valid = 0;
258*7c478bd9Sstevel@tonic-gate 				break;
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 			    default:
261*7c478bd9Sstevel@tonic-gate 				(void) sscanf(input++, "%1c", &ltrl);
262*7c478bd9Sstevel@tonic-gate 			}
263*7c478bd9Sstevel@tonic-gate 		} else {
264*7c478bd9Sstevel@tonic-gate 			dfl = '\0';
265*7c478bd9Sstevel@tonic-gate 			(void) sscanf(input, "%1c", &dfl);
266*7c478bd9Sstevel@tonic-gate 			input++;
267*7c478bd9Sstevel@tonic-gate 		}
268*7c478bd9Sstevel@tonic-gate 		fmt++;
269*7c478bd9Sstevel@tonic-gate 	}
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 	if (!(*fmt) && (input) && (*input))
272*7c478bd9Sstevel@tonic-gate 		valid = 0;
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 	return ((valid == 0));
275*7c478bd9Sstevel@tonic-gate }
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate int
278*7c478bd9Sstevel@tonic-gate cktime_err(char *fmt, char *error)
279*7c478bd9Sstevel@tonic-gate {
280*7c478bd9Sstevel@tonic-gate 	char	defmesg[128];
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 	if ((fmt != NULL) && (fmtcheck(fmt) == 1))
283*7c478bd9Sstevel@tonic-gate 		return (4);
284*7c478bd9Sstevel@tonic-gate 	setmsg(defmesg, fmt);
285*7c478bd9Sstevel@tonic-gate 	puterror(stdout, defmesg, error);
286*7c478bd9Sstevel@tonic-gate 	return (0);
287*7c478bd9Sstevel@tonic-gate }
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate int
290*7c478bd9Sstevel@tonic-gate cktime_hlp(char *fmt, char *help)
291*7c478bd9Sstevel@tonic-gate {
292*7c478bd9Sstevel@tonic-gate 	char	defmesg[128];
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	if ((fmt != NULL) && (fmtcheck(fmt) == 1))
295*7c478bd9Sstevel@tonic-gate 		return (4);
296*7c478bd9Sstevel@tonic-gate 	setmsg(defmesg, fmt);
297*7c478bd9Sstevel@tonic-gate 	puthelp(stdout, defmesg, help);
298*7c478bd9Sstevel@tonic-gate 	return (0);
299*7c478bd9Sstevel@tonic-gate }
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate /*
302*7c478bd9Sstevel@tonic-gate  *	A little state machine that checks out the format to
303*7c478bd9Sstevel@tonic-gate  *	make sure it is acceptable.
304*7c478bd9Sstevel@tonic-gate  *		return value 1: NG
305*7c478bd9Sstevel@tonic-gate  *		return value 0: OK
306*7c478bd9Sstevel@tonic-gate  */
307*7c478bd9Sstevel@tonic-gate int
308*7c478bd9Sstevel@tonic-gate fmtcheck(char *fmt)
309*7c478bd9Sstevel@tonic-gate {
310*7c478bd9Sstevel@tonic-gate 	int	percent = 0;
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 	while (*fmt) {
313*7c478bd9Sstevel@tonic-gate 		switch (*fmt++) {
314*7c478bd9Sstevel@tonic-gate 			case '%': /* previous state must be start or letter */
315*7c478bd9Sstevel@tonic-gate 				if (percent == 0)
316*7c478bd9Sstevel@tonic-gate 					percent = 1;
317*7c478bd9Sstevel@tonic-gate 				else
318*7c478bd9Sstevel@tonic-gate 					return (1);
319*7c478bd9Sstevel@tonic-gate 				break;
320*7c478bd9Sstevel@tonic-gate 			case 'H': /* previous state must be "%" */
321*7c478bd9Sstevel@tonic-gate 			case 'M':
322*7c478bd9Sstevel@tonic-gate 			case 'S':
323*7c478bd9Sstevel@tonic-gate 			case 'T':
324*7c478bd9Sstevel@tonic-gate 			case 'R':
325*7c478bd9Sstevel@tonic-gate 			case 'r':
326*7c478bd9Sstevel@tonic-gate 			case 'I':
327*7c478bd9Sstevel@tonic-gate 			case 'p':
328*7c478bd9Sstevel@tonic-gate 				if (percent == 1)
329*7c478bd9Sstevel@tonic-gate 					percent = 0;
330*7c478bd9Sstevel@tonic-gate 				else
331*7c478bd9Sstevel@tonic-gate 					return (1);
332*7c478bd9Sstevel@tonic-gate 				break;
333*7c478bd9Sstevel@tonic-gate 			case TAB: /* previous state must be start or letter */
334*7c478bd9Sstevel@tonic-gate 			case BLANK:
335*7c478bd9Sstevel@tonic-gate 			case DELIM1:
336*7c478bd9Sstevel@tonic-gate 				if (percent == 1)
337*7c478bd9Sstevel@tonic-gate 					return (1);
338*7c478bd9Sstevel@tonic-gate 				break;
339*7c478bd9Sstevel@tonic-gate 			default:
340*7c478bd9Sstevel@tonic-gate 				return (1);
341*7c478bd9Sstevel@tonic-gate 		}
342*7c478bd9Sstevel@tonic-gate 	}
343*7c478bd9Sstevel@tonic-gate 	return (percent);
344*7c478bd9Sstevel@tonic-gate }
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate int
347*7c478bd9Sstevel@tonic-gate cktime(char *tod, char *fmt, char *defstr, char *error, char *help,
348*7c478bd9Sstevel@tonic-gate 	char *prompt)
349*7c478bd9Sstevel@tonic-gate {
350*7c478bd9Sstevel@tonic-gate 	char	input[MAX_INPUT],
351*7c478bd9Sstevel@tonic-gate 		defmesg[128];
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate 	if ((fmt != NULL) && (fmtcheck(fmt) == 1))
354*7c478bd9Sstevel@tonic-gate 		return (4);
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 	if (fmt == NULL)
357*7c478bd9Sstevel@tonic-gate 		fmt = DEFAULT;
358*7c478bd9Sstevel@tonic-gate 	setmsg(defmesg, fmt);
359*7c478bd9Sstevel@tonic-gate 	if (!prompt)
360*7c478bd9Sstevel@tonic-gate 		prompt = "Enter a time of day";
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate start:
363*7c478bd9Sstevel@tonic-gate 	putprmpt(stderr, prompt, NULL, defstr);
364*7c478bd9Sstevel@tonic-gate 	if (getinput(input))
365*7c478bd9Sstevel@tonic-gate 		return (1);
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 	if (!strlen(input)) {
368*7c478bd9Sstevel@tonic-gate 		if (defstr) {
369*7c478bd9Sstevel@tonic-gate 			(void) strcpy(tod, defstr);
370*7c478bd9Sstevel@tonic-gate 			return (0);
371*7c478bd9Sstevel@tonic-gate 		}
372*7c478bd9Sstevel@tonic-gate 		puterror(stderr, defmesg, error);
373*7c478bd9Sstevel@tonic-gate 		goto start;
374*7c478bd9Sstevel@tonic-gate 	}
375*7c478bd9Sstevel@tonic-gate 	if (strcmp(input, "?") == 0) {
376*7c478bd9Sstevel@tonic-gate 		puthelp(stderr, defmesg, help);
377*7c478bd9Sstevel@tonic-gate 		goto start;
378*7c478bd9Sstevel@tonic-gate 	}
379*7c478bd9Sstevel@tonic-gate 	if (ckquit && (strcmp(input, "q") == 0))
380*7c478bd9Sstevel@tonic-gate 		return (3);
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate 	if (cktime_val(fmt, input)) {
383*7c478bd9Sstevel@tonic-gate 		puterror(stderr, defmesg, error);
384*7c478bd9Sstevel@tonic-gate 		goto start;
385*7c478bd9Sstevel@tonic-gate 	}
386*7c478bd9Sstevel@tonic-gate 	(void) strcpy(tod, input);
387*7c478bd9Sstevel@tonic-gate 	return (0);
388*7c478bd9Sstevel@tonic-gate }
389