/* Program: uptime reports time since system booted. (Like the Unix command) Under Fervices for Unix (Interix), this is reliant on the start time of the interix posix subsystem which (like Unix) writes records to the wtmp file. This contains logins etc, but also boot records Author: David Horton ww.chapelhill.homeip.net dhorton@iprimus.com.au david_horton@acslink.net.au 14-May-2005 */ #include #include #ifndef _PATH_WTMPX #define _PATH_WTMPX "/var/adm/wtmpx" #endif #include main(void) { /* Sample output Linux: 00:36:46 up 36 days, 10:24, 4 users, load average: 0.00, 0.00, 0.00 (time of day) (uptime) (logins) (load average) Solaris: 12:34am up 36 day(s), 10:04, 2 users, load average: 0.28, 0.16, 0.14 */ int fd = -1; struct utmpx wtmp; time_t up = 0; /* time system came up */ time_t now = 0; /* the time now */ time_t uptime; /* elapsed time since system came up */ char bufn[100]; char bufu[100]; struct tm now_tm; struct tm uptime_tm; if (0 > (fd = open(_PATH_WTMPX,O_RDONLY))) { perror("open(" _PATH_WTMPX); exit(1); } while( read(fd, &wtmp, sizeof(wtmp))) { if (wtmp.ut_type == BOOT_TIME) { up = wtmp.ut_tv.tv_sec; } /*printf("type=%d\n", wtmp.ut_type);*/ } close (fd); now = time(NULL); uptime = now - up; (void) localtime_r(&now, &now_tm); (void) gmtime_r(&uptime, &uptime_tm); (void) strftime(bufn, sizeof(bufn), "%H:%M:%S", &now_tm); sprintf(bufu,"%d day(s), %2.2d:%2.2d:%2.2d", uptime_tm.tm_yday, uptime_tm.tm_hour, uptime_tm.tm_min, uptime_tm.tm_sec); /*printf (" up %d seconds\n", uptime);*/ printf ("%s up %s\n", bufn, bufu); exit (0); }