]> git.scottworley.com Git - tl-append/blobdiff - tl-append.c
Fix memory leak
[tl-append] / tl-append.c
index 21e23a6ae1cacbdf6d6f83f735986d0c3e058966..edea9e9c16cd5fbbbdb7efcb644b15fe695e3c96 100644 (file)
@@ -1,21 +1,65 @@
+#define _POSIX_C_SOURCE 199309L
+
+#include <errno.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <sys/file.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "common.h"
+
+const int BUF_SIZE = 1024;
+
+const char PROMPT[] = "\33[H" /* Move cursor 'home' */
+                      "\33[J" /* Clear screen */
+                      "> ";
+const char ACKNOWLEDGE[] = "[OK]";
+const char WAITING[] = "[Waiting on lock...]\n";
+const struct timespec ACKNOWLEDGE_DELAY = {0, 300000000};
 
-const char *FILENAME = "tl.log";
-const size_t BUF_SIZE = 1024;
+typedef struct {
+  int interactive;
+  int fcntl_lock;
+  int flock_lock;
+} conf_t;
 
-static void die(const char *message) {
-  fputs(message, stderr);
-  fputc('\n', stderr);
-  exit(1);
+void usage() {
+  die("usage: tl-append [-i] [-C dir] [--no-fnctl-lock] [--no-flock-lock]");
 }
 
-static void die_err(const char *message) {
-  perror(message);
-  exit(1);
+conf_t parse_command_line(int argc, char *argv[]) {
+  conf_t conf;
+  conf.interactive = 0;
+  conf.fcntl_lock = 1;
+  conf.flock_lock = 1;
+
+  for (int i = 1; i < argc; i++) {
+    if (strcmp(argv[i], "-i") == 0 && isatty(2))
+      conf.interactive = 1;
+    else if (strcmp(argv[i], "--no-fnctl-lock") == 0)
+      conf.fcntl_lock = 0;
+    else if (strcmp(argv[i], "--no-flock-lock") == 0)
+      conf.flock_lock = 0;
+    else if (strcmp(argv[i], "-C") == 0) {
+      if (i + 1 >= argc)
+        die("-C requires a directory");
+      if (chdir(argv[i + 1]) == -1)
+        die_err("Couldn't change directory");
+      i++;
+    } else
+      usage();
+  }
+
+  return conf;
 }
 
-static void read_line(char *buf) {
+static void read_line(conf_t *conf, char *buf) {
+  if (conf->interactive)
+    if (fputs(PROMPT, stderr) == EOF)
+      die("I/O error writing prompt");
   if (fgets(buf, BUF_SIZE, stdin) == NULL) {
     if (ferror(stdin))
       die("I/O error reading line");
@@ -27,20 +71,89 @@ static void read_line(char *buf) {
   }
 }
 
-static void write_line(const char *line) {
-  FILE *f = fopen(FILENAME, "a");
+static void write_line(const char *now, FILE *f, const char *line) {
   if (f == NULL)
     die_err("Error opening output file");
+  if (fputs(now, f) == EOF)
+    die("Error writing to output file");
+  if (fputc(' ', f) == EOF)
+    die("Error writing to output file");
   if (fputs(line, f) == EOF)
     die("Error writing to output file");
+}
+
+static void take_fcntl_lock(conf_t *conf, FILE *f) {
+  if (!conf->fcntl_lock)
+    return;
+  struct flock lock;
+  lock.l_type = F_WRLCK;
+  lock.l_whence = SEEK_SET;
+  lock.l_start = 0;
+  lock.l_len = 0;
+  int fd = fileno(f);
+  if (fd == -1)
+    die_err("Couldn't get file descriptor for locking");
+  if (fcntl(fd, F_SETLK, &lock) == 0)
+    return;
+  if (errno != EACCES && errno != EAGAIN)
+    die_err("Couldn't take fcntl lock");
+  if (fputs(WAITING, stderr) == EOF)
+    die("Error writing waiting message");
+  if (fcntl(fd, F_SETLKW, &lock) == 0)
+    return;
+  die_err("Couldn't take fcntl lock");
+}
+
+static void take_flock_lock(conf_t *conf, FILE *f) {
+  if (!conf->flock_lock)
+    return;
+  int fd = fileno(f);
+  if (fd == -1)
+    die_err("Couldn't get file descriptor for locking");
+  if (flock(fd, LOCK_EX | LOCK_NB) == 0)
+    return;
+  if (errno != EWOULDBLOCK)
+    die_err("Couldn't take flock lock");
+  if (fputs(WAITING, stderr) == EOF)
+    die("Error writing waiting message");
+  if (flock(fd, LOCK_EX) == 0)
+    return;
+  die_err("Couldn't take flock lock");
+}
+
+static void take_lock(conf_t *conf, FILE *f) {
+  take_fcntl_lock(conf, f);
+  take_flock_lock(conf, f);
+}
+
+static void write_acknowledgment(conf_t *conf) {
+  if (conf->interactive) {
+    if (fputs(ACKNOWLEDGE, stderr) == EOF)
+      die("Error writing acknowledgment");
+    if (nanosleep(&ACKNOWLEDGE_DELAY, NULL) == -1 && errno != EINTR)
+      die_err("Error sleeping");
+  }
+}
+
+static void lock_and_write_line(conf_t *conf, const char *line) {
+  char *now = encode_time(time(NULL));
+  FILE *f = fopen(FILENAME, "a");
+
+  take_lock(conf, f);
+  write_line(now, f, line);
+
   if (fclose(f) != 0)
     die_err("Error closing output file");
+
+  free(now);
+  write_acknowledgment(conf);
 }
 
-int main() {
+int main(int argc, char *argv[]) {
+  conf_t conf = parse_command_line(argc, argv);
   char buf[BUF_SIZE];
-  for (read_line(buf); buf[0]; read_line(buf)) {
-    write_line(buf);
+  for (read_line(&conf, buf); buf[0]; read_line(&conf, buf)) {
+    lock_and_write_line(&conf, buf);
   }
   return 0;
 }