morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base
Log | Files | Refs

watch.c (1069B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <errno.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <unistd.h>
      6 
      7 #include "util.h"
      8 
      9 static void
     10 usage(void)
     11 {
     12 	eprintf("usage: %s [-t] [-n interval] command\n", argv0);
     13 }
     14 
     15 int
     16 main(int argc, char *argv[])
     17 {
     18 	char cmd[BUFSIZ];
     19 	char *end;
     20 	useconds_t interval = 2 * 1E6;
     21 	float period;
     22 	int i;
     23 
     24 	ARGBEGIN {
     25 	case 't':
     26 		break;
     27 	case 'n':
     28 		period = strtof(EARGF(usage()), &end);
     29 		if (*end != '\0' || errno != 0)
     30 			eprintf("invalid interval\n");
     31 		if (period < 0)
     32 			period = 0.1f;
     33 		interval = period * 1E6;
     34 		break;
     35 	default:
     36 		usage();
     37 	} ARGEND;
     38 
     39 	if (argc < 1)
     40 		usage();
     41 
     42 	if (strlcpy(cmd, argv[0], sizeof(cmd)) >= sizeof(cmd))
     43 		eprintf("command too long\n");
     44 	for (i = 1; i < argc; i++) {
     45 		if (strlcat(cmd, " ", sizeof(cmd)) >= sizeof(cmd))
     46 			eprintf("command too long\n");
     47 		if (strlcat(cmd, argv[i], sizeof(cmd)) >= sizeof(cmd))
     48 			eprintf("command too long\n");
     49 	}
     50 
     51 	for (;;) {
     52 		printf("\x1b[2J\x1b[H"); /* clear */
     53 		fflush(NULL);
     54 		system(cmd);
     55 		usleep(interval);
     56 	}
     57 	return 0;
     58 }