Add watch(1)

This commit is contained in:
sin 2013-09-04 11:27:29 +01:00
parent 65e02d66b5
commit 899d589db0
5 changed files with 85 additions and 1 deletions

View File

@ -13,6 +13,8 @@ LIB = \
util/proc.o \ util/proc.o \
util/putword.o \ util/putword.o \
util/recurse.o \ util/recurse.o \
util/strlcat.o \
util/strlcpy.o \
util/tty.o util/tty.o
SRC = \ SRC = \
@ -40,7 +42,8 @@ SRC = \
truncate.c \ truncate.c \
umount.c \ umount.c \
unshare.c \ unshare.c \
uptime.c uptime.c \
watch.c
OBJ = $(SRC:.c=.o) $(LIB) OBJ = $(SRC:.c=.o) $(LIB)
BIN = $(SRC:.c=) BIN = $(SRC:.c=)

2
util.h
View File

@ -14,4 +14,6 @@ void eprintf(const char *, ...);
long estrtol(const char *, int); long estrtol(const char *, int);
void putword(const char *); void putword(const char *);
void recurse(const char *, void (*)(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); char *ttytostr(int, int);

18
util/strlcat.c Normal file
View File

@ -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;
}

15
util/strlcpy.c Normal file
View File

@ -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;
}

46
watch.c Normal file
View File

@ -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;
}