]> git.scottworley.com Git - tl-append/blame - tl-append.c
Release 1.0.0
[tl-append] / tl-append.c
CommitLineData
e81b7a5f
SW
1#define _POSIX_C_SOURCE 199309L
2
3#include <errno.h>
b56e29cb 4#include <fcntl.h>
b3d5ed96
SW
5#include <stdio.h>
6#include <stdlib.h>
e81b7a5f 7#include <string.h>
d212cd99 8#include <sys/file.h>
e81b7a5f
SW
9#include <time.h>
10#include <unistd.h>
b3d5ed96 11
d522116b 12#include "common.h"
b3d5ed96 13
4b4feda2 14const int BUF_SIZE = 1024;
b3d5ed96 15
e81b7a5f
SW
16const char PROMPT[] = "\33[H" /* Move cursor 'home' */
17 "\33[J" /* Clear screen */
18 "> ";
19const char ACKNOWLEDGE[] = "[OK]";
b56e29cb 20const char WAITING[] = "[Waiting on lock...]\n";
e81b7a5f
SW
21const struct timespec ACKNOWLEDGE_DELAY = {0, 300000000};
22
23typedef struct {
24 int interactive;
675caf99 25 int fcntl_lock;
d212cd99 26 int flock_lock;
e81b7a5f
SW
27} conf_t;
28
7521e30b 29void usage() {
ff3c08a3 30 die("usage: tl-append [-i] [-C dir] [--no-fnctl-lock] [--no-flock-lock]");
7521e30b
SW
31}
32
e81b7a5f 33conf_t parse_command_line(int argc, char *argv[]) {
d19cfb47
SW
34 conf_t conf;
35 conf.interactive = 0;
675caf99 36 conf.fcntl_lock = 1;
d212cd99 37 conf.flock_lock = 1;
e81b7a5f 38
d19cfb47
SW
39 for (int i = 1; i < argc; i++) {
40 if (strcmp(argv[i], "-i") == 0 && isatty(2))
41 conf.interactive = 1;
7521e30b 42 else if (strcmp(argv[i], "--no-fnctl-lock") == 0)
675caf99 43 conf.fcntl_lock = 0;
7521e30b 44 else if (strcmp(argv[i], "--no-flock-lock") == 0)
d212cd99 45 conf.flock_lock = 0;
ff3c08a3
SW
46 else if (strcmp(argv[i], "-C") == 0) {
47 if (i + 1 >= argc)
48 die("-C requires a directory");
49 if (chdir(argv[i + 1]) == -1)
50 die_err("Couldn't change directory");
51 i++;
52 } else
7521e30b 53 usage();
d19cfb47 54 }
e81b7a5f
SW
55
56 return conf;
57}
58
88eed985
SW
59static void read_line(conf_t *conf, char *buf) {
60 if (conf->interactive)
e81b7a5f
SW
61 if (fputs(PROMPT, stderr) == EOF)
62 die("I/O error writing prompt");
b3d5ed96
SW
63 if (fgets(buf, BUF_SIZE, stdin) == NULL) {
64 if (ferror(stdin))
65 die("I/O error reading line");
66 if (feof(stdin)) {
67 buf[0] = '\0'; /* Unclear if fgets does this already */
68 return;
69 }
70 die("Unexpected error reading line");
71 }
72}
73
b56e29cb 74static void write_line(const char *now, FILE *f, const char *line) {
b3d5ed96
SW
75 if (f == NULL)
76 die_err("Error opening output file");
2998af81
SW
77 if (fputs(now, f) == EOF)
78 die("Error writing to output file");
79 if (fputc(' ', f) == EOF)
80 die("Error writing to output file");
b3d5ed96
SW
81 if (fputs(line, f) == EOF)
82 die("Error writing to output file");
b56e29cb
SW
83}
84
d212cd99 85static void take_fcntl_lock(conf_t *conf, FILE *f) {
675caf99
SW
86 if (!conf->fcntl_lock)
87 return;
b56e29cb
SW
88 struct flock lock;
89 lock.l_type = F_WRLCK;
90 lock.l_whence = SEEK_SET;
91 lock.l_start = 0;
92 lock.l_len = 0;
93 int fd = fileno(f);
94 if (fd == -1)
95 die_err("Couldn't get file descriptor for locking");
96 if (fcntl(fd, F_SETLK, &lock) == 0)
97 return;
98 if (errno != EACCES && errno != EAGAIN)
d212cd99 99 die_err("Couldn't take fcntl lock");
b56e29cb
SW
100 if (fputs(WAITING, stderr) == EOF)
101 die("Error writing waiting message");
102 if (fcntl(fd, F_SETLKW, &lock) == 0)
103 return;
d212cd99
SW
104 die_err("Couldn't take fcntl lock");
105}
106
107static void take_flock_lock(conf_t *conf, FILE *f) {
108 if (!conf->flock_lock)
109 return;
110 int fd = fileno(f);
111 if (fd == -1)
112 die_err("Couldn't get file descriptor for locking");
113 if (flock(fd, LOCK_EX | LOCK_NB) == 0)
114 return;
115 if (errno != EWOULDBLOCK)
116 die_err("Couldn't take flock lock");
117 if (fputs(WAITING, stderr) == EOF)
118 die("Error writing waiting message");
119 if (flock(fd, LOCK_EX) == 0)
120 return;
121 die_err("Couldn't take flock lock");
122}
123
124static void take_lock(conf_t *conf, FILE *f) {
125 take_fcntl_lock(conf, f);
126 take_flock_lock(conf, f);
b56e29cb
SW
127}
128
88eed985
SW
129static void write_acknowledgment(conf_t *conf) {
130 if (conf->interactive) {
e81b7a5f
SW
131 if (fputs(ACKNOWLEDGE, stderr) == EOF)
132 die("Error writing acknowledgment");
133 if (nanosleep(&ACKNOWLEDGE_DELAY, NULL) == -1 && errno != EINTR)
134 die_err("Error sleeping");
135 }
b3d5ed96
SW
136}
137
88eed985 138static void lock_and_write_line(conf_t *conf, const char *line) {
b56e29cb
SW
139 const char *now = encode_time(time(NULL));
140 FILE *f = fopen(FILENAME, "a");
141
675caf99 142 take_lock(conf, f);
b56e29cb
SW
143 write_line(now, f, line);
144
145 if (fclose(f) != 0)
146 die_err("Error closing output file");
147
148 write_acknowledgment(conf);
149}
150
e81b7a5f
SW
151int main(int argc, char *argv[]) {
152 conf_t conf = parse_command_line(argc, argv);
b3d5ed96 153 char buf[BUF_SIZE];
88eed985
SW
154 for (read_line(&conf, buf); buf[0]; read_line(&conf, buf)) {
155 lock_and_write_line(&conf, buf);
b3d5ed96
SW
156 }
157 return 0;
158}