Here's a simple program which runs a command whenever you modify a file.
Today's procedure is strcpy
.
#include "procs-2019-10-12.c"
#include <sys/stat.h>
#include <time.h>
int main(int argc, char **argv) {
struct stat st;
struct timespec lastmod = {0};
char *command = sbrk(1);
int i;
if (argc < 2) {
writes(stderr, "Please provide a file to watch.\n");
return exitfailure;
}
if (stat(argv[1], &st)) {
writes(stderr, "Could not stat file.\n");
return exitfailure;
}
for (i = 2; i < argc; i++) {
sbrk(strlen(argv[i]) + 1);
}
{
char *p = command;
for (i = 2; i < argc; i++) {
p = strcpy(p, argv[i]);
if (i != argc-1)
*p++ = ' ';
}
*p = 0;
}
while (1) {
typedef unsigned long useconds_t;
extern int usleep(useconds_t usec);
extern void system(const char *command);
if (stat(argv[1], &st) == 0) {
struct timespec thismod = st.st_mtim;
if (thismod.tv_sec != lastmod.tv_sec
|| thismod.tv_nsec != lastmod.tv_nsec) {
system(command);
lastmod = thismod;
}
usleep(10000);
} else {
writes(stderr, "Warning: could not stat file.\n");
return exitfailure;
}
}
return 0;
}