tload: new usage & fix coding style
Coding style fixed and more readable help output. Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
parent
05765771c2
commit
a08e0a6ef2
79
tload.c
79
tload.c
@ -11,15 +11,18 @@
|
||||
*/
|
||||
#include "proc/version.h"
|
||||
#include "proc/sysinfo.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <getopt.h>
|
||||
#include <setjmp.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <termios.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static char *screen;
|
||||
|
||||
@ -30,9 +33,8 @@ static int fd=1;
|
||||
static int dly = 5;
|
||||
static jmp_buf jb;
|
||||
|
||||
static void alrm(int signo)
|
||||
static void alrm(int signo __attribute__ ((__unused__)))
|
||||
{
|
||||
(void)signo;
|
||||
signal(SIGALRM, alrm);
|
||||
alarm(dly);
|
||||
}
|
||||
@ -54,16 +56,31 @@ static void setsize(int i)
|
||||
else
|
||||
screen = (char *)realloc(screen, scr_size);
|
||||
|
||||
if (screen == NULL) {
|
||||
perror("");
|
||||
exit(1);
|
||||
}
|
||||
if (screen == NULL)
|
||||
err(EXIT_FAILURE, "cannot allocate %zu bytes", scr_size);
|
||||
|
||||
memset(screen, ' ', scr_size - 1);
|
||||
*(screen + scr_size - 2) = '\0';
|
||||
if (i)
|
||||
longjmp(jb, 0);
|
||||
}
|
||||
|
||||
static void __attribute__ ((__noreturn__))
|
||||
usage(FILE * out)
|
||||
{
|
||||
fprintf(out,
|
||||
"\nUsage: %s [options] [tty]\n"
|
||||
"\nOptions:\n", program_invocation_short_name);
|
||||
fprintf(out,
|
||||
" -d, --delay <secs> update delay in seconds\n"
|
||||
" -s, --scale <num> vertical scale\n"
|
||||
" -h, --help display this help text\n"
|
||||
" -V, --version display version information and exit\n");
|
||||
fprintf(out, "\nFor more information see tload(1).\n");
|
||||
|
||||
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int lines, row, col = 0;
|
||||
@ -72,22 +89,35 @@ int main(int argc, char **argv)
|
||||
static double max_scale, scale_fact;
|
||||
char *scale_arg = NULL;
|
||||
|
||||
while ((opt = getopt(argc, argv, "s:d:V")) != -1)
|
||||
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}
|
||||
};
|
||||
|
||||
while ((opt = getopt_long(argc, argv, "s:d:Vh", longopts, NULL)) != -1)
|
||||
switch (opt) {
|
||||
case 's': scale_arg = optarg; break;
|
||||
case 'd': dly = atoi(optarg); break;
|
||||
case 'V': display_version(); exit(0); break;
|
||||
case 's':
|
||||
scale_arg = optarg;
|
||||
break;
|
||||
case 'd':
|
||||
dly = atoi(optarg);
|
||||
break;
|
||||
case 'V':
|
||||
display_version();
|
||||
exit(0);
|
||||
break;
|
||||
case 'h':
|
||||
usage(stdout);
|
||||
default:
|
||||
printf("usage: tload [-V] [-d delay] [-s scale] [tty]\n");
|
||||
exit(1);
|
||||
usage(stderr);
|
||||
}
|
||||
|
||||
if (argc > optind) {
|
||||
if ((fd = open(argv[optind], 1)) == -1) {
|
||||
perror("tty");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if (argc > optind)
|
||||
if ((fd = open(argv[optind], 1)) == -1)
|
||||
err(EXIT_FAILURE, "can not open tty");
|
||||
|
||||
setsize(0);
|
||||
|
||||
@ -141,8 +171,7 @@ int main(int argc, char **argv)
|
||||
for (row = nrows - 2; row >= 0; --row)
|
||||
*(screen + row * ncols + col) = ' ';
|
||||
}
|
||||
i = sprintf(screen, " %.2f, %.2f, %.2f",
|
||||
av[0], av[1], av[2]);
|
||||
i = sprintf(screen, " %.2f, %.2f, %.2f", av[0], av[1], av[2]);
|
||||
if (i > 0)
|
||||
screen[i] = ' ';
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user