Compiler warnings removed.
[cake.git] / workbench / c / Wait.c
blobd32fcb790f8b3c25e1145f8f8718515c7eb32334
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Wait CLI Command.
6 */
8 /******************************************************************************
11 NAME
13 Wait [(n)] [SEC | SECS | MIN | MINS] [ UNTIL (time) ]
15 SYNOPSIS
17 TIME/N,SEC=SECS/S,MIN=MINS/S,UNTIL/K
19 LOCATION
21 Workbench:C
23 FUNCTION
25 Wait a certain amount of time or until a specified time. Using
26 Wait without any arguments waits for one second.
28 INPUTS
30 TIME -- the number of time units to wait (default is seconds)
31 SEC=SECS -- set the time unit to seconds
32 MIN=MINS -- set the time unit to minutes
33 UNTIL -- wait until the specified time is reached. The time
34 is given in the format HH:MM.
36 RESULT
38 NOTES
40 EXAMPLE
42 BUGS
44 SEE ALSO
46 INTERNALS
48 ******************************************************************************/
50 #include <exec/execbase.h>
51 #include <exec/libraries.h>
52 #include <devices/timer.h>
53 #include <dos/dos.h>
55 #include <proto/dos.h>
56 #include <proto/exec.h>
58 #include <string.h>
59 #include <stdio.h>
61 const TEXT version[] = "$VER: Wait 41.2 (30.4.2000)\n";
63 int __nocommandline;
65 int main (void)
67 IPTR args[4] = { 0, 0, 0, 0 };
68 struct RDArgs *rda;
69 LONG error = RETURN_OK;
70 ULONG delay = 1;
72 #define ERROR(a) { error = a; goto end; }
74 rda = ReadArgs("TIME/N,SEC=SECS/S,MIN=MINS/S,UNTIL/K", args, NULL);
76 if (rda == NULL)
78 PrintFault(IoErr(),"Wait");
79 ERROR(RETURN_FAIL);
82 if (args[3])
84 /* UNTIL */
85 struct DateTime dt;
86 struct DateStamp ds;
87 LONG now_secs, then_secs, diff_secs;
88 UBYTE timestring[9];
90 DateStamp(&ds);
91 now_secs = ds.ds_Minute * 60 + ds.ds_Tick / TICKS_PER_SECOND;
93 if (strlen((char *)args[3]) > 5)
95 puts("Time should be HH:MM");
96 ERROR(RETURN_FAIL);
99 strcpy(timestring, (UBYTE *)args[3]);
100 strcat(timestring, ":00");
102 memset(&dt, 0, sizeof(dt));
103 dt.dat_StrTime = timestring;
105 if (!StrToDate(&dt))
107 puts("Time should be HH:MM");
108 ERROR(RETURN_FAIL);
111 then_secs = dt.dat_Stamp.ds_Minute * 60 + dt.dat_Stamp.ds_Tick / TICKS_PER_SECOND;
112 diff_secs = then_secs - now_secs;
114 if (diff_secs < 0)
116 diff_secs += 60L * 60L * 24L;
119 delay = diff_secs * TICKS_PER_SECOND;
122 else
124 if (args[0])
126 delay = *((ULONG *)args[0]);
129 if (args[2])
131 delay *= 60L;
134 delay *= TICKS_PER_SECOND;
137 if (delay > 0)
139 if (delay <= TICKS_PER_SECOND)
141 /* Don't care about breaking if delay is less than 1 second */
142 Delay (delay);
144 else
146 struct MsgPort *timermp;
147 struct timerequest *timerio;
148 BOOL memok = FALSE, devok = FALSE;
150 if ((timermp = CreateMsgPort()))
152 if ((timerio = (struct timerequest *)CreateIORequest(timermp, sizeof(struct timerequest))))
154 memok = TRUE;
155 if (OpenDevice("timer.device", UNIT_VBLANK, &timerio->tr_node, 0) == 0)
157 ULONG timermask, sigs;
158 BOOL done = FALSE;
160 devok = TRUE;
162 timerio->tr_node.io_Command = TR_ADDREQUEST;
163 timerio->tr_time.tv_secs = delay / TICKS_PER_SECOND;
164 timerio->tr_time.tv_micro = 1000000UL / TICKS_PER_SECOND * (delay % TICKS_PER_SECOND);
166 timermask = 1L << timermp->mp_SigBit;
168 SendIO(&timerio->tr_node);
170 while(!done)
172 sigs = Wait(SIGBREAKF_CTRL_C | timermask);
174 if (sigs & timermask)
176 done = TRUE;
179 if (sigs & SIGBREAKF_CTRL_C)
181 if (!CheckIO(&timerio->tr_node)) AbortIO(&timerio->tr_node);
182 WaitIO(&timerio->tr_node);
184 error = RETURN_WARN;
185 done = TRUE;
188 } /* while(!finished) */
189 CloseDevice(&timerio->tr_node);
191 } /* if (OpenDevice("timer.device", UNIT_VBLANK, &timerio->tr_node, 0) == 0) */
192 DeleteIORequest(&timerio->tr_node);
194 } /* if (timerio = (struct timerequest *)CreateIORequest(timermp, sizeof(struct timerequest))) */
195 DeleteMsgPort(timermp);
197 } /* if ((timermp = CreateMsgPort())) */
199 if (!memok)
201 PrintFault(ERROR_NO_FREE_STORE,"Wait");
202 ERROR(RETURN_FAIL);
204 else if (!devok)
206 puts("Wait: Could not open timer.device!");
207 ERROR(RETURN_FAIL);
212 } /* if (delay > 0) */
214 end:
215 if (rda)
217 FreeArgs(rda);
220 return error;