2 * tload.c - terminal version of xload
5 * -s initial scaling exponent (default = 6)
8 * Copyright (c) 1992 Branko Lankester
9 * /proc changes by David Engel (david@ods.com)
10 * Made a little more efficient by Michael K. Johnson (johnsonm@sunsite.unc.edu)
12 #include "proc/version.h"
13 #include "proc/sysinfo.h"
22 #include <sys/ioctl.h>
26 static int nrows = 25;
27 static int ncols = 80;
36 static void alrm(int signo)
39 signal(SIGALRM, alrm);
43 static void setsize(int i)
47 signal(SIGWINCH, setsize);
48 if (ioctl(fd, TIOCGWINSZ, &win) != -1) {
54 scr_size = nrows * ncols;
56 screen = (char *) malloc(scr_size);
58 screen = (char *) realloc(screen, scr_size);
64 memset(screen, ' ', scr_size-1);
65 *(screen + scr_size - 2) = '\0';
70 int main(int argc, char **argv)
72 int lines, row, col=0;
75 static double max_scale, scale_fact;
76 char *scale_arg = NULL;
78 while ((opt = getopt(argc, argv, "s:d:V")) != -1)
80 case 's': scale_arg = optarg; break;
81 case 'd': dly = atoi(optarg); break;
82 case 'V': display_version(); exit(0); break;
84 printf("usage: tload [-V] [-d delay] [-s scale] [tty]\n");
89 if ((fd = open(argv[optind], 1)) == -1) {
98 max_scale = atof(scale_arg);
102 scale_fact = max_scale;
110 if (scale_fact < max_scale)
111 scale_fact *= 2.0; /* help it drift back up. */
113 loadavg(&av[0], &av[1], &av[2]);
116 lines = av[0] * scale_fact;
119 while (--lines >= 0) {
120 *(screen + row * ncols + col) = '*';
127 *(screen + row-- * ncols + col) = ' ';
131 row = nrows - (i * scale_fact);
134 if (*(p = screen + row * ncols + col) == ' ')
140 if (++col == ncols) {
142 memmove(screen, screen + 1, scr_size-1);
144 for(row = nrows-2; row >= 0; --row)
145 *(screen + row * ncols + col) = ' ';
147 i = sprintf(screen, " %.2f, %.2f, %.2f",
148 av[0], av[1], av[2]);
152 write(fd, "\033[H", 3);
153 write(fd, screen, scr_size - 1);