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]
23 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * date.c: support for the scadm date option (change/display service
39 #include <time.h> /* required by rsc.h */
44 static void ADM_Get_Date();
45 static void ADM_Set_Date(int argc
, char *argv
[]);
46 static int ADM_Decode_Date(char *String
, dp_set_date_time_t
*Date
);
47 static int ADM_twodigits(char *s
);
50 extern int cftime(char *, char *, const time_t *);
53 ADM_Process_date(int argc
, char *argv
[])
55 static dp_set_date_time_t DateTime
;
61 if ((argc
!= 2) && (argc
!= 3)) {
67 if (strcasecmp(argv
[2], "-s") != 0) {
68 if (ADM_Decode_Date(argv
[2], &DateTime
) != 0) {
79 } else if (argc
== 3) {
80 if (strcasecmp(argv
[2], "-s") == 0) {
81 currentTime
= time(NULL
);
82 (void) cftime(date
, "%m""%d""%H""%M""%Y", ¤tTime
);
87 ADM_Set_Date(largc
, largv
);
89 ADM_Set_Date(argc
, argv
);
99 struct timespec Timeout
;
100 dp_get_date_time_r_t
*dateInfo
;
104 Message
.type
= DP_GET_DATE_TIME
;
111 Timeout
.tv_sec
= ADM_TIMEOUT
;
112 ADM_Recv(&Message
, &Timeout
,
113 DP_GET_DATE_TIME_R
, sizeof (dp_get_date_time_r_t
));
115 if (*(int *)Message
.data
!= 0) {
116 (void) fprintf(stderr
, "\n%s\n\n",
117 gettext("scadm: could not read date from SC"));
121 dateInfo
= (dp_get_date_time_r_t
*)Message
.data
;
124 (void) setlocale(LC_ALL
, "");
125 tp
= gmtime((time_t *)&dateInfo
->current_datetime
);
126 (void) strftime(buf
, 64, nl_langinfo(D_T_FMT
), tp
);
127 (void) printf("%s\n", buf
);
134 ADM_Set_Date(int argc
, char *argv
[])
136 static dp_set_date_time_t specTime
;
138 struct timespec Timeout
;
141 /* should have caught this earlier */
142 (void) fprintf(stderr
, "\n%s\n\n",
143 gettext("scadm: arguments error in set date"));
147 if (ADM_Decode_Date(argv
[2], &specTime
) != 0) {
148 /* should have caught this earlier */
149 (void) fprintf(stderr
, "\n%s\n\n",
150 gettext("scadm: INTERNAL ERROR in set date"));
154 /* Correct month to be 0 - 11. Why does firmware want this? */
155 /* Correct year to be offset from 1900. Why does firmware want this? */
156 if (specTime
.month
!= DP_SET_DATE_TIME_IGNORE_FIELD
)
157 specTime
.month
= specTime
.month
- 1;
158 if (specTime
.year
!= DP_SET_DATE_TIME_IGNORE_FIELD
)
159 specTime
.year
= specTime
.year
- 1900;
161 Message
.type
= DP_SET_DATE_TIME
;
162 Message
.len
= sizeof (dp_set_date_time_t
);
163 Message
.data
= &specTime
;
167 Timeout
.tv_sec
= ADM_TIMEOUT
;
168 ADM_Recv(&Message
, &Timeout
,
169 DP_SET_DATE_TIME_R
, sizeof (dp_set_date_time_r_t
));
171 if (*(int *)Message
.data
!= 0) {
172 (void) fprintf(stderr
, "\n%s\n\n",
173 gettext("scadm: could not set date on SC"));
182 ADM_twodigits(char *s
)
186 n
= ((s
[0] - '0') * 10) + (s
[1] - '0');
192 ADM_Decode_Date(char *String
, dp_set_date_time_t
*Date
)
197 if (strlen(String
) == 4) {
200 Date
->month
= DP_SET_DATE_TIME_IGNORE_FIELD
;
201 Date
->day
= DP_SET_DATE_TIME_IGNORE_FIELD
;
202 Date
->hour
= ADM_twodigits(&String
[0]);
203 Date
->minute
= ADM_twodigits(&String
[2]);
204 Date
->second
= DP_SET_DATE_TIME_IGNORE_FIELD
;
205 Date
->year
= DP_SET_DATE_TIME_IGNORE_FIELD
;
206 if (((int)Date
->hour
< 0) || (Date
->hour
> 23))
208 if (((int)Date
->minute
< 0) || (Date
->minute
> 59))
211 } else if (strlen(String
) == 7) {
214 Date
->month
= DP_SET_DATE_TIME_IGNORE_FIELD
;
215 Date
->day
= DP_SET_DATE_TIME_IGNORE_FIELD
;
216 Date
->hour
= ADM_twodigits(&String
[0]);
217 Date
->minute
= ADM_twodigits(&String
[2]);
218 Date
->second
= ADM_twodigits(&String
[5]);
219 Date
->year
= DP_SET_DATE_TIME_IGNORE_FIELD
;
220 if (((int)Date
->hour
< 0) || (Date
->hour
> 23))
222 if (((int)Date
->minute
< 0) || (Date
->minute
> 59))
224 if (((int)Date
->second
< 0) || (Date
->second
> 59))
227 } else if (strlen(String
) == 8) {
230 Date
->month
= ADM_twodigits(&String
[0]);
231 Date
->day
= ADM_twodigits(&String
[2]);
232 Date
->hour
= ADM_twodigits(&String
[4]);
233 Date
->minute
= ADM_twodigits(&String
[6]);
234 Date
->second
= DP_SET_DATE_TIME_IGNORE_FIELD
;
235 Date
->year
= DP_SET_DATE_TIME_IGNORE_FIELD
;
236 if ((Date
->month
< 1) || (Date
->month
> 12))
238 if ((Date
->day
< 1) || (Date
->day
> 31))
240 if (((int)Date
->hour
< 0) || (Date
->hour
> 23))
242 if (((int)Date
->minute
< 0) || (Date
->minute
> 59))
245 } else if (strlen(String
) == 11) {
248 Date
->month
= ADM_twodigits(&String
[0]);
249 Date
->day
= ADM_twodigits(&String
[2]);
250 Date
->hour
= ADM_twodigits(&String
[4]);
251 Date
->minute
= ADM_twodigits(&String
[6]);
252 Date
->second
= ADM_twodigits(&String
[9]);
253 Date
->year
= DP_SET_DATE_TIME_IGNORE_FIELD
;
254 if ((Date
->month
< 1) || (Date
->month
> 12))
256 if ((Date
->day
< 1) || (Date
->day
> 31))
258 if (((int)Date
->hour
< 0) || (Date
->hour
> 23))
260 if (((int)Date
->minute
< 0) || (Date
->minute
> 59))
262 if (((int)Date
->second
< 0) || (Date
->second
> 59))
265 } else if (strlen(String
) == 10) {
268 Date
->month
= ADM_twodigits(&String
[0]);
269 Date
->day
= ADM_twodigits(&String
[2]);
270 Date
->hour
= ADM_twodigits(&String
[4]);
271 Date
->minute
= ADM_twodigits(&String
[6]);
272 Date
->second
= DP_SET_DATE_TIME_IGNORE_FIELD
;
273 localDate
= ADM_twodigits(&String
[8]);
275 Date
->year
= localDate
+ 1900;
277 Date
->year
= localDate
+ 2000;
279 if ((Date
->month
< 1) || (Date
->month
> 12))
281 if ((Date
->day
< 1) || (Date
->day
> 31))
283 if (((int)Date
->hour
< 0) || (Date
->hour
> 23))
285 if (((int)Date
->minute
< 0) || (Date
->minute
> 59))
287 if ((Date
->year
< 1970) || (Date
->year
> 2038))
290 } else if (strlen(String
) == 13) {
293 Date
->month
= ADM_twodigits(&String
[0]);
294 Date
->day
= ADM_twodigits(&String
[2]);
295 Date
->hour
= ADM_twodigits(&String
[4]);
296 Date
->minute
= ADM_twodigits(&String
[6]);
297 Date
->second
= ADM_twodigits(&String
[11]);
298 localDate
= ADM_twodigits(&String
[8]);
300 Date
->year
= localDate
+ 1900;
302 Date
->year
= localDate
+ 2000;
304 if ((Date
->month
< 1) || (Date
->month
> 12))
306 if ((Date
->day
< 1) || (Date
->day
> 31))
308 if (((int)Date
->hour
< 0) || (Date
->hour
> 23))
310 if (((int)Date
->minute
< 0) || (Date
->minute
> 59))
312 if ((Date
->year
< 1970) || (Date
->year
> 2038))
314 if (((int)Date
->second
< 0) || (Date
->second
> 59))
317 } else if (strlen(String
) == 12) {
320 Date
->month
= ADM_twodigits(&String
[0]);
321 Date
->day
= ADM_twodigits(&String
[2]);
322 Date
->hour
= ADM_twodigits(&String
[4]);
323 Date
->minute
= ADM_twodigits(&String
[6]);
324 Date
->second
= DP_SET_DATE_TIME_IGNORE_FIELD
;
325 Date
->year
= (ADM_twodigits(&String
[8]) * 100) +
326 ADM_twodigits(&String
[10]);
327 if ((Date
->month
< 1) || (Date
->month
> 12))
329 if ((Date
->day
< 1) || (Date
->day
> 31))
331 if (((int)Date
->hour
< 0) || (Date
->hour
> 23))
333 if (((int)Date
->minute
< 0) || (Date
->minute
> 59))
335 if ((Date
->year
< 1970) || (Date
->year
> 2038))
338 } else if (strlen(String
) == 15) {
340 /* mmddHHMMccyy.SS */
341 Date
->month
= ADM_twodigits(&String
[0]);
342 Date
->day
= ADM_twodigits(&String
[2]);
343 Date
->hour
= ADM_twodigits(&String
[4]);
344 Date
->minute
= ADM_twodigits(&String
[6]);
345 Date
->second
= ADM_twodigits(&String
[13]);
346 Date
->year
= (ADM_twodigits(&String
[8]) * 100) +
347 ADM_twodigits(&String
[10]);
348 if ((Date
->month
< 1) || (Date
->month
> 12))
350 if ((Date
->day
< 1) || (Date
->day
> 31))
352 if (((int)Date
->hour
< 0) || (Date
->hour
> 23))
354 if (((int)Date
->minute
< 0) || (Date
->minute
> 59))
356 if ((Date
->year
< 1970) || (Date
->year
> 2038))
358 if (((int)Date
->second
< 0) || (Date
->second
> 59))
372 (void) fprintf(stderr
, "\n%s\n\n",
373 gettext("USAGE: scadm date [-s] "
374 "| [[mmdd]HHMM | mmddHHMM[cc]yy][.SS]\n"
379 " 1970 <= ccyy <= 2038\n"