xref: /onnv-gate/usr/src/lib/libadm/common/ckdate.c (revision 13114:80cabdfb1113)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*13114SAjaykumar.Venkatesulu@Sun.COM  * Common Development and Distribution License (the "License").
6*13114SAjaykumar.Venkatesulu@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
220Sstevel@tonic-gate /*	  All Rights Reserved  	*/
230Sstevel@tonic-gate 
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
26*13114SAjaykumar.Venkatesulu@Sun.COM  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*LINTLIBRARY*/
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <ctype.h>
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <limits.h>
370Sstevel@tonic-gate #include "libadm.h"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate static int	fmtcheck(char *);
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #define	MSGSIZ	64
420Sstevel@tonic-gate #define	PROMPT	"Enter the date"
430Sstevel@tonic-gate #define	MESG	"Please enter a date"
440Sstevel@tonic-gate #define	DEFAULT	"%m/%d/%y"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static char	*p_ndigit(char *, int *, int);
470Sstevel@tonic-gate static char	*p_date(char *, int, int, int);
480Sstevel@tonic-gate static char	*p_eday(char *, int, int);
490Sstevel@tonic-gate static char	*p_dlm(char *, char);
500Sstevel@tonic-gate 
51*13114SAjaykumar.Venkatesulu@Sun.COM #define	MLIM 10
520Sstevel@tonic-gate #define	STDIG 2
530Sstevel@tonic-gate #define	LD2 10
540Sstevel@tonic-gate #define	LD 01
550Sstevel@tonic-gate #define	UD 31
560Sstevel@tonic-gate #define	LM 01
570Sstevel@tonic-gate #define	UM 12
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * All digits are valid for a YY year format
600Sstevel@tonic-gate  * 70-99 refer to the 20th Century
610Sstevel@tonic-gate  * 00-69 refer to the 21st Century
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate #define	LY 00
640Sstevel@tonic-gate #define	UY 99
650Sstevel@tonic-gate #define	LCY 1970
660Sstevel@tonic-gate #define	UCY 9999
670Sstevel@tonic-gate #define	CCYY 4
680Sstevel@tonic-gate #define	DELIM1 '/'
690Sstevel@tonic-gate #define	DELIM2 '-'
700Sstevel@tonic-gate #define	BLANK ' '
710Sstevel@tonic-gate #define	TAB '	'
720Sstevel@tonic-gate 
730Sstevel@tonic-gate static void
setmsg(char * msg,char * fmt)740Sstevel@tonic-gate setmsg(char *msg, char *fmt)
750Sstevel@tonic-gate {
760Sstevel@tonic-gate 	if ((fmt == NULL) || strcmp(fmt, "%D") == 0)
770Sstevel@tonic-gate 		fmt = "%m/%d/%y";
780Sstevel@tonic-gate 	(void) sprintf(msg, "%s. Format is <%s>.", MESG, fmt);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static char *
p_ndigit(char * string,int * value,int n)820Sstevel@tonic-gate p_ndigit(char *string, int *value, int n)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	char *ptr;
850Sstevel@tonic-gate 	int accum = 0;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	if (!string)
880Sstevel@tonic-gate 		return (NULL);
890Sstevel@tonic-gate 	for (ptr = string; *ptr && n > 0; n--, ptr++) {
900Sstevel@tonic-gate 		if (! isdigit((unsigned char)*ptr))
910Sstevel@tonic-gate 			return (NULL);
920Sstevel@tonic-gate 		accum = (10 * accum) + (*ptr - '0');
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 	if (n)
950Sstevel@tonic-gate 		return (NULL);
960Sstevel@tonic-gate 	*value = accum;
970Sstevel@tonic-gate 	return (ptr);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate static char *
p_date(char * string,int llim,int ulim,int ndig)1010Sstevel@tonic-gate p_date(char *string, int llim, int ulim, int ndig)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate 	char *ptr;
1040Sstevel@tonic-gate 	int begin = -1;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	if (!(ptr = p_ndigit(string, &begin, ndig)))
1070Sstevel@tonic-gate 		return (NULL);
1080Sstevel@tonic-gate 	if (begin >= llim && begin <= ulim)
1090Sstevel@tonic-gate 		return (ptr);
1100Sstevel@tonic-gate 	else
1110Sstevel@tonic-gate 		return (NULL);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate static char *
p_eday(char * string,int llim,int ulim)1150Sstevel@tonic-gate p_eday(char *string, int llim, int ulim)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate 	char *ptr, *copy;
1180Sstevel@tonic-gate 	char daynum[3];
1190Sstevel@tonic-gate 	int begin = -1;
1200Sstevel@tonic-gate 	int iday = 0;
1210Sstevel@tonic-gate 	int idaymax = 2;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	daynum[0] = '\0';
1240Sstevel@tonic-gate 	if (*string == BLANK) {
1250Sstevel@tonic-gate 		string++;
1260Sstevel@tonic-gate 		idaymax--;
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate 	copy = string;
1290Sstevel@tonic-gate 	while (isdigit((unsigned char)*copy) && (iday < idaymax)) {
1300Sstevel@tonic-gate 		daynum[iday] = *copy++;
1310Sstevel@tonic-gate 		iday++;
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 	daynum[iday] = '\0';
1340Sstevel@tonic-gate 	if (iday == 1) {
1350Sstevel@tonic-gate 		llim = 1;
1360Sstevel@tonic-gate 		ulim = 9;
1370Sstevel@tonic-gate 	} else if (iday == 2) {
1380Sstevel@tonic-gate 		llim = 10;
1390Sstevel@tonic-gate 		ulim = 31;
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 	if (iday == 0)
1420Sstevel@tonic-gate 		return (NULL);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (!(ptr = p_ndigit(string, &begin, iday)))
1450Sstevel@tonic-gate 		return (NULL);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if (begin >= llim && begin <= ulim)
1480Sstevel@tonic-gate 		return (ptr);
1490Sstevel@tonic-gate 	else
1500Sstevel@tonic-gate 		return (NULL);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate /* p_month will parse the string for the month - abbr. form i.e. JAN - DEC */
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate static char *
p_month(char * string,char mnabr)1560Sstevel@tonic-gate p_month(char *string, char mnabr)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	static char *fmonth[] = {
1590Sstevel@tonic-gate 		    "JANUARY", "FEBRUARY", "MARCH", "APRIL",
1600Sstevel@tonic-gate 		    "MAY", "JUNE", "JULY", "AUGUST",
1610Sstevel@tonic-gate 		    "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"
1620Sstevel@tonic-gate 	};
1630Sstevel@tonic-gate 	static char *amonth[] = {
1640Sstevel@tonic-gate 		    "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
1650Sstevel@tonic-gate 		    "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
1660Sstevel@tonic-gate 	};
1670Sstevel@tonic-gate 	int ichng, icnt;
1680Sstevel@tonic-gate 	char *mnth[12];
1690Sstevel@tonic-gate 	char *copy;
1700Sstevel@tonic-gate 	char mletter[MLIM];
1710Sstevel@tonic-gate 	int mlen;
1720Sstevel@tonic-gate 	int imnth = 0;
1730Sstevel@tonic-gate 	int legit = 0;
1740Sstevel@tonic-gate 	int n = 0;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	if (mnabr == 'a') {
1770Sstevel@tonic-gate 		mlen = 3;
1780Sstevel@tonic-gate 		for (icnt = 0; icnt < 12; icnt++)
1790Sstevel@tonic-gate 			mnth[icnt] = amonth[icnt];
1800Sstevel@tonic-gate 	} else {
1810Sstevel@tonic-gate 		mlen = 9;
1820Sstevel@tonic-gate 		for (icnt = 0; icnt < 12; icnt++)
1830Sstevel@tonic-gate 			mnth[icnt] = fmonth[icnt];
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	copy = string;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	while (((islower((unsigned char)*copy)) ||
189*13114SAjaykumar.Venkatesulu@Sun.COM 	    (isupper((unsigned char)*copy))) && (imnth < mlen)) {
1900Sstevel@tonic-gate 		mletter[imnth] = toupper((unsigned char)*copy++);
1910Sstevel@tonic-gate 		imnth++;
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate 	mletter[imnth] = '\0';
1940Sstevel@tonic-gate 	while (!(legit) && (n < 12)) {
1950Sstevel@tonic-gate 		if (strncmp(mletter, mnth[n],
1960Sstevel@tonic-gate 		    (imnth = (int)strlen(mnth[n]))) == 0)
1970Sstevel@tonic-gate 			legit = 1;	/* found legitimate string */
1980Sstevel@tonic-gate 		n++;
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 	if (legit) {
2010Sstevel@tonic-gate 		for (ichng = 0; ichng < imnth; ichng++) {
2020Sstevel@tonic-gate 			*string = toupper((unsigned char)*string);
2030Sstevel@tonic-gate 			string++;
2040Sstevel@tonic-gate 		}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 		return (string);
2070Sstevel@tonic-gate 		/*
2080Sstevel@tonic-gate 		 * I know this causes side effects, but it's less
2090Sstevel@tonic-gate 		 * code  than adding in a copy for string and using that
2100Sstevel@tonic-gate 		 */
2110Sstevel@tonic-gate 	} else
2120Sstevel@tonic-gate 		return (NULL);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate static char *
p_dlm(char * string,char dchoice)2160Sstevel@tonic-gate p_dlm(char *string, char dchoice)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate 	char dlm;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	if (! string)
2220Sstevel@tonic-gate 		return (NULL);
2230Sstevel@tonic-gate 	(void) sscanf(string, "%1c", &dlm);
2240Sstevel@tonic-gate 	if (dchoice == '/')
2250Sstevel@tonic-gate 		return (((dlm == DELIM1) || (dlm == DELIM2)) ? string+1 : NULL);
2260Sstevel@tonic-gate 	else
2270Sstevel@tonic-gate 		return ((dlm == dchoice) ? string + 1 : NULL);
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate int
ckdate_err(char * fmt,char * error)2310Sstevel@tonic-gate ckdate_err(char	*fmt, char *error)
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate 	char	defmesg[MSGSIZ];
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	if ((fmt != NULL) && (fmtcheck(fmt) == 1))
2360Sstevel@tonic-gate 		return (4);
2370Sstevel@tonic-gate 	setmsg(defmesg, fmt);
2380Sstevel@tonic-gate 	puterror(stdout, defmesg, error);
2390Sstevel@tonic-gate 	return (0);
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate int
ckdate_hlp(char * fmt,char * help)2430Sstevel@tonic-gate ckdate_hlp(char *fmt, char *help)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate 	char	defmesg[MSGSIZ];
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	if ((fmt != NULL) && (fmtcheck(fmt) == 1))
2480Sstevel@tonic-gate 		return (4);
2490Sstevel@tonic-gate 	setmsg(defmesg, fmt);
2500Sstevel@tonic-gate 	puthelp(stdout, defmesg, help);
2510Sstevel@tonic-gate 	return (0);
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate /*
2550Sstevel@tonic-gate  *	A little state machine that checks out the format to
2560Sstevel@tonic-gate  *	make sure it is acceptable.
2570Sstevel@tonic-gate  *		return value 1: NG
2580Sstevel@tonic-gate  *		return value 0: OK
2590Sstevel@tonic-gate  */
2600Sstevel@tonic-gate static int
fmtcheck(char * fmt)2610Sstevel@tonic-gate fmtcheck(char *fmt)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate 	int	percent = 0;
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	while (*fmt) {
2660Sstevel@tonic-gate 		switch (*fmt++) {
2670Sstevel@tonic-gate 			case '%': /* previous state must be start or letter */
2680Sstevel@tonic-gate 				if (percent == 0)
2690Sstevel@tonic-gate 					percent = 1;
2700Sstevel@tonic-gate 				else
2710Sstevel@tonic-gate 					return (1);
2720Sstevel@tonic-gate 				break;
2730Sstevel@tonic-gate 			case 'd': /* previous state must be "%" */
2740Sstevel@tonic-gate 			case 'e':
2750Sstevel@tonic-gate 			case 'm':
2760Sstevel@tonic-gate 			case 'y':
2770Sstevel@tonic-gate 			case 'Y':
2780Sstevel@tonic-gate 			case 'D':
2790Sstevel@tonic-gate 			case 'h':
2800Sstevel@tonic-gate 			case 'b':
2810Sstevel@tonic-gate 			case 'B':
2820Sstevel@tonic-gate 				if (percent == 1)
2830Sstevel@tonic-gate 					percent = 0;
2840Sstevel@tonic-gate 				else
2850Sstevel@tonic-gate 					return (1);
2860Sstevel@tonic-gate 				break;
2870Sstevel@tonic-gate 			case TAB: /* previous state must be start or letter */
2880Sstevel@tonic-gate 			case BLANK:
2890Sstevel@tonic-gate 			case DELIM1:
2900Sstevel@tonic-gate 			case DELIM2:
2910Sstevel@tonic-gate 				if (percent == 1)
2920Sstevel@tonic-gate 					return (1);
2930Sstevel@tonic-gate 				break;
2940Sstevel@tonic-gate 			default:
2950Sstevel@tonic-gate 				return (1);
2960Sstevel@tonic-gate 		}
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 	return (percent);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate int
ckdate_val(char * fmt,char * input)3020Sstevel@tonic-gate ckdate_val(char *fmt, char *input)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	char ltrl, dfl;
3050Sstevel@tonic-gate 	int valid = 1; 	/* time of day string is valid for format */
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	if ((fmt != NULL) && (fmtcheck(fmt) == 1))
3080Sstevel@tonic-gate 		return (4);
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	if (fmt == NULL)
3110Sstevel@tonic-gate 		fmt = DEFAULT;
3120Sstevel@tonic-gate 	ltrl = '\0';
3130Sstevel@tonic-gate 	while (*fmt && valid) {
3140Sstevel@tonic-gate 		if ((*fmt) == '%') {
3150Sstevel@tonic-gate 			fmt++;
3160Sstevel@tonic-gate 			switch (*fmt) {
317*13114SAjaykumar.Venkatesulu@Sun.COM 				case 'd':
318*13114SAjaykumar.Venkatesulu@Sun.COM 					input = p_date(input, LD, UD, STDIG);
319*13114SAjaykumar.Venkatesulu@Sun.COM 					if (!input)
320*13114SAjaykumar.Venkatesulu@Sun.COM 						valid = 0;
321*13114SAjaykumar.Venkatesulu@Sun.COM 					break;
3220Sstevel@tonic-gate 
323*13114SAjaykumar.Venkatesulu@Sun.COM 				case 'e':
324*13114SAjaykumar.Venkatesulu@Sun.COM 					input = p_eday(input, LD2, UD);
325*13114SAjaykumar.Venkatesulu@Sun.COM 					if (!input)
326*13114SAjaykumar.Venkatesulu@Sun.COM 						valid = 0;
327*13114SAjaykumar.Venkatesulu@Sun.COM 					break;
3280Sstevel@tonic-gate 
329*13114SAjaykumar.Venkatesulu@Sun.COM 				case 'm':
330*13114SAjaykumar.Venkatesulu@Sun.COM 					input = p_date(input, LM, UM, STDIG);
331*13114SAjaykumar.Venkatesulu@Sun.COM 					if (!input)
332*13114SAjaykumar.Venkatesulu@Sun.COM 						valid = 0;
333*13114SAjaykumar.Venkatesulu@Sun.COM 					break;
3340Sstevel@tonic-gate 
335*13114SAjaykumar.Venkatesulu@Sun.COM 				case 'y':
336*13114SAjaykumar.Venkatesulu@Sun.COM 					input = p_date(input, LY, UY, STDIG);
337*13114SAjaykumar.Venkatesulu@Sun.COM 					if (!input)
338*13114SAjaykumar.Venkatesulu@Sun.COM 						valid = 0;
339*13114SAjaykumar.Venkatesulu@Sun.COM 					break;
3400Sstevel@tonic-gate 
341*13114SAjaykumar.Venkatesulu@Sun.COM 				case 'Y':
342*13114SAjaykumar.Venkatesulu@Sun.COM 					input = p_date(input, LCY, UCY, CCYY);
343*13114SAjaykumar.Venkatesulu@Sun.COM 					if (!input)
344*13114SAjaykumar.Venkatesulu@Sun.COM 						valid = 0;
345*13114SAjaykumar.Venkatesulu@Sun.COM 					break;
3460Sstevel@tonic-gate 
347*13114SAjaykumar.Venkatesulu@Sun.COM 				case 'D':
348*13114SAjaykumar.Venkatesulu@Sun.COM 					input = p_date(input, LM, UM, STDIG);
349*13114SAjaykumar.Venkatesulu@Sun.COM 					if (!input) {
350*13114SAjaykumar.Venkatesulu@Sun.COM 						valid = 0;
3510Sstevel@tonic-gate 					break;
3520Sstevel@tonic-gate 				}
3530Sstevel@tonic-gate 				input = p_dlm(input, DELIM1);
3540Sstevel@tonic-gate 				if (!input) {
3550Sstevel@tonic-gate 					valid = 0;
3560Sstevel@tonic-gate 					break;
3570Sstevel@tonic-gate 				}
3580Sstevel@tonic-gate 				input = p_date(input, LD, UD, STDIG);
3590Sstevel@tonic-gate 				if (!input) {
3600Sstevel@tonic-gate 					valid = 0;
3610Sstevel@tonic-gate 					break;
3620Sstevel@tonic-gate 				}
3630Sstevel@tonic-gate 				input = p_dlm(input, DELIM1);
3640Sstevel@tonic-gate 				if (!input) {
3650Sstevel@tonic-gate 					valid = 0;
3660Sstevel@tonic-gate 					break;
3670Sstevel@tonic-gate 				}
3680Sstevel@tonic-gate 				input = p_date(input, LY, UY, STDIG);
3690Sstevel@tonic-gate 				if (!input)
3700Sstevel@tonic-gate 					valid = 0;
3710Sstevel@tonic-gate 				break;
3720Sstevel@tonic-gate 
373*13114SAjaykumar.Venkatesulu@Sun.COM 				case 'h':
374*13114SAjaykumar.Venkatesulu@Sun.COM 				case 'b':
375*13114SAjaykumar.Venkatesulu@Sun.COM 					input = p_month(input, 'a');
376*13114SAjaykumar.Venkatesulu@Sun.COM 					if (!input)
377*13114SAjaykumar.Venkatesulu@Sun.COM 						valid = 0;
378*13114SAjaykumar.Venkatesulu@Sun.COM 					break;
3790Sstevel@tonic-gate 
380*13114SAjaykumar.Venkatesulu@Sun.COM 				case 'B':
381*13114SAjaykumar.Venkatesulu@Sun.COM 					input = p_month(input, 'f');
382*13114SAjaykumar.Venkatesulu@Sun.COM 					if (!input)
383*13114SAjaykumar.Venkatesulu@Sun.COM 						valid = 0;
384*13114SAjaykumar.Venkatesulu@Sun.COM 					break;
3850Sstevel@tonic-gate 
386*13114SAjaykumar.Venkatesulu@Sun.COM 				default:
387*13114SAjaykumar.Venkatesulu@Sun.COM 					(void) sscanf(input, "%1c", &ltrl);
388*13114SAjaykumar.Venkatesulu@Sun.COM 					input++;
3890Sstevel@tonic-gate 			}
3900Sstevel@tonic-gate 		} else {
3910Sstevel@tonic-gate 			dfl = '\0';
3920Sstevel@tonic-gate 			(void) sscanf(input, "%1c", &dfl);
3930Sstevel@tonic-gate 			input++;
3940Sstevel@tonic-gate 		}
3950Sstevel@tonic-gate 		fmt++;
3960Sstevel@tonic-gate 	}	 /* end of while fmt and valid */
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	if ((*fmt == NULL) && ((input != NULL) && *input != 0)) {
3990Sstevel@tonic-gate 		if (*input != NULL)
4000Sstevel@tonic-gate 			valid = 0;
4010Sstevel@tonic-gate 	}
4020Sstevel@tonic-gate 	return ((valid == 0));
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate int
ckdate(char * date,char * fmt,char * defstr,char * error,char * help,char * prompt)4060Sstevel@tonic-gate ckdate(char *date, char *fmt, char *defstr, char *error, char *help,
4070Sstevel@tonic-gate     char *prompt)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate 	char	defmesg[MSGSIZ];
4100Sstevel@tonic-gate 	char	input[MAX_INPUT];
4110Sstevel@tonic-gate 	char	*ept, end[128];
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	ept = end;
4140Sstevel@tonic-gate 	*ept = '\0';
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	if ((fmt != NULL) && (fmtcheck(fmt) == 1))
4170Sstevel@tonic-gate 		return (4);
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	setmsg(defmesg, fmt);
4200Sstevel@tonic-gate 	(void) sprintf(ept, "[?,q]");
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	if (!prompt)
4230Sstevel@tonic-gate 		prompt = PROMPT;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate start:
4260Sstevel@tonic-gate 	putprmpt(stderr, prompt, NULL, defstr);
4270Sstevel@tonic-gate 	if (getinput(input))
4280Sstevel@tonic-gate 		return (1);
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	if (!strlen(input)) {
4310Sstevel@tonic-gate 		if (defstr) {
4320Sstevel@tonic-gate 			(void) strcpy(date, defstr);
4330Sstevel@tonic-gate 			return (0);
4340Sstevel@tonic-gate 		}
4350Sstevel@tonic-gate 		puterror(stderr, defmesg, error);
4360Sstevel@tonic-gate 		goto start;
4370Sstevel@tonic-gate 	} else if (strcmp(input, "?") == 0) {
4380Sstevel@tonic-gate 		puthelp(stderr, defmesg, help);
4390Sstevel@tonic-gate 		goto start;
4400Sstevel@tonic-gate 	} else if (ckquit && strcmp(input, "q") == 0) {
4410Sstevel@tonic-gate 		return (3);
4420Sstevel@tonic-gate 	} else if (ckdate_val(fmt, input)) {
4430Sstevel@tonic-gate 		puterror(stderr, defmesg, error);
4440Sstevel@tonic-gate 		goto start;
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate 	(void) strcpy(date, input);
4470Sstevel@tonic-gate 	return (0);
4480Sstevel@tonic-gate }
449