Listtree.mcc: implement proxying of DisplayHook
[AROS.git] / workbench / c / Date.c
blobcefc069e772a2cd2e158d2ed49a1c3ffe0c95094
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Date CLI command
6 Lang: English
7 */
8 /******************************************************************************
10 NAME
12 Date [<day>] [<date>] [<time>] [TO | VER <filename>]
14 SYNOPSIS
16 DAY,DATE,TIME,TO=VER/K
18 LOCATION
22 FUNCTION
24 Displays or sets the system date and/or time.
26 INPUTS
28 DAY -- sets date by name (monday, tuesday, ... , tomorrow, yesterday)
29 DATE -- sets date in format DD-MMM-YY.
30 For MMM either the number or the first 3 letters of the
31 month in English
32 TIME -- sets time in format HH:MM:SS
33 TO -- output is sent to file
35 RESULT
37 NOTES
39 EXAMPLE
41 date 2-feb-06
42 date 21:10
44 BUGS
46 SEE ALSO
48 INTERNALS
50 HISTORY
52 ******************************************************************************/
54 #include <proto/exec.h>
55 #include <exec/errors.h>
56 #include <exec/io.h>
57 #include <proto/dos.h>
58 #include <dos/dos.h>
59 #include <dos/datetime.h>
60 #include <devices/timer.h>
62 #include <string.h>
64 const TEXT version[] = "$VER: Date 41.4 (15.11.2011)\n";
66 #define ARG_STRING "DAY,DATE,TIME,TO=VER/K"
67 #define ARG_DAY 0
68 #define ARG_DATE 1
69 #define ARG_TIME 2
70 #define ARG_VER 3
71 #define ARG_COUNT 4
73 static WORD chrcount(STRPTR s, UBYTE c)
75 UBYTE sc;
76 WORD retval = 0;
78 while((sc = *s++))
80 if (sc == c) retval++;
83 return retval;
86 int setdate(STRPTR *day_date_time)
88 int error = RETURN_OK;
89 BYTE timererror;
90 struct timerequest *timerReq;
91 struct MsgPort *timerMP;
92 struct DateTime dt;
93 WORD i, count;
94 UBYTE fulltime[9];
95 STRPTR realtime = NULL, realdate = NULL;
97 for(i = 0; i < 3; i++)
99 if (day_date_time[i] == NULL) continue;
101 if (chrcount(day_date_time[i], '-'))
103 /* must be date */
105 realdate = day_date_time[i];
107 else if ((count = chrcount(day_date_time[i], ':')))
109 /* must be time */
110 if (count == 1)
112 /* seconds are missing */
114 if (strlen(day_date_time[i]) <= 5)
116 strcpy(fulltime, day_date_time[i]);
117 strcat(fulltime, ":00");
118 realtime = fulltime;
120 else
122 realtime = day_date_time[i];
125 else
127 realtime = day_date_time[i];
130 else
132 /* must be week day name */
134 if (!realdate) realdate = day_date_time[i];
139 dt.dat_Format = FORMAT_DOS;
140 dt.dat_Flags = DTF_FUTURE;
141 dt.dat_StrDay = NULL; /* StrToDate ignores this anyway */
142 dt.dat_StrDate = realdate;
143 dt.dat_StrTime = realtime;
145 DateStamp(&dt.dat_Stamp);
147 if((!realdate && !realtime) || (StrToDate(&dt) == 0))
149 PutStr( "***Bad args:\n"
150 "- use DD-MMM-YY or <dayname> or yesterday etc. to set date\n"
151 " HH:MM:SS or HH:MM to set time\n");
152 error = RETURN_FAIL;
154 else
156 timerMP = CreateMsgPort();
157 if (timerMP)
159 timerReq = (struct timerequest *)CreateIORequest(timerMP, sizeof(struct timerequest));
161 if (timerReq)
163 timererror = OpenDevice(TIMERNAME, UNIT_VBLANK,
164 &timerReq->tr_node, 0L);
165 if(timererror == 0)
167 timerReq->tr_time.tv_secs = dt.dat_Stamp.ds_Days*60*60*24 +
168 dt.dat_Stamp.ds_Minute*60 +
169 dt.dat_Stamp.ds_Tick / TICKS_PER_SECOND;
170 timerReq->tr_time.tv_micro = 0;
171 timerReq->tr_node.io_Command = TR_SETSYSTIME;
172 timerReq->tr_node.io_Flags |= IOF_QUICK;
174 DoIO(&timerReq->tr_node);
176 CloseDevice(&timerReq->tr_node);
178 else
180 PutStr("Date: Error opening timer.device\n");
181 error = RETURN_FAIL;
183 DeleteIORequest(&timerReq->tr_node);
185 else
187 PutStr("Date: Error creating timerequest\n");
188 error = RETURN_FAIL;
190 DeleteMsgPort(timerMP);
192 else
194 PutStr("Date: Error creating MsgPort\n");
195 error = RETURN_FAIL;
199 return error;
203 int printdate(STRPTR filename)
205 BPTR file = Output();
206 int ownfile = 0;
207 int error = RETURN_OK;
208 struct DateTime dt;
209 char daystring[LEN_DATSTRING * 2], datestring[LEN_DATSTRING * 2],
210 timestring[LEN_DATSTRING * 2], resstring[LEN_DATSTRING*6+1];
212 if(filename != NULL)
214 file = Open(filename, MODE_NEWFILE);
215 ownfile = 1;
218 if(file != (BPTR)NULL)
220 int pos = 0;
222 DateStamp(&dt.dat_Stamp);
224 dt.dat_Format = FORMAT_DOS; // FORMAT_DEF;
225 dt.dat_Flags = 0;
226 dt.dat_StrDay = daystring;
227 dt.dat_StrDate = datestring;
228 dt.dat_StrTime = timestring;
229 DateToStr(&dt);
231 CopyMem(daystring, resstring, strlen(daystring));
232 pos += strlen(daystring);
233 resstring[pos++] = ' ';
234 CopyMem(datestring, resstring + pos, strlen(datestring));
235 pos += strlen(datestring);
236 resstring[pos++] = ' ';
237 CopyMem(timestring, resstring + pos, strlen(timestring));
238 pos += strlen(timestring);
239 resstring[pos++] = 0x0a;
241 if(Write(file, resstring, pos) < pos)
243 PrintFault(IoErr(), "Date");
244 error = RETURN_FAIL;
247 if(ownfile == 1)
249 Close(file);
252 else
254 PrintFault(IoErr(), "Date");
255 error = RETURN_FAIL;
258 return error;
261 int __nocommandline = 1;
263 int main(void)
265 int error = RETURN_OK;
266 STRPTR args[ARG_COUNT] = {NULL, NULL, NULL, NULL};
267 struct RDArgs *rda;
269 rda = ReadArgs(ARG_STRING, (IPTR *)args, NULL);
271 if (rda != NULL)
273 if (args[ARG_DAY] != NULL || args[ARG_DATE] != NULL || args[ARG_TIME] != NULL)
275 if (((error = setdate(args)) == RETURN_OK))
277 if (args[ARG_VER] != NULL)
278 printdate(args[ARG_VER]);
281 else
283 error = printdate(args[ARG_VER]);
286 FreeArgs(rda);
288 else
290 PrintFault(IoErr(), "Date");
291 error = RETURN_FAIL;
294 return error;