childs -> children.
[AROS.git] / workbench / c / SetClock.c
blob23469b3a69b1fc654a10dd55abcf265ef9ca0670
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: SetClock - set/save the date from/to the BBU clock.
6 Lang: English
7 */
9 /*************************************************************************
11 NAME
12 SetClock
14 FORMAT
15 SetClock {LOAD|SAVE|RESET}
17 SYNOPSIS
18 LOAD/S,SAVE/S,RESET/S
20 LOCATION
23 FUNCTION
24 SetClock can be used to:
25 o Load the time from the battery backed-up clock,
26 o Save the time to the battery backed-up clock,
27 o Reset the battery backed up clock.
29 EXAMPLE
31 SetClock LOAD
33 will set the system time from the battery backed-up clock.
34 In most systems this will be done automatically during
35 system startup.
37 SetClock SAVE
39 will set the time of the battery backed-up clock from the
40 current system clock time.
42 SetClock RESET
44 will reset the battery backed-up to a value of the
45 1st January 1978 00:00:00. This is mostly used if the
46 battery backed-up clock has an error and will not
47 respond to normal load and save commands.
49 SEE ALSO
50 Date, Sys:Prefs/Time
52 *************************************************************************/
54 #include <aros/debug.h>
55 #include <exec/types.h>
56 #include <dos/dosextens.h>
57 #include <dos/rdargs.h>
58 #include <devices/timer.h>
59 #include <libraries/locale.h>
60 #include <utility/tagitem.h>
62 #include <proto/exec.h>
63 #include <proto/dos.h>
64 #include <proto/battclock.h>
65 #include <proto/locale.h>
66 #include <proto/timer.h>
68 const char version[] = "$VER: SetClock 42.0 (" ADATE ")";
69 const char exthelp[] =
70 "SetClock : Set or save the date from/to the battery backed-up clock\n"
71 "\tLOAD Load the time from the battery-backed-up clock\n"
72 "\tSAVE Save the time to the battery-backed-up clock\n"
73 "\tRESET Reset the battery-backed-up clock\n";
75 #define ARG_TEMPLATE "LOAD/S,SAVE/S,RESET/S"
76 #define ARG_LOAD 0
77 #define ARG_SAVE 1
78 #define ARG_RESET 2
79 #define TOTAL_ARGS 3
81 int main(int argc, char **av)
83 IPTR args[TOTAL_ARGS] = { 0, 0, 0 };
84 struct RDArgs *rda, *rd;
85 struct Library *BattClockBase = NULL;
86 struct Device *TimerBase = NULL;
87 ULONG time, error = 0;
88 struct timerequest *tr;
89 struct MsgPort *mp;
90 struct timeval t;
92 rda = AllocDosObject(DOS_RDARGS, NULL);
93 if(rda != NULL)
95 rda->RDA_ExtHelp = (STRPTR)exthelp;
96 rd = ReadArgs(ARG_TEMPLATE, args, rda);
98 if(rd)
100 BattClockBase = OpenResource("battclock.resource");
101 if(BattClockBase)
103 if((mp = CreateMsgPort()))
105 if((tr = (struct timerequest *)
106 CreateIORequest(mp, sizeof(struct timerequest))))
108 if(OpenDevice("timer.device", UNIT_VBLANK,
109 (struct IORequest *)tr, 0) == 0)
111 struct Locale *l;
112 struct Process *me = (struct Process *)FindTask(NULL);
114 /* Suppress 'Please insert volume' requesters because we can run without ENV: assign */
115 me->pr_WindowPtr = (APTR)-1;
118 * Open default preferences manually because we run before IPrefs,
119 * and default locale isn't set yet.
121 l = OpenLocale("ENV:SYS/locale.prefs");
122 if (!l)
123 l = OpenLocale("SYS:Prefs/Env-Archive/SYS/locale.prefs");
125 D(Printf("Preferences locale: 0x%p\n", l));
127 TimerBase = tr->tr_node.io_Device;
129 if(args[0])
131 /* Loading */
132 time = ReadBattClock();
134 if (l)
136 D(Printf("Locale flags 0x%08lx, offset %ld\n", l->loc_Flags, l->loc_GMTOffset));
138 if (l->loc_Flags & LOCF_GMT_CLOCK)
140 /* loc_GMTOffset actually expresses difference from local time to GMT */
141 time -= l->loc_GMTOffset * 60;
145 /* Set timer.device clock */
146 tr->tr_node.io_Command = TR_SETSYSTIME;
147 tr->tr_time.tv_secs = time;
148 tr->tr_time.tv_micro = 0;
149 tr->tr_node.io_Flags = IOF_QUICK;
150 DoIO((struct IORequest *)tr);
151 if(tr->tr_node.io_Error != 0)
153 FPuts(Output(), "Error: Could not set system time!\n");
156 else if(args[1])
158 /* Saving */
159 GetSysTime(&t);
161 if (l)
163 if (l->loc_Flags & LOCF_GMT_CLOCK)
164 t.tv_secs += l->loc_GMTOffset * 60;
166 WriteBattClock(t.tv_secs);
168 else if(args[2])
170 /* Resetting */
171 ResetBattClock();
173 else
174 error = ERROR_REQUIRED_ARG_MISSING;
176 if (l)
177 CloseLocale(l);
179 } /* OpenDevice() */
180 else
181 error = ERROR_INVALID_RESIDENT_LIBRARY;
183 DeleteIORequest((struct IORequest *)tr);
185 } /* CreateIORequest() */
186 else
187 error = ERROR_NO_FREE_STORE;
189 DeleteMsgPort(mp);
190 } /* CreateMsgPort() */
192 else
193 error = ERROR_NO_FREE_STORE;
195 } /* OpenResource() */
196 FreeArgs(rd);
198 } /* ReadArgs() */
199 else
200 error = IoErr();
202 FreeDosObject(DOS_RDARGS, rda);
203 } /* AllocDosObject() */
204 else
205 error = IoErr();
207 if(error != 0)
209 PrintFault(error, "SetClock");
210 return RETURN_FAIL;
212 SetIoErr(0);
213 return 0;