/* * Copyright (c) 2002 Pedro Bastos * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the name of his contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY PEDRO BASTOS AND HIS CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * */ #ifdef OPENBSD #include #include #include #include #include #include #include #include #include #include "lncurses.h" #include "global.h" #include "extern.h" #endif #ifdef FREEBSD #include #include #include #include #include #include #include #include #include "lncurses.h" #include "global.h" #include "extern.h" #endif double maxload; volatile sig_atomic_t sigrecv; static struct tm tm; int d_opsys(void) { struct utsname uts; output("~WSystem: "); if (uname(&uts) == 0) { output("~w%s %s (%s)\n", uts.sysname, uts.release, uts.machine); return (1); } else { output("~RUnknown System"); return (0); } } int d_load(void) { double load[3]; output("~WLoad: "); if (getloadavg(load, 3) != 3) { output("~RCouldn't find out."); return (1); } output("~w%.2f, %.2f, %.2f (avg. %.2f)\n", load[0], load[1], load[2], (load[0] + load[1] + load[2]) / 3); load_ks(load, 3); return (1); } int d_htnm(void) { char host[16], domain[16]; wmove(window[0], 0, 3); if (gethostname(host, sizeof host) == -1) return (-1); if (getdomainname(domain, sizeof domain) == -1) return (-1); if (host[0] != 0x0 || domain[0] != 0x0) { (void) wattrset(window[0], COLOR_PAIR(CYAN)|A_BOLD); (void) wprintw(window[0], "<"); (void) mvwprintw(window[0], 0, 4 + strlen(host) + strlen(domain) + 1, ">"); (void) wattrset(window[0], COLOR_PAIR(WHITE)|A_BOLD); (void) mvwprintw(window[0], 0, 4, "%s.%s", host, domain); (void) wattrset(window[0], COLOR_PAIR(WHITE)); return (1); } return (0); } int d_users(void) { char buf[16]; int f, tmp, nusers = 0; time_t diff; struct utmp utmp_str; struct tm tm; FILE *utmp_file; if (!(utmp_file = (FILE *) fopen(_PATH_UTMP, "r"))) return(1); (void) memset(buf, 0, sizeof buf); (void) output("~WUsers logged on: "); while (fread((char *) &utmp_str, sizeof(utmp_str), 1, utmp_file) == 1) { if (*utmp_str.ut_name && *utmp_str.ut_line) { output("~w\n%s\ton %s", utmp_str.ut_name, utmp_str.ut_line); if (!*utmp_str.ut_host) output(" from (local) "); else output(" from ~G%s~w ", utmp_str.ut_host); tm = *localtime(&utmp_str.ut_time); (void) strftime(buf, sizeof buf, "%H:%M, %d/%m", &tm); diff = difftime(time(NULL), utmp_str.ut_time); tmp = (diff / SECSPERDAY); if (tmp) { output("%d day%s", tmp, plural((int)tmp)); f++; } else { f = 0; } diff %= SECSPERDAY; tmp = (diff / SECSPERHOUR); if (tmp) { output("%s%d hour%s", and(f), tmp, plural((int)tmp)); f++; } else { f = 0; } diff %= SECSPERHOUR; tmp = (diff / SECSPERMIN); if (tmp) { output("%s%d minute%s", and(f), tmp, plural((int)tmp)); f++; } else { f = 0; } nusers++; f = 0; } } output("\n"); output("~W%d~w users logged on.\n", nusers); (void) fclose(utmp_file); return (1); } void load_ks(double loads[], int howmany) { char buff[_POSIX2_LINE_MAX]; time_t maxload_t; double *cur; for (cur = loads; cur < &loads[howmany]; cur++) if (*cur > maxload) { maxload = *cur; (void) time(&maxload_t); tm = *localtime(&maxload_t); } (void) strftime(buff, sizeof buff, "%R, %d/%m", &tm); output("~WMaximum Load: ~w%.2f on %s\n", maxload, buff); return; }