-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimeme.c
129 lines (106 loc) · 3.19 KB
/
timeme.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* timeme.c
* calcs how long a command takes to complete
* compile with vbcc: vc -c99 -lamiga -o timeme timeme.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <proto/exec.h>
#include <proto/timer.h>
#include <clib/alib_protos.h>
#include <clib/dos_protos.h>
void delete_timer ( struct timerequest *tr )
{
struct MsgPort *tp;
if ( tr != 0 )
{
tp = tr->tr_node.io_Message.mn_ReplyPort;
if ( tp != 0 )
DeletePort ( tp );
CloseDevice ( ( struct IORequest * ) tr );
DeleteExtIO ( ( struct IORequest * ) tr );
}
}
struct timerequest *create_timer ( ULONG unit )
{
/* return a pointer to a timer request. If any problem, return NULL */
LONG error;
struct MsgPort *timerport;
struct timerequest *TimerIO;
timerport = ( struct MsgPort * ) CreatePort ( 0, 0 );
if ( timerport == NULL )
return ( NULL );
TimerIO = ( struct timerequest * )
CreateExtIO ( timerport, sizeof ( struct timerequest ) );
if ( TimerIO == NULL )
{
DeletePort ( timerport ); /* Delete message port */
return ( NULL );
}
error = OpenDevice ( TIMERNAME, unit, ( struct IORequest * ) TimerIO, 0L );
if ( error != 0 )
{
delete_timer ( TimerIO );
return ( NULL );
}
return ( TimerIO );
}
LONG get_sys_time ( struct timeval *tv )
{
struct timerequest *tr;
tr = create_timer ( UNIT_MICROHZ );
/* non zero return says error */
if ( tr == 0 )
return ( -1 );
tr->tr_node.io_Command = TR_GETSYSTIME;
DoIO ( ( struct IORequest * ) tr );
/* structure assignment */
*tv = tr->tr_time;
delete_timer ( tr );
return ( 0 );
}
void error_exit ( char *msg )
{
printf ( "%s\n", msg );
exit ( 0 );
}
int main ( int argc, char **argv )
{
struct Library * TimerBase;
int size=0,i,h=0,m=0,ml=0,s=0;
char *exec;
struct timeval time1, time2;
struct timerequest *tr;
if ( argc <= 1 )
{
printf ( "timeme 1.0 - Times how long a command takes to execute.\n" );
printf ( "Usage: timeme (command) [params] ...\nContact: [email protected]\n" );
exit ( 0 );
}
/* count the arguments */
for ( i=1;i<argc;i++ ) size += strlen ( argv[i] );
size += argc-2;
exec = ( char * ) AllocMem ( size+1, MEMF_PUBLIC|MEMF_CLEAR );
/* build the execute line */
sprintf ( exec, "%s", argv[1] );
for ( i=2;i<argc;i++ )
{
sprintf ( exec, "%s %s", exec, argv[i] );
}
if ( get_sys_time ( &time1 ) ) error_exit ( "Error getting timer.device." );
( void ) Execute ( exec, 0,0 );
if ( get_sys_time ( &time2 ) ) error_exit ( "Error getting timer.device." );
if ( ! ( tr = create_timer ( UNIT_MICROHZ ) ) ) error_exit ( "Error getting timer.device." );
TimerBase = ( struct Library * ) tr->tr_node.io_Device;
SubTime ( &time2,&time1 );
TimerBase = ( struct Library * ) ( -1 );
delete_timer ( tr );
h = time2.tv_secs/3600;
m = time2.tv_secs/60;
ml = m%60;
printf ( "\n-- %d hours, %d minutes, %ld secs, %ld msec --\n", h,ml,time2.tv_secs%60,time2.tv_micro );
FreeMem ( exec, size+1 );
exit ( 0 );
}