]> git.scottworley.com Git - tl-append/blob - tl-append.c
52d260248fdbe0779e8942ee970f1cd6ffa5f140
[tl-append] / tl-append.c
1 #define _POSIX_C_SOURCE 199309L
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/file.h>
9 #include <time.h>
10 #include <unistd.h>
11
12 #include "common.h"
13
14 const int BUF_SIZE = 1024;
15
16 const char PROMPT[] = "\33[H" /* Move cursor 'home' */
17 "\33[J" /* Clear screen */
18 "> ";
19 const char ACKNOWLEDGE[] = "[OK]";
20 const char WAITING[] = "[Waiting on lock...]\n";
21 const struct timespec ACKNOWLEDGE_DELAY = {0, 300000000};
22
23 typedef struct {
24 int interactive;
25 int fcntl_lock;
26 int flock_lock;
27 } conf_t;
28
29 void usage() {
30 die("usage: tl-append [-i] [-C dir] [--no-fnctl-lock] [--no-flock-lock]");
31 }
32
33 conf_t parse_command_line(int argc, char *argv[]) {
34 conf_t conf;
35 conf.interactive = 0;
36 conf.fcntl_lock = 1;
37 conf.flock_lock = 1;
38
39 for (int i = 1; i < argc; i++) {
40 if (strcmp(argv[i], "-i") == 0 && isatty(2))
41 conf.interactive = 1;
42 else if (strcmp(argv[i], "--no-fnctl-lock") == 0)
43 conf.fcntl_lock = 0;
44 else if (strcmp(argv[i], "--no-flock-lock") == 0)
45 conf.flock_lock = 0;
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
53 usage();
54 }
55
56 return conf;
57 }
58
59 static void read_line(conf_t *conf, char *buf) {
60 if (conf->interactive)
61 if (fputs(PROMPT, stderr) == EOF)
62 die("I/O error writing prompt");
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
74 static void write_line(const char *now, FILE *f, const char *line) {
75 if (f == NULL)
76 die_err("Error opening output file");
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");
81 if (fputs(line, f) == EOF)
82 die("Error writing to output file");
83 }
84
85 static void take_fcntl_lock(conf_t *conf, FILE *f) {
86 if (!conf->fcntl_lock)
87 return;
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)
99 die_err("Couldn't take fcntl lock");
100 if (fputs(WAITING, stderr) == EOF)
101 die("Error writing waiting message");
102 if (fcntl(fd, F_SETLKW, &lock) == 0)
103 return;
104 die_err("Couldn't take fcntl lock");
105 }
106
107 static 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
124 static void take_lock(conf_t *conf, FILE *f) {
125 take_fcntl_lock(conf, f);
126 take_flock_lock(conf, f);
127 }
128
129 static void write_acknowledgment(conf_t *conf) {
130 if (conf->interactive) {
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 }
136 }
137
138 static void lock_and_write_line(conf_t *conf, const char *line) {
139 const char *now = encode_time(time(NULL));
140 FILE *f = fopen(FILENAME, "a");
141
142 take_lock(conf, f);
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
151 int main(int argc, char *argv[]) {
152 conf_t conf = parse_command_line(argc, argv);
153 char buf[BUF_SIZE];
154 for (read_line(&conf, buf); buf[0]; read_line(&conf, buf)) {
155 lock_and_write_line(&conf, buf);
156 }
157 return 0;
158 }