commit c460ba9cb9d79d9007327c8ace7a7ac2c17e6277
parent b7f7a502a074741d72d324b39b8f6b2036e8ff50
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Thu, 23 Aug 2018 13:58:17 +0200
sfeed_tail: file need to exist the first run, but not after
+ dont memcpy the struct stat(2) when nothing changed.
Diffstat:
1 file changed, 18 insertions(+), 7 deletions(-)
diff --git a/sfeed_tail.c b/sfeed_tail.c
@@ -14,7 +14,7 @@
static char *line;
static size_t linesize;
-static int changed;
+static int changed, firsttime = 1;
static time_t comparetime;
struct line {
@@ -144,10 +144,19 @@ main(int argc, char *argv[])
comparetime -= 86400;
for (i = 1; i < argc; i++) {
- if (!(fp = fopen(argv[i], "r")))
- err(1, "fopen: %s", argv[i]);
- if (fstat(fileno(fp), &st) == -1)
- err(1, "fstat: %s", argv[i]);
+ if (!(fp = fopen(argv[i], "r"))) {
+ if (firsttime)
+ err(1, "fopen: %s", argv[i]);
+ /* NOTE: don't report when the file is missing */
+ continue;
+ }
+ if (fstat(fileno(fp), &st) == -1) {
+ if (firsttime)
+ err(1, "fstat: %s", argv[i]);
+ warn("fstat: %s", argv[i]);
+ fclose(fp);
+ continue;
+ }
/* did the file change? by size, modification */
if (stfiles[i - 1].st_size != st.st_size ||
@@ -155,9 +164,10 @@ main(int argc, char *argv[])
name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i];
printfeed(fp, name);
if (ferror(fp))
- err(1, "ferror: %s", argv[i]);
+ warn("ferror: %s", argv[i]);
+ memcpy(&stfiles[i - 1], &st, sizeof(st));
}
- memcpy(&stfiles[i - 1], &st, sizeof(st));
+
fclose(fp);
}
@@ -169,6 +179,7 @@ main(int argc, char *argv[])
}
sleep(1);
slept++;
+ firsttime = 0;
}
return 0;
}