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