xref: /titanic_50/usr/src/cmd/valtools/ckdate.c (revision 9acbbeaf2a1ffe5c14b244867d427714fab43c5c)
1 /*
2  * CDDL HEADER START
3  *
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.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <stdio.h>
34 #include <string.h>
35 #include <signal.h>
36 #include <stdlib.h>
37 #include <locale.h>
38 #include <libintl.h>
39 #include <limits.h>
40 #include "usage.h"
41 #include "libadm.h"
42 
43 #define	BADPID	(-2)
44 
45 static char	*prog;
46 static char	*deflt = NULL, *prompt = NULL, *error = NULL, *help = NULL;
47 static int	signo = 0;
48 static int	kpid = BADPID;
49 static char	*fmt = NULL;
50 
51 static const char	vusage[] = "f";
52 static const char	husage[] = "fWh";
53 static const char	eusage[] = "fWe";
54 
55 #define	MYFMT "%s:ERROR:invalid format\n" \
56 	"valid format descriptors are:\n" \
57 	"\t%%b  #abbreviated month name\n" \
58 	"\t%%B  #full month name\n" \
59 	"\t%%d  #day of month (01-31)\n" \
60 	"\t%%D  #date as %%m/%%d/%%y or %%m-%%d-%%y (default)\n" \
61 	"\t%%e  #day of month (1-31)\n" \
62 	"\t%%m  #month of year (01-12)\n" \
63 	"\t%%y  #year within century (YY)\n" \
64 	"\t%%Y  #year as CCYY\n"
65 
66 static void
67 usage(void)
68 {
69 	switch (*prog) {
70 	default:
71 		(void) fprintf(stderr,
72 			gettext("usage: %s [options] [-f format]\n"), prog);
73 		(void) fprintf(stderr, gettext(OPTMESG));
74 		(void) fprintf(stderr, gettext(STDOPTS));
75 		break;
76 
77 	case 'v':
78 		(void) fprintf(stderr,
79 			gettext("usage: %s [-f format] input\n"), prog);
80 		break;
81 
82 	case 'h':
83 		(void) fprintf(stderr,
84 			gettext("usage: %s [options] [-f format]\n"), prog);
85 		(void) fprintf(stderr, gettext(OPTMESG));
86 		(void) fprintf(stderr,
87 			gettext("\t-W width\n\t-h help\n"));
88 		break;
89 
90 	case 'e':
91 		(void) fprintf(stderr,
92 			gettext("usage: %s [options] [-f format]\n"), prog);
93 		(void) fprintf(stderr, gettext(OPTMESG));
94 		(void) fprintf(stderr,
95 			gettext("\t-W width\n\t-h error\n"));
96 		break;
97 	}
98 	exit(1);
99 }
100 
101 /*
102  * Given argv[0], return a pointer to the basename of the program.
103  */
104 static char *
105 prog_name(char *arg0)
106 {
107 	char *str;
108 
109 	/* first strip trailing '/' characters (exec() allows these!) */
110 	str = arg0 + strlen(arg0);
111 	while (str > arg0 && *--str == '/')
112 		*str = '\0';
113 	if ((str = strrchr(arg0, '/')) != NULL)
114 		return (str + 1);
115 	return (arg0);
116 }
117 
118 int
119 main(int argc, char **argv)
120 {
121 	int	c, n;
122 	char	*date;
123 	size_t	len;
124 
125 	(void) setlocale(LC_ALL, "");
126 
127 #if	!defined(TEXT_DOMAIN)
128 #define	TEXT_DOMAIN	"SYS_TEST"
129 #endif
130 	(void) textdomain(TEXT_DOMAIN);
131 
132 	prog = prog_name(argv[0]);
133 
134 	while ((c = getopt(argc, argv, "f:d:p:e:h:k:s:QW:?")) != EOF) {
135 		/* check for invalid option */
136 		if ((*prog == 'v') && !strchr(vusage, c))
137 			usage();
138 		if ((*prog == 'e') && !strchr(eusage, c))
139 			usage();
140 		if ((*prog == 'h') && !strchr(husage, c))
141 			usage();
142 
143 		switch (c) {
144 		case 'Q':
145 			ckquit = 0;
146 			break;
147 
148 		case 'W':
149 			ckwidth = atoi(optarg);
150 			if (ckwidth < 0) {
151 				(void) fprintf(stderr,
152 		gettext("%s: ERROR: negative display width specified\n"),
153 					prog);
154 				exit(1);
155 			}
156 			break;
157 
158 		case 'f':
159 			fmt = optarg;
160 			break;
161 
162 		case 'd':
163 			deflt = optarg;
164 			break;
165 
166 		case 'p':
167 			prompt = optarg;
168 			break;
169 
170 		case 'e':
171 			error = optarg;
172 			break;
173 
174 		case 'h':
175 			help = optarg;
176 			break;
177 
178 		case 'k':
179 			kpid = atoi(optarg);
180 			break;
181 
182 		case 's':
183 			signo = atoi(optarg);
184 			break;
185 
186 		default:
187 			usage();
188 		}
189 	}
190 
191 	if (signo) {
192 		if (kpid == BADPID)
193 			usage();
194 	} else
195 		signo = SIGTERM;
196 
197 	if (*prog == 'v') {
198 		if (argc != (optind + 1))
199 			usage();
200 		n = (ckdate_val(fmt, argv[optind]));
201 		if (n == 4)
202 			(void) fprintf(stderr, gettext(MYFMT), prog);
203 		exit(n);
204 	}
205 
206 	if (optind != argc)
207 		usage();
208 
209 	if (*prog == 'e') {
210 		ckindent = 0;
211 		if (ckdate_err(fmt, error)) {
212 			(void) fprintf(stderr, gettext(MYFMT), prog);
213 			exit(4);
214 		} else
215 			exit(0);
216 
217 	} else if (*prog == 'h') {
218 		ckindent = 0;
219 		if (ckdate_hlp(fmt, help)) {
220 			(void) fprintf(stderr, gettext(MYFMT), prog);
221 			exit(4);
222 		} else
223 			exit(0);
224 
225 	}
226 
227 	if (deflt) {
228 		len = strlen(deflt) + 1;
229 		if (len < MAX_INPUT)
230 			len = MAX_INPUT;
231 	} else {
232 		len = MAX_INPUT;
233 	}
234 	date = (char *)malloc(len);
235 	if (!date) {
236 		(void) fprintf(stderr,
237 			gettext("Not enough memory\n"));
238 		exit(1);
239 	}
240 	n = ckdate(date, fmt, deflt, error, help, prompt);
241 	if (n == 3) {
242 		if (kpid > -2)
243 			(void) kill(kpid, signo);
244 		(void) puts("q");
245 	} else if (n == 0)
246 		(void) printf("%s", date);
247 	if (n == 4)
248 		(void) fprintf(stderr, gettext(MYFMT), prog);
249 	return (n);
250 }
251