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
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]
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
27 * Copyright (c) 1997,1998 by Sun Microsystems, Inc.
28 * All rights reserved.
31 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
40 #include <sys/types.h>
44 static int fmtcheck(char *);
46 #define PROMPT "Enter the time of day"
47 #define ERRMSG "Please enter the time of day. Format is"
48 #define DEFAULT "%H:%M"
63 setmsg(char *msg
, char *fmt
, size_t sz
)
67 (void) snprintf(msg
, sz
, "%s <%s>.", ERRMSG
, fmt
);
71 p_ndig(char *string
, int *value
)
79 for (ptr
= string
; *ptr
&& n
> 0; n
--, ptr
++) {
80 if (! isdigit((unsigned char)*ptr
))
82 accum
= (10 * accum
) + (*ptr
- '0');
91 p_time(char *string
, int llim
, int ulim
)
95 if (!(ptr
= p_ndig(string
, &begin
)))
97 if (begin
>= llim
&& begin
<= ulim
)
102 /* p_meridian will parse the string for the meridian - AM/PM or am/pm */
105 p_meridian(char *string
)
107 static char *middle
[] = { "AM", "PM", "am", "pm" };
114 (void) sscanf(string
, "%2s", mid
);
115 while (!(legit
) && (n
< 4)) {
116 if ((strncmp(mid
, middle
[n
], 2)) == 0)
117 legit
= 1; /* found legitimate string */
126 p_delim(char *string
, char dchoice
)
132 (void) sscanf(string
, "%1c", &dlm
);
133 return ((dlm
== dchoice
) ? string
+ 1 : NULL
);
137 cktime_val(char *fmt
, char *input
)
140 int valid
= 1; /* time of day string is valid for format */
142 if ((fmt
!= NULL
) && (fmtcheck(fmt
) == 1))
148 while (*fmt
&& valid
) {
152 input
= p_time(input
, LH
, UH
);
158 input
= p_time(input
, LM
, UM
);
164 input
= p_time(input
, LS
, US
);
170 input
= p_time(input
, LH
, UH
);
176 input
= p_delim(input
, DELIM1
);
181 input
= p_time(input
, LM
, UM
);
186 input
= p_delim(input
, DELIM1
);
191 input
= p_time(input
, LS
, US
);
197 input
= p_time(input
, LH
, UH
);
202 input
= p_delim(input
, DELIM1
);
207 input
= p_time(input
, LM
, UM
);
215 input
= p_time(input
, LH
, USH
);
220 input
= p_delim(input
, DELIM1
);
225 input
= p_time(input
, LM
, UM
);
230 input
= p_delim(input
, DELIM1
);
235 input
= p_time(input
, LS
, US
);
240 input
= p_delim(input
, BLANK
);
245 input
= p_meridian(input
);
251 input
= p_time(input
, LH
, USH
);
257 input
= p_meridian(input
);
263 (void) sscanf(input
++, "%1c", <rl
);
267 (void) sscanf(input
, "%1c", &dfl
);
273 if (!(*fmt
) && (input
) && (*input
))
276 return ((valid
== 0));
280 cktime_err(char *fmt
, char *error
)
284 if ((fmt
!= NULL
) && (fmtcheck(fmt
) == 1))
286 setmsg(defmesg
, fmt
, sizeof (defmesg
));
287 puterror(stdout
, defmesg
, error
);
292 cktime_hlp(char *fmt
, char *help
)
296 if ((fmt
!= NULL
) && (fmtcheck(fmt
) == 1))
298 setmsg(defmesg
, fmt
, sizeof (defmesg
));
299 puthelp(stdout
, defmesg
, help
);
304 * A little state machine that checks out the format to
305 * make sure it is acceptable.
316 case '%': /* previous state must be start or letter */
322 case 'H': /* previous state must be "%" */
335 case TAB
: /* previous state must be start or letter */
349 cktime(char *tod
, char *fmt
, char *defstr
, char *error
, char *help
,
352 char input
[MAX_INPUT
],
355 if ((fmt
!= NULL
) && (fmtcheck(fmt
) == 1))
360 setmsg(defmesg
, fmt
, sizeof (defmesg
));
362 prompt
= "Enter a time of day";
365 putprmpt(stderr
, prompt
, NULL
, defstr
);
369 if (!strlen(input
)) {
371 (void) strcpy(tod
, defstr
);
374 puterror(stderr
, defmesg
, error
);
377 if (strcmp(input
, "?") == 0) {
378 puthelp(stderr
, defmesg
, help
);
381 if (ckquit
&& (strcmp(input
, "q") == 0))
384 if (cktime_val(fmt
, input
)) {
385 puterror(stderr
, defmesg
, error
);
388 (void) strcpy(tod
, input
);