commit 899d589db06a2a8892283a8f12099d641f77aad8
parent 65e02d66b534c2c6b11e42c9ed6d2501b63e955f
Author: sin <sin@2f30.org>
Date: Wed, 4 Sep 2013 11:27:29 +0100
Add watch(1)
Diffstat:
5 files changed, 85 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
@@ -13,6 +13,8 @@ LIB = \
util/proc.o \
util/putword.o \
util/recurse.o \
+ util/strlcat.o \
+ util/strlcpy.o \
util/tty.o
SRC = \
@@ -40,7 +42,8 @@ SRC = \
truncate.c \
umount.c \
unshare.c \
- uptime.c
+ uptime.c \
+ watch.c
OBJ = $(SRC:.c=.o) $(LIB)
BIN = $(SRC:.c=)
diff --git a/util.h b/util.h
@@ -14,4 +14,6 @@ void eprintf(const char *, ...);
long estrtol(const char *, int);
void putword(const char *);
void recurse(const char *, void (*)(const char *));
+size_t strlcat(char *, const char *, size_t);
+size_t strlcpy(char *, const char *, size_t);
char *ttytostr(int, int);
diff --git a/util/strlcat.c b/util/strlcat.c
@@ -0,0 +1,18 @@
+#include <stdio.h>
+#include <string.h>
+
+size_t
+strlcat(char *dest, const char *src, size_t count)
+{
+ size_t dsize = strlen(dest);
+ size_t len = strlen(src);
+ size_t res = dsize + len;
+
+ dest += dsize;
+ count -= dsize;
+ if (len >= count)
+ len = count-1;
+ memcpy(dest, src, len);
+ dest[len] = 0;
+ return res;
+}
diff --git a/util/strlcpy.c b/util/strlcpy.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+#include <string.h>
+
+size_t
+strlcpy(char *dest, const char *src, size_t size)
+{
+ size_t ret = strlen(src);
+
+ if (size) {
+ size_t len = (ret >= size) ? size - 1 : ret;
+ memcpy(dest, src, len);
+ dest[len] = '\0';
+ }
+ return ret;
+}
diff --git a/watch.c b/watch.c
@@ -0,0 +1,46 @@
+/* See LICENSE file for copyright and license details. */
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#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];
+ int i, interval = 2;
+
+ ARGBEGIN {
+ case 't':
+ break;
+ case 'n':
+ /* Only whole seconds for now */
+ interval = estrtol(EARGF(usage()), 10);
+ break;
+ default:
+ usage();
+ } ARGEND;
+
+ if (argc < 1)
+ usage();
+
+ strlcpy(cmd, argv[0], sizeof(cmd));
+ for (i = 1; i < argc; i++) {
+ strlcat(cmd, " ", sizeof(cmd));
+ strlcat(cmd, argv[i], sizeof(cmd));
+ }
+
+ for (;;) {
+ fflush(NULL);
+ system(cmd);
+ sleep(interval);
+ }
+ return 0;
+}