2 ** from http://www.beatjapan.org/mirror/www.be.com/beware/Utilities/uptime.html
3 ** archive ftp://ftp.halcyon.com/pub/users/jrashmun/uptime.zip
11 #include <SupportDefs.h>
18 printf( "\nUsage: uptime [-u]\n" );
19 printf( "Prints the current date and time, as well\n" );
20 printf( "as the time elapsed since the system was started.\n" );
21 printf( "The optional argument omits the current date and time.\n\n" );
24 int main( int argc
, char* argv
[] )
27 const char* day_strings
[] = { "day", "days" };
28 const char* hour_strings
[] = { "hour", "hours" };
29 const char* minute_strings
[] = { "minute", "minutes" };
30 const char* day_string
= 0;
31 const char* hour_string
= 0;
32 const char* minute_string
= 0;
33 int print_uptime_only
= 0;
34 time_t current_time
= 0;
35 const time_t secs_per_day
= 86400;
36 const time_t secs_per_hour
= 3600;
37 const time_t secs_per_minute
= 60;
38 time_t uptime_days
= 0;
39 time_t uptime_hours
= 0;
40 time_t uptime_minutes
= 0;
41 time_t uptime_secs
= 0;
42 const bigtime_t usecs_per_sec
= 1000000L;
43 bigtime_t uptime_usecs
= 0;
44 bigtime_t usecs_since_sec
= 0;
54 if( !strcmp( argv
[1], "-u" ) )
55 print_uptime_only
= 1;
63 uptime_usecs
= system_time( );
65 uptime_secs
= uptime_usecs
/ usecs_per_sec
;
66 usecs_since_sec
= uptime_usecs
% usecs_per_sec
;
67 if( usecs_since_sec
>= (usecs_per_sec
/ 2) )
70 uptime_days
= uptime_secs
/ secs_per_day
;
71 uptime_hours
= (uptime_secs
% secs_per_day
) / secs_per_hour
;
72 uptime_minutes
= (uptime_secs
% secs_per_hour
) / secs_per_minute
;
74 current_time
= time( 0 );
76 if( !print_uptime_only
)
78 strcpy( buf
, ctime( ¤t_time
) );
80 buf
[strlen( buf
) - 1] = '\0';
83 day_string
= (uptime_days
== 1) ? day_strings
[0] : day_strings
[1];
84 hour_string
= (uptime_hours
== 1) ? hour_strings
[0] : hour_strings
[1];
85 minute_string
= (uptime_minutes
== 1) ? minute_strings
[0] : minute_strings
[1];
87 if( uptime_days
&& uptime_hours
&& uptime_minutes
)
89 if( !print_uptime_only
)
90 printf( "%s, up %ld %s, %ld %s, %ld %s\n", buf
,
91 uptime_days
, day_string
,
92 uptime_hours
, hour_string
,
93 uptime_minutes
, minute_string
);
95 printf( "up %ld %s, %ld %s, %ld %s\n",
96 uptime_days
, day_string
,
97 uptime_hours
, hour_string
,
98 uptime_minutes
, minute_string
);
100 else if( !uptime_days
)
102 if( uptime_hours
&& uptime_minutes
)
104 if( !print_uptime_only
)
105 printf( "%s, up %ld %s, %ld %s\n", buf
,
106 uptime_hours
, hour_string
,
107 uptime_minutes
, minute_string
);
109 printf( "up %ld %s, %ld %s\n",
110 uptime_hours
, hour_string
,
111 uptime_minutes
, minute_string
);
113 else if( uptime_hours
)
115 if( !print_uptime_only
)
116 printf( "%s, up %ld %s\n", buf
,
117 uptime_hours
, hour_string
);
119 printf( "up %ld %s\n",
120 uptime_hours
, hour_string
);
124 if( !print_uptime_only
)
125 printf( "%s, up %ld %s\n", buf
,
126 uptime_minutes
, minute_string
);
128 printf( "up %ld %s\n",
129 uptime_minutes
, minute_string
);
132 else if( uptime_hours
)
134 if( !print_uptime_only
)
135 printf( "%s, up %ld %s, %ld %s\n", buf
,
136 uptime_days
, day_string
,
137 uptime_hours
, hour_string
);
139 printf( "up %ld %s, %ld %s\n",
140 uptime_days
, day_string
,
141 uptime_hours
, hour_string
);
143 else if( uptime_minutes
)
145 if( !print_uptime_only
)
146 printf( "%s, up %ld %s, %ld %s\n", buf
,
147 uptime_days
, day_string
,
148 uptime_minutes
, minute_string
);
150 printf( "up %ld %s, %ld %s\n",
151 uptime_days
, day_string
,
152 uptime_minutes
, minute_string
);
156 if( !print_uptime_only
)
157 printf( "%s, up %ld %s\n", buf
,
158 uptime_days
, day_string
);
160 printf( "up %ld %s\n",
161 uptime_days
, day_string
);