int main(int argc, char **argv) { int lines, row, col=0; int i, opt; double av[3]; static double max_scale, scale_fact; char *scale_arg = NULL; while ((opt = getopt(argc, argv, "s:d:V")) != -1) switch (opt) { case 's': scale_arg = optarg; break; case 'd': dly = atoi(optarg); break; case 'V': display_version(); exit(0); break; default: printf("usage: tload [-V] [-d delay] [-s scale] [tty]\n"); exit(1); } if (argc > optind) { if ((fd = open(argv[optind], 1)) == -1) { perror(argv[optind]); exit(1); } } setsize(0); if (scale_arg) max_scale = atof(scale_arg); else max_scale = nrows; scale_fact = max_scale; setjmp(jb); col = 0; alrm(0); while (1) { if (scale_fact < max_scale) scale_fact *= 2.0; /* help it drift back up. */ loadavg(&av[0], &av[1], &av[2]); repeat: lines = av[0] * scale_fact; row = nrows-1; while (--lines >= 0) { *(screen + row * ncols + col) = '*'; if (--row < 0) { scale_fact /= 2.0; goto repeat; } } while (row >= 0) *(screen + row-- * ncols + col) = ' '; for (i = 1; ; ++i) { char *p; row = nrows - (i * scale_fact); if (row < 0) break; if (*(p = screen + row * ncols + col) == ' ') *p = '-'; else *p = '='; } if (++col == ncols) { --col; memmove(screen, screen + 1, scr_size-1); for(row = nrows-2; row >= 0; --row) *(screen + row * ncols + col) = ' '; } i = sprintf(screen, " %.2f, %.2f, %.2f", av[0], av[1], av[2]); if (i>0) screen[i] = ' '; write(fd, "\033[H", 3); write(fd, screen, scr_size - 1); pause(); } }
int main(int argc, char **argv) { int lines, row, col = 0; int i, opt; double av[3]; static double max_scale = 0, scale_fact; long tmpdly; static const struct option longopts[] = { {"scale", required_argument, NULL, 's'}, {"delay", required_argument, NULL, 'd'}, {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'V'}, {NULL, 0, NULL, 0} }; #ifdef HAVE_PROGRAM_INVOCATION_NAME program_invocation_name = program_invocation_short_name; #endif setlocale (LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((opt = getopt_long(argc, argv, "s:d:Vh", longopts, NULL)) != -1) switch (opt) { case 's': max_scale = strtod_or_err(optarg, _("failed to parse argument")); if (max_scale < 0) xerrx(EXIT_FAILURE, _("scale cannot be negative")); break; case 'd': tmpdly = strtol_or_err(optarg, _("failed to parse argument")); if (tmpdly < 1) xerrx(EXIT_FAILURE, _("delay must be positive integer")); else if (UINT_MAX < tmpdly) xerrx(EXIT_FAILURE, _("too large delay value")); dly = tmpdly; break; case 'V': printf(PROCPS_NG_VERSION); return EXIT_SUCCESS; break; case 'h': usage(stdout); default: usage(stderr); } if (argc > optind) if ((fd = open(argv[optind], 1)) == -1) xerr(EXIT_FAILURE, _("can not open tty")); setsize(0); if (max_scale == 0) max_scale = nrows; scale_fact = max_scale; setjmp(jb); col = 0; alrm(0); while (1) { if (scale_fact < max_scale) scale_fact *= 2.0; /* help it drift back up. */ loadavg(&av[0], &av[1], &av[2]); do { lines = av[0] * scale_fact; row = nrows - 1; while (0 <= --lines) { *(screen + row * ncols + col) = '*'; if (--row < 0) { scale_fact /= 2.0; break; } } } while (0 <= lines); while (row >= 0) *(screen + row-- * ncols + col) = ' '; for (i = 1;; ++i) { char *p; row = nrows - (i * scale_fact); if (row < 0) break; if (*(p = screen + row * ncols + col) == ' ') *p = '-'; else *p = '='; } if (++col == ncols) { --col; memmove(screen, screen + 1, scr_size - 1); for (row = nrows - 2; row >= 0; --row) *(screen + row * ncols + col) = ' '; } i = sprintf(screen, " %.2f, %.2f, %.2f", av[0], av[1], av[2]); if (i > 0) screen[i] = ' '; if (write(fd, "\033[H", 3) < 0) xerr(EXIT_FAILURE, _("writing to tty failed")); if (write(fd, screen, scr_size - 1) < 0) xerr(EXIT_FAILURE, _("writing to tty failed")); pause(); } }