xref: /illumos-gate/usr/src/lib/libadm/common/ckdate.c (revision f21450b8)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
559042621SAjaykumar Venkatesulu  * Common Development and Distribution License (the "License").
659042621SAjaykumar Venkatesulu  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
22*f21450b8SToomas Soome /*	  All Rights Reserved	*/
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
2659042621SAjaykumar Venkatesulu  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <ctype.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <limits.h>
377c478bd9Sstevel@tonic-gate #include "libadm.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static int	fmtcheck(char *);
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define	MSGSIZ	64
427c478bd9Sstevel@tonic-gate #define	PROMPT	"Enter the date"
437c478bd9Sstevel@tonic-gate #define	MESG	"Please enter a date"
447c478bd9Sstevel@tonic-gate #define	DEFAULT	"%m/%d/%y"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static char	*p_ndigit(char *, int *, int);
477c478bd9Sstevel@tonic-gate static char	*p_date(char *, int, int, int);
487c478bd9Sstevel@tonic-gate static char	*p_eday(char *, int, int);
497c478bd9Sstevel@tonic-gate static char	*p_dlm(char *, char);
507c478bd9Sstevel@tonic-gate 
5159042621SAjaykumar Venkatesulu #define	MLIM 10
527c478bd9Sstevel@tonic-gate #define	STDIG 2
537c478bd9Sstevel@tonic-gate #define	LD2 10
547c478bd9Sstevel@tonic-gate #define	LD 01
557c478bd9Sstevel@tonic-gate #define	UD 31
567c478bd9Sstevel@tonic-gate #define	LM 01
577c478bd9Sstevel@tonic-gate #define	UM 12
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * All digits are valid for a YY year format
607c478bd9Sstevel@tonic-gate  * 70-99 refer to the 20th Century
617c478bd9Sstevel@tonic-gate  * 00-69 refer to the 21st Century
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate #define	LY 00
647c478bd9Sstevel@tonic-gate #define	UY 99
657c478bd9Sstevel@tonic-gate #define	LCY 1970
667c478bd9Sstevel@tonic-gate #define	UCY 9999
677c478bd9Sstevel@tonic-gate #define	CCYY 4
687c478bd9Sstevel@tonic-gate #define	DELIM1 '/'
697c478bd9Sstevel@tonic-gate #define	DELIM2 '-'
707c478bd9Sstevel@tonic-gate #define	BLANK ' '
717c478bd9Sstevel@tonic-gate #define	TAB '	'
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static void
setmsg(char * msg,char * fmt,size_t sz)744656d474SGarrett D'Amore setmsg(char *msg, char *fmt, size_t sz)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	if ((fmt == NULL) || strcmp(fmt, "%D") == 0)
777c478bd9Sstevel@tonic-gate 		fmt = "%m/%d/%y";
784656d474SGarrett D'Amore 	(void) snprintf(msg, sz, "%s. Format is <%s>.", MESG, fmt);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static char *
p_ndigit(char * string,int * value,int n)827c478bd9Sstevel@tonic-gate p_ndigit(char *string, int *value, int n)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	char *ptr;
857c478bd9Sstevel@tonic-gate 	int accum = 0;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if (!string)
887c478bd9Sstevel@tonic-gate 		return (NULL);
897c478bd9Sstevel@tonic-gate 	for (ptr = string; *ptr && n > 0; n--, ptr++) {
907c478bd9Sstevel@tonic-gate 		if (! isdigit((unsigned char)*ptr))
917c478bd9Sstevel@tonic-gate 			return (NULL);
927c478bd9Sstevel@tonic-gate 		accum = (10 * accum) + (*ptr - '0');
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 	if (n)
957c478bd9Sstevel@tonic-gate 		return (NULL);
967c478bd9Sstevel@tonic-gate 	*value = accum;
977c478bd9Sstevel@tonic-gate 	return (ptr);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static char *
p_date(char * string,int llim,int ulim,int ndig)1017c478bd9Sstevel@tonic-gate p_date(char *string, int llim, int ulim, int ndig)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	char *ptr;
1047c478bd9Sstevel@tonic-gate 	int begin = -1;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if (!(ptr = p_ndigit(string, &begin, ndig)))
1077c478bd9Sstevel@tonic-gate 		return (NULL);
1087c478bd9Sstevel@tonic-gate 	if (begin >= llim && begin <= ulim)
1097c478bd9Sstevel@tonic-gate 		return (ptr);
1107c478bd9Sstevel@tonic-gate 	else
1117c478bd9Sstevel@tonic-gate 		return (NULL);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static char *
p_eday(char * string,int llim,int ulim)1157c478bd9Sstevel@tonic-gate p_eday(char *string, int llim, int ulim)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	char *ptr, *copy;
1187c478bd9Sstevel@tonic-gate 	int begin = -1;
1197c478bd9Sstevel@tonic-gate 	int iday = 0;
1207c478bd9Sstevel@tonic-gate 	int idaymax = 2;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (*string == BLANK) {
1237c478bd9Sstevel@tonic-gate 		string++;
1247c478bd9Sstevel@tonic-gate 		idaymax--;
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 	copy = string;
1277c478bd9Sstevel@tonic-gate 	while (isdigit((unsigned char)*copy) && (iday < idaymax)) {
1281d2945e8SToomas Soome 		copy++;
1297c478bd9Sstevel@tonic-gate 		iday++;
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 	if (iday == 1) {
1327c478bd9Sstevel@tonic-gate 		llim = 1;
1337c478bd9Sstevel@tonic-gate 		ulim = 9;
1347c478bd9Sstevel@tonic-gate 	} else if (iday == 2) {
1357c478bd9Sstevel@tonic-gate 		llim = 10;
1367c478bd9Sstevel@tonic-gate 		ulim = 31;
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 	if (iday == 0)
1397c478bd9Sstevel@tonic-gate 		return (NULL);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if (!(ptr = p_ndigit(string, &begin, iday)))
1427c478bd9Sstevel@tonic-gate 		return (NULL);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (begin >= llim && begin <= ulim)
1457c478bd9Sstevel@tonic-gate 		return (ptr);
1467c478bd9Sstevel@tonic-gate 	else
1477c478bd9Sstevel@tonic-gate 		return (NULL);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /* p_month will parse the string for the month - abbr. form i.e. JAN - DEC */
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static char *
p_month(char * string,char mnabr)1537c478bd9Sstevel@tonic-gate p_month(char *string, char mnabr)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	static char *fmonth[] = {
1567c478bd9Sstevel@tonic-gate 		    "JANUARY", "FEBRUARY", "MARCH", "APRIL",
1577c478bd9Sstevel@tonic-gate 		    "MAY", "JUNE", "JULY", "AUGUST",
1587c478bd9Sstevel@tonic-gate 		    "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"
1597c478bd9Sstevel@tonic-gate 	};
1607c478bd9Sstevel@tonic-gate 	static char *amonth[] = {
1617c478bd9Sstevel@tonic-gate 		    "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
1627c478bd9Sstevel@tonic-gate 		    "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
1637c478bd9Sstevel@tonic-gate 	};
1647c478bd9Sstevel@tonic-gate 	int ichng, icnt;
1657c478bd9Sstevel@tonic-gate 	char *mnth[12];
1667c478bd9Sstevel@tonic-gate 	char *copy;
1677c478bd9Sstevel@tonic-gate 	char mletter[MLIM];
1687c478bd9Sstevel@tonic-gate 	int mlen;
1697c478bd9Sstevel@tonic-gate 	int imnth = 0;
1707c478bd9Sstevel@tonic-gate 	int legit = 0;
1717c478bd9Sstevel@tonic-gate 	int n = 0;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (mnabr == 'a') {
1747c478bd9Sstevel@tonic-gate 		mlen = 3;
1757c478bd9Sstevel@tonic-gate 		for (icnt = 0; icnt < 12; icnt++)
1767c478bd9Sstevel@tonic-gate 			mnth[icnt] = amonth[icnt];
1777c478bd9Sstevel@tonic-gate 	} else {
1787c478bd9Sstevel@tonic-gate 		mlen = 9;
1797c478bd9Sstevel@tonic-gate 		for (icnt = 0; icnt < 12; icnt++)
1807c478bd9Sstevel@tonic-gate 			mnth[icnt] = fmonth[icnt];
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	copy = string;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	while (((islower((unsigned char)*copy)) ||
18659042621SAjaykumar Venkatesulu 	    (isupper((unsigned char)*copy))) && (imnth < mlen)) {
1877c478bd9Sstevel@tonic-gate 		mletter[imnth] = toupper((unsigned char)*copy++);
1887c478bd9Sstevel@tonic-gate 		imnth++;
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 	mletter[imnth] = '\0';
1917c478bd9Sstevel@tonic-gate 	while (!(legit) && (n < 12)) {
1927c478bd9Sstevel@tonic-gate 		if (strncmp(mletter, mnth[n],
1937c478bd9Sstevel@tonic-gate 		    (imnth = (int)strlen(mnth[n]))) == 0)
1947c478bd9Sstevel@tonic-gate 			legit = 1;	/* found legitimate string */
1957c478bd9Sstevel@tonic-gate 		n++;
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 	if (legit) {
1987c478bd9Sstevel@tonic-gate 		for (ichng = 0; ichng < imnth; ichng++) {
1997c478bd9Sstevel@tonic-gate 			*string = toupper((unsigned char)*string);
2007c478bd9Sstevel@tonic-gate 			string++;
2017c478bd9Sstevel@tonic-gate 		}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		return (string);
2047c478bd9Sstevel@tonic-gate 		/*
2057c478bd9Sstevel@tonic-gate 		 * I know this causes side effects, but it's less
2067c478bd9Sstevel@tonic-gate 		 * code  than adding in a copy for string and using that
2077c478bd9Sstevel@tonic-gate 		 */
2087c478bd9Sstevel@tonic-gate 	} else
2097c478bd9Sstevel@tonic-gate 		return (NULL);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate static char *
p_dlm(char * string,char dchoice)2137c478bd9Sstevel@tonic-gate p_dlm(char *string, char dchoice)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	char dlm;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if (! string)
2197c478bd9Sstevel@tonic-gate 		return (NULL);
2207c478bd9Sstevel@tonic-gate 	(void) sscanf(string, "%1c", &dlm);
2217c478bd9Sstevel@tonic-gate 	if (dchoice == '/')
2227c478bd9Sstevel@tonic-gate 		return (((dlm == DELIM1) || (dlm == DELIM2)) ? string+1 : NULL);
2237c478bd9Sstevel@tonic-gate 	else
2247c478bd9Sstevel@tonic-gate 		return ((dlm == dchoice) ? string + 1 : NULL);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate int
ckdate_err(char * fmt,char * error)2287c478bd9Sstevel@tonic-gate ckdate_err(char	*fmt, char *error)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	char	defmesg[MSGSIZ];
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	if ((fmt != NULL) && (fmtcheck(fmt) == 1))
2337c478bd9Sstevel@tonic-gate 		return (4);
2344656d474SGarrett D'Amore 	setmsg(defmesg, fmt, MSGSIZ);
2357c478bd9Sstevel@tonic-gate 	puterror(stdout, defmesg, error);
2367c478bd9Sstevel@tonic-gate 	return (0);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate int
ckdate_hlp(char * fmt,char * help)2407c478bd9Sstevel@tonic-gate ckdate_hlp(char *fmt, char *help)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	char	defmesg[MSGSIZ];
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if ((fmt != NULL) && (fmtcheck(fmt) == 1))
2457c478bd9Sstevel@tonic-gate 		return (4);
2464656d474SGarrett D'Amore 	setmsg(defmesg, fmt, MSGSIZ);
2477c478bd9Sstevel@tonic-gate 	puthelp(stdout, defmesg, help);
2487c478bd9Sstevel@tonic-gate 	return (0);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  *	A little state machine that checks out the format to
2537c478bd9Sstevel@tonic-gate  *	make sure it is acceptable.
2547c478bd9Sstevel@tonic-gate  *		return value 1: NG
2557c478bd9Sstevel@tonic-gate  *		return value 0: OK
2567c478bd9Sstevel@tonic-gate  */
2577c478bd9Sstevel@tonic-gate static int
fmtcheck(char * fmt)2587c478bd9Sstevel@tonic-gate fmtcheck(char *fmt)
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate 	int	percent = 0;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	while (*fmt) {
2637c478bd9Sstevel@tonic-gate 		switch (*fmt++) {
2647c478bd9Sstevel@tonic-gate 			case '%': /* previous state must be start or letter */
2657c478bd9Sstevel@tonic-gate 				if (percent == 0)
2667c478bd9Sstevel@tonic-gate 					percent = 1;
2677c478bd9Sstevel@tonic-gate 				else
2687c478bd9Sstevel@tonic-gate 					return (1);
2697c478bd9Sstevel@tonic-gate 				break;
2707c478bd9Sstevel@tonic-gate 			case 'd': /* previous state must be "%" */
2717c478bd9Sstevel@tonic-gate 			case 'e':
2727c478bd9Sstevel@tonic-gate 			case 'm':
2737c478bd9Sstevel@tonic-gate 			case 'y':
2747c478bd9Sstevel@tonic-gate 			case 'Y':
2757c478bd9Sstevel@tonic-gate 			case 'D':
2767c478bd9Sstevel@tonic-gate 			case 'h':
2777c478bd9Sstevel@tonic-gate 			case 'b':
2787c478bd9Sstevel@tonic-gate 			case 'B':
2797c478bd9Sstevel@tonic-gate 				if (percent == 1)
2807c478bd9Sstevel@tonic-gate 					percent = 0;
2817c478bd9Sstevel@tonic-gate 				else
2827c478bd9Sstevel@tonic-gate 					return (1);
2837c478bd9Sstevel@tonic-gate 				break;
2847c478bd9Sstevel@tonic-gate 			case TAB: /* previous state must be start or letter */
2857c478bd9Sstevel@tonic-gate 			case BLANK:
2867c478bd9Sstevel@tonic-gate 			case DELIM1:
2877c478bd9Sstevel@tonic-gate 			case DELIM2:
2887c478bd9Sstevel@tonic-gate 				if (percent == 1)
2897c478bd9Sstevel@tonic-gate 					return (1);
2907c478bd9Sstevel@tonic-gate 				break;
2917c478bd9Sstevel@tonic-gate 			default:
2927c478bd9Sstevel@tonic-gate 				return (1);
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 	return (percent);
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate int
ckdate_val(char * fmt,char * input)2997c478bd9Sstevel@tonic-gate ckdate_val(char *fmt, char *input)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 	char ltrl, dfl;
302*f21450b8SToomas Soome 	int valid = 1;	/* time of day string is valid for format */
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	if ((fmt != NULL) && (fmtcheck(fmt) == 1))
3057c478bd9Sstevel@tonic-gate 		return (4);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	if (fmt == NULL)
3087c478bd9Sstevel@tonic-gate 		fmt = DEFAULT;
3097c478bd9Sstevel@tonic-gate 	ltrl = '\0';
3107c478bd9Sstevel@tonic-gate 	while (*fmt && valid) {
3117c478bd9Sstevel@tonic-gate 		if ((*fmt) == '%') {
3127c478bd9Sstevel@tonic-gate 			fmt++;
3137c478bd9Sstevel@tonic-gate 			switch (*fmt) {
31459042621SAjaykumar Venkatesulu 				case 'd':
31559042621SAjaykumar Venkatesulu 					input = p_date(input, LD, UD, STDIG);
31659042621SAjaykumar Venkatesulu 					if (!input)
31759042621SAjaykumar Venkatesulu 						valid = 0;
31859042621SAjaykumar Venkatesulu 					break;
3197c478bd9Sstevel@tonic-gate 
32059042621SAjaykumar Venkatesulu 				case 'e':
32159042621SAjaykumar Venkatesulu 					input = p_eday(input, LD2, UD);
32259042621SAjaykumar Venkatesulu 					if (!input)
32359042621SAjaykumar Venkatesulu 						valid = 0;
32459042621SAjaykumar Venkatesulu 					break;
3257c478bd9Sstevel@tonic-gate 
32659042621SAjaykumar Venkatesulu 				case 'm':
32759042621SAjaykumar Venkatesulu 					input = p_date(input, LM, UM, STDIG);
32859042621SAjaykumar Venkatesulu 					if (!input)
32959042621SAjaykumar Venkatesulu 						valid = 0;
33059042621SAjaykumar Venkatesulu 					break;
3317c478bd9Sstevel@tonic-gate 
33259042621SAjaykumar Venkatesulu 				case 'y':
33359042621SAjaykumar Venkatesulu 					input = p_date(input, LY, UY, STDIG);
33459042621SAjaykumar Venkatesulu 					if (!input)
33559042621SAjaykumar Venkatesulu 						valid = 0;
33659042621SAjaykumar Venkatesulu 					break;
3377c478bd9Sstevel@tonic-gate 
33859042621SAjaykumar Venkatesulu 				case 'Y':
33959042621SAjaykumar Venkatesulu 					input = p_date(input, LCY, UCY, CCYY);
34059042621SAjaykumar Venkatesulu 					if (!input)
34159042621SAjaykumar Venkatesulu 						valid = 0;
34259042621SAjaykumar Venkatesulu 					break;
3437c478bd9Sstevel@tonic-gate 
34459042621SAjaykumar Venkatesulu 				case 'D':
34559042621SAjaykumar Venkatesulu 					input = p_date(input, LM, UM, STDIG);
34659042621SAjaykumar Venkatesulu 					if (!input) {
34759042621SAjaykumar Venkatesulu 						valid = 0;
3487c478bd9Sstevel@tonic-gate 					break;
3497c478bd9Sstevel@tonic-gate 				}
3507c478bd9Sstevel@tonic-gate 				input = p_dlm(input, DELIM1);
3517c478bd9Sstevel@tonic-gate 				if (!input) {
3527c478bd9Sstevel@tonic-gate 					valid = 0;
3537c478bd9Sstevel@tonic-gate 					break;
3547c478bd9Sstevel@tonic-gate 				}
3557c478bd9Sstevel@tonic-gate 				input = p_date(input, LD, UD, STDIG);
3567c478bd9Sstevel@tonic-gate 				if (!input) {
3577c478bd9Sstevel@tonic-gate 					valid = 0;
3587c478bd9Sstevel@tonic-gate 					break;
3597c478bd9Sstevel@tonic-gate 				}
3607c478bd9Sstevel@tonic-gate 				input = p_dlm(input, DELIM1);
3617c478bd9Sstevel@tonic-gate 				if (!input) {
3627c478bd9Sstevel@tonic-gate 					valid = 0;
3637c478bd9Sstevel@tonic-gate 					break;
3647c478bd9Sstevel@tonic-gate 				}
3657c478bd9Sstevel@tonic-gate 				input = p_date(input, LY, UY, STDIG);
3667c478bd9Sstevel@tonic-gate 				if (!input)
3677c478bd9Sstevel@tonic-gate 					valid = 0;
3687c478bd9Sstevel@tonic-gate 				break;
3697c478bd9Sstevel@tonic-gate 
37059042621SAjaykumar Venkatesulu 				case 'h':
37159042621SAjaykumar Venkatesulu 				case 'b':
37259042621SAjaykumar Venkatesulu 					input = p_month(input, 'a');
37359042621SAjaykumar Venkatesulu 					if (!input)
37459042621SAjaykumar Venkatesulu 						valid = 0;
37559042621SAjaykumar Venkatesulu 					break;
3767c478bd9Sstevel@tonic-gate 
37759042621SAjaykumar Venkatesulu 				case 'B':
37859042621SAjaykumar Venkatesulu 					input = p_month(input, 'f');
37959042621SAjaykumar Venkatesulu 					if (!input)
38059042621SAjaykumar Venkatesulu 						valid = 0;
38159042621SAjaykumar Venkatesulu 					break;
3827c478bd9Sstevel@tonic-gate 
38359042621SAjaykumar Venkatesulu 				default:
38459042621SAjaykumar Venkatesulu 					(void) sscanf(input, "%1c", &ltrl);
38559042621SAjaykumar Venkatesulu 					input++;
3867c478bd9Sstevel@tonic-gate 			}
3877c478bd9Sstevel@tonic-gate 		} else {
3887c478bd9Sstevel@tonic-gate 			dfl = '\0';
3897c478bd9Sstevel@tonic-gate 			(void) sscanf(input, "%1c", &dfl);
3907c478bd9Sstevel@tonic-gate 			input++;
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 		fmt++;
3937c478bd9Sstevel@tonic-gate 	}	 /* end of while fmt and valid */
3947c478bd9Sstevel@tonic-gate 
395*f21450b8SToomas Soome 	if ((*fmt == '\0') && ((input != NULL) && *input != '\0')) {
396*f21450b8SToomas Soome 		valid = 0;
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 	return ((valid == 0));
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate int
ckdate(char * date,char * fmt,char * defstr,char * error,char * help,char * prompt)4027c478bd9Sstevel@tonic-gate ckdate(char *date, char *fmt, char *defstr, char *error, char *help,
4037c478bd9Sstevel@tonic-gate     char *prompt)
4047c478bd9Sstevel@tonic-gate {
4057c478bd9Sstevel@tonic-gate 	char	defmesg[MSGSIZ];
4067c478bd9Sstevel@tonic-gate 	char	input[MAX_INPUT];
4077c478bd9Sstevel@tonic-gate 	char	*ept, end[128];
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	ept = end;
4107c478bd9Sstevel@tonic-gate 	*ept = '\0';
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	if ((fmt != NULL) && (fmtcheck(fmt) == 1))
4137c478bd9Sstevel@tonic-gate 		return (4);
4147c478bd9Sstevel@tonic-gate 
4154656d474SGarrett D'Amore 	setmsg(defmesg, fmt, MSGSIZ);
4167c478bd9Sstevel@tonic-gate 	(void) sprintf(ept, "[?,q]");
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	if (!prompt)
4197c478bd9Sstevel@tonic-gate 		prompt = PROMPT;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate start:
4227c478bd9Sstevel@tonic-gate 	putprmpt(stderr, prompt, NULL, defstr);
4237c478bd9Sstevel@tonic-gate 	if (getinput(input))
4247c478bd9Sstevel@tonic-gate 		return (1);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if (!strlen(input)) {
4277c478bd9Sstevel@tonic-gate 		if (defstr) {
4287c478bd9Sstevel@tonic-gate 			(void) strcpy(date, defstr);
4297c478bd9Sstevel@tonic-gate 			return (0);
4307c478bd9Sstevel@tonic-gate 		}
4317c478bd9Sstevel@tonic-gate 		puterror(stderr, defmesg, error);
4327c478bd9Sstevel@tonic-gate 		goto start;
4337c478bd9Sstevel@tonic-gate 	} else if (strcmp(input, "?") == 0) {
4347c478bd9Sstevel@tonic-gate 		puthelp(stderr, defmesg, help);
4357c478bd9Sstevel@tonic-gate 		goto start;
4367c478bd9Sstevel@tonic-gate 	} else if (ckquit && strcmp(input, "q") == 0) {
4377c478bd9Sstevel@tonic-gate 		return (3);
4387c478bd9Sstevel@tonic-gate 	} else if (ckdate_val(fmt, input)) {
4397c478bd9Sstevel@tonic-gate 		puterror(stderr, defmesg, error);
4407c478bd9Sstevel@tonic-gate 		goto start;
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 	(void) strcpy(date, input);
4437c478bd9Sstevel@tonic-gate 	return (0);
4447c478bd9Sstevel@tonic-gate }
445