]> git.scottworley.com Git - tl-append/blame - tl-append.c
Also take flock locks
[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
29conf_t parse_command_line(int argc, char *argv[]) {
d19cfb47
SW
30 conf_t conf;
31 conf.interactive = 0;
675caf99 32 conf.fcntl_lock = 1;
d212cd99 33 conf.flock_lock = 1;
e81b7a5f 34
d19cfb47
SW
35 for (int i = 1; i < argc; i++) {
36 if (strcmp(argv[i], "-i") == 0 && isatty(2))
37 conf.interactive = 1;
675caf99
SW
38 if (strcmp(argv[i], "--no-fnctl-lock") == 0)
39 conf.fcntl_lock = 0;
d212cd99
SW
40 if (strcmp(argv[i], "--no-flock-lock") == 0)
41 conf.flock_lock = 0;
d19cfb47 42 }
e81b7a5f
SW
43
44 return conf;
45}
46
88eed985
SW
47static void read_line(conf_t *conf, char *buf) {
48 if (conf->interactive)
e81b7a5f
SW
49 if (fputs(PROMPT, stderr) == EOF)
50 die("I/O error writing prompt");
b3d5ed96
SW
51 if (fgets(buf, BUF_SIZE, stdin) == NULL) {
52 if (ferror(stdin))
53 die("I/O error reading line");
54 if (feof(stdin)) {
55 buf[0] = '\0'; /* Unclear if fgets does this already */
56 return;
57 }
58 die("Unexpected error reading line");
59 }
60}
61
b56e29cb 62static void write_line(const char *now, FILE *f, const char *line) {
b3d5ed96
SW
63 if (f == NULL)
64 die_err("Error opening output file");
2998af81
SW
65 if (fputs(now, f) == EOF)
66 die("Error writing to output file");
67 if (fputc(' ', f) == EOF)
68 die("Error writing to output file");
b3d5ed96
SW
69 if (fputs(line, f) == EOF)
70 die("Error writing to output file");
b56e29cb
SW
71}
72
d212cd99 73static void take_fcntl_lock(conf_t *conf, FILE *f) {
675caf99
SW
74 if (!conf->fcntl_lock)
75 return;
b56e29cb
SW
76 struct flock lock;
77 lock.l_type = F_WRLCK;
78 lock.l_whence = SEEK_SET;
79 lock.l_start = 0;
80 lock.l_len = 0;
81 int fd = fileno(f);
82 if (fd == -1)
83 die_err("Couldn't get file descriptor for locking");
84 if (fcntl(fd, F_SETLK, &lock) == 0)
85 return;
86 if (errno != EACCES && errno != EAGAIN)
d212cd99 87 die_err("Couldn't take fcntl lock");
b56e29cb
SW
88 if (fputs(WAITING, stderr) == EOF)
89 die("Error writing waiting message");
90 if (fcntl(fd, F_SETLKW, &lock) == 0)
91 return;
d212cd99
SW
92 die_err("Couldn't take fcntl lock");
93}
94
95static void take_flock_lock(conf_t *conf, FILE *f) {
96 if (!conf->flock_lock)
97 return;
98 int fd = fileno(f);
99 if (fd == -1)
100 die_err("Couldn't get file descriptor for locking");
101 if (flock(fd, LOCK_EX | LOCK_NB) == 0)
102 return;
103 if (errno != EWOULDBLOCK)
104 die_err("Couldn't take flock lock");
105 if (fputs(WAITING, stderr) == EOF)
106 die("Error writing waiting message");
107 if (flock(fd, LOCK_EX) == 0)
108 return;
109 die_err("Couldn't take flock lock");
110}
111
112static void take_lock(conf_t *conf, FILE *f) {
113 take_fcntl_lock(conf, f);
114 take_flock_lock(conf, f);
b56e29cb
SW
115}
116
88eed985
SW
117static void write_acknowledgment(conf_t *conf) {
118 if (conf->interactive) {
e81b7a5f
SW
119 if (fputs(ACKNOWLEDGE, stderr) == EOF)
120 die("Error writing acknowledgment");
121 if (nanosleep(&ACKNOWLEDGE_DELAY, NULL) == -1 && errno != EINTR)
122 die_err("Error sleeping");
123 }
b3d5ed96
SW
124}
125
88eed985 126static void lock_and_write_line(conf_t *conf, const char *line) {
b56e29cb
SW
127 const char *now = encode_time(time(NULL));
128 FILE *f = fopen(FILENAME, "a");
129
675caf99 130 take_lock(conf, f);
b56e29cb
SW
131 write_line(now, f, line);
132
133 if (fclose(f) != 0)
134 die_err("Error closing output file");
135
136 write_acknowledgment(conf);
137}
138
e81b7a5f
SW
139int main(int argc, char *argv[]) {
140 conf_t conf = parse_command_line(argc, argv);
b3d5ed96 141 char buf[BUF_SIZE];
88eed985
SW
142 for (read_line(&conf, buf); buf[0]; read_line(&conf, buf)) {
143 lock_and_write_line(&conf, buf);
b3d5ed96
SW
144 }
145 return 0;
146}