ubase/watch.c

59 lines
1.0 KiB
C
Raw Normal View History

2013-09-04 10:27:29 +00:00
/* See LICENSE file for copyright and license details. */
#include <errno.h>
2013-09-04 10:27:29 +00:00
#include <stdio.h>
#include <stdlib.h>
2014-06-30 18:03:41 +00:00
#include <unistd.h>
2013-09-04 10:27:29 +00:00
#include "util.h"
static void
usage(void)
{
eprintf("usage: %s [-t] [-n interval] command\n", argv0);
}
int
main(int argc, char *argv[])
{
char cmd[BUFSIZ];
char *end;
useconds_t interval = 2 * 1E6;
float period;
int i;
2013-09-04 10:27:29 +00:00
ARGBEGIN {
case 't':
break;
case 'n':
period = strtof(EARGF(usage()), &end);
if (*end != '\0' || errno != 0)
eprintf("invalid interval\n");
if (period < 0)
period = 0.1f;
interval = period * 1E6;
2013-09-04 10:27:29 +00:00
break;
default:
usage();
} ARGEND;
if (argc < 1)
usage();
2014-08-25 18:14:34 +00:00
if (strlcpy(cmd, argv[0], sizeof(cmd)) >= sizeof(cmd))
eprintf("command too long\n");
2013-09-04 10:27:29 +00:00
for (i = 1; i < argc; i++) {
2014-08-25 18:14:34 +00:00
if (strlcat(cmd, " ", sizeof(cmd)) >= sizeof(cmd))
eprintf("command too long\n");
if (strlcat(cmd, argv[i], sizeof(cmd)) >= sizeof(cmd))
eprintf("command too long\n");
2013-09-04 10:27:29 +00:00
}
for (;;) {
printf("\x1b[2J\x1b[H"); /* clear */
2013-09-04 10:27:29 +00:00
fflush(NULL);
system(cmd);
usleep(interval);
2013-09-04 10:27:29 +00:00
}
2014-10-02 22:45:25 +00:00
return 0;
2013-09-04 10:27:29 +00:00
}