2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
5 Desc: SetClock - set/save the date from/to the BBU clock.
9 /*************************************************************************
15 SetClock {LOAD|SAVE|RESET}
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.
33 will set the system time from the battery backed-up clock.
34 In most systems this will be done automatically during
39 will set the time of the battery backed-up clock from the
40 current system clock time.
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.
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"
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
;
92 rda
= AllocDosObject(DOS_RDARGS
, NULL
);
95 rda
->RDA_ExtHelp
= (STRPTR
)exthelp
;
96 rd
= ReadArgs(ARG_TEMPLATE
, args
, rda
);
100 BattClockBase
= OpenResource("battclock.resource");
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)
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");
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
;
132 time
= ReadBattClock();
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");
163 if (l
->loc_Flags
& LOCF_GMT_CLOCK
)
164 t
.tv_secs
+= l
->loc_GMTOffset
* 60;
166 WriteBattClock(t
.tv_secs
);
174 error
= ERROR_REQUIRED_ARG_MISSING
;
181 error
= ERROR_INVALID_RESIDENT_LIBRARY
;
183 DeleteIORequest((struct IORequest
*)tr
);
185 } /* CreateIORequest() */
187 error
= ERROR_NO_FREE_STORE
;
190 } /* CreateMsgPort() */
193 error
= ERROR_NO_FREE_STORE
;
195 } /* OpenResource() */
202 FreeDosObject(DOS_RDARGS
, rda
);
203 } /* AllocDosObject() */
209 PrintFault(error
, "SetClock");