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