Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!caip!meccts!dayton!sjm From: sjm@dayton.UUCP (sjm) Newsgroups: net.sources Subject: Re: uptime for System V Message-ID: <199@dayton.UUCP> Date: Fri, 19-Sep-86 16:44:00 EDT Article-I.D.: dayton.199 Posted: Fri Sep 19 16:44:00 1986 Date-Received: Sat, 20-Sep-86 21:01:51 EDT References: <191@dayton.UUCP> Reply-To: sjm@dayton.UUCP (Steven J. McDowall) Distribution: world Organization: Dayton-Hudson Dept. Store Co. Lines: 95 Keywords: uptime SysV Summary: Newer version Well, it happens to the best of us (which certainly excludes me :-) Two days after posting the original uptime.c program for Sys V, Paul Jatkowski @ cuuxb reminds me that the times() call will return the number of clock ticks since boot time. (Though the System V definition only says that it will return some arbitrary number of ticks (e.g. clock ticks)). Anyway, needless to say this is a much faster method of determining uptime (although not as fun :-) than reading kernel memory. This version also doesn't need to be set uid-ed. Also, an option (any argument) will instead cause the date of the last boot. ---------------------------CUT HERE------------------------------ /* * uptime.c -- Print how long the system has been up * System V Implmenentation * * 8/19/86 - Version 1.0 * (sjm@dayton) Steven McDowall * * 9/19/86 - Version 1.1 * Get boot time much faster from times() call * (pej@cuuxb) Paul Jatkowski * * cc -O -o uptime uptime.c */ # include /* system types */ # include /* for HZ */ # include # include #define MINUTES 60 #define HOURS (MINUTES * 60) #define DAYS (HOURS * 24) int memfd; main(argc, argv) int argc; char *argv[]; { time_t uptime, times(); struct tm *ts; struct tms tbuf; void ptime(); /* Get the number of clock cycles the system has been running and divide by the clock rate (HZ) to get seconds */ uptime = (times(tbuf) / HZ); /* Print out information in one of 2 formats */ if (argc > 1) /* assume we want boot date */ { uptime = time((long *) 0) - uptime; ts = localtime(&uptime); printf("System was last booted on %s", asctime(ts)); } else /* we want old uptime format */ ptime(uptime); } /* main */ /* Print the time in a nice format */ void ptime(secs) time_t secs; { short days, hours, minutes; secs -= (days = secs / DAYS) * DAYS; secs -= (hours = secs / HOURS) * HOURS; secs -= (minutes = secs / MINUTES) * MINUTES; printf("The system has been up for "); if (days != 0) printf("%d %s ", days, (days == 1 ? "day" : "days")); if (hours != 0) printf("%d %s ", hours, (hours == 1 ? "hour" : "hours")); if (minutes != 0) printf("%d %s ", minutes, (minutes == 1 ? "minute" : "minutes")); printf("and %d %s.\n", secs, (secs == 1 ? "second" : "seconds")); } -- Steven J. McDowall Dayton-Hudson Dept. Store. Co. UUCP: ihnp4!rosevax!dayton!sjm 700 on the Mall ATT: 1 612 375 2816 Mpls, Mn. 55408